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

/**
  * 活动购买记录
*/
@index({ roleId: 1, activityId: 1 })

export default class Activity_Buy_Records extends BaseModel {
    @prop({ required: true })
    activityId: number;				// 活动Id
    @prop({ required: true })
    roleId: string;			        // 用户Id
    @prop({ required: true })
    type: number;				// 活动类型
    @prop({ required: true })
    pageIndex: number;			    // 第几页（成长基金使用）


    //添加购买记录
    public static async addRecord(activityId: number, roleId: string, type: number, pageIndex: number, lean = true) {
        let result: ActivityBuyRecordsModelType = await ActivityBuyRecordsModel.findOneAndUpdate({ roleId, activityId, type, pageIndex },
            {}, { upsert: true, new: true }).lean(lean);
        return result;
    }

    //根据活动id查询数据
    public static async findRecordsByActivityId(activityId: number, roleId: string, lean = true) {
        let result: ActivityBuyRecordsModelType[] = await ActivityBuyRecordsModel.find({ roleId, activityId }).lean(lean);
        return result;
    }

    //查询第几页的活动数据
    public static async findDataBypageIndex(activityId: number, type: number, roleId: string, pageIndex: number, lean = true) {
        let result: ActivityBuyRecordsModelType[] = await ActivityBuyRecordsModel.find({ roleId, type, activityId, pageIndex }).lean(lean);
        return result;
    }

    //删除活动购买记录
    public static async deleteActivity(activityId: number, roleId: string, pageIndex: number, cellIndex: number) {
        await ActivityBuyRecordsModel.deleteMany({ roleId, activityId, pageIndex, cellIndex });
    }
}

export const ActivityBuyRecordsModel = getModelForClass(Activity_Buy_Records);

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