'use strict'; const moment = require("moment"); async function findPatrolRecord (ctx, next) { let rslt = []; let error = { name: 'FindError', message: '获取巡检记录失败' }; try { const models = ctx.fs.dc.models; const { startTime, endTime, alarm, patrolPlanId, pointId } = ctx.params; // patrolPlanId传all查所有 let generalInclude = [{ model: models.PatrolPlan, attributes: ['name'] }, { model: models.PatrolRecordIssueHandle } ] if (patrolPlanId == 'all') { /* 如果有startTime && endTime,查询所有符合条件的数据 */ if (startTime !== 'null' && endTime !== 'null') { if (pointId !== 'null') { if (alarm == 'null') { rslt = await models.PatrolRecord.findAll({ where: { inspectionTime: { $between: [startTime, endTime] }, pointId: { $in: pointId.split(',') } }, include: generalInclude }); } else { rslt = await models.PatrolRecord.findAll({ where: { alarm, inspectionTime: { $between: [startTime, endTime] }, pointId: { $in: pointId.split(',') } }, include: generalInclude }); } } else { if (alarm == 'null') { rslt = await models.PatrolRecord.findAll({ where: { inspectionTime: { $between: [startTime, endTime] } }, include: generalInclude }); } else { rslt = await models.PatrolRecord.findAll({ where: { alarm, inspectionTime: { $between: [startTime, endTime] } }, include: generalInclude }); } } } else { /* 如果没有startTime && endTime,查询每个点位最新一条符合条件的数据 */ let a = [] if (pointId !== 'null') { a = await models.PatrolRecord.findAll({ where: { pointId: { $in: pointId.split(',') } }, include: generalInclude }); } rslt = pointId.split(',').map(i => { return a.filter(t => t.pointId == i).sort((a, b) => b.id - a.id)[0] || null }) } } else { if (startTime !== 'null' && endTime !== 'null') { if (pointId !== 'null') { rslt = await models.PatrolRecord.findAll({ where: { patrolPlanId: { $in: patrolPlanId.split(',') }, alarm, inspectionTime: { $between: [startTime, endTime] }, pointId: { $in: pointId.split(',') } }, include: generalInclude }); } else { rslt = await models.PatrolRecord.findAll({ where: { patrolPlanId: { $in: patrolPlanId.split(',') }, alarm, inspectionTime: { $between: [startTime, endTime] } }, include: generalInclude }); } } else { let a = [] /* 如果没有startTime && endTime,查询每个点位最新一条符合条件的数据 */ if (pointId !== 'null') { a = await models.PatrolRecord.findAll({ where: { patrolPlanId: { $in: patrolPlanId.split(',') }, pointId: { $in: pointId.split(',') } }, include: generalInclude }); } else { a = await models.PatrolRecord.findAll({ where: { patrolPlanId: { $in: patrolPlanId.split(',') } }, include: generalInclude }); } rslt = pointId.split(',').map(i => { return a.filter(t => t.pointId == i).sort((a, b) => b.id - a.id)[0] || null }) } } let userInfo = ctx.fs.api.userInfo; rslt = rslt.filter(f => f) if (userInfo.username != 'SuperAdmin') { if (userInfo.structure) { rslt = rslt.filter(s => userInfo.structure.find(x => x == s.points.project.id)) } else { rslt = [] } } ctx.status = 200; ctx.body = rslt; error = null } catch (error) { ctx.fs.logger.error(`path: ${ctx.path}, error: ${error}`); ctx.status = 400; ctx.body = { "message": "获取巡检记录失败" } } } async function findPatrolRecordUnlicensed (ctx, next) { let rslt = []; let error = { name: 'FindError', message: '获取巡检记录失败' }; try { const models = ctx.fs.dc.models; const { startTime, endTime } = ctx.query; const options = { where: {} } if (startTime && endTime) { options.where.inspectionTime = { $between: [startTime, endTime] }; } rslt = await models.PatrolRecord.findAll(options); ctx.status = 200; ctx.body = rslt; } catch (error) { ctx.fs.logger.error(`path: ${ctx.path}, error: ${error}`); ctx.status = 400; ctx.body = { "message": "获取巡检记录失败" } } } async function addPatrolRecord (ctx, next) { let error = { name: 'addError', message: '新增巡检记录失败' }; const transaction = await ctx.fs.dc.orm.transaction(); try { const models = ctx.fs.dc.models; const data = ctx.request.body; let { patrolPlanId, inspectionTime, points, alarm, pointId } = data const pointRecord = await models.PatrolRecord.findAll({ where: { pointId: pointId }, order: [['inspectionTime', 'desc']], attributes: ['inspectionTime'], }); const lastInspectionTime = pointRecord.length ? pointRecord[0].dataValues.inspectionTime : null; let record = { patrolPlanId, lastInspectionTime, inspectionTime, points, alarm, pointId } const recordRes = await models.PatrolRecord.create(record, transaction); if (alarm) { await models.PatrolRecord.create({ patrolRecordId: recordRes.id, state: 1, }, transaction); } // 更新巡检次数统计 const curPlanRecord = await models.PatrolRecord.findAndCountAll({ where: { patrolPlanId } }); const patrolCount = curPlanRecord.count; await models.PatrolPlan.update({ patrolCount }, { where: { id: patrolPlanId }, transaction }) await transaction.commit(); ctx.status = 204; error = null } catch (error) { await transaction.rollback(); ctx.fs.logger.error(`path: ${ctx.path}, error: ${error}`); ctx.status = 400; ctx.body = { "message": '新增巡检记录失败' } } } async function getPatrolRecordIssueHandle (ctx) { try { const { models } = ctx.fs.dc; const { userId, userInfo } = ctx.fs.api const { type, pointId, startTime, endTime } = ctx.query let findOption = { where: { $or: [] }, include: [{ model: models.PatrolRecord, where: { } }] } if (pointId) { findOption.include[0].where.pointId = { $in: pointId.split(',') } } if (startTime && endTime) { findOption.where.createTime = { $between: [moment(parseInt(startTime)).format(), moment(parseInt(endTime)).format()] } } let isSuperA = userInfo.username == 'SuperAdmin' let focusStruct = userInfo.structure || [] if (!isSuperA) { findOption.include[0].where.points = { project: { id: { $in: focusStruct } } } } if (type == 'backlog') { // 待办 if (userInfo && userInfo.userResources.includes('ZHIDINGJIHUA')) { findOption.where['$or'].push({ state: 1 }) findOption.where['$or'].push({ state: 3 }) } // 有审批权限且关注此结构物 if (userInfo && userInfo.userResources.includes('SHENHE')) { findOption.where['$or'].push({ state: 2 }) } findOption.where['$or'].push({ state: 4, repairPerson: { id: userId } }) findOption.where['$or'].push({ state: 5, checkPerson: { id: userId } }) findOption.where['$or'].push({ state: 7, repairPerson: { id: userId } }) } else if (type == 'haveDone') { // 已办 findOption.where['$or'].push({ state: { $notIn: [1, 2] }, creator: { id: userId } }) findOption.where['$or'].push({ state: { $gt: 2 }, approvePerson: { id: userId } }) findOption.where['$or'].push({ state: { $gt: 4, $ne: 7 }, repairPerson: { id: userId } }) findOption.where['$or'].push({ state: { $gt: 5 }, checkPerson: { id: userId } }) } const res = await models.PatrolRecordIssueHandle.findAll(findOption) ctx.status = 200; ctx.body = res } catch (error) { ctx.fs.logger.error(`path: ${ctx.path}, error: ${error}`); ctx.status = 400; ctx.body = { message: typeof error == 'string' ? error : undefined } } } async function getPatrolRecordIssueHandleById (ctx) { try { const { models } = ctx.fs.dc; const { id } = ctx.params const res = await models.PatrolRecordIssueHandle.findOne({ where: { id }, include: [{ model: models.PatrolRecord }] }) ctx.status = 200; ctx.body = res } catch (error) { ctx.fs.logger.error(`path: ${ctx.path}, error: ${error}`); ctx.status = 400; ctx.body = { message: typeof error == 'string' ? error : undefined } } } // 新建维修处理计划成功 function addPatrolRecordIssueHandle (opts) { return async function (ctx, next) { const models = ctx.fs.dc.models; try { let rslt = ctx.request.body; await models.PatrolRecordIssueHandle.create(Object.assign({}, rslt)) ctx.status = 204; ctx.body = { message: '新建维修处理计划成功' } } catch (error) { ctx.fs.logger.error(`path: ${ctx.path}, error: ${error}`); ctx.status = 400; ctx.body = { message: '新建修处理计划失败' } } } } // 修改维修处理计划 function editPatrolRecordIssueHandle (opts) { return async function (ctx, next) { try { const models = ctx.fs.dc.models; const { id } = ctx.params; const body = ctx.request.body; await models.PatrolRecordIssueHandle.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: '修改维修处理计划失败' } } } } module.exports = { findPatrolRecord, findPatrolRecordUnlicensed, addPatrolRecord, getPatrolRecordIssueHandle, getPatrolRecordIssueHandleById, addPatrolRecordIssueHandle, editPatrolRecordIssueHandle }