def get(self): parteam = Parteam(self.settings["parteam_api_url"]) state = intval(self.get_argument("state", 100)) start = self.get_argument("start", "") end = self.get_argument("end", "") orders = TeamOrder.select( TeamOrder, MatchMember ).join( MatchMember, on=(TeamOrder.id == MatchMember.order_id).alias("match_member") ).where( TeamOrder.team == self.current_team ).order_by( TeamOrder.id.desc() ) if start: start = "%s 00:00:00" % start orders = orders.where(TeamOrder.created >= start) if end: end = "%s 23:59:59" % end orders = orders.where(TeamOrder.created <= end) if state != 100: orders = orders.where(TeamOrder.state == TeamOrder.OrderState(state)) query = self.paginate_query(orders) orders = [] for item in query: orders.append(dict(item.info, order_type_name=item.order_type_name, state_name=item.state_name, payment_method_name=item.payment_method_name, match_member=item.match_member.info)) user_ids = [order["match_member"]["user_id"] for order in orders if order["match_member"]["user_id"]] if user_ids: user_infos = parteam.parteam_user(user_ids) for order in orders: order["user_info"] =\ user_infos.get(order["match_member"]["user_id"]) self.render("orders/orders.html", states=TeamOrder.ORDER_STATES, orders=orders, pagination=query.pagination )
def get(self): """订单列表""" state = intval(self.get_argument("state", 100)) start = self.get_argument("start", "") end = self.get_argument("end", "") query = TeamOrder.select( TeamOrder, Team, Activity, ).join( Team, on=(Team.id == TeamOrder.team).alias("team") ).switch( TeamOrder ).join( Activity, join_type=JOIN.LEFT_OUTER, on=(Activity.id == TeamOrder.activity_id).alias("activity") ) is_restrict_by_areas = self.current_user.is_restrict_by_areas() if is_restrict_by_areas: query = query.join( User, on=(User.id == Team.owner_id) ).where( User.province << self.current_user.valid_manage_provinces ) kw = self.get_argument("kw", "") if kw: query = query.where(TeamOrder.title ** ("%%%s%%" % kw)) if start: start = "%s 00:00:00" % start query = query.where(TeamOrder.created >= start) if end: end = "%s 23:59:59" % end query = query.where(TeamOrder.created <= end) if state != 100: query = query.where(TeamOrder.state == TeamOrder.OrderState(state)) query = query.order_by(TeamOrder.id.desc()) orders = self.paginate_query(query) self.render("order/orders.html", states=TeamOrder.ORDER_STATES, orders=orders, )
def get_order(self, order_no: str) -> Union[TeamOrder, None]: """ 获取 TeamOrder :param order_no: :return: """ try: order = TeamOrder.select()\ .where(TeamOrder.order_no == order_no)\ .get() except TeamOrder.DoesNotExist: msg = "订单不存在" logging.error(msg) order = None return order
def get(self, order_no): # order = TeamOrder.get_or_404(user=self.current_user, # order_no=order_no) try: order = TeamOrder.select(TeamOrder, Activity)\ .join(Activity, on=(TeamOrder.activity_id == Activity.id) .alias("activity"))\ .where(TeamOrder.user == self.current_user, TeamOrder.order_no == order_no) user = User.select().where(User.id == self.current_user.id) team = Team.select() order_pf = prefetch(order, user, team).get() except TeamOrder.DoesNotExist: raise ApiException(404, "订单不存在") self.write(SecureOrderSerializer(instance=order_pf).data)
def get(self, user_id): query = TeamOrder.select().where(TeamOrder.team == self.current_team, TeamOrder.user == user_id) query = self.paginate_query(query, per_page=20) orders = [] for order in query: orders_info = order.info orders_info['state_name'] = order.state_name orders_info['refund_state_name'] = order.refund_state_name orders_info['payment_method_name'] = order.payment_method_name orders_info['order_type_name'] = order.order_type_name orders.append(orders_info) self.write({"orders": orders, "pagination": query.pagination.info})
def get(self): """当前登录用户的订单列表""" query = TeamOrder.select( TeamOrder, Activity ).join( Activity, on=(Activity.id == TeamOrder.activity_id).alias( "activity") ).where(TeamOrder.user == self.current_user) \ .order_by(TeamOrder.id.desc()) query = self.filter_query(query) page = self.paginate_query(query) data = self.get_paginated_data(page=page, alias="orders", serializer=SecureOrderSerializer) self.write(data)
def finish_activity(activity_id): """结算活动场次 1. 将用户在线支付费用转到俱乐部账户 2. 标记场次和订单状态为完成 3. 记录俱乐部账户变化 """ activity = Activity.get_or_404(id=activity_id) if activity.end_time > datetime.now(): raise Exception("活动场次未结束") if activity.state == Activity.ActivityState.cancelled: raise Exception("活动场次已取消") if activity.state == Activity.ActivityState.finished: raise Exception("活动场次已结算") # 计算在线支付完成交易的总额 online_paid_amount = TeamOrder.select(fn.SUM(TeamOrder.payment_fee)).where( TeamOrder.activity_id == activity_id, TeamOrder.state >= TeamOrder.OrderState.TRADE_BUYER_PAID.value, TeamOrder.refund_state == TeamOrder.OrderRefundState.NO_REFUND, TeamOrder.payment_method << TeamOrder.ONLINE_PAYMENT_METHODS).scalar() or 0 # 计算余额支付完成交易的总额 credit_paid_amount = TeamOrder.select(fn.SUM(TeamOrder.credit_fee)).where( TeamOrder.activity_id == activity_id, TeamOrder.state >= TeamOrder.OrderState.TRADE_BUYER_PAID.value).scalar() or 0 # 使用次数 free_times_amount = ActivityMember.select(fn.SUM( ActivityMember.free_times)).where( ActivityMember.state == ActivityMember.ActivityMemberState.confirmed).scalar() or 0 # online_paid_amount= DecimalField(default=Decimal(0), verbose_name="在线支付收入") # credit_paid_amount= DecimalField(default=Decimal(0), verbose_name="余额支付收入") # cash_paid_amount= DecimalField(default=Decimal(0), verbose_name="现金支付收入") # free_times_amount = IntegerField(default=0, verbose_name="次卡支付数量") with app.db.transaction() as txn: team = Team.select().where( Team.id == activity.team.id).for_update().get() # 将收入打到俱乐部账上 Team.update( credit=Team.credit + online_paid_amount, total_receipts=Team.total_receipts + online_paid_amount, updated=datetime.now()).where(Team.id == team.id).execute() # 将订单修改状态为已完成 TeamOrder.update( state=TeamOrder.OrderState.TRADE_FINISHED.value, finished=datetime.now()).where( TeamOrder.activity_id == activity_id, TeamOrder.state == TeamOrder.OrderState.TRADE_BUYER_PAID.value, TeamOrder.refund_state == TeamOrder.OrderRefundState.NO_REFUND.value).execute() # 修改场次状态为已结算 Activity.update(state=Activity.ActivityState.finished, finished=datetime.now(), online_paid_amount=online_paid_amount, credit_paid_amount=credit_paid_amount, free_times_amount=free_times_amount).where( Activity.id == activity_id).execute() # 记录俱乐部账户变更 TeamAccountLog.create(team_id=team.id, credit_change=online_paid_amount, change_type=0, credit_before=team.credit, credit_after=team.credit + online_paid_amount, note="活动结算:%s(%s)" % (activity.title, activity.start_time), activity_id=activity_id, operator_id=0) # 生成下期活动 gen_next_activity(activity_id)