145 lines
6.0 KiB
TypeScript
145 lines
6.0 KiB
TypeScript
import BaseModel from './BaseModel';
|
|
import { index, getModelForClass, prop, DocumentType } from '@typegoose/typegoose';
|
|
import { CounterModel } from './Counter';
|
|
import { COUNTER } from '../consts';
|
|
import { ActivityGroupModel } from './ActivityGroup';
|
|
|
|
/**
|
|
* 活动系统
|
|
*/
|
|
@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; // 开启时间 timeType=3
|
|
@prop({ required: true })
|
|
endTime: Date; // 结束时间 timeType=3
|
|
@prop({ required: true })
|
|
type: number; // 活动类型
|
|
@prop({ required: true })
|
|
data: string; // 活动表中的数据
|
|
|
|
|
|
@prop({ required: true })
|
|
timeType: number; // 活动时间类型 ACTIVITY_TIME_TYPE
|
|
@prop({ required: true })
|
|
days: number; // 活动持续天数 timeType=1、2
|
|
|
|
|
|
//根据活动类型查询开启的活动数据
|
|
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 findOpenActivityByTypes(activityGroupId: number[], types: number[], date: Date) {
|
|
let result: ActivityModelType[] = await ActivityModel.find(
|
|
{ groupId: { $in: activityGroupId }, type: { $in: types }, 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[], groupId: number, beginTime: Date, endTime: Date, type: number, data: string, uid = 1) {
|
|
let result: ActivityModelType[] = [];
|
|
for (let activityId of aids) {
|
|
if (!activityId) activityId = await CounterModel.getNewCounter(COUNTER.ACTIVITY);
|
|
let update = { type, data }
|
|
if (beginTime != undefined) {
|
|
update["beginTime"] = beginTime;
|
|
}
|
|
if (endTime != undefined) {
|
|
update["endTime"] = endTime;
|
|
}
|
|
let rec: ActivityModelType = await ActivityModel.findOneAndUpdate({ activityId }, { $set: {...update, updatedBy: uid}, $setOnInsert: { createdBy: uid } },
|
|
{ new: true, upsert: true }).lean(true);
|
|
result.push(rec);
|
|
}
|
|
if (groupId != undefined) {
|
|
if (groupId == 0) {
|
|
let newGroup = await ActivityGroupModel.createGroup();
|
|
groupId = newGroup.groupId;
|
|
}
|
|
await ActivityGroupModel.addActivitiesToGroupData(groupId, aids, uid);
|
|
}
|
|
return result;
|
|
}
|
|
|
|
//删除活动
|
|
public static async deleteActivity(activityId: number, uid = 1) {
|
|
let result = await ActivityModel.deleteMany({ activityId });
|
|
await ActivityGroupModel.pullByActivityIds([activityId], uid);
|
|
return result;
|
|
}
|
|
|
|
//查询
|
|
public static async findByCondition(page: number, pageSize: number, sortField: string, sortOrder: string, type: number = 0, groupId: number = 0, current: boolean = false, activityId: number = 0) {
|
|
let searchObj = {};
|
|
if (type != 0) searchObj['type'] = type;
|
|
if (groupId != 0) searchObj['groupId'] = groupId;
|
|
if (activityId != 0) searchObj['activityId'] = activityId;
|
|
if (current) {
|
|
searchObj['beginTime'] = { $lte: new Date };
|
|
searchObj['endTime'] = { $gte: new Date };
|
|
}
|
|
let sort = {};
|
|
if(sortField && sortOrder) {
|
|
if(sortOrder == 'ascend') {
|
|
sort[sortField] = 1;
|
|
} else if (sortOrder == 'descend') {
|
|
sort[sortField] = -1;
|
|
}
|
|
}
|
|
const result: ActivityModelType[] = await ActivityModel.find(searchObj, { _id: 0 }).limit(pageSize).skip((page - 1) * pageSize).sort(sort).lean();
|
|
return result;
|
|
}
|
|
|
|
// 获得活动数量
|
|
public static async countByCondition(type: number = 0, groupId: number = 0, current: boolean = false, activityId: number = 0) {
|
|
let searchObj = {};
|
|
if (type != 0) searchObj['type'] = type;
|
|
if (groupId != 0) searchObj['groupId'] = groupId;
|
|
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;
|
|
}
|
|
|
|
//根据多个活动id查询活动数据
|
|
public static async findActivityByIds(activityIds: number[]) {
|
|
let result: ActivityModelType[] = await ActivityModel.find({ activityId: { $in: activityIds } }).lean();
|
|
return result;
|
|
}
|
|
|
|
public static async updateGroupId(activityIds: number[], groupId: number, uid = 1) {
|
|
let result = await ActivityModel.updateMany({ activityId: { $in: activityIds } }, { $set: { groupId, updatedBy: uid }, $setOnInsert: { createdBy: uid } });
|
|
return result;
|
|
}
|
|
}
|
|
|
|
export const ActivityModel = getModelForClass(Activity);
|
|
|
|
export interface ActivityModelType extends Pick<DocumentType<Activity>, keyof Activity> { }
|
|
export type ActivityModelTypeParam = Partial<ActivityModelType>; // 将所有字段变成可选项
|