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

/**
  * 30天目标活动
*/
@index({ roleId: 1, activityId: 1 })

export default class Activity_Thirty_Days extends BaseModel {
    @prop({ required: true })
    serverId: number;				// 服Id
    @prop({ required: true })
    activityId: number;				// 活动Id
    @prop({ required: true })
    roleId: string;			        // 用户Id
    @prop({ required: true })
    pageIndex: number;				// 第几页
    @prop({ required: true })
    cellIndex: number;			    // 第*页的第几个奖励
    @prop({ required: true })
    tab: number;			        // 具体任务id
    @prop({ required: true })
    taskType: number;			        // 任务类型
    @prop({ required: true })
    totalCount: number;				// 累计达成次数
    @prop({ required: true })
    isReceive: boolean;				// 是否领取过奖励
    @prop({ required: true })
    data: string;					// 数据信息
    @prop({ required: true, type: String })
    records: string[];					// 数据信息

    //添加领取记录
    public static async addRecord(serverId: number, activityId: number, roleId: string, pageIndex: number, cellIndex: number, tab: number) {
        let result: ActivityThirtyDaysModelType = await ActivityThirtyDaysModel.findOneAndUpdate({ serverId, roleId, activityId, pageIndex, cellIndex, tab },
            { $set: { isReceive: true } }, { upsert: true, new: true }).lean(true);
        return result;
    }

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

    //查询第几页的活动数据
    public static async findDataBypageIndex(serverId: number, activityId: number, roleId: string, pageIndex: number, lean = true) {
        let result: ActivityThirtyDaysModelType[] = await ActivityThirtyDaysModel.find({ serverId, roleId, activityId, pageIndex }).lean(lean);
        return result;
    }


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



    //查询第*页的某个的活动数据
    public static async findDataByCellIndex(serverId: number, activityId: number, roleId: string, pageIndex: number, cellIndex: number, tab: number,) {
        let result: ActivityThirtyDaysModelType = await ActivityThirtyDaysModel.findOne({ serverId, roleId, activityId, pageIndex, cellIndex, tab }).lean(true);
        return result;
    }

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

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

export const ActivityThirtyDaysModel = getModelForClass(Activity_Thirty_Days);

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