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


export default async function (call: ApiCall<ReqOpen, ResOpen>) {
    let payids = G.gc.shiwuleichong.tasks.map(
        (i) => i.payid
    );

    let day: number = 1;
    let _zt = PublicShared.getToDayZeroTime();
    let pays = await PayFun.getPayLogs(call.uid, payids);
    for (; day <= G.gc.shiwuleichong.tasks.length; day++) {
        let pay = pays[G.gc.shiwuleichong.tasks[day - 1].payid];
        if (!pay || pay.length == 0) {
            break
        } else {
            // 如果是最后一天奖励
            if (day >= G.gc.shiwuleichong.tasks.length) {
                // 充值订单隔天了
                if (pay.slice(-1)[0].time < _zt) {
                    // 重置所有订单
                    await PayFun.delPayLog(call.uid, ...payids.map(i => {
                        return {payId: i, val: []}
                    }))
                    day = 1;
                    pays = {};
                }
                break;
            } else if (pay.slice(-1)[0].time >= _zt) {
                break
            }
        }
    }

    call.succ({
        day: day,
        payIds: Object.keys(pays).filter((i) => pays[i].length > 0)
    });
}