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

/**
  * 首个礼包
*/
@index({ roleId: 1, activityId: 1 })

export default class Activity_First_Gift extends BaseModel {
    @prop({ required: true })
    activityId: number;				// 活动Id
    @prop({ required: true })
    roleId: string;			        // 用户Id
    @prop({ required: true })
    pageIndex: number;			    // 活动第几页
    @prop({ required: true })
    days: Array<number>;		    // 领取过的奖励
    @prop({ required: true })
    isOver: boolean;		    // 是否全部领取结束

    //添加领取记录
    public static async addRecord(activityId: number, roleId: string, pageIndex: number, indexArray: number[], isOver: boolean) {
        let result: ActivityFirstGiftModelType = await ActivityFirstGiftModel.findOneAndUpdate({ roleId, activityId, pageIndex },
            { $push: { days: { $each: indexArray } }, $set: { isOver: isOver } }, { new: true }).lean(true);
        return result;
    }

    //可以领取首充礼包，充值时间记录
    public static async begin(activityId: number, roleId: string, pageIndex: number) {
        let result: ActivityFirstGiftModelType = await ActivityFirstGiftModel.findOneAndUpdate({ roleId, activityId, pageIndex },
            {}, { upsert: true, new: true }).lean(true);
        return result;
    }


    public static async findData(activityId: number, roleId: string) {
        let result: ActivityFirstGiftModelType[] = await ActivityFirstGiftModel.find({ roleId, activityId }, {}).lean(true);
        return result;
    }

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

export const ActivityFirstGiftModel = getModelForClass(Activity_First_Gift);

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