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

class MemberRecord {
    @prop({ required: true })
    roleId: string;
    @prop({ required: true })
    roleName: string;
    @prop({ required: true })
    price: number;          // 金额
    @prop({ required: true })
    hasQuit: boolean;          // 金额
}


/**
  * 活动系统 - 成长任务活动积分点兑换奖励
*/
@index({ guildCode: 1, activityId: 1, roundIndex: 1 })

export default class Activity_Guild_Pay extends BaseModel {
    @prop({ required: true })
    serverId: number;				// 区服id

    @prop({ required: true })
    activityId: number;				// 活动Id

    @prop({ required: true })
    roundIndex: number;				// 活动第几轮

    @prop({ required: true })
    guildCode: string;			        // 军团code

    @prop({ required: true, type: MemberRecord, _id: false })
    memberRecord: MemberRecord[];	    // 充值的玩家

    //当日奖章领取记录
    public static async addRecord(serverId: number, activityId: number, roundIndex: number, guildCode: string, memberRecord: MemberRecord) {        
        let result: ActivityGuildPayModelType = await ActivityGuildPayModel.findOne({ serverId, guildCode, activityId, roundIndex }).lean();
        let hasUser = result?.memberRecord?.find(cur => cur.roleId == memberRecord.roleId);
        if(!hasUser) {
            result = await ActivityGuildPayModel.findOneAndUpdate({ serverId, guildCode, activityId, roundIndex }, { $push: { memberRecord } }, { new: true, upsert: true }).lean();
        } else {
            result = await ActivityGuildPayModel.findOneAndUpdate({ serverId, guildCode, activityId, roundIndex, 'memberRecord.roleId': memberRecord.roleId }, { 
                $set: { 'memberRecord.$.roleName': memberRecord.roleName, 'memberRecord.$.hasQuit': false },
                $inc: { 'memberRecord.$.price': memberRecord.price } 
            }, { new: true }).lean();
        }
        let isNew = hasUser? hasUser.hasQuit: true;
        return { result, isNew };
    }

    //根据活动id查询活动数据
    public static async findData(serverId: number, activityId: number, roundIndex: number, guildCode: string) {
        let result: ActivityGuildPayModelType = await ActivityGuildPayModel.findOne({ serverId, activityId, roundIndex, guildCode }).lean(true);
        return result;
    }

    public static async quitGuild(serverId: number, activityId: number, roundIndex: number, guildCode: string, roleId: string) {
        let result: ActivityGuildPayModelType = await ActivityGuildPayModel.findOneAndUpdate({ serverId, guildCode, activityId, roundIndex, 'memberRecord.roleId': roleId }, { $set: { 'memberRecord.$.hasQuit': true } }, { new: true }).lean();
        return result;
    }
}

export const ActivityGuildPayModel = getModelForClass(Activity_Guild_Pay);

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