Files
ZYZ/gm-server/app/service/Activity.ts
2021-05-20 19:25:52 +08:00

63 lines
2.3 KiB
TypeScript

import { Service } from 'egg';
import { STATUS } from '@consts';
import { ActivityModel } from '@db/Activity';
/**
* Test Service
*/
export default class Activity extends Service {
/**
* 获得活动列表
*/
public async getActivityList(page: number, pageSize: number, type: number = 0, serverId: number = 0, current: boolean = false, activityId: number = 0) {
const { ctx } = this;
// console.log('***', page, pageSize, type, serverId, current, activityId)
const list = await ActivityModel.findByCondition(page, pageSize, type, serverId, current, activityId);
const total = await ActivityModel.countByCondition(type, serverId, current, activityId);
return ctx.service.utils.resResult(STATUS.SUCCESS, {
list: list.map(cur => {
return {
...cur, beginTime: cur.beginTime.getTime(), endTime: cur.endTime.getTime()
}
}), total
});
}
public async updateActivity(activityId: number | string, serverId: number, beginTime: number, endTime: number, type: number, data: string) {
const { ctx } = this;
if (!type || !data) {
return ctx.service.utils.resResult(STATUS.WRONG_PARMS);
}
try {
let result = JSON.parse(data);
ctx.service.utils.checkActivityData(result, type);
} catch (e) {
return ctx.service.utils.resResult(STATUS.WRONG_PARMS);
}
let aids: number[] = [];
if (typeof activityId == 'number') {
aids.push(activityId);
} else {
activityId.split(',').forEach(aidStr => {
aids.push(parseInt(aidStr));
});
}
const activity = await ActivityModel.addActivity(aids, serverId, beginTime ? new Date(beginTime) : undefined, endTime ? new Date(endTime) : undefined, type, data);
return ctx.service.utils.resResult(STATUS.SUCCESS, {
activity
})
}
public async deleteActivity(activityId: number) {
const { ctx } = this;
const result = await ActivityModel.deleteActivity(activityId);
if (result) {
return ctx.service.utils.resResult(STATUS.SUCCESS);
} else {
return ctx.service.utils.resResult(STATUS.ACTIVITY_MISSING);
}
}
}