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

export class Activity_Sign_In_Record {
    @prop({ required: true })
    dayIndex: number;				// 第几天
    @prop({ required: true })
    time: number;			        // 时间

    constructor(dayIndex: number, time: number) {
        this.dayIndex = dayIndex;
        this.time = time;
    }
}

/**
  * 签到
*/
@index({ roleId: 1, activityId: 1 })

export default class Activity_Sign_In extends BaseModel {
    @prop({ required: true })
    activityId: number;				// 活动Id
    @prop({ required: true })
    roleId: string;			        // 用户Id
    @prop({ required: true })
    roundIndex: number;				// 周期数，从活动设置的开始时间计算
    @prop({ required: true, type: Activity_Sign_In_Record, _id: false })
    records: Activity_Sign_In_Record[];			    // 领取记录

    //签到记录
    public static async addSignInRecord(activityId: number, roleId: string, roundIndex: number, dayIndex: number) {
        let result: ActivitySignInModelType = await ActivitySignInModel.findOneAndUpdate({ roleId, activityId, roundIndex },
            { $push: { records: new Activity_Sign_In_Record(dayIndex, Date.now()) } }, { upsert: true, new: true }).lean(true);
        return result;
    }

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

    //购买vip记录
    public static async buyVIP(activityId: number, roleId: string, roundIndex: number) {
        let result: ActivitySignInModelType = await ActivitySignInModel.findOneAndUpdate({ roleId, activityId, roundIndex },
            {}, { upsert: true, new: true }).lean(true);
        return result;
    }

    //删除签到记录
    public static async deleteActivity(activityId: number, roleId: string, roundIndex: number) {
        await ActivitySignInModel.deleteMany({ roleId, activityId, roundIndex });
    }
}

export const ActivitySignInModel = getModelForClass(Activity_Sign_In);

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