56 lines
1.8 KiB
TypeScript
56 lines
1.8 KiB
TypeScript
import BaseModel from './BaseModel';
|
|
import { index, getModelForClass, prop, DocumentType } from '@typegoose/typegoose';
|
|
import { COUNTER } from './../consts';
|
|
import { CounterModel } from './Counter';
|
|
|
|
class Report {
|
|
@prop({ required: true })
|
|
roleName: string;
|
|
|
|
@prop({ required: true })
|
|
time: number;
|
|
|
|
@prop({ required: true })
|
|
id: number;//捐献类型id
|
|
}
|
|
|
|
export default class Donation extends BaseModel {
|
|
|
|
@prop({ required: true })
|
|
guildCode: string;
|
|
|
|
@prop({ required: true, default: 0})
|
|
donateFund: number;
|
|
|
|
@prop({ required: true, default: 0})
|
|
hisDonateFund: number; //历史捐赠最高
|
|
|
|
@prop({ required: true, type: Report, default:[] })
|
|
reports: Array<Report>;
|
|
|
|
@prop({ required: true, default: 0})
|
|
refTime: number;
|
|
|
|
public static async createDonation(guildCode: string, lean = true) {
|
|
const doc = new Donation();
|
|
const result: DonationType = await DonationModel.findOneAndUpdate({ guildCode }, {$set:doc}, {upsert: true, new: true}).lean(lean);
|
|
return result;
|
|
}
|
|
|
|
public static async getDonation(guildCode: string, select?: string, lean = true) {
|
|
const result: DonationType = await DonationModel.findOne({ guildCode }).select(select).lean(lean);
|
|
return result;
|
|
}
|
|
|
|
public static async donation(guildCode: string, donateFund: number, pushDate: {reports?: Report}, lean = true) {
|
|
const result: DonationType = await DonationModel.findOneAndUpdate({ guildCode }, {$inc:{ donateFund, hisDonateFund: donateFund }, $push:pushDate}, {upsert: true, new: true}).lean(lean);
|
|
return result;
|
|
}
|
|
|
|
}
|
|
|
|
export const DonationModel = getModelForClass(Donation);
|
|
|
|
export interface DonationType extends Pick<DocumentType<Donation>, keyof Donation>{}
|
|
export type DonationUpdateParam = Partial<DonationType>; // 将所有字段变成可选项
|