105 lines
4.3 KiB
TypeScript
105 lines
4.3 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 })
|
|
groupId: 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(activityGroupId: number[], type: number, date: Date) {
|
|
let result: ActivityModelType[] = await ActivityModel.find(
|
|
{ groupId: { $in: activityGroupId }, type, beginTime: { $lte: date }, endTime: { $gte: date } }
|
|
).sort({ activityId: -1 }).lean(true);
|
|
return result;
|
|
}
|
|
|
|
//根据活动类型查询活动数据
|
|
public static async findActivityByType(activityGroupId: number[], type: number, sort: number) {
|
|
let result: ActivityModelType[] = await ActivityModel.find({ groupId: { $in: activityGroupId }, type }).sort({ activityId: sort }).lean(true);
|
|
return result;
|
|
}
|
|
|
|
//根据活动id查询活动数据
|
|
public static async findActivity(activityId: number) {
|
|
let result: ActivityModelType = await ActivityModel.findOne({ activityId }).lean(true);
|
|
return result;
|
|
}
|
|
|
|
//新增活动
|
|
public static async addActivity(aids: number[], serverId: number, beginTime: Date, endTime: Date, type: number, data: string) {
|
|
let result: ActivityModelType[] = [];
|
|
for (let activityId of aids) {
|
|
if (!activityId) activityId = await CounterModel.getNewCounter(COUNTER.ACTIVITY);
|
|
let update = { type, data }
|
|
if (serverId != undefined) {
|
|
update["serverId"] = serverId;
|
|
}
|
|
if (beginTime != undefined) {
|
|
update["beginTime"] = beginTime;
|
|
}
|
|
if (endTime != undefined) {
|
|
update["endTime"] = endTime;
|
|
}
|
|
let rec: ActivityModelType = await ActivityModel.findOneAndUpdate({ activityId }, { $set: update },
|
|
{ new: true, upsert: true }).lean(true);
|
|
result.push(rec);
|
|
}
|
|
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>; // 将所有字段变成可选项
|