import {ApiCall} from "tsrpc";
import {Reqpopup, Respopup} from "../../monopoly/protocols/gift/Ptlpopup";
import {redisClient} from "../../setRedis";

export default async function (call: ApiCall<Reqpopup, Respopup>) {
    // 给用户推送礼包的逻辑实现
    let popup_info = call.req

    console.log("收到礼包推送", popup_info)

    let userPid = await redisClient.hGet(G.redis.fromatKey("uid2processId"), popup_info.game_user_id)

    let giftLog: any = {...popup_info}

    if (userPid) {
        let duration = popup_info.duration || (popup_info.template_id == '100' && 86400) || (popup_info.template_id == '200' && 8640000) || 86400

        giftLog = {
            ...popup_info,
            duration: duration,
            showTime: G.time,
            endTime: G.time + duration
        }
        G.server.sendMsgByUid(popup_info.game_user_id, 'msg_s2c/SendGift', giftLog)
    }

    G.mongodb.collection('giftLog').updateOne({popup_id: giftLog.popup_id}, {$set: giftLog}, {upsert: true});

    call.succ({})
}