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


export class Record {
    @prop({ required: true })
    warId: number; //关卡id
    @prop({ required: true })
    time: Date; //时间
}

/**
  * 活动系统 - 新将演绎活动
*/
@index({ roleId: 1, activityId: 1 })

export default class Activity_New_Hero_GK extends BaseModel {
    @prop({ required: true })
    serverId: number;				// 服id
    @prop({ required: true })
    activityId: number;				// 活动Id
    @prop({ required: true })
    roleId: string;			        // 用户Id
    @prop({ required: true })
    records: Record[];				// 通关记录

    //通关记录
    public static async addRecord(serverId: number, activityId: number, roleId: string, warId: number) {
        let result: ActivityNewHeroGKModelType = await ActivityNewHeroGKModel.findOneAndUpdate({ serverId, roleId, activityId },
            { $push: { records: { warId, time: new Date() } } }, { upsert: true, new: true }).lean(true);
        return result;
    }

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

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

export const ActivityNewHeroGKModel = getModelForClass(Activity_New_Hero_GK);

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