def get_checkpoint_cid_list(race_cid): """ 获取活动的所有关卡的cid,并缓存 :param race_cid: :return: """ key = '%s_CHECK_POINT_CID' % race_cid map_key = '%s_CHECK_POINT_MAP' % race_cid check_point_cid_list_str = RedisCache.get(key) check_point_map_str = RedisCache.get(map_key) if check_point_map_str and check_point_cid_list_str: check_point_cid_list = check_point_cid_list_str.split(',') checkpoint_map = json.loads(check_point_map_str) else: check_point_list = RaceGameCheckPoint.sync_find({ 'race_cid': race_cid }).sort('index').to_list(None) check_point_cid_list = [] checkpoint_map = dict() for check_point in check_point_list: check_point_cid_list.append(check_point.cid) checkpoint_map[check_point.cid] = check_point.index # 缓存所有关卡的cid RedisCache.set(key, ",".join(check_point_cid_list), 6 * 60 * 60) RedisCache.set(map_key, json.dumps(checkpoint_map), 6 * 60 * 60) return check_point_cid_list, checkpoint_map
def adjust_race_report_accuracy(race_cid): """ 活动报表线上数据填充正确率数据 :param race_cid: :return: """ race = Race.sync_find_one({'cid': race_cid}) if not race: print("活动不存在, 请检查!") return # 找到该活动下面所有的关卡 check_point_cid_list = RaceGameCheckPoint.sync_distinct("cid", {'race_cid': race_cid}) print(check_point_cid_list) if not check_point_cid_list: print("该活动下面没有关卡") return # 给race_mapping从历史记录统计正确率 try: for check_point_cid in check_point_cid_list: check_point_history_list = MemberCheckPointHistory.sync_find({"check_point_cid": check_point_cid}).to_list( None) for check_point_history in check_point_history_list: race_mapping = RaceMapping.sync_find_one( {'member_cid': check_point_history.member_cid, "race_cid": race_cid}, read_preference=ReadPreference.PRIMARY) race_mapping.total_count += len(check_point_history.result) num = 0 for result in check_point_history.result: if result.get("true_answer"): num += 1 race_mapping.total_correct += num race_mapping.sync_save() except Exception as e: print(str(e))
def get_game_check_point_rules(race_cid, race_check_point_cid=None): """ 获取关卡抽题规则 :param check_point_cid: :return: """ try: cp_cid_list = [] if race_check_point_cid: cp_cid_list.append(race_check_point_cid) else: cid_list = RaceGameCheckPoint.sync_distinct( 'rule_cid', { 'race_cid': race_cid, 'rule_cid': { '$ne': None } }) if cid_list: cp_cid_list.extend(cid_list) if cp_cid_list: race_rule_list = RaceSubjectChoiceRules.sync_find({ 'race_cid': race_cid, 'cid': { '$in': cp_cid_list } }).to_list(None) if race_rule_list: return race_rule_list except Exception: logger.error(traceback.format_exc()) return None
def check(race_cid: str): """ 检查多少人参与活动但是没有答过题目的 :param race_cid: :return: """ race = Race.sync_get_by_cid(race_cid) # 市级活动,如六安市,扬州市 if race.city_code: city_code_list = AdministrativeDivision.sync_distinct( 'code', {'code': race.city_code}) # 该活动的所属城市范围 city_name_list = AdministrativeDivision.sync_distinct( 'title', {'code': race.city_code}) else: prov = AdministrativeDivision.sync_find_one( {'code': race.province_code}) city_code_list = AdministrativeDivision.sync_distinct( 'code', {'parent_code': race.province_code}) city_name_list = AdministrativeDivision.sync_distinct( 'title', {'parent_code': race.province_code}) dist_list = [] print(city_name_list, '1') for city_code in city_code_list: # 该活动区县的范围 dist_list += AdministrativeDivision.sync_distinct( 'title', {'parent_code': city_code}) quantity = 0 member_cid_list = RaceMapping.sync_distinct( "member_cid", { 'race_cid': race_cid, 'auth_address.city': { '$in': city_name_list }, 'auth_address.province': { '$ne': None }, 'auth_address.district': { '$in': dist_list } }) check_point_cid = RaceGameCheckPoint.sync_distinct("cid", {"race_cid": race_cid}) for i in member_cid_list: member = Member.sync_get_by_cid(i) if member: history = MemberCheckPointHistory.sync_find_one({ 'check_point_cid': { '$in': check_point_cid }, "member_cid": i }) if not history: quantity += 1 print(quantity)
def get_all_last_checkpoint(): """ 获取每个活动的最后一关 :return: """ _lp = {} race_list = Race.sync_find().to_list(None) for race in race_list: checkpoint = RaceGameCheckPoint.sync_find({'race_cid': race.cid}).sort([('index', ASC)]).to_list(None) if checkpoint: _lp[race.cid] = checkpoint[-1].cid return _lp
def get_all_race_checkpoint_map(): """ 获取所有活动 :return: """ _mp = {} cursor = RaceGameCheckPoint.sync_find() while True: try: cp = cursor.next() _mp[cp.cid] = cp.race_cid except StopIteration: break return _mp
async def _verify_choice_rules(self, race_cid=None): """ 检查抽题规则是否正确 :param race_cid: :return: """ try: rules_cids = list() query_dict = { 'race_cid': race_cid, 'status': STATUS_SUBJECT_CHOICE_RULE_ACTIVE, 'dimension_rules': { '$ne': {} } } if await self._verify_check_point(race_cid): # 如果关卡存在,则查找关卡关联的抽题规则是否正确 point_cursor = RaceGameCheckPoint.find({ 'race_cid': race_cid, 'status': STATUS_GAME_CHECK_POINT_ACTIVE }) while await point_cursor.fetch_next: point = point_cursor.next_object() if point and point.rule_cid: rules_cids.append(point.rule_cid) query_dict['cid'] = {'$in': rules_cids} rules_cids = set(rules_cids) # 去重 rules_count = await RaceSubjectChoiceRules.count(query_dict) if 'cid' in query_dict.keys(): if rules_count == len(rules_cids): return True else: if rules_count > 0: return True return False except: logger.error(traceback.format_exc()) return False
def statistic_award_of_race(race_cid, daily_codes): """ 该活动下每个关卡参与人数、抽奖人数、中奖人数记录 :param race_cid: :return: """ check_point_list = RaceGameCheckPoint.sync_find({ "race_cid": race_cid }).to_list(None) result = [] check_point_list.sort(key=lambda checkpoint: checkpoint.index) for check_point in check_point_list: print("统计", check_point.alias, check_point.cid) statistic_award_info = statistic_award_info_of_check_point( check_point.cid, daily_codes) result.append(CheckPointAward(check_point, statistic_award_info)) print(statistic_award_info) # check_point_sort = sorted(check_point_list,key=lambda check_point: check_point.index) # check_point_alias = [x.alias for x in check_point_sort] # export_to_excel(result, check_point_alias) return result
async def do_copy_checkpoint(race_cid, new_race_cid, user_cid): """ 复制关卡 :param race_cid: :param new_race_cid: :param user_cid: :return: """ if not race_cid or not new_race_cid or not user_cid: raise Exception('miss parameters') old_checkpoint_cursor = RaceGameCheckPoint.find( { 'race_cid': race_cid }, read_preference=ReadPreference.PRIMARY).batch_size(32) rule_map = json.loads( RedisCache.get(KEY_CACHE_RACE_COPY_MAP + race_cid + '_rule_copy')) red_packet_map = json.loads( RedisCache.get(KEY_CACHE_RACE_COPY_MAP + race_cid + '_red_packet_rule_copy')) new_checkpoint_list = [] checkpoint_map = {} while await old_checkpoint_cursor.fetch_next: checkpoint = old_checkpoint_cursor.next_object() new_checkpoint = copy.deepcopy(checkpoint) new_checkpoint.cid = get_new_cid() new_checkpoint.race_cid = new_race_cid new_checkpoint.rule_cid = rule_map[checkpoint.rule_cid] new_checkpoint.redpkt_rule_cid = red_packet_map[ checkpoint.redpkt_rule_cid] if checkpoint.redpkt_rule_cid else '' new_checkpoint.updated_id = user_cid checkpoint_map[checkpoint.cid] = new_checkpoint.cid new_checkpoint_list.append(new_checkpoint) RedisCache.set(KEY_CACHE_RACE_COPY_MAP + race_cid + '_checkpoint_copy', json.dumps(checkpoint_map)) await RaceGameCheckPoint.insert_many(new_checkpoint_list)
def export_history(race_cid): """ :param race_cid: :return: """ checkpoint_cids = RaceGameCheckPoint.sync_distinct('cid', { 'race_cid': race_cid, 'redpkt_rule_cid': { '$ne': '' } }) cursor = MemberCheckPointHistory.sync_aggregate( [ MatchStage({ 'check_point_cid': { '$in': checkpoint_cids }, 'status': STATUS_RESULT_CHECK_POINT_WIN }), ], read_preference=ReadPreference.PRIMARY).batch_size(2048) index = 0 history_list = set() while True: try: data: MemberCheckPointHistory = cursor.next() history_list.add('%s-%s' % (data.check_point_cid, data.member_cid)) print('has exec', index) index += 1 except StopIteration: break with open('./history-%s.json' % race_cid, 'w', encoding='utf-8') as f: dist_list = json.dumps(list(history_list), ensure_ascii=False) f.write(dist_list)
def statistic_member_info(race_cid: str, province_code, yesterday): """ 会员信息导入,只根据race_mapping表中race_cid删选,没有过滤address为空的 :param race_cid: :return: """ city_code_list = AdministrativeDivision.sync_distinct( 'code', {'parent_code': province_code}) city_name_list = AdministrativeDivision.sync_distinct( 'title', {'parent_code': province_code}) district_name_list = AdministrativeDivision.sync_distinct( 'title', {'parent_code': { '$in': city_code_list }}) # race_member_match = {"race_cid": race_cid, 'record_flag': 1, 'auth_address.province': {'$ne': None}, # 'auth_address.city': {"$in": city_name_list}, # 'auth_address.district': {"$in": district_name_list}, # 'created_dt': {'$lte': datetime.now().replace(hour=0, minute=0, second=0, microsecond=0)}} race_member_match = { "race_cid": race_cid, 'record_flag': 1, 'auth_address.province': { '$ne': None }, 'auth_address.city': { "$in": city_name_list }, 'auth_address.district': { "$in": district_name_list }, 'created_dt': { '$lte': yesterday } } match = {"race_cid": race_cid, 'record_flag': 1} # 当前活动参与会员 memberCidList = RaceMapping.sync_distinct("member_cid", race_member_match) memberCidList.sort() # 当前活动关卡cid checkPointList = RaceGameCheckPoint.sync_aggregate([MatchStage(match)]) checkPointMap = {} lastCheckPoint = None maxCheckPointIndex = 0 for check_point in checkPointList: checkPointMap[check_point.cid] = check_point.alias if check_point.index > maxCheckPointIndex: maxCheckPointIndex = check_point.index lastCheckPoint = check_point.cid checkPointCidList = list(checkPointMap.keys()) # print("该活动下的会员", len(memberCidList), memberCidList) # print("该活动下的会员数", len(set(memberCidList))) # print("该活动下的关卡", len(checkPointCidList), checkPointCidList) temp_list = splitList(memberCidList, 2000) threads = [] thread_num = 1 for my_list in temp_list: # print(my_list) t = MyThread(get_member_info, args=(thread_num, my_list, race_cid, checkPointCidList, lastCheckPoint, checkPointMap)) threads.append(t) t.start() thread_num += 1 results = [] for each_thread in threads: each_thread.join() result = each_thread.get_result() if len(result) > 0: results.extend(result) return results
def export_lottery_detail(race_cid: str): """ :param race_cid: :return: """ race = Race.sync_find_one({'cid': race_cid}) workbook = xlsxwriter.Workbook('%s中奖人员名单.xlsx' % race.title) worksheet = workbook.add_worksheet() _b_dt = datetime.now().replace(2019, 5, 20, 0, 0, 0, 0) _e_dt = datetime.now() cursor = RedPacketBox.sync_aggregate( [ MatchStage({ 'draw_dt': { '$gt': _b_dt, '$lt': _e_dt }, 'draw_status': 0, 'race_cid': race_cid }), LookupStage(Member, 'member_cid', 'cid', 'member_list'), # LookupStage(RaceMapping, 'member_cid', 'member_cid', 'map_list'), # LookupStage(RedPacketItemSetting, 'award_cid', 'cid', 'item_list') ], read_preference=ReadPreference.PRIMARY).batch_size(128) cols = [ '序号', '时间', '用户OPENID', '微信昵称', '奖品', '奖励金额', '领取状态', '省', '市', '区', '领取时间', '关卡序号', '获奖时间', '答题时间', '红包id' ] for col_index, col_name in enumerate(cols): worksheet.write(0, col_index, col_name) _row = 1 while True: try: box = cursor.next() worksheet.write_number(_row, 0, _row) worksheet.write_string(_row, 1, datetime2str(box.draw_dt)) worksheet.write_string( _row, 2, box.member_list[0].open_id if box.member_list else '未知') worksheet.write_string( _row, 3, box.member_list[0].nick_name if box.member_list else '未知') # worksheet.write_string(_row, 4, box.item_list[0].title if box.item_list else '未知') worksheet.write_number(_row, 5, box.award_amount) worksheet.write_string( _row, 6, STATUS_REDPACKET_AWARD_DICT.get(box.draw_status)) # if box.map_list: # worksheet.write_string(_row, 7, box.map_list[0].auth_address.get('province', '')) # worksheet.write_string(_row, 8, box.map_list[0].auth_address.get('city', '')) # worksheet.write_string(_row, 9, box.map_list[0].auth_address.get('district', '')) worksheet.write_string(_row, 10, datetime2str(box.request_dt)) checkpt = RaceGameCheckPoint.sync_get_by_cid(box.checkpoint_cid) if checkpt: worksheet.write_number(_row, 11, checkpt.index) worksheet.write_string(_row, 12, datetime2str(box.draw_dt)) # his = MemberCheckPointHistory.sync_find_one( # {'member_cid': box.member_cid, 'check_point_cid': box.checkpoint_cid}) # if his: # worksheet.write_string(_row, 13, datetime2str(his.created_dt)) worksheet.write_string(_row, 14, box.cid) print('has exec', _row) _row += 1 except StopIteration: break workbook.close()
Member, RedPacketAwardHistory, RedPacketLotteryHistory, RaceGameCheckPoint from motorengine import ASC from motorengine.stages import MatchStage, SortStage STATUS_REDPACKET_AWARDED = 0 # 已被领取 STATUS_REDPACKET_NOT_AWARDED = 1 # 还没有被领取 STATUS_REDPACKET_AWARDING = 2 # 奖励发放 STATUS_REDPACKET_AWARD_FAILED = 5 # 领取失败 # 活动 race_title = '2019扬州市迎新春科学素质大擂台' race = Race.sync_find_one({'title': race_title}) # 关卡 checkpoint_list = RaceGameCheckPoint.sync_aggregate([ MatchStage({'race_cid': race.cid}), SortStage([('index', ASC)]) ]).to_list(None) last_ck = checkpoint_list[-1] # 红包规则 rule = RedPacketRule.sync_find_one({'race_cid': race.cid}) if not rule: rule = RedPacketRule() rule.code = 'rule01' rule.title = 'rule01' rule.status = STATUS_ACTIVE rule.race_cid = race.cid rule.category = CATEGORY_REDPACKET_RULE_LOTTERY rule_oid = rule.sync_save()
async def post(self): r_dict = {'code': 0} game_checkpoint_id = None race_cid = self.get_argument('race_cid', '') try: if race_cid: index = self.get_argument('index', None) alias = self.get_argument('alias', None) unlock_quantity = self.get_argument('unlock_quantity', None) rule_cid = self.get_argument('rule_cid', None) # 红包规则 redpkt_rule_cid = self.get_argument('red_rule', None) rule_subject_quantity = self.get_argument('rule_quantity') comment = self.get_argument('comment', None) status = self.get_argument('status', None) # 状态 answer_limit_quantity = self.get_argument( 'answer_limit_quantity', '') if index and unlock_quantity and rule_subject_quantity and rule_cid: if int(unlock_quantity) > int(rule_subject_quantity): r_dict['code'] = -8 else: r_count = await RaceGameCheckPoint.count( dict(race_cid=race_cid, record_flag=1, index=int(index))) if r_count > 0: r_dict['code'] = -6 else: if status == 'on': status = STATUS_GAME_CHECK_POINT_ACTIVE else: status = STATUS_GAME_CHECK_POINT_INACTIVE race_game_checkpoint = RaceGameCheckPoint( index=int(index), unlock_quantity=int(unlock_quantity), rule_cid=rule_cid, redpkt_rule_cid=redpkt_rule_cid) race_game_checkpoint.alias = alias race_game_checkpoint.race_cid = race_cid race_game_checkpoint.status = status race_game_checkpoint.comment = comment if answer_limit_quantity: race_game_checkpoint.answer_limit_quantity = answer_limit_quantity race_game_checkpoint.created_id = self.current_user.oid race_game_checkpoint.updated_id = self.current_user.oid game_checkpoint_id = await race_game_checkpoint.save( ) r_dict['code'] = 1 else: if not index: r_dict['code'] = -1 if not unlock_quantity: r_dict['code'] = -3 if not rule_cid: r_dict['code'] = -5 except RuntimeError: if game_checkpoint_id: game_checkpoint = await RaceGameCheckPoint.get_by_id( game_checkpoint_id) if game_checkpoint: await RaceGameCheckPoint.delete_by_ids( [game_checkpoint.oid]) logger.error(traceback.format_exc()) return r_dict