def has_joined_match(self, match, user_id) -> MatchMember: """ 用户是否加入赛事 :param match: :param user_id: :return: """ try: return MatchMember.get(user_id=user_id, match_id=match.id) except MatchMember.DoesNotExist: raise ApiException(422, "未加入赛事, 无须退出")
def join_match_done(self, match_id: int, member_id: int): """ 参加赛事完成, 调用派队消息推送接口 :param: self: celery.task.Context :param match_id: int, Match.id :param member_id: int, MatchMember.id """ pt = Parteam(app.settings["parteam_api_url"]) match = Match.get(id=match_id) # type: Team team = Team.get(id=match.team_id) member = MatchMember.get(match_id=match_id, id=member_id) # type: MatchMember user_info = {"mobile": member.mobile, "userId": member.user_id} message = JoinMatchDone(user_infos=[user_info], match_fee=int(member.total_fee * 100), match_id=match_id, match_name=match.title, sponsor_name=team.name, sponsor_pic_url=team.get_cover_url(size="medium")) if not pt.push_message(message=message): raise self.retry(exc=ParteamRequestError("调用派队推送接口失败"))
def leave(cls, user_id, match: Match, notify_url: str = None, insists=False, role: int = 1): """ 退出赛事 :param user_id: :param match: :param notify_url: 派队回调地址 :param insists: 强制退出 :param role: 退赛发起人, 1 用户, 2 赛事方 :return: """ logging.debug("notify_url: {0}, insists: {1}".format( notify_url, insists)) if not insists and notify_url is None: raise AssertionError("非强制退出 `insists=False` 操作需要提供退款回调" "地址 `notify_url`") if insists is False and not match.can_leave(): raise MatchException("赛事无法退出") # 退出赛事 member = MatchMember.get(user_id=user_id, match_id=match.id) # type: MatchMember with Match._meta.database.transaction(): if insists: match.leave(member) # Warning: 数据库事物中尝试远程 HTTP 调用, 需要修复 else: # match.leave_request(member) # 调用退款接口 pt = Parteam(app.settings["parteam_api_url"]) if member.order_id: # 有支付信息, 调用退款接口 order = TeamOrder.get( id=member.order_id) # type: TeamOrder refund_fee = int(order.payment_fee * 100) try: pt.order_refund(user_id=user_id, order_no=member.pt_order_no, refund_fee=refund_fee, notify_url=notify_url, role=role) except NotPaidError as e: logging.warning("调用派队退款接口发现订单未支付: {0}".format(str(e))) TeamOrder.update(state=TeamOrder.OrderState .TRADE_CLOSED_BY_USER.value) \ .where(TeamOrder.id == member.order_id) \ .execute() match.leave(member) except RefundError as e: raise MatchException(e) else: # 更新订单状态为 `退款`, 订单退款状态为 `全部退款` TeamOrder.update( state=TeamOrder.OrderState.TRADE_CLOSED.value, refund_state=TeamOrder.OrderRefundState.FULL_REFUNDED.value, refunded_time=datetime.now())\ .where(TeamOrder.id == member.order_id)\ .execute() match.leave(member) else: # 无支付信息直接退赛 match.leave(member)