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

/**
  * 自助商店货架上的商品
*/
@index({ roleId: 1, activityId: 1 })

export default class Activity_Self_Service_Goods extends BaseModel {
    @prop({ required: true })
    activityId: number;				// 活动Id
    @prop({ required: true })
    roundIndex: number;				// 活动第几周期 从1开始，根据活动开始时间计算    
    @prop({ required: true })
    roleId: string;			        // 用户Id
    @prop({ required: true })
    index: number;				    // 第几个货架从1开始
    @prop({ required: true })
    cellIndex: number;				// 第几个坑位从1开始
    @prop({ required: true })
    rewardIndex: number;		    // 礼包中第几项奖励


    //添加选中的物品
    public static async addGoods(activityId: number, roleId: string, roundIndex: number, index: number, cellIndex: number, rewardIndex: number) {
        let result: ActivitySelfServiceGoodsModelType = await ActivitySelfServiceGoodsModel.findOneAndUpdate({ roleId, activityId, roundIndex, index, cellIndex },
            { $set: { rewardIndex } }, { upsert: true, new: true }).lean(true);
        return result;
    }

    public static async findData(activityId: number, roleId: string, roundIndex: number, lean = true) {
        let result: ActivitySelfServiceGoodsModelType[] = await ActivitySelfServiceGoodsModel.find({ roleId, activityId, roundIndex }, {
            index: 1, cellIndex: 1, rewardIndex: 1, _id: -1
        }).lean(lean);
        return result;
    }

    //查询第几个货架数据
    public static async findDataByIndex(activityId: number, roleId: string, roundIndex: number, index: number, lean = true) {
        let result: ActivitySelfServiceGoodsModelType[] = await ActivitySelfServiceGoodsModel.find({ roleId, activityId, roundIndex, index }).lean(lean);
        return result;
    }

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

export const ActivitySelfServiceGoodsModel = getModelForClass(Activity_Self_Service_Goods);

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