99 lines
3.8 KiB
TypeScript
99 lines
3.8 KiB
TypeScript
import BaseModel from './BaseModel';
|
||
import { index, getModelForClass, prop, DocumentType } from '@typegoose/typegoose';
|
||
import { nowSeconds } from '../pubUtils/timeUtil';
|
||
import { MAIL_STATUS } from '../consts/constModules/mailConst';
|
||
class RewardInter {
|
||
id: number;
|
||
count: number;
|
||
}
|
||
@index({ roleId: 1 })
|
||
@index({ roleId: 1, mailId: 1, type: 1 })
|
||
|
||
export default class Mail extends BaseModel {
|
||
@prop({ required: true })
|
||
roleId: string;
|
||
|
||
@prop({ required: true })
|
||
mailId: string;
|
||
|
||
@prop({ required: true, type: RewardInter, default: [], _id: false })
|
||
goods: RewardInter[];
|
||
|
||
@prop({ required: true, default: nowSeconds() })
|
||
sendTime: number;
|
||
|
||
@prop({ required: true })
|
||
params: string[];
|
||
|
||
@prop({ required: true, default: MAIL_STATUS.CREATE })
|
||
status: number;
|
||
|
||
@prop({ required: true })
|
||
mailTemType: number; //1:表示模板邮件,2:表示系统邮件
|
||
|
||
@prop({ required: true })
|
||
sendName: string;
|
||
|
||
@prop({ required: true })
|
||
endTime: number;
|
||
public static async addMails(mails: Array<MailType>) {
|
||
await MailModel.insertMany(mails);
|
||
}
|
||
|
||
public static async addMail(params: { roleId: string, goods: Array<RewardInter>, sendName: string, mailId: string, endTime: number, mailTemType: number, params?: string[], sendTime?: number }) {
|
||
const doc = new MailModel();
|
||
const mail = Object.assign(doc.toJSON(), params);
|
||
await MailModel.create(mail);
|
||
return mail;
|
||
}
|
||
|
||
public static async getMailsByRoleId(roleId: string, lean = true) {
|
||
const result: MailType[] = await MailModel.find({ roleId, status: { $ne: -1 } }).lean(lean);
|
||
return result;
|
||
}
|
||
|
||
public static async getMail(roleId: string, mailId: string, mailTemType: number, lean = true) {
|
||
const result: MailType = await MailModel.findOne({ roleId, mailId, mailTemType }).lean(lean);
|
||
return result;
|
||
}
|
||
|
||
public static async getMailById(_id: string, lean = true) {
|
||
const result: MailType = await MailModel.findOne({ _id }).lean(lean);
|
||
return result;
|
||
}
|
||
|
||
public static async updateMailByStatus(_id: string, conditions: number[], update: MailTypeParam, lean = true) {
|
||
const result: MailType = await MailModel.findOneAndUpdate({ _id, status: { $in: conditions } }, { $set: update }, { new: true }).lean(lean);
|
||
return result;
|
||
}
|
||
|
||
public static async updateMail(_id: string, update: MailTypeParam, lean = true) {
|
||
const result: MailType = await MailModel.findOneAndUpdate({ _id }, { $set: update }, { new: true }).lean(lean);
|
||
return result;
|
||
}
|
||
|
||
public static async findReadAndRewardsMails(roleId: string, lean = true) {
|
||
const ids: string[] = await MailModel.find({ roleId, $or: [{ $and: [{ status: MAIL_STATUS.READ, goods: [] }] }, { status: MAIL_STATUS.RECEIVED }] }).select('_id').lean(lean);
|
||
return ids;
|
||
}
|
||
|
||
public static async updateMailStatus(ids: string[], status: number) {
|
||
await MailModel.update({ _id: { $in: ids } }, { $set: { status } });
|
||
}
|
||
|
||
public static async delMail(_id: string, lean = true) {
|
||
const result: MailType = await MailModel.findOneAndUpdate({ _id, $or: [{ $and: [{ status: MAIL_STATUS.READ, goods: [] }] }, { status: MAIL_STATUS.RECEIVED }] }, { $set: { status: MAIL_STATUS.DELETE } }, { new: true }).lean(lean);
|
||
return result;
|
||
}
|
||
|
||
public static async findRewardsMails(roleId: string, lean = true) {
|
||
const result: MailType[] = await MailModel.find({ roleId, $and: [{ status: MAIL_STATUS.READ, goods: { $ne: [] } }] }).select('_id goods').lean(lean);
|
||
return result;
|
||
}
|
||
}
|
||
|
||
export const MailModel = getModelForClass(Mail);
|
||
|
||
export interface MailType extends Pick<DocumentType<Mail>, keyof Mail> { };
|
||
export type MailTypeParam = Partial<MailType>; // 将所有字段变成可选项
|