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

/**
  * 购买记录
*/

export class ActivityNewHeroGiftRecord {
    @prop({ required: true })
    index: number; //下标记录
    @prop({ required: true })
    point: number; //消耗点数
    @prop({ required: true })
    time: Date; //兑换时间
}


/**
  * 活动系统 -  新将好礼(很多红包，用积分兑换一遍结束),点数兑换奖品记录
*/
@index({ roleId: 1, activityId: 1 })

export default class Activity_New_Hero_Gift extends BaseModel {
    @prop({ required: true })
    serverId: number;				// 服id
    @prop({ required: true })
    activityId: number;				// 活动Id
    @prop({ required: true })
    roleId: string;			        // 用户Id
    @prop({ required: true })
    totalPoint: number;			    // 获得总点数
    @prop({ required: true })
    records: ActivityNewHeroGiftRecord[];		        // 兑换红包消耗点数记录

    //兑换记录
    public static async addPoint(serverId: number, activityId: number, roleId: string, addPoint: number) {
        let result: ActivityNewHeroGiftModelType = await ActivityNewHeroGiftModel.findOneAndUpdate({ serverId, roleId, activityId, },
            { $inc: { totalPoint: addPoint } }, { upsert: true, new: true }).lean(true);
        return result;
    }

    //兑换记录
    public static async addReceiveRecord(serverId: number, activityId: number, roleId: string, index: number, point: number) {
        let result: ActivityNewHeroGiftModelType = await ActivityNewHeroGiftModel.findOneAndUpdate({ serverId, roleId, activityId, },
            { $push: { records: { index, point, time: new Date() } } }, { upsert: true, new: true }).lean(true);
        return result;
    }

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

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

export const ActivityNewHeroGiftModel = getModelForClass(Activity_New_Hero_Gift);

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