import { ApiCall } from "tsrpc";
import { PayFun } from '../../../public/pay';
import { ReqOpen, ResOpen } from "../../../shared/protocols/event/zhoulibao/PtlOpen";

export default async function (call: ApiCall<ReqOpen, ResOpen>) {

    let db = await G.mongodb.cEvent('zhoulibao').findOne({ uid: call.uid, type: 'zhoulibao' });

    if (!db) {
        G.mongodb.cEvent('zhoulibao').updateOne(
            { uid: call.uid, type: 'zhoulibao' },
            { $set: { sTime: G.time } },
            { upsert: true }
        );
    } else if (db.sTime + G.gc.dixiaheishi.zhoulibao.time < G.time) {
        db.sTime = G.time;
        G.mongodb.cEvent('zhoulibao').updateOne(
            { uid: call.uid, type: 'zhoulibao' },
            { $set: { sTime: G.time } },
        );
        PayFun.delPayLog(call.uid, ...G.gc.dixiaheishi.zhoulibao.pays.map(p => { return { payId: p.payId, val: [] }; }));
    }

    call.succ({
        sTime: db?.sTime || G.time
    });
}