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

/**
  * 累计充值活动 
*/
@index({ roleId: 1 })

export default class Activity_Recharge_Money extends BaseModel {

    @prop({ required: true })
    serverId: number;		    // 区号
    @prop({ required: true })
    roleId: string;				// 用户id
    @prop({ required: true })
    beginTime: Date;			// 充值开始统计时间
    @prop({ required: true })
    endTime: Date;			// 充值结束统计时间
    @prop({ required: true })
    RMB: number;			// 充值金额
    @prop({ required: true })
    index: number;		    // 第几个格子
    @prop({ required: true })
    reward: string;		    // 领取奖励内容
    @prop({ required: true })
    rewardTime: Date;		    // 领取奖励时间
    @prop({ required: true })
    isEnd: boolean;		    // 结束

    //添加领取记录
    public static async addRecord(_id: string, id: number, reward: string) {
        let result: ActivityRechargeMoneyModelType = await ActivityRechargeMoneyModel.findOneAndUpdate({ _id },
            { $set: { index: id, reward, rewardTime: new Date() } },
            { upsert: true, new: true }).lean(true);
        return result;
    }

    //本轮结束
    public static async setEndState(serverId: number, roleId: string, _ids: string[]) {
        await ActivityRechargeMoneyModel.updateMany({ serverId, roleId, _id: { $in: _ids } },
            { $set: { isEnd: true } }, { multi: true, });
    }

    //查询
    public static async findData(serverId: number, roleId: string) {
        let result: ActivityRechargeMoneyModelType[] = await ActivityRechargeMoneyModel.find({ serverId, roleId, isEnd: { $ne: true } }).lean(true);
        return result;
    }

    //统计支付金额
    public static async addRMB(serverId: number, roleId: string, RMB: number, beginTime: Date, endTime: Date) {
        let result: ActivityRechargeMoneyModelType = await ActivityRechargeMoneyModel.findOneAndUpdate({ serverId, roleId, beginTime, endTime },
            { $inc: { RMB } },
            { upsert: true, new: true }).lean(true);
        return result;
    }
}

export const ActivityRechargeMoneyModel = getModelForClass(Activity_Recharge_Money);

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