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

/**
  * 累计充值活动 
*/
@index({ roleId: 1, activityId: 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 })
    poolId: number;		    // 奖池唯一id
    @prop({ required: true })
    reward: string;		    // 领取奖励内容
    @prop({ required: true })
    rewardTime: Date;		    // 领取奖励时间

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

    //查询
    public static async findDataByTime(serverId: number, roleId: string, beginTime: Date, endTime: Date) {
        let result: ActivityRechargeMoneyModelType[] = await ActivityRechargeMoneyModel.find({ serverId, roleId, beginTime: { $gte: beginTime }, endTime: { $lte: endTime } }).sort({ beginTime: 1 }).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>; // 将所有字段变成可选项