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

class RewardRecord {
    @prop({ required: true })
    id: number;		    // 奖励id
    @prop({ required: true })
    time: Date;		    // 领取时间
}

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

export default class Activity_Vip_Recharge_Money extends BaseModel {

    @prop({ required: true })
    serverId: number;		    // 区号
    @prop({ required: true })
    activityId: number;		    // 活动id
    @prop({ required: true })
    roleId: string;				// 用户id
    @prop({ required: true })
    RMB: number;			   // 充值金额
    @prop({ required: true })
    record: RewardRecord[];		// 领取奖励记录
    @prop({ required: true })
    isEnd: boolean;		    // 结束

    //添加领取记录
    public static async addRecord(serverId: number, activityId: number, roleId: string, id: number) {
        let result: ActivityVipRechargeMoneyModelType = await ActivityVipRechargeMoneyModel.findOneAndUpdate({ serverId, activityId, roleId },
            { $push: { record: { id, time: new Date() } } },
            { upsert: true, new: true }).lean(true);
        return result;
    }

    //活动结束
    public static async setEndState(serverId: number, activityId: number, roleId: string) {
        await ActivityVipRechargeMoneyModel.findOneAndUpdate({ serverId, activityId, roleId },
            { $set: { isEnd: true } }).lean(true);
    }

    //查询
    public static async findData(serverId: number, roleId: string, activityId: number) {
        let result: ActivityVipRechargeMoneyModelType = await ActivityVipRechargeMoneyModel.findOne({ serverId, roleId, activityId }).lean(true);
        return result;
    }

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

export const ActivityVipRechargeMoneyModel = getModelForClass(Activity_Vip_Recharge_Money);

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