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

/**
  * 藏宝图掉落记录
  */
@index({ roleId: 1, battleId: 1 })

export default class BattleBlueprtDrop extends BaseModel {
  @prop({ required: true })
  roleId: string;					  // 角色 id
  @prop({ required: true })
  refTime: Date;            // 刷新日期
  
  @prop({ required: true, default: 0 })
  getNum: number;						// 获取次数，当curCostAp超过ap重置
  @prop({ required: true })
  curCostAp: number;				// 这一轮消耗的ap，单独设置出了以防以后修改PER_AP
  @prop({ required: true, default: 0 })
  getSum: number;						// 获取道具的全部次数
  @prop({ required: true })
  costAp: number;						// 当天消耗的ap

  public static async findByTime(roleId: string, refTime: Date, lean = true) {
    const result: BattleBlueprtDropType = await BattleBlueprtDropModel.findOneAndUpdate({ roleId,  refTime }, {}, {new: true, upsert: true}).lean(lean);
    return result;
  }

  public static async updateByTime(roleId: string, refTime: Date, params: {getNum: number, curCostAp: number, getSum: number, costAp: number}, lean = true) {
    const result: BattleBlueprtDropType = await BattleBlueprtDropModel.findOneAndUpdate({roleId, refTime}, {$set: params}).lean(lean);
    return result;
  }

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

  
}

export const BattleBlueprtDropModel = getModelForClass(BattleBlueprtDrop);

export interface BattleBlueprtDropType extends Pick<DocumentType<BattleBlueprtDrop>, keyof BattleBlueprtDrop>{}