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

/**
  * 累计充值活动 
*/
@index({ refTime: -1 })

export default class HiddenData extends BaseModel {
  
  @prop({ required: true })
  refTime: number;		    // 刷新时间

  @prop({ required: true, type: Number })
  heroes: number[];		    // 武将id

  @prop({ required: true, type: Number })
  goods: number[];           // 物品id

  public static async clearData() {
    await HiddenDataModel.deleteMany({});
  }

  public static async updateHiddenData(refTime: number, heroes: number[], goods: number[], uid: number) {
    let result: HiddenDataModelType = await HiddenDataModel.findOneAndUpdate({ refTime }, { $setOnInsert: { createdBy: uid }, $set: { heroes, goods, updatedBy: uid } }, { new: true, upsert: true});
    return result;
  }

  public static async findCurData(now: number) {
    let result: HiddenDataModelType = await HiddenDataModel.findOne({ refTime: { $lte: now }}).sort({ refTime: -1 });
    return result;
  }

  public static async findNextData(now: number) {
    // console.log('####', now)
    let result: HiddenDataModelType = await HiddenDataModel.findOne({ refTime: { $gt: now } }).sort({ refTime: 1 });
    return result;
  }
}

export const HiddenDataModel = getModelForClass(HiddenData);

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