Exemplo n.º 1
0
async def generate_awards_by_config(config: RedPacketConf):
    """

    :param config:
    :return:
    """
    if not config or config.quantity == 0:
        raise Exception('parameter error')

    await RedPacketBox.delete_many({'rule_cid': config.rule_cid})
    award_list = list()

    random_list = []
    if config.category == CATEGORY_REDPACKET_RANDOM:
        random_list = generate_random_list(config.quantity,
                                           config.total_amount)

    index = 0
    while index < config.quantity:
        index += 1
        box = RedPacketBox()
        box.race_cid = config.race_cid
        box.rule_cid = config.rule_cid
        box.award_cid = config.cid
        box.award_msg = config.msg
        if config.category == CATEGORY_REDPACKET_CONST:
            box.award_amount = config.total_amount / config.quantity

        if config.category == CATEGORY_REDPACKET_RANDOM:
            current_sum_amount = sum(
                [award.award_amount for award in award_list])
            if current_sum_amount < config.total_amount:
                box.award_amount = random_list[len(award_list)]

        award_list.append(box)
        if len(award_list) > 2000:
            await RedPacketBox.insert_many(award_list)
            award_list = []

    if award_list:
        await RedPacketBox.insert_many(award_list)
Exemplo n.º 2
0
async def generate_awards_by_item_settings(basic_setting, settings):
    """

    :param basic_setting:
    :param settings:
    :return:
    """
    if not (basic_setting and settings):
        raise Exception('no basic_setting or settings')

    # 删除还未发出的红包
    await RedPacketBox.delete_many({
        'rule_cid': basic_setting.rule_cid,
        'member_cid': None
    })
    box_list = await RedPacketBox.aggregate(
        [
            MatchStage({
                'rule_cid': basic_setting.rule_cid,
                'award_cid': {
                    '$ne': None
                }
            }),
            GroupStage('award_cid', sum={'$sum': 1})
        ],
        read_preference=ReadPreference.PRIMARY).to_list(None)

    # 红包的发放情况
    get_situ = {box.id: box.sum for box in box_list}

    award_list = list()
    for config in settings:
        for _ in range(config.quantity - get_situ.get(config.cid, 0)):
            box = RedPacketBox()
            box.race_cid = basic_setting.race_cid
            box.rule_cid = basic_setting.rule_cid
            box.award_cid = config.cid
            box.award_msg = config.message
            box.award_amount = config.amount
            award_list.append(box)

    has_sent_count = sum(get_situ.values())
    while len(award_list) < basic_setting.expect_num - has_sent_count:
        box = RedPacketBox()
        box.race_cid = basic_setting.race_cid
        box.rule_cid = basic_setting.rule_cid
        box.award_msg = basic_setting.fail_msg
        award_list.append(box)

    shuffle(award_list)
    if award_list:
        for award in award_list:
            await award.save()
Exemplo n.º 3
0
setting_map = {}
for s in settings:
    s.rule_cid = rule.cid
    setting_map[s.cid] = s
    s.sync_save()
# db.TBL_RED_PACKET_ITEM_SETTING.update({}, {$rename : {"count" : "quantity"}}, false, true)

# 奖池
for entry in RedPacketEntry.sync_find({'race_cid': race.cid}):
    item = setting_map[entry.award_cid]
    box = RedPacketBox()
    box.race_cid = race.cid
    box.rule_cid = rule.cid
    box.award_cid = entry.award_cid
    box.award_msg = item.message
    box.award_amount = item.amount

    if entry.open_id:
        member = Member.sync_find_one({'open_id': entry.open_id})
        box.member_cid = member.cid

    hist = RedPacketAwardHistory.sync_find_one({'race_cid': race.cid, 'award_cid': entry.cid, 'issue_status': {'$ne': 1}})
    if hist:
        box.draw_status = STATUS_REDPACKET_AWARDED
        box.draw_dt = hist.request_dt
        box.error_msg = hist.error_msg
        box.request_dt = hist.request_dt
        box.checkpoint_cid = last_ck.cid
    else:
        box.draw_status = STATUS_REDPACKET_AWARD_FAILED