From a0e280a81df840b70cd23080540eb17cc8e2c92f Mon Sep 17 00:00:00 2001
From: CODE <1650192445@qq.com>
Date: Wed, 6 Sep 2023 14:40:07 +0800
Subject: [PATCH] =?UTF-8?q?EM=E6=8E=A8=E9=80=81?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
 api/app/lib/schedule/alarms_push.js | 116 ++++++++++++++++++----------
 1 file changed, 75 insertions(+), 41 deletions(-)
diff --git a/api/app/lib/schedule/alarms_push.js b/api/app/lib/schedule/alarms_push.js
index d901773..0dc6264 100644
--- a/api/app/lib/schedule/alarms_push.js
+++ b/api/app/lib/schedule/alarms_push.js
@@ -15,7 +15,7 @@ module.exports = function (app, opts) {
          // interval: '12 0 0 0 */1 *',
          immediate: isDev,
          proRun: !isDev,
-         // proRun: true
+         // disabled: true
       },
       async () => {
          try {
@@ -140,11 +140,6 @@ module.exports = function (app, opts) {
                         }
                      }
 
-                     console.log('corPomsProject', corPomsProject);
-                     console.log('pepProjectName_', pepProjectName_);
-                     console.log('pepProject_name', pepProject_name);
-
-
                      const anxinProjectId = [...curAnxinProjectId]
                      // 查当前 poms 下的结构物 并把不包含的去掉 
                      // 可能有结构物已解绑
@@ -198,8 +193,6 @@ module.exports = function (app, opts) {
                         })
                      })
 
-                     console.log('strucListRes', strucListRes);
-
                      let strucThingId = []
                      let strucMap = {}
                      let searchStrucIds = strucListRes.map(s => {
@@ -505,10 +498,6 @@ module.exports = function (app, opts) {
                         if (c.alarmSubType) dataAlarmSubType = dataAlarmSubType.concat(c.alarmSubType['device_exception'])
                      }
 
-
-                     console.log('dataAlarmGroupOption', dataAlarmGroupOption);
-                     console.log('searchStrucIds', searchStrucIds);
-
                      // 查数据告警 三警合一
                      if (dataAlarmGroupOption.length && searchStrucIds.length) {
                         dataAlarmGroupOption.push(-1)
@@ -741,8 +730,7 @@ module.exports = function (app, opts) {
                         n: '监测因素(中断比例)',
                         k: 'factor',
                         // f: (d) => {
-                        //    // d.factor.join('')
-                        //    // console.log(21211231131,d.factor);
+                        //    // d.factor.join('') 
                         //    let data = []
                         //    d.factor.map(f => data.push(f.name + '(' + f.breakData + '/' + f.sum + ')'))
                         //    return data.join('
')
@@ -890,10 +878,10 @@ module.exports = function (app, opts) {
                      let dataAlarmG45 = [];
                      let deviceStatistic = new Set()
                      let dataAlarmDetails = []
-                     if (proDebug) {
-                        console.log(`查得数据告警 ${dataAlarms.length} 条`);
-                        console.log(dataAlarms);
-                     }
+
+                     pLog(`查得数据告警 ${dataAlarms.length} 条`);
+                     pLog(dataAlarms);
+
                      if (dataAlarms.length) {
                         const alarmIds = dataAlarms
                            .map(ar => "'" + ar.AlarmId + "'")
@@ -904,12 +892,8 @@ module.exports = function (app, opts) {
                                  AND AlarmState = 0
                            `).toPromise()
                      }
-                     if (proDebug) {
-                        console.log(`查得数据告警详情数据  ${dataAlarmDetails.length} 条`);
-                     }
-                     if (proDebug) {
-                        console.log(`pomsStrucFactorId:`, pomsStrucFactorId);
-                     }
+
+                     pLog(`查得数据告警详情数据  ${dataAlarmDetails.length} 条`);
 
                      let deviceIds = new Set()
                      for (let d of dataAlarms) {
@@ -1135,15 +1119,15 @@ module.exports = function (app, opts) {
                         const factorData = await clickHouse.anxinyun.query(
                            `  
                               SELECT 
-                                    t_structure_factor.structure AS structureId,
-                                    t_factor.name AS name,
-                                    t_factor.id AS id
+                                 t_structure_factor.structure AS structureId,
+                                 t_factor.name AS name,
+                                 t_factor.id AS id
                               FROM 
-                                    t_structure_factor
+                                 t_structure_factor
                               LEFT JOIN t_factor
                                  ON t_factor.id = t_structure_factor.factor
                               WHERE 
-                              t_structure_factor.structure IN (${[...dataAlarmG1StructureId]})
+                                 t_structure_factor.structure IN (${[...dataAlarmG1StructureId]})
                           `
                         ).toPromise() || []
 
@@ -1163,7 +1147,7 @@ module.exports = function (app, opts) {
                                  LEFT JOIN t_device_sensor
                                     ON t_device_sensor.sensor = t_sensor.id
                               WHERE 
-                              t_sensor.structure IN (${[...dataAlarmG1StructureId]})
+                              t_sensor.structure IN (${[...dataAlarmG1StructureId]},)
                               AND
                               t_sensor.factor IN (${factorId.join(',')})
                             `
@@ -1180,7 +1164,8 @@ module.exports = function (app, opts) {
                                  alarms.StartTime AS StartTime,
                                  alarms.SourceName AS SourceName,
                                  alarms.AlarmCode AS AlarmCode,
-                                 alarms.SourceTypeId AS SourceTypeId
+                                 alarms.SourceTypeId AS SourceTypeId,
+                                 alarms.StructureId AS StructureId
                               FROM 
                                  alarms
                               WHERE 
@@ -1193,11 +1178,14 @@ module.exports = function (app, opts) {
                         ).toPromise() || []
 
                         // 为设备分配自己产生的告警数据
+                        let matchedAlarmIds = []
+                        // 这一步能被分配的告警应该也能最终体现在邮件中
                         equipment.map(f => {
                            f.alarmDatas = []
                            alarmDatas.map(r => {
                               if (r.sourceId == f.iotaDeviceId) {
                                  f.alarmDatas.push({ ...r })
+                                 matchedAlarmIds.push(r.alarmId)
                               }
                            })
                         })
@@ -1238,9 +1226,60 @@ module.exports = function (app, opts) {
                            })
                         }
 
-                        // console.log('数据组合', JSON.stringify(pepProject_name));
                         pLog('数据组合' + JSON.stringify(pepProject_name))
 
+                        // 上面的代码自内而外构建数据
+                        // 下面的逻辑自外向内!!!
+
+                        // 处理那些没有被匹配到的告警信息
+                        // 将设备没有绑定到测点的告警也推出去
+                        for (let a of alarmDatas) {
+                           if (!matchedAlarmIds.includes(a.alarmId)) {
+                              let curStruc = strucMap[a.StructureId]
+                              if (curStruc) {
+                                 // curStruc 可以推出 告警属于哪个 project -> pomsProject
+                                 let curProject = pepProject_name.filter(p => p.anxinProjectId && p.anxinProjectId.includes(curStruc.projectId));
+                                 for (let cp of curProject) {
+                                    // cp.projects 是结构物信息
+                                    let curStruc = cp.projects.find(cpp => cpp.id == a.StructureId)
+                                    if (!curStruc) {
+                                       console.error(`没有查到结构物信息:${a.StructureId}?!不应该不应该!`);
+                                    } else {
+                                       // let emptyFactor = { id: -1 }
+                                       let emptyFactor = curStruc.factor.find(f => f.id == -1)
+                                       if (!emptyFactor) {
+                                          curStruc.factor.push({
+                                             id: -1,
+                                             devices: [{
+                                                alarmDatas: [
+                                                   { ...a }
+                                                ],
+                                                iotaDeviceId: a.sourceId,
+                                                name: a.SourceName,
+                                                structureId: a.StructureId,
+                                             }]
+                                          })
+                                       } else {
+                                          let curDevice = emptyFactor.devices.find(d => d.iotaDeviceId == a.sourceId)
+                                          if (!curDevice) {
+                                             emptyFactor.devices.push({
+                                                alarmDatas: [
+                                                   { ...a }
+                                                ],
+                                                iotaDeviceId: a.sourceId,
+                                                name: a.SourceName,
+                                                structureId: a.StructureId,
+                                             })
+                                          } else {
+                                             curDevice.alarmDatas.push({ ...a })
+                                          }
+                                       }
+                                    }
+                                 }
+                              }
+                           }
+                        }
+
                         pepProject_name.forEach(h => {
                            let rowspan1 = 0
                            if (h.projects.length) {
@@ -1301,7 +1340,7 @@ module.exports = function (app, opts) {
                            h.rowspan = rowspan1
                         })
 
-                        dataAlarmG1 = pepProject_name.filter(b => b.projects && b.projects.length > 0) || []
+                        dataAlarmG1 = pepProject_name //.filter(b => b.projects && b.projects.length > 0) || []
                      }
 
 
@@ -1359,7 +1398,7 @@ module.exports = function (app, opts) {
                                                                      break;
                                                                   case 'factor':
                                                                      if (showOne3 && showOne4) {
-                                                                        tableData += `