52 lines
2.0 KiB
TypeScript
52 lines
2.0 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, serverId: number, beginTime: number, endTime: number, type: number, data: string) {
|
|
const { ctx } = this;
|
|
if(serverId == undefined || !beginTime || !endTime || !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);
|
|
}
|
|
const activity = await ActivityModel.addActivity(activityId, serverId, new Date(beginTime), new Date(endTime), 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);
|
|
}
|
|
}
|
|
}
|