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


/**
  * 自助商店
*/
@index({ roleId: 1, activityId: 1 })

export default class Activity_Self_Service_Shop extends BaseModel {
    @prop({ required: true })
    serverId: number;				// 服
    @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 })
    goods: string;		            // 商品信息

    //添加购买记录
    public static async addBuyRecord(serverId: number, activityId: number, roleId: string, roundIndex: number, index: number, goods: string) {
        await ActivitySelfServiceShopModel.insertMany([
            { serverId, roleId, activityId, roundIndex, index, goods }
        ])
    }

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

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

    //查询购买价格类型的数据
    public static async findDataByPriceType(serverId: number, activityId: number, roleId: string, roundIndex: number, lean = true) {
        let result: ActivitySelfServiceShopModelType[] = await ActivitySelfServiceShopModel.find({ serverId, roleId, activityId, roundIndex }).lean(lean);
        return result;
    }

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

export const ActivitySelfServiceShopModel = getModelForClass(Activity_Self_Service_Shop);

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