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.

182 lines
5.7 KiB

2 years ago
'use strict';
2 years ago
const moment = require('moment');
2 years ago
async function getPatrolPlan (ctx, next) {
2 years ago
try {
const models = ctx.fs.dc.models;
const { limit, page, userId } = ctx.query;
2 years ago
let where = {}
let userWhere = {};
2 years ago
let options = {
order: [['id', 'desc']],
2 years ago
where,
2 years ago
include: [{
required: userId ? true : false,
2 years ago
model: models.User,
attributes: ['id', 'name'],
where: userWhere,
2 years ago
include: [{
model: models.Department,
attributes: ['id', 'name'],
}]
}, {
model: models.Project,
attributes: ['id', 'name'],
}],
distinct: true,
2 years ago
};
if (limit) {
options.limit = Number(limit);
}
if (page && limit) {
options.offset = Number(page) * Number(limit);
}
if (userId) {
userWhere.id = userId;
2 years ago
where.startTime = { $lte: moment().format('YYYY-MM-DD') + ' 23:59:59' }
where.endTime = { $gte: moment().format('YYYY-MM-DD') + ' 00:00:00' }
where.nextTime = { $lte: moment().format('YYYY-MM-DD HH:mm:ss') }
}
2 years ago
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();
2 years ago
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 };
2 years ago
const patrolPlanRes = await models.PatrolPlan.create({ ...plan, nextTime: moment().format() }, { transaction });
await models.PatrolPlanUser.bulkCreate(
userIds.map(uid => {
return {
patrolPlanId: patrolPlanRes.id,
userId: uid
}
}), { transaction }
)
await transaction.commit();
2 years ago
ctx.status = 204;
} catch (error) {
await transaction.rollback();
2 years ago
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();
2 years ago
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;
2 years ago
2 years ago
const frequencyNum = Number(frequency.split('次')[0]);
const unit = frequency.split('/')[1];
const sTime = moment().startOf(unit === '天' ? 'day' : unit === '周' ? 'isoWeek' : 'month');
const eTime = moment().endOf(unit === '天' ? 'day' : unit === '周' ? 'isoWeek' : 'month');
const curPlanRecord = await models.PatrolRecord.findAll({
where: {
patrolPlanId: data.id,
inspectionTime: { $between: [sTime.format(), eTime.format()] },
}
});
let plan = { name, way, structureId, startTime, endTime, frequency, points, templateId,
nextTime: curPlanRecord.length >= frequencyNum ? eTime.format() : sTime.format()
};
2 years ago
if (data && data.id) {
await models.PatrolPlan.update(plan, {
where: { id: data.id },
transaction
2 years ago
})
await models.PatrolPlanUser.destroy({
where: { patrolPlanId: data.id },
transaction
})
await models.PatrolPlanUser.bulkCreate(
userIds.map(uid => {
return {
patrolPlanId: data.id,
userId: uid
}
}), { transaction }
)
2 years ago
} else {
errMsg = '请传入巡检计划id';
throw errMsg;
}
await transaction.commit();
2 years ago
ctx.status = 204;
} catch (error) {
await transaction.rollback();
2 years ago
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();
2 years ago
try {
let errMsg = '删除巡检计划失败';
2 years ago
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
})
2 years ago
await models.PatrolPlan.destroy({
where: { id },
transaction
2 years ago
})
await transaction.commit();
2 years ago
ctx.status = 204;
} catch (error) {
await transaction.rollback();
2 years ago
ctx.fs.logger.error(`path: ${ctx.path}, error: ${error}`);
ctx.status = 400;
ctx.body = { message: error }
2 years ago
}
}
module.exports = {
getPatrolPlan,
createPatrolPlan,
updatePatrolPlan,
delPatrolPlan,
}