async function findPatrolRecords(ctx) { const models = ctx.fs.dc.models; const sequelize = ctx.fs.dc.orm; try { const { projectId } = ctx.query let generalInclude = [ { model: models.PatrolRecordIssueHandle }, ] const rslt = await models.PatrolRecord.findAll({ where: { projectId: { $in: projectId.split(',') }, alarm: true }, include: generalInclude, }) const res = rslt.reduce((acc, record) => { const pointId = record.pointId const inspectionTime = record.inspectionTime const obj = acc.find(r => r.pointId === pointId); if (!obj) { acc.push(record) } else if (obj && inspectionTime > record.inspectionTime) { record.findIndex(r => r.pointId === pointId) acc.splice(index, 1) acc.push(record) } return acc; }, []); ctx.body = res ctx.status = 200 } catch (error) { ctx.fs.logger.error(`path: ${ctx.path}, error: ${error}`); ctx.status = 400; ctx.body = { message: '运行监控的列表' } } } async function getProjectPoints(ctx) { let rslt = null try { const { projectId } = ctx.query const models = ctx.fs.dc.models; rslt = await models.Point.findAll({ attributes: ['id', 'name', 'equipmentNo', 'equipmentModel'], where: { projectId: { $in: projectId.split(',') } } }) ctx.status = 200; ctx.body = rslt; } catch (err) { ctx.fs.logger.error(`path: ${ctx.path}, error: ${err}`); ctx.status = 400; ctx.body = { name: 'FindError', message: '获取结构物点位列表失败' } } } async function getDeployPoints(ctx) { let rslt = null; try { const { pictureId } = ctx.query; const models = ctx.fs.dc.models; const heatmap = await models.ProjectGraph.findOne({ where: { id: { $in: pictureId.split(',') } } }) if (heatmap) { rslt = await models.ProjectPointsDeploy.findAll({ where: { graphId: { $in: pictureId.split(',') } } }) ctx.status = 200; ctx.body = rslt; } else { throw new Error('pictureId not found'); } } catch (err) { ctx.fs.logger.error(`path: ${ctx.path}, error: ${err}`); ctx.status = 400; ctx.body = { name: 'FindError', message: '获取结构物平面图测点布设失败' } } } async function findSingleGraph(ctx, next) { const models = ctx.fs.dc.models; let error = { name: 'FindSingleError', message: '查询单一数据失败' }; let rslt = null; const { projectId,subType } = ctx.query; try { rslt = await ctx.fs.dc.models.Project.findAll({ where: { id: { $in: projectId.split(',') },subType}, attributes: ['id', 'sub_type',], include: [{ model: models.ProjectGraph } ] }); error = null; } catch (err) { ctx.fs.logger.error(`path: ${ctx.path}, error: ${err}`); } if (error) { ctx.status = 400; ctx.body = error; } else { ctx.status = 200; ctx.body = rslt; } } async function findProjects(ctx, next) { let rslt = {} const { projectId } = ctx.query; try { rslt = await ctx.fs.dc.models.Project.findAll({ where: { id: { $in: projectId.split(',') } } }) ctx.status = 200; ctx.body = rslt; } catch (error) { ctx.fs.logger.error(`path: ${ctx.path}, error: ${error}`); ctx.status = 400; ctx.body = { message: '运行监控的列表' } } } async function findNewestRecord(ctx, next) { const models = ctx.fs.dc.models; const { pointId } = ctx.query; const sequelize = ctx.fs.dc.orm; try{ const rs=await sequelize.query(` SELECT p.* FROM ( SELECT id,point_id,alarm,points,inspection_time, ROW_NUMBER() OVER (PARTITION BY point_id ORDER BY inspection_time DESC) AS row_num FROM patrol_record WHERE point_id in (${pointId}) ) AS p WHERE p.row_num = 1; `) ctx.status = 200 ctx.body = rs[0]; }catch(error){ ctx.fs.logger.error(`path: ${ctx.path}, error: ${error}`); ctx.status = 400; ctx.body = { message: '查询巡检最新记录失败' } } } module.exports = { findPatrolRecords, getProjectPoints, getDeployPoints, findSingleGraph, findProjects, findNewestRecord }