From 2764e0137db5ab78ad850714580d0e25a29024b4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=88=98=E6=AC=A3=E7=A8=8B?= <1447340602@qq.com> Date: Wed, 18 Jan 2023 16:29:28 +0800 Subject: [PATCH] =?UTF-8?q?=E5=B7=A1=E6=A3=80=E8=AE=B0=E5=BD=95=E6=9F=A5?= =?UTF-8?q?=E7=9C=8B=E5=8F=8A=E6=96=B0=E5=A2=9E=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controllers/patrolRecord/patrolRecord.js | 52 +++++++++++++++++++ api/app/lib/models/patrol_record.js | 50 ++++++++++++++++++ .../lib/routes/patrolRecord/patrolRecord.js | 10 ++++ 3 files changed, 112 insertions(+) create mode 100644 api/app/lib/controllers/patrolRecord/patrolRecord.js create mode 100644 api/app/lib/models/patrol_record.js create mode 100644 api/app/lib/routes/patrolRecord/patrolRecord.js diff --git a/api/app/lib/controllers/patrolRecord/patrolRecord.js b/api/app/lib/controllers/patrolRecord/patrolRecord.js new file mode 100644 index 0000000..b320ce3 --- /dev/null +++ b/api/app/lib/controllers/patrolRecord/patrolRecord.js @@ -0,0 +1,52 @@ +'use strict'; + +async function findPatrolRecord (ctx, next) { + let rslt = []; + let error = { name: 'FindError', message: '获取巡检记录失败' }; + try { + const models = ctx.fs.dc.models; + const { startTime, endTime, alarm, patrolPlanId } = ctx.params; + if (patrolPlanId.length == 0) { + error = { name: 'FindError', message: '获取巡检记录失败,无巡检计划id' }; + } else { + rslt = await models.PatrolRecord.findAll({ + where: { patrolPlanId: { $in: patrolPlanId.split(',') }, alarm, inspectionTime: { $between: [startTime, endTime] } }, + }); + } + ctx.status = 200; + ctx.body = rslt; + error = null + } catch (error) { + ctx.fs.logger.error(`path: ${ctx.path}, error: ${error}`); + ctx.status = 400; + ctx.body = { + "message": "获取巡检记录失败" + } + } +} + +async function addPatrolRecord (ctx, next) { + let error = { name: 'addError', message: '新增巡检记录失败' }; + try { + const models = ctx.fs.dc.models; + const data = ctx.request.body; + let { patrolPlanId, lastInspectionTime, inspectionTime, points, alarm } = data + let record = { patrolPlanId, lastInspectionTime, inspectionTime, points, alarm } + + await models.PatrolRecord.create(record); + + ctx.status = 204; + error = null + } catch (error) { + ctx.fs.logger.error(`path: ${ctx.path}, error: ${error}`); + ctx.status = 400; + ctx.body = { + "message": '新增巡检计划失败' + } + } +} + +module.exports = { + findPatrolRecord, + addPatrolRecord, +} \ No newline at end of file diff --git a/api/app/lib/models/patrol_record.js b/api/app/lib/models/patrol_record.js new file mode 100644 index 0000000..e875d95 --- /dev/null +++ b/api/app/lib/models/patrol_record.js @@ -0,0 +1,50 @@ +/* eslint-disable*/ +'use strict'; + +module.exports = dc => { + const DataTypes = dc.ORM; + const sequelize = dc.orm; + const PatrolRecord = sequelize.define("PatrolRecord", { + id: { + field: "id", + type: DataTypes.INTEGER, + allowNull: false, + primaryKey: true, + autoIncrement: true, + }, + patrolPlanId: { + field: "patrol_plan_id", + type: DataTypes.INTEGER, + allowNull: false, + primaryKey: false, + autoIncrement: false + }, + lastInspectionTime: { + field: "last_inspection_time", + type: DataTypes.DATE, + allowNull: true, + }, + inspectionTime: { + field: "inspection_time", + type: DataTypes.DATE, + allowNull: true, + }, + points: { + field: "points", + type: DataTypes.JSONB, + allowNull: true, + }, + alarm: { + field: "alarm", + type: DataTypes.BOOLEAN, + allowNull: false, + defaultValue: false, + }, + }, { + tableName: "patrol_record", + comment: "", + indexes: [] + }); + dc.models.PatrolRecord = PatrolRecord; + return PatrolRecord; +}; \ No newline at end of file diff --git a/api/app/lib/routes/patrolRecord/patrolRecord.js b/api/app/lib/routes/patrolRecord/patrolRecord.js new file mode 100644 index 0000000..8bc08a4 --- /dev/null +++ b/api/app/lib/routes/patrolRecord/patrolRecord.js @@ -0,0 +1,10 @@ +'use strict'; +const patrolRecord = require('../../controllers/patrolRecord/patrolRecord'); + +module.exports = function (app, router, opts) { + app.fs.api.logAttr['GET/patrolRecord/:patrolPlanId/:startTime/:endTime/:alarm'] = { content: '获取巡检记录', visible: true }; + router.get('/patrolRecord/:patrolPlanId/:startTime/:endTime/:alarm', patrolRecord.findPatrolRecord); + + app.fs.api.logAttr['POST/patrolRecord/add'] = { content: '新增巡检记录', visible: true } + router.post('/patrolRecord/add', patrolRecord.addPatrolRecord); +}; \ No newline at end of file