101 lines
3.4 KiB
TypeScript
101 lines
3.4 KiB
TypeScript
|
|
import BaseModel from './BaseModel';
|
|
import { getModelForClass, prop, DocumentType } from '@typegoose/typegoose';
|
|
import { MailType } from './Mail';
|
|
import { GroupMailType } from './GroupMail';
|
|
import { ServerMailType } from './ServerMail';
|
|
import { GM_MAIL_TYPE } from '../consts';
|
|
import { MailParam } from '../domain/roleField/mail';
|
|
|
|
class Reward {
|
|
@prop({ required: true })
|
|
id: number;
|
|
@prop({ required: true })
|
|
count: number;
|
|
}
|
|
|
|
export default class GMMailRecord extends BaseModel {
|
|
|
|
@prop({ required: true })
|
|
content: string; // 内容
|
|
|
|
@prop({ required: true })
|
|
sendName: string; // 发件人
|
|
|
|
@prop({ required: true })
|
|
sendTime: number; // 发送时间
|
|
|
|
@prop({ required: true })
|
|
endTime: number; // 过期时间
|
|
|
|
@prop({ required: true, type: Reward })
|
|
goods: Reward[]; // 奖励
|
|
|
|
@prop({ required: true, enum: GM_MAIL_TYPE })
|
|
mailType: GM_MAIL_TYPE;
|
|
|
|
@prop({ required: true })
|
|
roleIds: string[];
|
|
|
|
@prop({ required: true })
|
|
serverIds: number[];
|
|
|
|
@prop({ required: true })
|
|
mailId: string;
|
|
|
|
/**
|
|
* @description 创建记录
|
|
* @param mailType 邮件类型
|
|
* @param mail 邮件
|
|
* @param uid 操作人
|
|
*/
|
|
public static async createRecord(mailType: GM_MAIL_TYPE, mail: MailType|GroupMailType|ServerMailType, uid = 1) {
|
|
const doc = new GMMailRecordModel();
|
|
let params = new MailParam(mailType, mail);
|
|
let roleIds: string[] = [], serverIds: number[] = [];
|
|
if(mailType == GM_MAIL_TYPE.SINGLE) {
|
|
roleIds.push((<MailType>mail).roleId);
|
|
} else if (mailType == GM_MAIL_TYPE.GROUP) {
|
|
let roleStatus = (<GroupMailType>mail).roleStatus;
|
|
for(let {roleId} of roleStatus) {
|
|
roleIds.push(roleId);
|
|
}
|
|
} else if (mailType == GM_MAIL_TYPE.SERVER) {
|
|
serverIds.push((<ServerMailType>mail).serverId)
|
|
}
|
|
const update = Object.assign(doc.toJSON(), params, { roleIds, serverIds, mailId: mail._id, createdBy: uid, updatedBy: uid });
|
|
delete update._id;
|
|
let rec: MailType = await GMMailRecordModel.findByIdAndUpdate(doc._id, update, { new: true, upsert: true }).lean();
|
|
return rec;
|
|
}
|
|
|
|
// private static getSearchObj(form: {_id?: string, content?: string}) {
|
|
// let searchObj = {};
|
|
// if(form['_id']) searchObj['_id'] = form._id;
|
|
// if(form['content']) searchObj['context'] = { $regex: new RegExp(form.content.toString(), 'i') }
|
|
// return searchObj
|
|
// }
|
|
|
|
public static async findByCondition(page: number, pageSize: number, sortField: string, sortOrder: string, form: {} = {}) {
|
|
|
|
let searchObj = form;
|
|
let sort = {};
|
|
if(sortField && sortOrder) {
|
|
if(sortOrder == 'ascend') {
|
|
sort[sortField] = 1;
|
|
} else if (sortOrder == 'descend') {
|
|
sort[sortField] = -1;
|
|
}
|
|
}
|
|
const result: GMMailRecordType[] = await GMMailRecordModel.find(searchObj).limit(pageSize).skip((page - 1) * pageSize).sort(sort).lean({ getters: true, virtuals: true });
|
|
return result;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
export const GMMailRecordModel = getModelForClass(GMMailRecord);
|
|
|
|
export interface GMMailRecordType extends Pick<DocumentType<GMMailRecord>, keyof GMMailRecord> { };
|
|
export type GMMailRecordParam = Partial<GMMailRecordType>; // 将所有字段变成可选项
|