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

/**
  * 活动系统 - 每日活动
  * 1.每日特惠礼包（游戏币）
*/
@index({ roleId: 1, activityId: 1 })

export default class Activity_Daily_Gifts 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 })
    buyCount: number;		        // 购买次数

    //购买记录
    public static async buyRecord(serverId: number, activityId: number, roleId: string, dayIndex: number, cellIndex: number, count: number, lean = true) {
        let result: ActivityDailyGiftsModelType = await ActivityDailyGiftsModel.findOneAndUpdate({ serverId, roleId, activityId, dayIndex, cellIndex },
            { $inc: { buyCount: count } }, { upsert: true, new: true }).lean(lean);
        return result;
    }

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

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

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

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

export const ActivityDailyGiftsModel = getModelForClass(Activity_Daily_Gifts);

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