'use strict'; async function getPatrolPlan (ctx, next) { try { const models = ctx.fs.dc.models; const { limit, page, userId } = ctx.query; let userWhere = {}; let options = { include: [{ required: true, model: models.User, attributes: ['id', 'name'], where: userWhere, include: [{ required: true, model: models.Department, attributes: ['id', 'name'], }] }, { required: true, model: models.Project, attributes: ['id', 'name'], }] }; 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) { try { const models = ctx.fs.dc.models; const data = ctx.request.body; const { name, way, structureId, startTime, endTime, frequency, points, userId, templateId } = data; let plan = { name, way, structureId, startTime, endTime, frequency, points, userId, templateId }; await models.PatrolPlan.create(plan); ctx.status = 204; } catch (error) { ctx.fs.logger.error(`path: ${ctx.path}, error: ${error}`); ctx.status = 400; ctx.body = { "message": '新增巡检计划失败' } } } async function updatePatrolPlan (ctx, next) { try { let errMsg = '修改巡检计划失败'; const models = ctx.fs.dc.models; const data = ctx.request.body; const { name, way, structureId, startTime, endTime, frequency, points, userId, templateId } = data; let plan = { name, way, structureId, startTime, endTime, frequency, points, userId, templateId }; if (data && data.id) { await models.PatrolPlan.update(plan, { where: { id: data.id } }) } else { errMsg = '请传入巡检计划id'; throw errMsg; } ctx.status = 204; } catch (error) { ctx.fs.logger.error(`path: ${ctx.path}, error: ${error}`); ctx.status = 400; ctx.body = { "message": errMsg } } } async function delPatrolPlan (ctx, next) { 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.PatrolPlan.destroy({ where: { id } }) ctx.status = 204; } catch (error) { ctx.fs.logger.error(`path: ${ctx.path}, error: ${error}`); ctx.status = 400; ctx.body = { message: error } } } module.exports = { getPatrolPlan, createPatrolPlan, updatePatrolPlan, delPatrolPlan, }