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

/**
  * 活动系统 - 每日兑换铜币
*/
@index({ roleId: 1, activityId: 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

    @prop({ required: true })
    roundIndex: number;			    // 周期数
    @prop({ required: true, type: Number })
    receivedBox: number[];			    // 已领取宝箱

    @prop({ required: true })
    hasNotReceivedBox: boolean;		    // 是否有没有领取的宝箱

    @prop({ required: true })
    repaireBoxed: number[];		    // 是否有没有领取的宝箱

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

    // 领取宝箱
    public static async receiveBox(serverId: number, activityId: number, roleId: string, roundIndex: number, cellIndex: number, hasNotReceivedBox: boolean) {
        let result = await ActivityDailyCoinModel.findOneAndUpdate({ serverId, activityId, roleId, roundIndex },
            { $push: { receivedBox: cellIndex }, $set: { hasNotReceivedBox } }, { upsert: true, new: true }).lean(true)
        return result;
    }
    
    //根据活动时间查询活动数据
    public static async findData(serverId: number, activityId: number, roleId: string, roundIndex: number) {
        let result: ActivityDailyCoinModelType = await ActivityDailyCoinModel.findOne({
            serverId, roleId, activityId, roundIndex,
        }).lean(true);
        return result;
    }

    // 未发放完
    public static async findUnreceivedBox(beginTime: Date, endTime: Date) {
        let result: ActivityDailyCoinModelType[] = await ActivityDailyCoinModel.find({
            createdAt: { $gte: beginTime, $lte: endTime }, hasNotReceivedBox: true
        }).lean(true);
        return result;
    }

    // 凌晨5点补发邮箱
    public static async repaireBox(_id: string, boxIds: number[]) {
        let result = await ActivityDailyCoinModel.findByIdAndUpdate({ _id },
            { $push: { repaireBoxed: { $each: boxIds } } }, { upsert: true, new: true }).lean(true)
        return result;
    }

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

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>; // 将所有字段变成可选项