diff --git a/api/app/lib/controllers/alarm/video.js b/api/app/lib/controllers/alarm/video.js index a7084a1..e8ab1b0 100644 --- a/api/app/lib/controllers/alarm/video.js +++ b/api/app/lib/controllers/alarm/video.js @@ -284,47 +284,75 @@ async function confirm(ctx) { } } +let structsAche = { + dataList: [], + expireTime: null//10分钟更新一次结构物列表 +} +async function getStructsAche(ctx) { + const { utils: { getAxyStructs } } = ctx.app.fs + try { + if (!structsAche.dataList.length || moment() > moment(structsAche.expireTime)) { + let structList = await getAxyStructs(); + structsAche.dataList = structList; + structsAche.expireTime = moment().add(10, 'minute').format('YYYY-MM-DD HH:mm:ss'); + } + return structsAche; + } catch (err) { + console.log(`获取结构物列表失败, error: ${err}`); + } +} + async function alarmAdded(ctx) { try { const { models } = ctx.fs.dc; const { clickHouse } = ctx.app.fs - const { utils: { anxinStrucIdRange, sendAppearToWeb } } = ctx.app.fs - let anxinStruc = await anxinStrucIdRange({ ctx }) + const { utils: { sendAppearToWeb } } = ctx.app.fs - const { serial_no, channel_no, create_time, description, status_id } = ctx.request.body; + let structsAche = await getStructsAche(ctx); + if (structsAche) { + let anxinStruc = structsAche.dataList;//结构物列表 + const { serial_no, channel_no, create_time, description, status_id } = ctx.request.body; - let belongToStruct = await clickHouse.anxinyun.query( - `SELECT name, structure FROM t_video_ipc WHERE serial_no='${serial_no}' and channel_no='${channel_no}'`).toPromise() - let structId = belongToStruct.length ? belongToStruct[0].structure : null + let belongToStruct = await clickHouse.anxinyun.query( + `SELECT name, structure FROM t_video_ipc WHERE serial_no='${serial_no}' and channel_no=${parseInt(channel_no)}`).toPromise() + let structId = belongToStruct.length ? belongToStruct[0].structure : null - if (structId) { - let exist = anxinStruc.find(s => s.strucId == structId); - let projects = exist.pomsProject.filter(d => !d.del).map(p => p.id); - let datas = projects.map(d => {//需要 项目,告警源,异常类型,时间 - return { - projectCorrelationId: d, - alarmInfo: { messageMode: 'AlarmGeneration', sourceName: belongToStruct[0].name, status_id, content: description },//AlarmGeneration代表告警首次产生 - time: create_time, - type: description - } - }) - let rslt = await models.AlarmAppearRecord.bulkCreate(datas, { returning: true }); - let dynamics = rslt.map(r => { - return { - time: r.time, - alarmAppearId: r.id, - projectCorrelationId: r.projectCorrelationId, - type: 1//发现 - } - }) - await models.LatestDynamicList.bulkCreate(dynamics); + if (structId) { + let exist = anxinStruc.find(s => s.strucId == structId); + if (exist) { + let projects = exist.pomsProject.filter(d => !d.del).map(p => p.id); + let datas = projects.map(d => {//需要 项目,告警源,异常类型,时间 + return { + projectCorrelationId: d, + alarmInfo: { messageMode: 'AlarmGeneration', sourceName: belongToStruct[0].name, status_id, content: description },//AlarmGeneration代表告警首次产生 + time: create_time, + type: description + } + }) + let rslt = await models.AlarmAppearRecord.bulkCreate(datas, { returning: true }); + let dynamics = rslt.map(r => { + return { + time: r.time, + alarmAppearId: r.id, + projectCorrelationId: r.projectCorrelationId, + type: 1//发现 + } + }) + await models.LatestDynamicList.bulkCreate(dynamics); - //消息推送到前端 - if (datas.length) { - await sendAppearToWeb(datas, 'video'); + //消息推送到前端 + if (datas.length) { + await sendAppearToWeb(datas, 'video'); + } + } + } + ctx.status = 200; + } else { + ctx.status = 400; + ctx.body = { + message: `获取结构物列表失败` } } - ctx.status = 200; } catch (error) { ctx.fs.logger.error(`path: ${ctx.path}, error: error`); ctx.status = 400;