'use strict'; const moment = require("moment"); async function findPatrolRecords(ctx, next) { const sequelize = ctx.fs.dc.orm; try { let rslt = { planCount: 0, done: 0 } const models = ctx.fs.dc.models const { projectId, startTime, endTime } = ctx.query const plan = await models.PatrolPlan.findAll({ where: { structureId: { $in: projectId.split(',') }, $or: [{ startTime: { $lte: startTime }, endTime: { $gte: endTime } }, { startTime: { $gt: startTime }, endTime: { $lt: endTime } }, { startTime: { $between: [startTime, endTime] }, }, { endTime: { $between: [startTime, endTime] } }, ] } }) if (plan && plan.length) { for (let { dataValues: c } of plan) { let frequency = Number(c.frequency.split('次')[0]) || 0 let points = c.points.length || 0 let done = 0 // if (moment(c.startTime).isSameOrBefore(moment(startTime)) && moment(c.endTime).isSameOrAfter(moment(endTime))) { done = await models.PatrolRecord.count({ where: { inspectionTime: { $between: [c.startTime, endTime] }, patrolPlanId: c.id } }) } else if (moment(c.startTime).isSameOrAfter(moment(startTime)) && moment(c.startTime).isSameOrBefore(moment(endTime))) { done = await models.PatrolRecord.count({ where: { inspectionTime: { $between: [c.startTime, endTime] }, patrolPlanId: c.id } }) } else if (moment(c.endTime).isSameOrAfter(moment(startTime)) && moment(c.endTime).isSameOrBefore(moment(endTime))) { done = await models.PatrolRecord.count({ where: { inspectionTime: { $between: [c.startTime, c.endTime] }, patrolPlanId: c.id } }) } else if (moment(c.startTime).isAfter(moment(startTime)) && moment(c.endTime).isBefore(moment(endTime))) { done = await models.PatrolRecord.count({ where: { inspectionTime: { $between: [c.startTime, c.endTime] }, patrolPlanId: c.id } }) } let dones = await models.PatrolRecord.count({ where: { inspectionTime: { $between: [startTime, endTime] }, patrolPlanId: c.id } }) rslt.planCount += frequency * points - done rslt.done += dones } } 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 findPatrolRate(ctx) { try { const models = ctx.fs.dc.models const sequelize = ctx.fs.dc.orm; const { projectId, startTime, endTime } = ctx.query const res = await models.PatrolRecord.findAll({ attributes: ['id'], where: { projectId: { $in: projectId.split(',') }, }, include: [ { model: models.PatrolRecordIssueHandle, required: true, where: { createTime: { $between: [startTime, endTime] }, }, attributes: ['createTime', 'state'], }, ], }); ctx.body = res ctx.status = 200 } catch (error) { ctx.fs.logger.error(`path: ${ctx.path}, error: ${error}`); ctx.status = 400; ctx.body = { message: '查询巡检完成率失败' } } } async function countByProject(ctx) { try { const models = ctx.fs.dc.models const sequelize = ctx.fs.dc.orm; const { projectId, } = ctx.query const res = await models.Project.findAll({ attributes: [ 'id', 'name', [sequelize.fn('COUNT', sequelize.literal('"PatrolRecords"."id"')), 'patrolRecordCount'], [sequelize.fn('COUNT', sequelize.literal('"PatrolRecords->patrolRecordIssueHandles"."id"')), 'issueHandleCount'], ], include: [ { model: models.PatrolRecord, // as: 'PatrolRecords', attributes: [], include: [ { model: models.PatrolRecordIssueHandle, attributes: [], // as: 'PatrolRecordIssueHandles', }, ], }, ], where: { id: { $in: projectId.split(',') }, }, group: ['project.id', 'project.name'], order: [ [sequelize.fn('COUNT', sequelize.literal('"PatrolRecords"."id"')), 'DESC'], ], }); ctx.body = res ctx.status = 200 } catch (error) { ctx.fs.logger.error(`path: ${ctx.path}, error: ${error}`); ctx.status = 400; ctx.body = { message: '根据结构物汇集失败' } } } async function devicesGuarantee(ctx) { try { let rslt = {} const models = ctx.fs.dc.models const sequelize = ctx.fs.dc.orm; const { projectId, } = ctx.query let option = { // where: searchWhere, order: [["id", "desc"]], include: [{ model: models.PointDevice, include: { model: models.Point, where: { projectId: { $in: projectId.split(',') } } } }] } const res = await models.Device.findAll(option); rslt.count = res.length let underGuarantee = 0 for (let { dataValues: c } of res) { if (moment(c.dateGuarantee).isAfter(moment())) { underGuarantee += 1 } } rslt.underGuarantee = underGuarantee const rs = await sequelize.query(` SELECT d.type, COUNT(d.id) as count FROM device d LEFT JOIN point_device pd ON d.id = pd.device_id LEFT JOIN point p ON pd.point_id = p.id WHERE p.project_id IN (${projectId}) GROUP by d.type`) rslt.rs = rs[0] ctx.body = rslt ctx.status = 200 } catch (error) { ctx.fs.logger.error(`path: ${ctx.path}, error: ${error}`); ctx.status = 400; ctx.body = { message: '查询设备失败' } } } async function getHistoricalFaults(ctx) { try { const models = ctx.fs.dc.models const sequelize = ctx.fs.dc.orm; const { projectId, startTime, endTime } = ctx.query const rslt = await sequelize.query(` select p.name, count(1) as count, count(case when prih.state=6 then 1 end) as bCount from project p left join patrol_record pr on p.id = pr.project_id left join patrol_record_issue_handle prih on pr.id = prih.patrol_record_id and prih.create_time between '${startTime}' and '${endTime}' where p.id in (${projectId}) group by p.name `) ctx.body = rslt[0] ctx.status = 200 } catch (error) { ctx.fs.logger.error(`path: ${ctx.path}, error: ${error}`); ctx.status = 400; ctx.body = { message: '查询历史故障失败' } } } async function getFaultsRank(ctx) { try { let rslt = [] const models = ctx.fs.dc.models const sequelize = ctx.fs.dc.orm; const { projectId, } = ctx.query const rs = await models.PatrolRecord.findAll( { where: { projectId: { $in: projectId.split(',') } } } ) for (let { dataValues: c } of rs) { if (c.points.inspectContent && c.points.inspectContent.length) { for (let p of c.points.inspectContent) { if (p.deviceId) { if(p.alarm){ const isExist = rslt.find(item => item.id === p.deviceId) if (!isExist) { rslt.push({ id: p.deviceId, name: p.deviceName, count: 1 }) } else { isExist.count += 1 } } } } } } rslt.sort((a, b) => b.count - a.count) ctx.body = rslt ctx.status = 200 } catch (error) { ctx.fs.logger.error(`path: ${ctx.path}, error: ${error}`); ctx.status = 400; ctx.body = { message: '查询故障排名失败' } } } async function getCenterData(ctx) { try{ let rslt = {} const models = ctx.fs.dc.models const sequelize = ctx.fs.dc.orm; const { projectId, } = ctx.query let option = { // where: searchWhere, order: [["id", "desc"]], include: [{ model: models.PointDevice, include: { model: models.Point, where: { projectId: { $in: projectId.split(',') } } } }] } const res = await models.Device.findAll(option) const rs = await sequelize.query(` select count(1) as count, count(case when prih.state=6 then 1 end) as bCount from project p left join patrol_record pr on p.id = pr.project_id left join patrol_record_issue_handle prih on pr.id = prih.patrol_record_id where p.id in (${projectId}) `) const record=await models.PatrolRecord.count({where:{projectId:{$in:projectId.split(',')}}}) const reportCount=await models.ReportInfo.count({ where:{ $or:[ {projectId:{$in:projectId.split(',') }}, { structureIds:{$contains:projectId.split(',').map(item=>parseInt(item))} } ] } }) rslt.deviceCount=res.length rslt.questions=rs[0][0]?Number(rs[0][0].count):0 rslt.handleCount=rs[0][0]?Number(rs[0][0].count):0 rslt.records=record rslt.reportCount=reportCount ctx.body = rslt ctx.status = 200 }catch(error){ ctx.fs.logger.error(`path: ${ctx.path}, error: ${error}`); ctx.status = 400; ctx.body = { message: '领导驾驶舱中间数据查询失败' } } } module.exports = { findPatrolRecords, findPatrolRate, countByProject, devicesGuarantee, getHistoricalFaults, getFaultsRank, getCenterData }