|
|
|
'use strict';
|
|
|
|
|
|
|
|
async function getPatrolPlan (ctx, next) {
|
|
|
|
try {
|
|
|
|
const models = ctx.fs.dc.models;
|
|
|
|
const { limit, page, userId } = ctx.query;
|
|
|
|
let userWhere = {};
|
|
|
|
let options = {
|
|
|
|
order: [['id', 'desc']],
|
|
|
|
include: [{
|
|
|
|
required: userId ? true : false,
|
|
|
|
model: models.User,
|
|
|
|
attributes: ['id', 'name'],
|
|
|
|
where: userWhere,
|
|
|
|
include: [{
|
|
|
|
model: models.Department,
|
|
|
|
attributes: ['id', 'name'],
|
|
|
|
}]
|
|
|
|
}, {
|
|
|
|
model: models.Project,
|
|
|
|
attributes: ['id', 'name'],
|
|
|
|
}],
|
|
|
|
distinct: true,
|
|
|
|
};
|
|
|
|
if (limit) {
|
|
|
|
options.limit = Number(limit);
|
|
|
|
}
|
|
|
|
if (page && limit) {
|
|
|
|
options.offset = Number(page) * Number(limit);
|
|
|
|
}
|
|
|
|
if (userId) {
|
|
|
|
userWhere.id = userId;
|
|
|
|
}
|
|
|
|
let res = await models.PatrolPlan.findAndCountAll(options);
|
|
|
|
ctx.status = 200;
|
|
|
|
ctx.body = res;
|
|
|
|
} catch (error) {
|
|
|
|
ctx.fs.logger.error(`path: ${ctx.path}, error: ${error}`);
|
|
|
|
ctx.status = 400;
|
|
|
|
ctx.body = {
|
|
|
|
"message": "获取巡检计划失败"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async function createPatrolPlan(ctx, next) {
|
|
|
|
const transaction = await ctx.fs.dc.orm.transaction();
|
|
|
|
try {
|
|
|
|
const models = ctx.fs.dc.models;
|
|
|
|
const data = ctx.request.body;
|
|
|
|
const { name, way, structureId, startTime, endTime, frequency, points, userIds, templateId } = data;
|
|
|
|
|
|
|
|
let plan = { name, way, structureId, startTime, endTime, frequency, points, templateId };
|
|
|
|
|
|
|
|
const patrolPlanRes = await models.PatrolPlan.create(plan, { transaction });
|
|
|
|
await models.PatrolPlanUser.bulkCreate(
|
|
|
|
userIds.map(uid => {
|
|
|
|
return {
|
|
|
|
patrolPlanId: patrolPlanRes.id,
|
|
|
|
userId: uid
|
|
|
|
}
|
|
|
|
}), { transaction }
|
|
|
|
)
|
|
|
|
|
|
|
|
await transaction.commit();
|
|
|
|
ctx.status = 204;
|
|
|
|
} catch (error) {
|
|
|
|
await transaction.rollback();
|
|
|
|
ctx.fs.logger.error(`path: ${ctx.path}, error: ${error}`);
|
|
|
|
ctx.status = 400;
|
|
|
|
ctx.body = {
|
|
|
|
"message": '新增巡检计划失败'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async function updatePatrolPlan (ctx, next) {
|
|
|
|
const transaction = await ctx.fs.dc.orm.transaction();
|
|
|
|
try {
|
|
|
|
let errMsg = '修改巡检计划失败';
|
|
|
|
const models = ctx.fs.dc.models;
|
|
|
|
const data = ctx.request.body;
|
|
|
|
const { name, way, structureId, startTime, endTime, frequency, points, userIds, templateId } = data;
|
|
|
|
|
|
|
|
let plan = { name, way, structureId, startTime, endTime, frequency, points, templateId };
|
|
|
|
|
|
|
|
if (data && data.id) {
|
|
|
|
await models.PatrolPlan.update(plan, {
|
|
|
|
where: { id: data.id },
|
|
|
|
transaction
|
|
|
|
})
|
|
|
|
await models.PatrolPlanUser.destroy({
|
|
|
|
where: { patrolPlanId: data.id },
|
|
|
|
transaction
|
|
|
|
})
|
|
|
|
await models.PatrolPlanUser.bulkCreate(
|
|
|
|
userIds.map(uid => {
|
|
|
|
return {
|
|
|
|
patrolPlanId: data.id,
|
|
|
|
userId: uid
|
|
|
|
}
|
|
|
|
}), { transaction }
|
|
|
|
)
|
|
|
|
} else {
|
|
|
|
errMsg = '请传入巡检计划id';
|
|
|
|
throw errMsg;
|
|
|
|
}
|
|
|
|
|
|
|
|
await transaction.commit();
|
|
|
|
ctx.status = 204;
|
|
|
|
} catch (error) {
|
|
|
|
await transaction.rollback();
|
|
|
|
ctx.fs.logger.error(`path: ${ctx.path}, error: ${error}`);
|
|
|
|
ctx.status = 400;
|
|
|
|
ctx.body = {
|
|
|
|
"message": errMsg
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async function delPatrolPlan (ctx, next) {
|
|
|
|
const transaction = await ctx.fs.dc.orm.transaction();
|
|
|
|
try {
|
|
|
|
let errMsg = '删除巡检计划失败';
|
|
|
|
|
|
|
|
const models = ctx.fs.dc.models;
|
|
|
|
const { id } = ctx.params;
|
|
|
|
|
|
|
|
const record = await models.PatrolRecord.findOne({
|
|
|
|
where: { patrolPlanId: id }
|
|
|
|
});
|
|
|
|
|
|
|
|
if (record) {
|
|
|
|
errMsg = '不能删除有巡检记录的计划';
|
|
|
|
throw errMsg;
|
|
|
|
}
|
|
|
|
|
|
|
|
await models.PatrolPlanUser.destroy({
|
|
|
|
where: { patrolPlanId: id },
|
|
|
|
transaction
|
|
|
|
})
|
|
|
|
|
|
|
|
await models.PatrolPlan.destroy({
|
|
|
|
where: { id },
|
|
|
|
transaction
|
|
|
|
})
|
|
|
|
|
|
|
|
await transaction.commit();
|
|
|
|
ctx.status = 204;
|
|
|
|
} catch (error) {
|
|
|
|
await transaction.rollback();
|
|
|
|
ctx.fs.logger.error(`path: ${ctx.path}, error: ${error}`);
|
|
|
|
ctx.status = 400;
|
|
|
|
ctx.body = { message: error }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
getPatrolPlan,
|
|
|
|
createPatrolPlan,
|
|
|
|
updatePatrolPlan,
|
|
|
|
delPatrolPlan,
|
|
|
|
}
|