89 lines
3.7 KiB
TypeScript
89 lines
3.7 KiB
TypeScript
import BaseModel from './BaseModel';
|
|
import { index, getModelForClass, prop, DocumentType } from '@typegoose/typegoose';
|
|
import { CounterModel } from './Counter';
|
|
import { COUNTER } from '@consts';
|
|
|
|
/**
|
|
* 活动系统
|
|
*/
|
|
@index({ activityId: 1 })
|
|
|
|
export default class Activity extends BaseModel {
|
|
@prop({ required: true })
|
|
serverId: number; // 服Id
|
|
@prop({ required: true })
|
|
activityId: number; // 活动Id
|
|
@prop({ required: true })
|
|
beginTime: Date; // 开启时间
|
|
@prop({ required: true })
|
|
endTime: Date; // 结束时间
|
|
@prop({ required: true })
|
|
type: number; // 活动类型
|
|
@prop({ required: true })
|
|
data: string; // 活动表中的数据
|
|
|
|
//根据活动类型查询开启的活动数据
|
|
public static async findOpenActivityByType(type: number, date: Date, lean = true) {
|
|
let result: ActivityModelType[] = await ActivityModel.find({ type, beginTime: { $lte: date }, endTime: { $gte: date } }).lean(lean);
|
|
return result;
|
|
}
|
|
|
|
//根据活动类型查询活动数据
|
|
public static async findActivityByType(type: number, lean = true) {
|
|
let result: ActivityModelType[] = await ActivityModel.find({ type }).lean(lean);
|
|
return result;
|
|
}
|
|
|
|
//根据活动id查询活动数据
|
|
public static async findActivity(activityId: number, lean = true) {
|
|
let result: ActivityModelType = await ActivityModel.findOne({ activityId }).lean(lean);
|
|
return result;
|
|
}
|
|
|
|
//新增活动
|
|
public static async addActivity(activityId: number, serverId: number, beginTime: Date, endTime: Date, type: number, data: string, lean = true) {
|
|
if(!activityId) activityId = await CounterModel.getNewCounter(COUNTER.ACTIVITY);
|
|
let result: ActivityModelType = await ActivityModel.findOneAndUpdate({ activityId }, { serverId, beginTime, endTime, type, data },
|
|
{ new: true, upsert: true }).lean(lean);
|
|
return result;
|
|
}
|
|
|
|
//删除活动
|
|
public static async deleteActivity(activityId: number) {
|
|
let result = await ActivityModel.deleteMany({ activityId });
|
|
return result;
|
|
}
|
|
|
|
//查询
|
|
public static async findByCondition(page: number, pageSize: number, type: number = 0, serverId: number = 0, current: boolean = false, activityId: number = 0) {
|
|
let searchObj = {};
|
|
if(type != 0) searchObj['type'] = type;
|
|
if(serverId != 0) searchObj['serverId'] = { $in: [0, serverId] };
|
|
if(activityId != 0) searchObj['activityId'] = activityId;
|
|
if(current) {
|
|
searchObj['beginTime'] = { $lte: new Date };
|
|
searchObj['endTime'] = { $gte: new Date };
|
|
}
|
|
const result: ActivityModelType[] = await ActivityModel.find(searchObj, { _id: 0 }).limit(pageSize).skip((page - 1) * pageSize).sort({updatedAt: -1}).lean();
|
|
return result;
|
|
}
|
|
|
|
// 获得活动数量
|
|
public static async countByCondition(type: number = 0, serverId: number = 0, current: boolean = false, activityId: number = 0) {
|
|
let searchObj = {};
|
|
if(type != 0) searchObj['type'] = type;
|
|
if(serverId != 0) searchObj['serverId'] = { $in: [0, serverId] };
|
|
if(activityId != 0) searchObj['activityId'] = activityId;
|
|
if(current) {
|
|
searchObj['beginTime'] = { $lte: new Date };
|
|
searchObj['endTime'] = { $gte: new Date };
|
|
}
|
|
const result = await ActivityModel.count(searchObj);
|
|
return result;
|
|
}
|
|
}
|
|
|
|
export const ActivityModel = getModelForClass(Activity);
|
|
|
|
export interface ActivityModelType extends Pick<DocumentType<Activity>, keyof Activity> { }
|
|
export type ActivityModelTypeParam = Partial<ActivityModelType>; // 将所有字段变成可选项
|