'use strict'; const moment = require('moment'); async function list (ctx) { try { const { models } = ctx.fs.dc; const { userId, pepUserId } = ctx.fs.api const linkListRes = await models.QuickLink.findAll({ attributes: { exclude: ['userId'] }, where: { userId, } }) ctx.status = 200; ctx.body = linkListRes } catch (error) { ctx.fs.logger.error(`path: ${ctx.path}, error: ${error}`); ctx.status = 400; ctx.body = { message: typeof error == 'string' ? error : undefined } } } async function edit (ctx) { try { const { models } = ctx.fs.dc; const { userId, pepUserId } = ctx.fs.api const { linkId, name, link } = ctx.request.body if (!name || !link) { throw '请将参数填写完整' } let findOption = { where: { userId: userId, $or: [{ name, }, { link, }] } } if (linkId) { findOption.where.id = { $ne: linkId } } const existRes = await models.QuickLink.findOne(findOption) if (existRes) { throw '已有相同名称/地址的工具' } if (linkId) { await models.QuickLink.update({ name, link, }, { where: { id: linkId } }) } else { await models.QuickLink.create({ userId, name, link, }) } ctx.status = 204; } catch (error) { ctx.fs.logger.error(`path: ${ctx.path}, error: ${error}`); ctx.status = 400; ctx.body = { message: typeof error == 'string' ? error : undefined } } } async function del (ctx) { try { const { models } = ctx.fs.dc; const { userId, pepUserId } = ctx.fs.api const { linkId } = ctx.params await models.QuickLink.destroy({ where: { id: linkId, userId, } }) ctx.status = 204; } catch (error) { ctx.fs.logger.error(`path: ${ctx.path}, error: ${error}`); ctx.status = 400; ctx.body = { message: typeof error == 'string' ? error : undefined } } } async function count (ctx) { try { const { models } = ctx.fs.dc; const { userId, pepUserId, userInfo = {}, pepUserInfo } = ctx.fs.api const { clickHouse } = ctx.app.fs const { utils: { judgeSuper, anxinStrucIdRange, pomsProjectRange } } = ctx.app.fs const { database: anxinyun } = clickHouse.anxinyun.opts.config const { pepProjectId } = ctx.request.query let anxinStruc = await anxinStrucIdRange({ ctx, pepProjectId }) let pomsProject = await pomsProjectRange({ ctx, pepProjectId, }) const pomsProjectIds = pomsProject.map(p => p.id) if (anxinStruc.length) { const anxinStrucIds = anxinStruc.map(a => a.strucId) || [] const dataAlarm = await clickHouse.dataAlarm.query(` SELECT AlarmId,State,StartTime,AlarmGroup FROM alarms WHERE alarms.StructureId IN (${anxinStrucIds.join(",")}) `).toPromise(); const confirmedAlarm = dataAlarm // TODO: 开发临时注释 .filter(ar => ar.State && ar.State > 2) .map(ar => "'" + ar.AlarmId + "'") //剩余数据告警 const dataSurplus = dataAlarm.filter(ar => ar.State && ar.State < 3 && ar.AlarmGroup < 4).length || 0 //剩余设备告警 const toolSurplus = dataAlarm.filter(ar => ar.State && ar.State < 3 && ar.AlarmGroup > 3).length || 0 //今日新增数据告警 const dataNewAdd = dataAlarm.filter(r => moment(moment().startOf('day').format('YYYY-MM-DD HH:mm:ss')).isBefore(r.StartTime) && moment(r.createTime).isBefore(moment().endOf('day').format('YYYY-MM-DD HH:mm:ss')) && r.AlarmGroup < 4).length || 0 const toolNewAdd = dataAlarm.filter(r => moment(moment().startOf('day').format('YYYY-MM-DD HH:mm:ss')).isBefore(r.StartTime) && moment(r.createTime).isBefore(moment().endOf('day').format('YYYY-MM-DD HH:mm:ss')) && r.AlarmGroup > 3).length || 0 //今日确认数据告警 const dataConfirme = confirmedAlarm.length ? await clickHouse.dataAlarm.query(` SELECT max(Time) AS Time, AlarmId , max(Content) AS Content, alarms.AlarmGroup AS AlarmGroup FROM alarm_details LEFT JOIN alarms ON alarm_details.AlarmId=alarms.AlarmId WHERE AlarmId IN (${confirmedAlarm.join(',')}) AND alarm_details.Time >= '${moment().startOf('day').format('YYYY-MM-DD HH:mm:ss')}' AND alarm_details.Time <= '${moment().endOf('day').format('YYYY-MM-DD HH:mm:ss')}' GROUP BY AlarmId,AlarmGroup `).toPromise() : []; let findOption = { where: { '$app->projectCorrelations.id$': { $in: pomsProjectIds } }, attributes: ['createTime', 'confirmTime'], include: [{ model: models.App, where: { }, attributes: ['id', 'name'], include: [{ model: models.ProjectCorrelation, where: { }, attributes: ['id'], }] }] } //应用总告警 const listRes = await models.AppAlarm.findAndCountAll(findOption) //剩余应用告警 const appSurplus = listRes.rows.filter(r => !r.confirmTime).length || 0 //今日新增应用告警 const appNewAdd = listRes.rows.filter(r => moment(moment().startOf('day').format('YYYY-MM-DD HH:mm:ss')).isBefore(r.createTime) && moment(r.createTime).isBefore(moment().endOf('day').format('YYYY-MM-DD HH:mm:ss'))).length || 0 //今日确认应用告警 const appConfirme = listRes.rows.filter(r => moment(moment().startOf('day').format('YYYY-MM-DD HH:mm:ss')).isBefore(r.confirmTime) && moment(r.confirmTime).isBefore(moment().endOf('day').format('YYYY-MM-DD HH:mm:ss'))).length || 0 const alarmRes = anxinStrucIds.length ? await clickHouse.vcmp.query( ` SELECT cameraAlarm.cameraId AS cameraId, cameraAlarm.cameraName AS cameraName, cameraAlarm.cameraSerialNo AS cameraSerialNo, cameraAlarm.cameraChannelNo AS cameraChannelNo, cameraAlarm.alarmId AS alarmId, cameraAlarm.createTime AS createTime, cameraAlarm.platform AS platform, cameraAlarm.confirmTime AS confirmTime, camera_status_resolve.id AS resolveId, camera_status.describe AS statusDescribe, camera_status_resolve.resolve AS resolve, "gbCamera".online AS cameraOnline, anxinIpc.t_video_ipc.name AS anxinIpcPosition, anxinStation.id AS anxinStationId, anxinStation.name AS anxinStationName, anxinStruc.name AS strucName, anxinStruc.id AS strucId FROM ( SELECT camera.id AS cameraId, camera.gb_id AS gbId, camera.name AS cameraName, camera_status_alarm.id AS alarmId, camera_status_alarm.create_time AS createTime, camera_status_alarm.platform AS platform, camera_status_alarm.status_id AS statusId, camera_status_alarm.serial_no AS cameraSerialNo, camera_status_alarm.channel_no AS cameraChannelNo, camera_status_alarm.confirm_time AS confirmTime FROM camera_status_alarm INNER JOIN camera ON camera.serial_no = camera_status_alarm.serial_no AND camera.channel_no = camera_status_alarm.channel_no LEFT JOIN vender ON vender.id = camera.vender_id WHERE camera.delete = false AND camera.recycle_time is null AND alarmId IN ( SELECT camera_status_alarm.id AS alarmId FROM camera_status_alarm RIGHT JOIN ${anxinyun}.t_video_ipc ON toString(${anxinyun}.t_video_ipc.channel_no) = camera_status_alarm.channel_no AND ${anxinyun}.t_video_ipc.serial_no = camera_status_alarm.serial_no ${`WHERE ${anxinyun}.t_video_ipc.structure IN (${anxinStrucIds.join(',')})` } ) ) AS cameraAlarm LEFT JOIN camera_status ON cameraAlarm.platform = camera_status.platform AND cameraAlarm.statusId = camera_status.id LEFT JOIN camera_status_resolve ON camera_status_resolve.status_id = camera_status.id LEFT JOIN "gbCamera" ON "gbCamera".id = cameraAlarm.gbId LEFT JOIN ${anxinyun}.t_video_ipc AS anxinIpc ON toString(anxinIpc.channel_no) = cameraAlarm.cameraChannelNo AND anxinIpc.serial_no = cameraAlarm.cameraSerialNo LEFT JOIN ${anxinyun}.t_structure AS anxinStruc ON anxinStruc.id = anxinIpc.structure AND anxinStruc.id IN (${anxinStrucIds.join(',')}) LEFT JOIN ${anxinyun}.t_video_ipc_station AS anxinIpcStation ON anxinIpcStation.ipc = anxinIpc.id LEFT JOIN ${anxinyun}.t_sensor AS anxinStation ON anxinStation.id = anxinIpcStation.station ` ).toPromise() : [] let returnD = [] let positionD = {} // 每个设备一个告警 for (let a of alarmRes) { if (positionD[a.cameraId]) { let curD = returnD[positionD[a.cameraId].positionReturnD] } else { let d = { cameraId: a.cameraId, cameraName: a.cameraName, createTime: a.createTime, alarmId: a.alarmId, confirmTime: a.confirmTime, } returnD.push(d) positionD[a.cameraId] = { positionReturnD: returnD.length - 1 } } } //剩余视频告警 const videoSurplus = returnD.filter(r => !r.confirmTime).length || 0 //今日新增视频告警 const videoNewAdd = returnD.filter(r => moment(moment().startOf('day').format('YYYY-MM-DD HH:mm:ss')).isBefore(r.createTime) && moment(r.createTime).isBefore(moment().endOf('day').format('YYYY-MM-DD HH:mm:ss'))).length || 0 //今日确认视频告警 const videoConfirme = returnD.filter(r => moment(moment().startOf('day').format('YYYY-MM-DD HH:mm:ss')).isBefore(r.confirmTime) && moment(r.confirmTime).isBefore(moment().endOf('day').format('YYYY-MM-DD HH:mm:ss'))).length || 0 let findOptions = { where: { del: false }, attributes: [] } if (!userInfo.role.includes('SuperAdmin') && !userInfo.role.includes('admin')) { findOptions.where.id = { $in: userInfo.correlationProject } } const projects = await models.ProjectCorrelation.findAndCountAll(findOptions) ctx.status = 200; ctx.body = { dataSurplus: dataSurplus + videoSurplus, dataNewAdd: dataNewAdd + videoNewAdd, dataConfirme: appConfirme + dataConfirme.filter(r => r.AlarmGroup < 4).length, toolSurplus: toolSurplus, toolNewAdd: toolNewAdd, toolConfirme: dataConfirme.filter(r => r.AlarmGroup > 3).length, appSurplus: appSurplus, appNewAdd: appNewAdd, appConfirme: appConfirme, projects: projects.count, } } else { ctx.body = { dataAlarm: 0, } } ctx.status = 200; } catch (error) { ctx.fs.logger.error(`path: ${ctx.path}, error: ${error}`); ctx.status = 400; ctx.body = { message: typeof error == 'string' ? error : undefined } } } module.exports = { list, edit, del, count };