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

/**
  * 战斗记录接口
  */
@index({ roleId: 1, battleId: 1, gid: 1 })

export default class BattleDrop extends BaseModel {
  @prop({ required: true })
  roleId: string;					// 角色 id
  @prop({ required: true })
  battleId: number;						// 关卡 id
  @prop({ required: true })
  gid: number;						// 关卡 id
  
  @prop({ required: true, default: 0 })
  getNum: number;						// 获取次数，当allNum超过配置次数重置
  @prop({ required: true, default: 0 })
  allNum: number;						// 全部次数，当超过配置次数重置
  @prop({ required: true, default: 0 })
  getSum: number;						// 获取道具的全部次数
  @prop({ required: true, default: 0 })
  allSum: number;						// 全部全部次数，不重置


  public static async findByGid(roleId: string, battleId: number, gid: number, lean = true) {
    const result: BattleDropType = await BattleDropModel.findOneAndUpdate({ roleId,  battleId, gid}, {}, {new: true, upsert: true}).lean(lean);
    return result;
  }

  public static async updateByGid(roleId: string, battleId: number, gid: number, params: {getNum: number, allNum: number, getSum: number, allSum: number}, lean = true) {
    const result: BattleDropType = await BattleDropModel.findOneAndUpdate({roleId, battleId, gid}, {$set: params}).lean(lean);
    return result;
  }

  public static async deleteAccount(roleId: string) {
    let result = await BattleDropModel.deleteMany({roleId});
    return result;
  }
}

export const BattleDropModel = getModelForClass(BattleDrop);

export interface BattleDropType extends Pick<DocumentType<BattleDrop>, keyof BattleDrop>{}