政务数据资源中心(Government data Resource center) 03专项3期主要建设内容
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

128 lines
4.0 KiB

'use strict';
// 新增采集任务
function addAcquisitionTask(opts) {
return async function (ctx, next) {
const models = ctx.fs.dc.models;
try {
const { taskName } = ctx.request.body
const checkName = await models.AcquisitionTask.findOne({ where: { taskName, taskName } });
if (checkName) {
ctx.status = 400;
ctx.body = { message: '该采集任务名称已存在' }
} else {
let rslt = ctx.request.body;
await models.AcquisitionTask.create(Object.assign({}, rslt))
let datasource = await models.AcquisitionTask.findOne({ where: { taskName } })
ctx.status = 200;
ctx.body = { message: '新建采集任务成功', id: datasource.id }
}
} catch (error) {
ctx.fs.logger.error(`path: ${ctx.path}, error: ${error}`);
ctx.status = 400;
ctx.body = { message: '新建采集任务失败' }
}
}
}
function getAcquisitionTask(opts) {
return async function (ctx, next) {
const models = ctx.fs.dc.models;
const { page, limit, taskName } = ctx.query;
let errMsg = { message: '获取采集任务失败' }
const Op = ctx.fs.dc.ORM.Op;
try {
let searchWhere = {}
let option = {
where: searchWhere,
order: [["id", "desc"]],
}
if (taskName) {
searchWhere.taskName = {
// 模糊查询
[Op.like]: '%' + taskName + '%',
};
}
option.where = searchWhere
let limit_ = limit || 10;
let page_ = page || 1;
let offset = (page_ - 1) * limit_;
if (limit && page) {
option.limit = limit_
option.offset = offset
}
const res = await models.AcquisitionTask.findAndCount(option);
ctx.status = 200;
ctx.body = res;
} catch (error) {
ctx.fs.logger.error(`path: ${ctx.path}, error: ${error}`);
ctx.status = 400;
ctx.body = errMsg
}
}
}
// 修改采集任务
function editAcquisitionTask(opts) {
return async function (ctx, next) {
try {
const models = ctx.fs.dc.models;
const { id } = ctx.params;
const body = ctx.request.body;
const { taskName } = ctx.request.body
const checkName = await models.AcquisitionTask.findOne({ where: { id: { $not: id }, taskName, taskName } });
if (checkName) {
ctx.status = 400;
ctx.body = { message: '该采集任务名称已存在' }
} else {
await models.AcquisitionTask.update(
body,
{ where: { id: id, } }
)
ctx.status = 204;
ctx.body = { message: '修改采集任务成功' }
}
} catch (error) {
ctx.fs.logger.error(`path: ${ctx.path}, error: ${error}`);
ctx.status = 400;
ctx.body = { message: '修改采集任务失败' }
}
}
}
// 删除采集任务
function deleteAcquisitionTask(opts) {
return async function (ctx, next) {
try {
const models = ctx.fs.dc.models;
const { id } = ctx.params;
await models.AcquisitionTask.destroy({
where: {
id: id
}
})
ctx.status = 204;
ctx.body = { message: '删除采集任务成功' }
} catch (error) {
ctx.fs.logger.error(`path: ${ctx.path}, error: ${error}`);
ctx.status = 400;
ctx.body = { message: '删除采集任务失败' }
}
}
}
module.exports = {
addAcquisitionTask,
getAcquisitionTask,
editAcquisitionTask,
deleteAcquisitionTask
}