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

/**
  * 活动系统 - 今日挑战活动
*/
@index({ roleId: 1 })

export default class Activity_Daily_Challenges extends BaseModel {
    @prop({ required: true })
    serverId: number;				// 服id
    @prop({ required: true })
    activityId: number;				// 活动Id
    @prop({ required: true })
    roleId: string;			        // 用户Id
    @prop({ required: true })
    dayIndex: number;				// 第几天
    @prop({ required: true })
    cellIndex: number;			    // 第几天的第几个奖励
    @prop({ required: true })
    type: number;			        // 任务类型
    @prop({ required: true })
    totalCount: number;				// 累计达成次数
    @prop({ required: true })
    receiveRewardCount: number;					// 领取奖励次数
    @prop({ required: true })
    data: string;					// 数据信息

    //任务领取记录
    public static async addCellRecord(serverId: number, activityId: number, roleId: string, dayIndex: number, cellIndex: number, type: number, count: number, lean = true) {
        let result: ActivityDailyChallengesModelType = await ActivityDailyChallengesModel.findOneAndUpdate({ serverId, roleId, activityId, dayIndex, cellIndex, type },
            { $inc: { receiveRewardCount: count } }, { upsert: true, new: true }).lean(lean);
        return result;
    }

    //根据活动统计完成任务次数
    public static async setTaskCount(serverId: number, activityId: number, roleId: string, dayIndex: number, cellIndex: number, type: number, count: number, lean = true) {
        let result: ActivityDailyChallengesModelType = await ActivityDailyChallengesModel.findOneAndUpdate({ serverId, roleId, activityId, dayIndex, cellIndex, type },
            { $set: { totalCount: count } }, { upsert: true, new: true }).lean(lean);
        return result;
    }

    //根据活动统计完成任务次数
    public static async addTaskCount(serverId: number, activityId: number, roleId: string, dayIndex: number, cellIndex: number, type: number, addCount: number, lean = true) {
        let result: ActivityDailyChallengesModelType = await ActivityDailyChallengesModel.findOneAndUpdate({ serverId, roleId, activityId, dayIndex, cellIndex, type },
            { $inc: { totalCount: addCount } }, { upsert: true, new: true }).lean(lean);
        return result;
    }

    //根据活动记录统计数据
    public static async addTaskRecord(serverId: number, activityId: number, roleId: string, dayIndex: number, cellIndex: number, type: number, data: string,) {
        let result: ActivityDailyChallengesModelType = await ActivityDailyChallengesModel.findOneAndUpdate({ serverId, roleId, activityId, dayIndex, cellIndex, type },
            { $set: { data: data } }, { upsert: true, new: true }).lean(true);
        return result;
    }


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

    //查询第几天的活动数据
    public static async findDataByDayIndex(serverId: number, activityId: number, roleId: string, dayIndex: number, lean = true) {
        let result: ActivityDailyChallengesModelType[] = await ActivityDailyChallengesModel.find({ serverId, roleId, activityId, dayIndex }).lean(lean);
        return result;
    }

    //查询第几天某个的活动数据
    public static async findDataByCellIndex(serverId: number, activityId: number, roleId: string, dayIndex: number, cellIndex: number, type: number) {
        let result: ActivityDailyChallengesModelType = await ActivityDailyChallengesModel.findOne({ serverId, roleId, activityId, dayIndex, cellIndex, type }).lean(true);
        return result;
    }

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

export const ActivityDailyChallengesModel = getModelForClass(Activity_Daily_Challenges);

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