Files
ZYZ/shared/db/Dividend.ts
2021-03-16 12:24:35 +08:00

45 lines
1.8 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import { LotRec, DividendRec } from './../domain/dbGeneral';
import BaseModel from './BaseModel';
import { index, getModelForClass, prop, DocumentType, modelOptions } from '@typegoose/typegoose';
import { genCode } from '../pubUtils/util';
/**
* 分红记录表
**/
@modelOptions({ schemaOptions: { id: false } })
@index({ code: 1 })
@index({ guildCode: 1 })
export default class Dividend extends BaseModel {
@prop({ required: true, default: 0 })
type: number; // 0初始值1演武2蛮夷入侵3诸侯混战4粮草先行
@prop({ required: true, default: '' })
guildCode: string; // 军团编号
@prop({ required: true, default: '' })
sourceCode: string; // 来源的唯一标识,如活动编号
@prop({ required: true, default: '' })
code: string; // 分红记录唯一标识
@prop({ required: true, type: LotRec, default: [] })
lots: LotRec[];
@prop({ required: true, default: 0 })
totalPrice: number; // 分红总金额
@prop({ required: true, type: DividendRec, default: [] })
dividends: DividendRec[];
public static async createDividend(data: DividendParam) {
const code = genCode(8);
const docData = new DividendModel();
const result: DividendType = await DividendModel.findOneAndUpdate({ code }, { ...docData.toJSON(), ...data, code }, { upsert: true, new: true }).select('-_id -__v').lean();
return result;
}
public static async findDividend(code: string) {
const result = await DividendModel.findOne({ code }).select('-_id -__v').lean();
return result;
}
}
export const DividendModel = getModelForClass(Dividend);
export interface DividendType extends Pick<DocumentType<Dividend>, keyof Dividend>{}
export type DividendParam = Partial<DividendType>;