import BaseModel from './BaseModel';
import { index, getModelForClass, prop, DocumentType } from '@typegoose/typegoose';
import { UpdateTaskParam } from '../domain/roleField/task';

/**
  * 活动系统 - 通用的刷新任务（分页，可刷新，限制领取次数）
*/
@index({ roleId: 1, activityId: 1 })

export default class Activity_Refresh_Task extends BaseModel {
    @prop({ required: true })
    serverId: number;				// 服id
    @prop({ required: true })
    activityId: number;				// 活动Id
    @prop({ required: true })
    roleId: string;			        // 用户Id
    @prop({ required: true })
    roundIndex: number;				// 周期数
    @prop({ required: true })
    pageIndex: number;			    // 第几页
    @prop({ required: true })
    refTime: number;		    	// 刷新时间
    @prop({ required: true })
    id: number;			            // id
    @prop({ required: true })
    taskType: number;			        // 任务类型
    @prop({ required: true })
    totalCount: number;				// 累计达成次数
    @prop({ required: true })
    receiveRewardCount: number;		// 领取奖励次数
    @prop({ required: true })
    data: string;					// 数据信息
    @prop({ required: true, type: String })
    records: string[];					// 数据信息

    //任务领取记录
    public static async addReceiveRecord(serverId: number, activityId: number, roleId: string, roundIndex: number, pageIndex: number, id: number, refTime: number, count: number) {
        let result: ActivityRefreshTaskModelType = await ActivityRefreshTaskModel.findOneAndUpdate({ serverId, roleId, activityId, roundIndex, pageIndex, id, refTime },
            { $inc: { receiveRewardCount: count } }, { upsert: true, new: true }).lean(true);
        return result;
    }


    // 更新任务
    public static async setOrIncTask(serverId: number, activityId: number, roleId: string, roundIndex: number, pageIndex: number, id: number, refTime: number, taskType: number, param: UpdateTaskParam) {
        if(param.set) {
            return await this.setTaskCount(serverId, activityId, roleId, roundIndex, pageIndex, id, refTime, taskType, param.set, param.records);
        } else if (param.inc) {
            return await this.addTaskCount(serverId, activityId, roleId, roundIndex, pageIndex, id, refTime, taskType, param.inc, param.records);
        } else if (param.records){
            let result: ActivityRefreshTaskModelType = await ActivityRefreshTaskModel.findOneAndUpdate({ serverId, roleId, activityId, roundIndex, pageIndex, id, refTime },
                { $set: { records: param.records, taskType } }, { upsert: true, new: true }).lean();
            return result;
        }
    }


    //根据活动统计完成任务次数
    public static async setTaskCount(serverId: number, activityId: number, roleId: string, roundIndex: number, pageIndex: number, id: number, refTime: number, taskType: number, count: number, records?: string[]) {
        let result: ActivityRefreshTaskModelType = await ActivityRefreshTaskModel.findOneAndUpdate({ serverId, roleId, activityId, roundIndex, pageIndex, id, refTime },
            { $set: { totalCount: count, records: records||[], taskType }}, { upsert: true, new: true }).lean();
        return result;
    }

    //根据活动统计完成任务次数
    public static async addTaskCount(serverId: number, activityId: number, roleId: string, roundIndex: number, pageIndex: number, id: number, refTime: number, taskType: number, count: number, records?: string[]) {
        let result: ActivityRefreshTaskModelType = await ActivityRefreshTaskModel.findOneAndUpdate({ serverId, roleId, activityId, roundIndex, pageIndex, id, refTime },
            { $inc: { totalCount: count }, $set: { records: records||[], taskType } }, { upsert: true, new: true }).lean();
        return result;
    }


    //根据活动id查询活动数据
    public static async findData(serverId: number, activityId: number, roleId: string, roundIndex: number,) {
        let result: ActivityRefreshTaskModelType[] = await ActivityRefreshTaskModel.find({ serverId, roleId, activityId, roundIndex }).lean(true);
        return result;
    }

    //查询活动数据
    public static async findDataById(serverId: number, activityId: number, roleId: string, roundIndex: number, pageIndex: number, id: number, refTime: number) {
        let result: ActivityRefreshTaskModelType = await ActivityRefreshTaskModel.findOne({ serverId, roleId, activityId, roundIndex, pageIndex, id, refTime }).lean(true);
        return result;
    }

    //删除活动领取记录
    public static async deleteActivity(serverId: number, activityId: number, roleId: string, roundIndex: number) {
        await ActivityRefreshTaskModel.deleteMany({ serverId, roleId, activityId, roundIndex });
    }
}

export const ActivityRefreshTaskModel = getModelForClass(Activity_Refresh_Task);

export interface ActivityRefreshTaskModelType extends Pick<DocumentType<Activity_Refresh_Task>, keyof Activity_Refresh_Task> { }
export type ActivityRefreshTaskModelTypeParam = Partial<ActivityRefreshTaskModelType>; // 将所有字段变成可选项