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

/**
  * 购买记录
*/

export class BuyRecord {
    @prop({ required: true })
    id: number; //物品id标识
    @prop({ required: true })
    time: Date; //购买时间
}


/**
  * 每日特惠礼包RMB购买4挡,一次性支付购买7天,每天只能领取当天的礼包奖励
*/


@index({ roleId: 1, activityId: 1 })

export default class Activity_Daily_RMB_Gifts extends BaseModel {
    @prop({ required: true })
    serverId: number;				// 服Id
    @prop({ required: true })
    activityId: number;				// 活动Id
    @prop({ required: true })
    roleId: string;			        // 用户Id
    @prop({ required: true })
    beginTime: Date;	            // 开始时间
    @prop({ required: true })
    todayIndex: number;	            // 开始时间
    @prop({ required: true })
    isBuy: boolean;	                // 是否已经购买过
    @prop({ required: true })
    records: BuyRecord[];	       // 领取记录

    //添加领取记录
    public static async receiveRecord(serverId: number, activityId: number, roleId: string, beginTime: Date, todayIndex: number, ids: number[]) {
        let data = [];
        for (let id of ids) {
            data.push({
                id,
                time: new Date()
            })
        }
        let result = await ActivityDailyRMBGiftsModel.findOneAndUpdate(
            { serverId, roleId, activityId, beginTime, todayIndex }, { $push: { records: { $each: data } } }, { upsert: true, new: true }
        ).lean(true)
        return result;
    }

    //添加购买记录
    public static async buyRecord(serverId: number, activityId: number, roleId: string, beginTime: Date, todayIndex: number, isBuy: boolean) {
        let result = await ActivityDailyRMBGiftsModel.findOneAndUpdate(
            { serverId, roleId, activityId, beginTime, todayIndex }, { $set: { isBuy } }, { upsert: true, new: true }
        ).lean(true)
        return result;
    }

    //历史领取的记录
    public static async findRecord(serverId: number, activityId: number, roleId: string, beginTime: Date, todayIndex: number) {
        let result: ActivityDailyRMBGiftsModelType = await ActivityDailyRMBGiftsModel.findOne(
            { serverId, roleId, activityId, beginTime, todayIndex }).lean(true);
        return result;
    }

    //获取已经购买还没有领取的数据
    public static async findBuyRecord(serverId: number, activityId: number, roleId: string, beginTime: Date, todayIndex: number) {
        let result: ActivityDailyRMBGiftsModelType[] = await ActivityDailyRMBGiftsModel.find(
            { serverId, roleId, activityId, beginTime, todayIndex: { $gte: todayIndex } }).lean(true);
        return result;
    }

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

export const ActivityDailyRMBGiftsModel = getModelForClass(Activity_Daily_RMB_Gifts);

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