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

/**
  * 月卡 
*/
@index({ roleId: 1, activityId: 1 })

export default class Activity_Monthly_Ticket extends BaseModel {

    @prop({ required: true })
    activityType: number;		// 活动类型
    @prop({ required: true })
    activityId: number;		    // 活动ID
    @prop({ required: true })
    serverId: number;		    // 区号
    @prop({ required: true })
    roleId: string;				// 用户id
    @prop({ required: true })
    endTime: Date;				// 结束时间
    @prop({ required: true })
    dayIndex: number;		    // 结算到第几天，从0开始，1表示第一天已经领取过(包括邮件发放)
    @prop({ required: true })
    isForever: boolean;		    // 是否是永久卡

    //记录领取的天数
    public static async setDayIndex(serverId: number, roleId: string, activityId: number, dayIndex: number) {
        let result: ActivityMonthlyTicketModelType = await ActivityMonthlyTicketModel.findOneAndUpdate({ serverId, roleId, activityId, dayIndex: { $lt: dayIndex } },
            { $set: { dayIndex } },
            { new: true }).lean(true);
        return result;
    }

    //查询月卡详情
    public static async findMonthlyTicket(serverId: number, roleId: string, activityId: number) {
        let result: ActivityMonthlyTicketModelType = await ActivityMonthlyTicketModel.findOne({ serverId, roleId, activityId }).sort({ endTime: -1 }).lean(true);
        return result;
    }

    //购买月卡
    public static async buyMonthlyTicket(serverId: number, roleId: string, activityId: number, activityType: number, endTime: Date, isForever: boolean) {
        await ActivityMonthlyTicketModel.findOneAndUpdate({ serverId, roleId, activityId, activityType }, { $set: { endTime, dayIndex: 0, isForever } }, { upsert: true, new: true }).lean();
    }
}

export const ActivityMonthlyTicketModel = getModelForClass(Activity_Monthly_Ticket);

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