4 changed files with 208 additions and 6 deletions
			
			
		| @ -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 | |||
| } | |||
| @ -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)); | |||
| 
 | |||
| 
 | |||
| }; | |||
					Loading…
					
					
				
		Reference in new issue