Files
ZYZ/shared/db/GMMail.ts
2021-06-28 13:31:29 +08:00

64 lines
2.1 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.
/**
* 邮件的模板在GM后台能看到的邮件列表
*/
import BaseModel from './BaseModel';
import { getModelForClass, prop, DocumentType } from '@typegoose/typegoose';
import { nowSeconds } from '../pubUtils/timeUtil';
import { GM_MAIL_TYPE } from '../consts/constModules/mailConst';
class Reward {
@prop({ required: true })
id: number;
@prop({ required: true })
count: number;
}
export default class GMMail extends BaseModel {
@prop({ required: true, type: Reward, default: [], _id: false })
goods: Reward[];
@prop({ required: true, default: nowSeconds() })
sendTime: number;
@prop({ required: true })
endTime: number;
@prop({ required: true })
content: string;
@prop({ required: true })
sendName: string;
@prop({ required: true, enum: GM_MAIL_TYPE })
mailType: GM_MAIL_TYPE; // 1-单人邮件 2-多人邮件 3-全服邮件
public static async addMail(params: GMMailTypeParam) {
const doc = new GMMailModel();
let mail = Object.assign(doc.toJSON(), params);
mail = await GMMailModel.findOneAndUpdate({ _id: mail._id }, { $set: mail }, {upsert: true, new: true}).lean();
return mail;
}
public static async getGmMailById(_id: string, lean = true) {
const result: GMMailType = await GMMailModel.findOne({ _id }).lean(lean);
return result;
}
public static async getMail(updatedMailAt: number, lean = true) {
const result: GMMailType[] = await GMMailModel.find({ $or: [{updatedAt: { $gte: new Date(updatedMailAt) }}, {sendTime: { $lte: nowSeconds() }}], endTime: { $gte: nowSeconds()} }).lean(lean);
return result;
}
public static async getMails( lean = true) {
const result: GMMailType[] = await GMMailModel.find({ endTime: { $gte: nowSeconds() }}).lean(lean);
return result;
}
}
export const GMMailModel = getModelForClass(GMMail);
export interface GMMailType extends Pick<DocumentType<GMMail>, keyof GMMail> { };
export type GMMailTypeParam = Partial<GMMailType>; // 将所有字段变成可选项