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

/**
  * 活动系统 - 弹出商店的记录
*/
@index({ roleId: 1, effectBeginTime: 1 })

export default class Activity_Pop_Up_Shop_Rec extends BaseModel {
    @prop({ required: true })
    serverId: number;				// 服id

    @prop({ required: true })
    activityId: number;				// 活动Id

    @prop({ required: true })
    roleId: string;			        // 用户Id

    @prop({ required: true })
    effectBeginTime: Date;		    // 刷新等生效开始时间

    @prop({ required: true })
    effectEndTime: Date;		    // 刷新等生效结束时间

    @prop({ required: true })
    id: number;				        // 礼包id

    @prop({ required: true })
    conditionType: number;				        // 礼包id
    
    @prop({ required: true })
    num: number;                    // 值

    public static async addRecord(serverId: number, activityId: number, roleId: string, id: number, effectBeginTime: Date, effectEndTime: Date, conditionType: number, num: number) {
        let rec: ActivityPopUpShopRecModelType = await ActivityPopUpShopRecModel.findOneAndUpdate(
            { roleId, serverId, activityId, effectBeginTime, effectEndTime, id }, 
            { $inc: { num }, $setOnInsert: { conditionType } },
            { new: true, upsert: true }).lean();
        return rec;
    }
}

export const ActivityPopUpShopRecModel = getModelForClass(Activity_Pop_Up_Shop_Rec);

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