From eba6554019e010b73c2bdc495aef3468b690badb Mon Sep 17 00:00:00 2001 From: xincheng <1447340602@qq.com> Date: Thu, 19 Jan 2023 10:44:32 +0800 Subject: [PATCH] =?UTF-8?q?=E5=B7=A1=E6=A3=80=E8=AE=B0=E5=BD=95=E6=9F=A5?= =?UTF-8?q?=E8=AF=A2=E6=8E=A5=E5=8F=A3=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controllers/patrolRecord/patrolRecord.js | 48 ++++++++++++++----- .../lib/routes/patrolRecord/patrolRecord.js | 2 + 2 files changed, 38 insertions(+), 12 deletions(-) diff --git a/api/app/lib/controllers/patrolRecord/patrolRecord.js b/api/app/lib/controllers/patrolRecord/patrolRecord.js index b19323d..f00a75c 100644 --- a/api/app/lib/controllers/patrolRecord/patrolRecord.js +++ b/api/app/lib/controllers/patrolRecord/patrolRecord.js @@ -10,28 +10,52 @@ async function findPatrolRecord (ctx, next) { if (patrolPlanId == 'all') { /* 如果有startTime && endTime,查询所有符合条件的数据 */ if (startTime && endTime) { - rslt = await models.PatrolRecord.findAll({ - where: { alarm, inspectionTime: { $between: [startTime, endTime] }, pointId: { $in: pointId.split(',') } }, - }); + if (pointId) { + rslt = await models.PatrolRecord.findAll({ + where: { alarm, inspectionTime: { $between: [startTime, endTime] }, pointId: { $in: pointId.split(',') } }, + }); + } else { + rslt = await models.PatrolRecord.findAll({ + where: { alarm, inspectionTime: { $between: [startTime, endTime] } }, + }); + } } else { /* 如果没有startTime && endTime,查询每个点位最新一条符合条件的数据 */ - a = await models.PatrolRecord.findAll({ - where: { alarm, pointId: { $in: pointId.split(',') } }, - }); + let a = [] + if (pointId) { + a = await models.PatrolRecord.findAll({ + where: { pointId: { $in: pointId.split(',') } }, + }); + } rslt = pointId.split(',').map(i => { return a.filter(t => t.pointId === i).sort((a, b) => b.id - a.id)[0] || null }) } } else { if (startTime && endTime) { - rslt = await models.PatrolRecord.findAll({ - where: { patrolPlanId: { $in: patrolPlanId.split(',') }, alarm, inspectionTime: { $between: [startTime, endTime] }, pointId: { $in: pointId.split(',') } }, - }); + if (pointId) { + rslt = await models.PatrolRecord.findAll({ + where: { patrolPlanId: { $in: patrolPlanId.split(',') }, alarm, inspectionTime: { $between: [startTime, endTime] }, pointId: { $in: pointId.split(',') } }, + }); + } else { + rslt = await models.PatrolRecord.findAll({ + where: { patrolPlanId: { $in: patrolPlanId.split(',') }, alarm, inspectionTime: { $between: [startTime, endTime] } }, + }); + } + } else { + let a = [] /* 如果没有startTime && endTime,查询每个点位最新一条符合条件的数据 */ - a = await models.PatrolRecord.findAll({ - where: { patrolPlanId: { $in: patrolPlanId.split(',') }, alarm, pointId: { $in: pointId.split(',') } }, - }); + if (pointId) { + a = await models.PatrolRecord.findAll({ + where: { patrolPlanId: { $in: patrolPlanId.split(',') }, pointId: { $in: pointId.split(',') } }, + }); + } else { + a = await models.PatrolRecord.findAll({ + where: { patrolPlanId: { $in: patrolPlanId.split(',') } }, + }); + } + rslt = pointId.split(',').map(i => { return a.filter(t => t.pointId === i).sort((a, b) => b.id - a.id)[0] || null }) diff --git a/api/app/lib/routes/patrolRecord/patrolRecord.js b/api/app/lib/routes/patrolRecord/patrolRecord.js index 7c63cf4..48f3d4b 100644 --- a/api/app/lib/routes/patrolRecord/patrolRecord.js +++ b/api/app/lib/routes/patrolRecord/patrolRecord.js @@ -3,6 +3,8 @@ const patrolRecord = require('../../controllers/patrolRecord/patrolRecord'); module.exports = function (app, router, opts) { app.fs.api.logAttr['GET/patrolRecord/:patrolPlanId/:startTime/:endTime/:alarm/:pointId'] = { content: '获取巡检记录', visible: true }; + // web端、小程序端查数据:patrolPlanId为all,不需要传pointId + // 小程序端查点位最新一条数据:startTime、endTime、alarm不传 router.get('/patrolRecord/:patrolPlanId/:startTime/:endTime/:alarm/:pointId', patrolRecord.findPatrolRecord); app.fs.api.logAttr['POST/patrolRecord/add'] = { content: '新增巡检记录', visible: true }