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

/**
  * 活动系统 - 每日兑换铜币
*/
@index({ roleId: 1 })

export default class Activity_Daily_Coin extends BaseModel {
    @prop({ required: true })
    serverId: number;				// 服id
    @prop({ required: true })
    activityId: number;				// 活动Id
    @prop({ required: true })
    roleId: string;			        // 用户Id
    @prop({ required: true })
    beginTime: number;		        // 开始统计的时间
    @prop({ required: true })
    exchangeCount: number;			// 兑换次数
    @prop({ required: true })
    coinCount: number;			    // 兑换获得铜币数量
    @prop({ required: true })
    recordMsg: string[];			    // 兑换记录 exchangeCount&cointCount



    //兑换记录
    public static async addExchangeRecord(serverId: number, activityId: number, roleId: string, beginTime: number, count: number, coinCount: number, msg: string) {
        let result = await ActivityDailyCoinModel.findOneAndUpdate({ serverId, activityId, roleId, beginTime },
            { $inc: { exchangeCount: count, coinCount: coinCount }, $push: { recordMsg: msg } }, { upsert: true, new: true }).lean(true)
        return result;
    }

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

    //删除活动兑换记录
    public static async deleteActivity(serverId: number, activityId: number, roleId: string, beginTime: number) {
        await ActivityDailyCoinModel.deleteMany({ serverId, roleId, activityId, beginTime });
    }
}

export const ActivityDailyCoinModel = getModelForClass(Activity_Daily_Coin);

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