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

/**
  * 30天目标活动
*/
@index({ roleId: 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 })
    type: number;			        // 任务类型
    @prop({ required: true })
    totalCount: number;				// 累计达成次数
    @prop({ required: true })
    isReceive: boolean;				// 是否领取过奖励
    @prop({ required: true })
    data: string;					// 数据信息
    @prop({ required: true })
    isPush: boolean;				// 推送过消息



    //添加领取记录
    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;
    }

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

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

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

    //推送标记
    public static async pushMessage(serverId: number, activityId: number, roleId: string, pageIndex: number, cellIndex: number, tab: number, type: number) {
        let result: ActivityThirtyDaysModelType = await ActivityThirtyDaysModel.findOneAndUpdate({ serverId, roleId, activityId, pageIndex, cellIndex, tab, type },
            { $set: { isPush: 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 findDataByCellIndex(serverId: number, activityId: number, roleId: string, pageIndex: number, cellIndex: number, tab: number, type: number,) {
        let result: ActivityThirtyDaysModelType = await ActivityThirtyDaysModel.findOne({ serverId, roleId, activityId, pageIndex, cellIndex, tab, type }).lean(true);
        return result;
    }

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

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>; // 将所有字段变成可选项