diff --git a/api/app/lib/controllers/device/index.js b/api/app/lib/controllers/device/index.js new file mode 100644 index 0000000..0765c2f --- /dev/null +++ b/api/app/lib/controllers/device/index.js @@ -0,0 +1,150 @@ +'use strict'; +const moment = require('moment') + +function getDeviceList(opts) { + return async function (ctx, next) { + + const models = ctx.fs.dc.models; + const { page, limit, name } = ctx.query; + const Op = ctx.fs.dc.ORM.Op; + let errMsg = { message: '获取设备失败' } + try { + let searchWhere = {} + let option = { + where: searchWhere, + order: [["id", "desc"]], + include: [{ model: models.PointDevice }] + } + + if (name) { + searchWhere.name = { $like: `%${name}%` }; + } + + option.where = searchWhere + let limit_ = limit || 10; + let page_ = page || 1; + let offset = (page_ - 1) * limit_; + if (limit && page) { + option.limit = limit_ + option.offset = offset + } + + const res = await models.Device.findAndCount(option); + ctx.status = 200; + ctx.body = res; + } catch (error) { + ctx.fs.logger.error(`path: ${ctx.path}, error: ${error}`); + ctx.status = 400; + ctx.body = errMsg + } + } +} + +// 新增设备 +function addDevice(opts) { + let message = '新建设备失败' + return async function (ctx, next) { + + const models = ctx.fs.dc.models; + try { + if (Array.isArray(ctx.request.body)) { + const names = ctx.request.body.map(s => s.name) + const checkName = await models.Device.findOne({ where: { name: { $in: names } } }); + if (checkName) { + message = checkName.name + '名称已存在' + throw new Error(message) + } else { + await models.Device.bulkCreate(ctx.request.body) + ctx.status = 204; + ctx.body = { message: '批量导入设备成功' } + } + + } else { + const { name } = ctx.request.body + const checkName = await models.Device.findOne({ where: { name } }); + if (checkName) { + message = '该设备名称已存在' + throw new Error(message) + } else { + let rslt = ctx.request.body; + await models.Device.create(Object.assign({}, rslt)) + ctx.status = 204; + ctx.body = { message: '新建设备成功' } + } + } + + } catch (error) { + ctx.fs.logger.error(`path: ${ctx.path}, error: ${error}`); + ctx.status = 400; + ctx.body = { message: message } + } + } +} + +// 修改设备 +function editDevice(opts) { + let message = '新建设备失败' + return async function (ctx, next) { + + try { + const models = ctx.fs.dc.models; + const { id } = ctx.params; + const body = ctx.request.body; + + const { name } = ctx.request.body; + + const checkName = await models.Device.findOne({ where: { id: { $not: id }, name } }); + if (checkName) { + message = '该设备名称已存在' + throw new Error(message) + } else { + await models.Device.update( + body, + { where: { id: id, } } + ) + ctx.status = 204; + ctx.body = { message: '修改设备成功' } + } + + + } catch (error) { + ctx.fs.logger.error(`path: ${ctx.path}, error: ${error}`); + ctx.status = 400; + ctx.body = { message: message } + } + } +} + +// 删除设备 +function deleteDevice(opts) { + return async function (ctx, next) { + + try { + const models = ctx.fs.dc.models; + const { id } = ctx.params; + if (id) { + await models.Device.destroy({ + where: { + id: { $in: id.split(',') } + } + }) + ctx.status = 204; + ctx.body = { message: '删除设备成功' } + } else { + throw new Error('参数异常!') + } + + } catch (error) { + ctx.fs.logger.error(`path: ${ctx.path}, error: ${error}`); + ctx.status = 400; + ctx.body = { message: '删除设备失败' } + } + } +} + +module.exports = { + getDeviceList, + addDevice, + editDevice, + deleteDevice +} \ No newline at end of file diff --git a/api/app/lib/controllers/projectRegime/projectSituation.js b/api/app/lib/controllers/projectRegime/projectSituation.js index dc67138..5e628a4 100644 --- a/api/app/lib/controllers/projectRegime/projectSituation.js +++ b/api/app/lib/controllers/projectRegime/projectSituation.js @@ -55,10 +55,10 @@ async function postAddProject(ctx, next) { const models = ctx.fs.dc.models; let userInfo = ctx.fs.api.userInfo; const data = ctx.request.body; - const { img, longitude, latitude, name, type, describe } = data + const { img, longitude, latitude, name, type, describe, subType } = data let errMsg = data.id ? '结构物编辑失败' : '结构物新增失败' - let project = { img, longitude, latitude, name, type, describe, userId: userInfo.id } + let project = { img, longitude, latitude, name, type, describe, userId: userInfo.id, subType } const alikeProject = await models.Project.findOne({ where: { @@ -225,8 +225,21 @@ async function addPosition(ctx, next) { }) } + const pointDevicesNow = await models.PointDevice.findAll({ pointId: data.id }) + let deleteIds = pointDevicesNow.filter(s => !data.devices.find(x => x == s.deviceId)).map(v => v.deviceId) + let addPointDevices = data.devices.filter(s => !pointDevicesNow.find(x => x.deviceId == s)).map(v => { + return { deviceId: v, pointId: data.id } + }) + await models.PointDevice.destroy({ where: { deviceId: { $in: deleteIds }, pointId: data.id } }) + await models.PointDevice.bulkCreate(addPointDevices) + } else { - await models.Point.create(pointData) + const point = await models.Point.create(pointData) + const pointDevices = [] + data.devices.map(s => { + pointDevices.push({ deviceId: s, pointId: point.dataValues.id }) + }) + await models.PointDevice.bulkCreate(pointDevices) } ctx.status = 204; @@ -251,7 +264,14 @@ async function position(ctx, next) { }, include: [{ model: models.Point, - },], + include: [{ + model: models.PointDevice, + attributes: ["deviceId"], + include: [{ + model: models.Device, + }] + }] + }], } if (limit) { options.limit = Number(limit) @@ -342,6 +362,8 @@ async function delPosition(ctx, next) { } }) + await models.PointDevice.destroy({ where: { pointId: id } }) + ctx.status = 204; } catch (error) { diff --git a/api/app/lib/index.js b/api/app/lib/index.js index 38d2f17..e749239 100644 --- a/api/app/lib/index.js +++ b/api/app/lib/index.js @@ -56,7 +56,8 @@ module.exports.models = function (dc) { // dc = { orm: Sequelize对象, ORM: Seq const { Department, User, UserResource, Resource, Project, Point, PatrolPlan, PatrolRecord, ReportInfo, PatrolPlanUser, CheckItems, CheckItemsGroup, - PatrolTemplate, PatrolTemplateCheckItems, PatrolRecordIssueHandle + PatrolTemplate, PatrolTemplateCheckItems, PatrolRecordIssueHandle, + Device, PointDevice } = dc.models; PatrolRecord.belongsTo(PatrolPlan, { foreignKey: 'patrolPlanId', targetKey: 'id' }); @@ -93,7 +94,7 @@ module.exports.models = function (dc) { // dc = { orm: Sequelize对象, ORM: Seq PatrolPlan.belongsTo(Project, { foreignKey: 'structureId', targetKey: 'id' }); Project.hasMany(PatrolPlan, { foreignKey: 'structureId', sourceKey: 'id' }); - PatrolPlan.belongsToMany(User, { through: PatrolPlanUser, foreignKey: 'patrolPlanId', otherKey: 'userId' }); + PatrolPlan.belongsToMany(User, { through: PatrolPlanUser, foreignKey: 'patrolPlanId', otherKey: 'userId' }); User.belongsToMany(PatrolPlan, { through: PatrolPlanUser, foreignKey: 'userId', otherKey: 'patrolPlanId' }); ReportInfo.belongsTo(Project, { foreignKey: 'projectId', targetKey: 'id' }); @@ -101,4 +102,10 @@ module.exports.models = function (dc) { // dc = { orm: Sequelize对象, ORM: Seq CheckItems.belongsTo(CheckItemsGroup, { foreignKey: 'groupId', targetKey: 'id' }); CheckItemsGroup.hasMany(CheckItems, { foreignKey: 'groupId', sourceKey: 'id' }); + + PointDevice.belongsTo(Point, { foreignKey: 'pointId', targetKey: 'id' }); + Point.hasMany(PointDevice, { foreignKey: 'pointId', sourceKey: 'id' }); + + PointDevice.belongsTo(Device, { foreignKey: 'deviceId', targetKey: 'id' }); + Device.hasMany(PointDevice, { foreignKey: 'deviceId', sourceKey: 'id' }); }; diff --git a/api/app/lib/routes/device/index.js b/api/app/lib/routes/device/index.js new file mode 100644 index 0000000..a27e264 --- /dev/null +++ b/api/app/lib/routes/device/index.js @@ -0,0 +1,23 @@ +'use strict'; + +const device = require('../../controllers/device/index'); + +module.exports = function (app, router, opts, AuthCode) { + + app.fs.api.logAttr['POST/device'] = { content: '增加设备信息', visible: true }; + router.post('/device', device.addDevice(opts)) + + // 修改设备信息 + app.fs.api.logAttr['PUT/device/:id'] = { content: '修改设备信息', visible: true }; + router.put('/device/:id', device.editDevice(opts)) + + // 删除设备信息 + app.fs.api.logAttr['DEL/device/:id'] = { content: '删除设备信息', visible: true }; + router.del('/device/:id', device.deleteDevice(opts)) + + // 获取设备信息列表 + app.fs.api.logAttr['GET/device'] = { content: '获取设备信息列表', visible: true }; + router.get('/device', device.getDeviceList(opts)); + + +};