Browse Source

修改巡检查询接口

master
xincheng 2 years ago
parent
commit
90cae3c057
  1. 37
      api/app/lib/controllers/patrolRecord/patrolRecord.js
  2. 5
      api/app/lib/models/patrol_record.js
  3. 4
      api/app/lib/routes/patrolRecord/patrolRecord.js

37
api/app/lib/controllers/patrolRecord/patrolRecord.js

@ -5,14 +5,39 @@ async function findPatrolRecord (ctx, next) {
let error = { name: 'FindError', message: '获取巡检记录失败' }; let error = { name: 'FindError', message: '获取巡检记录失败' };
try { try {
const models = ctx.fs.dc.models; const models = ctx.fs.dc.models;
const { startTime, endTime, alarm, patrolPlanId } = ctx.params; const { startTime, endTime, alarm, patrolPlanId, pointId } = ctx.params;
if (patrolPlanId.length == 0) { // patrolPlanId传all查所有
error = { name: 'FindError', message: '获取巡检记录失败,无巡检计划id' }; if (patrolPlanId == 'all') {
/* 如果有startTime && endTime,查询所有符合条件的数据 */
if (startTime && endTime) {
rslt = await models.PatrolRecord.findAll({
where: { alarm, inspectionTime: { $between: [startTime, endTime] }, pointId: { $in: pointId.split(',') } },
});
} else {
/* 如果没有startTime && endTime,查询每个点位最新一条符合条件的数据 */
a = await models.PatrolRecord.findAll({
where: { alarm, 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 { } else {
rslt = await models.PatrolRecord.findAll({ if (startTime && endTime) {
where: { patrolPlanId: { $in: patrolPlanId.split(',') }, alarm, inspectionTime: { $between: [startTime, endTime] } }, rslt = await models.PatrolRecord.findAll({
}); where: { patrolPlanId: { $in: patrolPlanId.split(',') }, alarm, inspectionTime: { $between: [startTime, endTime] }, pointId: { $in: pointId.split(',') } },
});
} else {
/* 如果没有startTime && endTime,查询每个点位最新一条符合条件的数据 */
a = await models.PatrolRecord.findAll({
where: { patrolPlanId: { $in: patrolPlanId.split(',') }, alarm, 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
})
}
} }
ctx.status = 200; ctx.status = 200;
ctx.body = rslt; ctx.body = rslt;
error = null error = null

5
api/app/lib/models/patrol_record.js

@ -40,6 +40,11 @@ module.exports = dc => {
allowNull: false, allowNull: false,
defaultValue: false, defaultValue: false,
}, },
pointId: {
field: "point_id",
type: DataTypes.INTEGER,
allowNull: false,
},
}, { }, {
tableName: "patrol_record", tableName: "patrol_record",
comment: "", comment: "",

4
api/app/lib/routes/patrolRecord/patrolRecord.js

@ -2,8 +2,8 @@
const patrolRecord = require('../../controllers/patrolRecord/patrolRecord'); const patrolRecord = require('../../controllers/patrolRecord/patrolRecord');
module.exports = function (app, router, opts) { module.exports = function (app, router, opts) {
app.fs.api.logAttr['GET/patrolRecord/:patrolPlanId/:startTime/:endTime/:alarm'] = { content: '获取巡检记录', visible: true }; app.fs.api.logAttr['GET/patrolRecord/:patrolPlanId/:startTime/:endTime/:alarm/:pointId'] = { content: '获取巡检记录', visible: true };
router.get('/patrolRecord/:patrolPlanId/:startTime/:endTime/:alarm', patrolRecord.findPatrolRecord); router.get('/patrolRecord/:patrolPlanId/:startTime/:endTime/:alarm/:pointId', patrolRecord.findPatrolRecord);
app.fs.api.logAttr['POST/patrolRecord/add'] = { content: '新增巡检记录', visible: true } app.fs.api.logAttr['POST/patrolRecord/add'] = { content: '新增巡检记录', visible: true }
router.post('/patrolRecord/add', patrolRecord.addPatrolRecord); router.post('/patrolRecord/add', patrolRecord.addPatrolRecord);

Loading…
Cancel
Save