diff --git a/api/app/lib/controllers/alarm/data.js b/api/app/lib/controllers/alarm/data.js index 6f53679..e8bef30 100644 --- a/api/app/lib/controllers/alarm/data.js +++ b/api/app/lib/controllers/alarm/data.js @@ -34,76 +34,81 @@ async function list (ctx) { const { utils: { judgeSuper, anxinStrucIdRange } } = ctx.app.fs const { database: anxinyun } = clickHouse.anxinyun.opts.config - const { pepProjectId, keyword, groupId, groupUnitId, sustainTimeStart, sustainTimeEnd, limit, page } = ctx.query + const { pepProjectId, keyword, groupId, groupUnitId, sustainTimeStart, sustainTimeEnd, limit, page, projectOrStructKeyword } = ctx.query const isSuper = judgeSuper(ctx) - let anxinStrucIds = await anxinStrucIdRange({ ctx, pepProjectId }) + let anxinStrucIds = await anxinStrucIdRange({ + ctx, pepProjectId, projectOrStructKeyword, + }) let whereOption = [] - if (anxinStrucIds) { + if (anxinStrucIds.length) { whereOption.push(`alarms.StructureId IN (${anxinStrucIds.join(",")})`) - } - if (groupId) { - whereOption.push(`alarms.AlarmGroup IN (${groupId})`) - } - if (groupUnitId) { - whereOption.push(`alarms.AlarmGroupUnit=${groupId}`) - } - if (sustainTimeStart && sustainTimeEnd) { - let momentStart = moment(sustainTimeStart).format() - let momentEnd = moment(sustainTimeEnd).format() - whereOption.push(` - ( - alarms."StartTime" - BETWEEN '${momentStart}' AND '${momentEnd}' - OR - "alarms"."EndTime" BETWEEN '${momentStart}' AND '${momentEnd}' - OR ( - "alarms"."StartTime" <= '${momentStart}' - AND - "alarms"."EndTime" >= '${momentEnd}' - ) - ) - `) - } - const alarmRes = await clickHouse.dataAlarm.query(` - SELECT - alarms.AlarmId AS AlarmId, - alarms.State AS State, - SourceName, StartTime, EndTime, - alarms.CurrentLevel AS CurrentLevel, - SourceTypeId, - AlarmAdviceProblem, AlarmGroup, AlarmGroupUnit, AlarmAdviceProblem, - ${anxinyun}.t_structure.name AS StructureName, - StructureId, - ${anxinyun}.t_alarm_code.name AS AlarmCodeName + if (groupId) { + whereOption.push(`alarms.AlarmGroup IN (${groupId})`) + } + if (groupUnitId) { + whereOption.push(`alarms.AlarmGroupUnit=${groupId}`) + } + if (sustainTimeStart && sustainTimeEnd) { + let momentStart = moment(sustainTimeStart).format() + let momentEnd = moment(sustainTimeEnd).format() + whereOption.push(` + ( + alarms."StartTime" + BETWEEN '${momentStart}' AND '${momentEnd}' + OR + "alarms"."EndTime" BETWEEN '${momentStart}' AND '${momentEnd}' + OR ( + "alarms"."StartTime" <= '${momentStart}' + AND + "alarms"."EndTime" >= '${momentEnd}' + ) + ) + `) + } - FROM - alarms - LEFT JOIN ${anxinyun}.t_structure - ON ${anxinyun}.t_structure.id = alarms.StructureId - LEFT JOIN ${anxinyun}.t_alarm_code - ON ${anxinyun}.t_alarm_code.code = alarms.AlarmTypeCode - - ${whereOption.length ? 'WHERE ' + whereOption.join(' AND ') : ''} - ORDER BY alarms.StartTime DESC - ${limit ? 'LIMIT ' + limit : ''} - ${limit && page ? 'OFFSET ' + parseInt(limit) * parseInt(page) : ''} - `).toPromise(); + const alarmRes = await clickHouse.dataAlarm.query(` + SELECT + alarms.AlarmId AS AlarmId, + alarms.State AS State, + SourceName, StartTime, EndTime, + alarms.CurrentLevel AS CurrentLevel, + SourceTypeId, + AlarmAdviceProblem, AlarmGroup, AlarmGroupUnit, AlarmAdviceProblem, + ${anxinyun}.t_structure.name AS StructureName, + StructureId, + ${anxinyun}.t_alarm_code.name AS AlarmCodeName + + FROM + alarms + LEFT JOIN ${anxinyun}.t_structure + ON ${anxinyun}.t_structure.id = alarms.StructureId + LEFT JOIN ${anxinyun}.t_alarm_code + ON ${anxinyun}.t_alarm_code.code = alarms.AlarmTypeCode + + ${whereOption.length ? 'WHERE ' + whereOption.join(' AND ') : ''} + ORDER BY alarms.StartTime DESC + ${limit ? 'LIMIT ' + limit : ''} + ${limit && page ? 'OFFSET ' + parseInt(limit) * parseInt(page) : ''} + `).toPromise(); - // alarm_details.Time, alarm_details.Content + // alarm_details.Time, alarm_details.Content - // LEFT JOIN alarm_details - // ON alarms.AlarmId = alarm_details.AlarmId - // AND alarm_details.Time = ( - // SELECT MAX(alarm_details.Time) from alarm_details WHERE AlarmId = alarms.AlarmId - // ) + // LEFT JOIN alarm_details + // ON alarms.AlarmId = alarm_details.AlarmId + // AND alarm_details.Time = ( + // SELECT MAX(alarm_details.Time) from alarm_details WHERE AlarmId = alarms.AlarmId + // ) - // State = 3 是 自动恢复 / 4 是 人工恢复 / 其他数字 是 需要恢复 - const SourceType = { 0: 'DTU', 1: '传感器', 2: '测点' }; + // State = 3 是 自动恢复 / 4 是 人工恢复 / 其他数字 是 需要恢复 + // const SourceType = { 0: 'DTU', 1: '传感器', 2: '测点' }; + ctx.body = alarmRes + } else { + ctx.body = [] + } ctx.status = 200; - ctx.body = alarmRes } catch (error) { ctx.fs.logger.error(`path: ${ctx.path}, error: error`); ctx.status = 400; @@ -207,7 +212,7 @@ async function detailAggregation (ctx) { FROM alarm_details WHERE - AlarmId=${alarmId} + AlarmId='${alarmId}' GROUP BY hours; `).toPromise(); diff --git a/api/app/lib/utils/dataRange.js b/api/app/lib/utils/dataRange.js index 570b410..eeea418 100644 --- a/api/app/lib/utils/dataRange.js +++ b/api/app/lib/utils/dataRange.js @@ -14,7 +14,7 @@ module.exports = function (app, opts) { } } - async function anxinStrucIdRange ({ ctx, pepProjectId }) { + async function anxinStrucIdRange ({ ctx, pepProjectId, projectOrStructKeyword }) { try { const { models } = ctx.fs.dc; const { userInfo = {} } = ctx.fs.api || {}; @@ -36,9 +36,12 @@ module.exports = function (app, opts) { // TODO 这儿也许需要判断传进来的 pepProjectId 在不在当前用户的关注范围内 const bindRes = await models.ProjectCorrelation.findAll(findOption) - const anxinStrucIds = [ + let pepProjectIds = [] + + const anxinProjectIds = [ ...bindRes.reduce( (arr, b) => { + pepProjectIds.push(b.pepProjectId) for (let sid of b.anxinProjectId) { arr.add(sid); } @@ -47,22 +50,42 @@ module.exports = function (app, opts) { new Set() ) ] + const pepProjectRes = pepProjectIds.length ? + await clickHouse.projectManage.query( + ` + SELECT + id, project_name, isdelete + FROM + t_pim_project + WHERE + id IN (${pepProjectIds.join(',')}) + ` + ).toPromise() : + [] - // 查结构物 左联 项目 ,项目 id 在关注的项目里 - const undelStrucRes = anxinStrucIds.length ? - await clickHouse.anxinyun.query(` - SELECT - id - FROM - t_project - WHERE - project_state != -1 - AND - id IN (${anxinStrucIds.join(',')}) - `).toPromise() : + const undelStrucRes = anxinProjectIds.length ? + await clickHouse.anxinyun.query( + ` + SELECT + t_project.id, t_structure.id AS strucId, project_state + FROM + t_project + LEFT JOIN + t_project_structure + ON t_project_structure.project = t_project.id + RIGHT JOIN + t_structure + ON t_structure.id = t_project_structure.structure + WHERE + project_state != -1 + AND + t_project.id IN (${anxinProjectIds.join(',')}) + ${projectOrStructKeyword ? `AND (t_project.name LIKE '%${projectOrStructKeyword}%' OR t_structure.name LIKE '%${projectOrStructKeyword}%')` : ''} + ` + ).toPromise() : [] - return undelStrucRes.map(s => s.id) + return [...new Set(...undelStrucRes.map(s => s.strucId))] } catch (error) { ctx.fs.logger.error(`path: ${ctx.path}, error: ${error}`); }