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

/**
  * 自增 ID
*/
@index({ sourceType: 1, time: 1, id: 1 })
export default class CounterLots extends BaseModel {

  @prop({ required: true })
  beginTime: Date;

  @prop({ required: true })
  id: number;

  @prop({ required: true })
  seq: number;

  public static async getNewCounter(id: number, beginTime: Date, inc: number) {
    let counter: CounterLotsType = await CounterLotsModel.findOneAndUpdate({ beginTime, id }, { $inc: { seq: inc } }, { new: true, upsert: true }).lean();
    return counter?.seq;
  }

}

export const CounterLotsModel = getModelForClass(CounterLots);

export interface CounterLotsType extends Pick<DocumentType<CounterLots>, keyof CounterLots>{};