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

/**
  * 活动系统 - 每日免费午餐、晚餐
*/
@index({ roleId: 1, activityId: 1 })

export default class Activity_Daily_Meal extends BaseModel {
    @prop({ required: true })
    serverId: number;				// 服id
    @prop({ required: true })
    activityId: number;				// 活动Id
    @prop({ required: true })
    roleId: string;			        // 用户Id
    @prop({ required: true })
    receiveTime: number;		    // 领取时间
    @prop({ required: true })
    type: number;			        // 1午餐、2晚餐类型
    @prop({ required: true })
    roundIndex: number;			    // 周期数




    //领取记录
    public static async addReceiveRecord(serverId: number, activityId: number, roleId: string, type: number, receiveTime: number, roundIndex: number) {
        await ActivityDailyMealModel.insertMany([
            {
                serverId,
                activityId,
                roleId,
                receiveTime,
                type,
                roundIndex,
            }
        ])
    }

    //根据活动时间查询活动数据
    public static async findData(serverId: number, activityId: number, roleId: string, type: number, roundIndex: number) {
        let result: ActivityDailyMealModelType[] = await ActivityDailyMealModel.find({
            serverId, roleId, activityId, type,
            roundIndex: roundIndex,
        }).lean(true);
        return result;
    }

    //删除活动领取记录
    public static async deleteActivity(serverId: number, activityId: number, roleId: string, type: number, beginTime: number, endTime: number) {
        await ActivityDailyMealModel.deleteMany({ serverId, roleId, activityId, type, receiveTime: { $gt: beginTime, $lt: endTime } });
    }
}

export const ActivityDailyMealModel = getModelForClass(Activity_Daily_Meal);

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