import { ApiCall } from "tsrpc";
import { ActionLog } from '../../../public/actionLog/actionLog';
import { ReqOpen, ResOpen } from "../../../shared/protocols/event/jierihuodong/PtlOpen";
import { PublicShared } from '../../../shared/public/public';

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

    let db = await G.mongodb.cEvent('jierihuodong').findOne({ uid: call.uid, type: 'jierihuodong' });
    let recTask = db?.recTask || [];

    if (!db || db.refreshTime < PublicShared.getToDayZeroTime()) {
        G.mongodb.cEvent('jierihuodong').updateOne(
            { uid: call.uid, type: 'jierihuodong' },
            { $set: { refreshTime: G.time, recTask: [] } },
            { upsert: true }
        );
        recTask = [];
    }

    let taskIds = [...new Set(G.gc.jierihuodong.huodongrenwu.map(r => r.taskId))];

    call.succ({
        recTask: recTask,
        recLogin: db?.recLogin || [],
        recordLibao: db?.recordLibao || {},
        recordDuihuan: db?.recordDuihuan || {},
        taskFinish: await ActionLog.getDayLog(call.uid, taskIds)
    });
}