110 lines
3.9 KiB
TypeScript
110 lines
3.9 KiB
TypeScript
import BaseModel from './BaseModel';
|
|
import { index, getModelForClass, prop, DocumentType, modelOptions } from '@typegoose/typegoose';
|
|
import { COUNTER } from '../consts'
|
|
import { CounterModel } from './Counter';
|
|
import moment = require('moment');
|
|
|
|
/**
|
|
* 游戏字段接口
|
|
*/
|
|
@index({ id: 1 })
|
|
@modelOptions({schemaOptions: {id: false}})
|
|
export default class Notice extends BaseModel {
|
|
|
|
@prop({ required: true })
|
|
id: number; // 公告id
|
|
@prop({ required: true })
|
|
title: string; // 公告标题
|
|
@prop({ required: true })
|
|
tag: number; // 公告显示标签,在系统参数表中配了
|
|
@prop({ required: true })
|
|
type: number; // 类型 1-公告 2-活动
|
|
@prop({ required: true })
|
|
content: string; // 公告内容
|
|
|
|
@prop({ required: true })
|
|
timeStr: string; // 时间显示
|
|
@prop({ required: true })
|
|
startTime: Date; // 活动开始时间
|
|
@prop({ required: true })
|
|
endTime: Date; // 活动结束时间
|
|
public get time() {
|
|
let startTime = moment(this.startTime).format('YYYY/MM/DD');
|
|
let endTime = moment(this.endTime).format('YYYY/MM/DD');
|
|
|
|
return this.timeStr.replace(/%startTime/g, startTime).replace(/%endTime/g, endTime);
|
|
}
|
|
|
|
@prop({ required: true, select: false })
|
|
sort: number;
|
|
|
|
@prop({ required: true, select: false })
|
|
showStartTime: Date;
|
|
@prop({ required: true, select: false })
|
|
showEndTime: Date;
|
|
@prop({ required: true, select: false })
|
|
serverType: string; // 服务器类型
|
|
@prop({ required: true, select: false })
|
|
isEnable: boolean;
|
|
|
|
|
|
public static async getAllNotice(serverType: string) {
|
|
let curTime = new Date();
|
|
let notices: NoticeType[] = await NoticeModel.find({ showStartTime: { $lte: curTime }, showEndTime: { $gte: curTime }, isEnable: true, serverType })
|
|
.sort({ sort: -1, createTime: -1 })
|
|
.lean({ virtuals: true });
|
|
return notices;
|
|
}
|
|
|
|
public static async updateNotice(id: number|string, values: NoticeTypeParam, uid = 1) {
|
|
if(id == 'new') {
|
|
id = await CounterModel.getNewCounter(COUNTER.NOTICE);
|
|
}
|
|
delete values.id;
|
|
let rec: NoticeType = await NoticeModel.findOneAndUpdate({ id }, { $set: {...values, updatedBy: uid}, $setOnInsert: { createdBy: uid } },
|
|
{ new: true, upsert: true }).lean(true);
|
|
return rec;
|
|
}
|
|
|
|
public static async delNotice(id: number) {
|
|
let rec: NoticeType = await NoticeModel.findOneAndDelete({ id }).lean();
|
|
return rec
|
|
}
|
|
|
|
private static getSearchObj(form: { type?: number, content?: string }) {
|
|
let searchObj = {};
|
|
if(form['type']) searchObj['type'] = form.type;
|
|
if(form['content']) searchObj['content'] = { $regex: new RegExp(form.content.toString(), 'i') };
|
|
return searchObj
|
|
}
|
|
|
|
public static async findByCondition(page: number, pageSize: number, sortField: string = 'updatedAt', sortOrder: string = 'descend', form: { type?: number, content?: string } = {}) {
|
|
|
|
let searchObj = this.getSearchObj(form);
|
|
let sort = {};
|
|
if(sortField && sortOrder) {
|
|
if(sortOrder == 'ascend') {
|
|
sort[sortField] = 1;
|
|
} else if (sortOrder == 'descend') {
|
|
sort[sortField] = -1;
|
|
}
|
|
}
|
|
const result: NoticeType[] = await NoticeModel.find(searchObj).limit(pageSize).skip((page - 1) * pageSize).sort(sort).select('+sort +showStartTime +showEndTime +serverType +isEnable').lean({ getters: true, virtuals: true });
|
|
return result;
|
|
|
|
}
|
|
|
|
public static async countByCondition(form: { type?: number, content?: string } = {}) {
|
|
|
|
let searchObj = this.getSearchObj(form);
|
|
const result = await NoticeModel.count(searchObj);
|
|
return result;
|
|
}
|
|
}
|
|
|
|
export const NoticeModel = getModelForClass(Notice);
|
|
|
|
export interface NoticeType extends Pick<DocumentType<Notice>, keyof Notice>{
|
|
id: number;
|
|
};
|
|
export type NoticeTypeParam = Partial<NoticeType>; // 将所有字段变成可选项
|