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

/**
  * 成长基金活动
*/
@index({ roleId: 1, activityId: 1 })

export default class Activity_Growth_Fund extends BaseModel {
    @prop({ required: true })
    activityId: number;				// 活动Id
    @prop({ required: true })
    roleId: string;			        // 用户Id
    @prop({ required: true })
    pageIndex: number;				// 第几页
    @prop({ required: true })
    cellIndex: number;			    // 第*页的第几个奖励
    @prop({ required: true })
    isReceive: boolean;				// 是否领取过奖励


    //添加领取记录
    public static async addRecord(activityId: number, roleId: string, pageIndex: number, cellIndex: number, lean = true) {
        let result: ActivityGrowthFundModelType = await ActivityGrowthFundModel.findOneAndUpdate({ roleId, activityId, pageIndex, cellIndex },
            { $set: { isReceive: true } }, { upsert: true, new: true }).lean(lean);
        return result;
    }

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

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

    //查询第*页的某个的活动数据
    public static async findDataByCellIndex(activityId: number, roleId: string, pageIndex: number, cellIndex: number, lean = true) {
        let result: ActivityGrowthFundModelType[] = await ActivityGrowthFundModel.find({ roleId, activityId, pageIndex, cellIndex }).lean(lean);
        return result;
    }

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

export const ActivityGrowthFundModel = getModelForClass(Activity_Growth_Fund);

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