@ -2,6 +2,7 @@
const moment = require ( "moment" ) ;
async function findPatrolRecord ( ctx , next ) {
let rslt = [ ] ;
let error = { name : 'FindError' , message : '获取巡检记录失败' } ;
@ -414,15 +415,15 @@ function editPatrolRecordIssueHandle(opts) {
}
//查询子系统的当月的巡检和维修
function getSubSystemPatrolAbout ( opts ) {
return async function ( ctx , next ) {
try {
let rslt = [ ]
return async function ( ctx , next ) {
try {
let rslt = [ ]
const models = ctx . fs . dc . models ;
const { STime , ETime , keywords } = ctx . query
let generalInclude = [ { model : models . PatrolRecordIssueHandle } , { model : models . Project , where : { subType : { $like : ` % ${ keywords } % ` } } } ]
rslt = await models . PatrolRecord . findAll ( {
where : { inspectionTime : { $between : [ STime , ETime ] } } ,
include : generalInclude
const { STime , ETime , keywords } = ctx . query
let generalInclude = [ { model : models . PatrolRecordIssueHandle } , { model : models . Project , where : { subType : { $like : ` % ${ keywords } % ` } } } ]
rslt = await models . PatrolRecord . findAll ( {
where : { inspectionTime : { $between : [ STime , ETime ] } } ,
include : generalInclude
} )
let userInfo = ctx . fs . api . userInfo ;
rslt = rslt . filter ( f => f )
@ -435,14 +436,151 @@ function getSubSystemPatrolAbout(opts) {
}
ctx . status = 200 ;
ctx . body = rslt
} catch ( error ) {
} catch ( error ) {
ctx . fs . logger . error ( ` path: ${ ctx . path } , error: ${ error } ` ) ;
ctx . status = 400 ;
ctx . body = { message : '子系统查询巡检记录失败' }
}
}
}
//
/ * *
* 查询故障风险统计
* @ param structures { String } 结构物id , example : 1 , 2 , 3
* /
function getPatrolRecordStatistic ( opts ) {
return async function ( ctx , next ) {
try {
let rslt = {
monthAlarmCount : 0 , // 本月上报风险
monthHandleCount : 0 , // 本月处理风险
historyTrend : [ ] , // 历史风险趋势
monthDeviceAlarm : [ ] , // 设备故障统计
} ;
const models = ctx . fs . dc . models ;
const sequelize = ctx . fs . dc . orm ;
const { structures } = ctx . query ;
const monthStartTime = moment ( ) . startOf ( "month" ) . format ( 'YYYY-MM-DD HH:mm:ss' ) ;
const historyStartTime = moment ( ) . startOf ( "month" ) . subtract ( 11 , 'months' ) . format ( 'YYYY-MM-DD HH:mm:ss' ) ;
const endTime = moment ( ) . endOf ( "month" ) . format ( 'YYYY-MM-DD HH:mm:ss' ) ;
const monthAlarm = await models . PatrolRecord . findAndCountAll ( {
where : {
inspectionTime : { $between : [ monthStartTime , endTime ] } ,
projectId : { $in : structures . split ( ',' ) } ,
alarm : true
} ,
include : [ {
model : models . Project ,
where : { type : '管廊' }
} ] ,
} )
rslt . monthAlarmCount = monthAlarm . count ;
let abnormalDevice = [ ] ;
for ( const r of monthAlarm . rows ) {
if ( Array . isArray ( r . points . inspectContent ) ) {
for ( const d of r . points . inspectContent ) {
if ( d . deviceName && d . alarm ) {
let abnormalCount = 0 , abnormalScore = 0 , slight = 0 , middle = 0 , severity = 0 , itemsCount = [ ] ;
const index = abnormalDevice . findIndex ( e => e . deviceId === d . deviceId ) ;
if ( index !== - 1 ) {
itemsCount = abnormalDevice [ index ] . itemsCount ;
slight = abnormalDevice [ index ] . slight ;
middle = abnormalDevice [ index ] . middle ;
severity = abnormalDevice [ index ] . severity ;
}
for ( const item of d . checkItems ) {
if ( item . isNormal === false ) {
abnormalCount += 1 ;
switch ( item . level ) {
case '轻微' :
slight += 1 ;
abnormalScore += 1 ;
break ;
case '中度' :
middle += 1 ;
abnormalScore += 3 ;
break ;
case '严重' :
severity += 1 ;
abnormalScore += 5 ;
break ;
default :
break ;
}
const itemIndex = itemsCount . findIndex ( i => i . name === item . name ) ;
if ( itemIndex === - 1 ) {
itemsCount . push ( { name : item . name , count : 1 } ) ;
} else {
itemsCount [ itemIndex ] . count += 1 ;
}
}
}
if ( index !== - 1 ) {
abnormalDevice [ index ] . abnormalCount += abnormalCount ;
abnormalDevice [ index ] . abnormalScore += abnormalScore ;
abnormalDevice [ index ] . itemsCount = itemsCount ;
abnormalDevice [ index ] . slight = slight ;
abnormalDevice [ index ] . middle = middle ;
abnormalDevice [ index ] . severity = severity ;
} else {
abnormalDevice . push ( {
deviceId : d . deviceId , deviceName : d . deviceName , project : r . points . project . name ,
abnormalCount , abnormalScore , itemsCount , slight , middle , severity ,
} )
}
}
}
}
}
rslt . monthDeviceAlarm = abnormalDevice ;
rslt . monthHandleCount = await models . PatrolRecord . count ( {
where : {
inspectionTime : { $between : [ monthStartTime , endTime ] } ,
projectId : { $in : structures . split ( ',' ) } ,
alarm : true
} ,
include : [ {
model : models . PatrolRecordIssueHandle ,
where : { state : 6 } // 验收通过
} , {
model : models . Project ,
where : { type : '管廊' }
} ] ,
} )
// rslt.historyTrend = await sequelize.query(
// `select to_char(inspection_time::DATE, 'YYYY-MM') as month, COUNT(*) as num from patrol_record INNER JOIN "project" ON "patrol_record"."project_id" = "project"."id" AND "project"."type" = '管廊' where inspection_time >= '${historyStartTime}' and inspection_time <= '${endTime}' group by month order by month;`
// );
const monthFn = sequelize . fn ( 'to_char' , sequelize . col ( 'inspection_time' ) , 'YYYY-MM' ) ;
rslt . historyTrend = await models . PatrolRecord . findAll ( {
attributes : [
[ monthFn , 'month' ] ,
[ sequelize . fn ( 'COUNT' , sequelize . col ( '*' ) ) , 'count' ] ,
] ,
group : [ monthFn ] ,
order : [ monthFn ] ,
where : {
inspectionTime : { $between : [ historyStartTime , endTime ] } ,
projectId : { $in : structures . split ( ',' ) } ,
alarm : true
} ,
include : [ {
attributes : [ ] ,
model : models . Project ,
where : { type : '管廊' }
} ] ,
} )
ctx . status = 200 ;
ctx . body = rslt
} catch ( error ) {
ctx . fs . logger . error ( ` path: ${ ctx . path } , error: ${ error } ` ) ;
ctx . status = 400 ;
ctx . body = { message : '查询故障风险统计失败' }
}
}
}
Array . prototype . group = function ( callback , thisArg = null ) {
// 参数合法性判断
@ -473,5 +611,6 @@ module.exports = {
getPatrolRecordIssueHandleById ,
addPatrolRecordIssueHandle ,
editPatrolRecordIssueHandle ,
getSubSystemPatrolAbout
getSubSystemPatrolAbout ,
getPatrolRecordStatistic
}