You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
208 lines
6.5 KiB
208 lines
6.5 KiB
'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) {
|
|
|
|
let message = '删除设备失败'
|
|
try {
|
|
const models = ctx.fs.dc.models;
|
|
const { id } = ctx.params;
|
|
if (id) {
|
|
let exitPointDevice = await models.PointDevice.findOne({ where: { deviceId: { $in: id.split(',') } } })
|
|
if (exitPointDevice) {
|
|
message = '该设备已绑定点位 无法删除!'
|
|
throw new Error(message)
|
|
}
|
|
|
|
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: message }
|
|
}
|
|
}
|
|
}
|
|
//获取相应设备信息
|
|
function getDevices(opts) {
|
|
return async function (ctx, next) {
|
|
const models = ctx.fs.dc.models;
|
|
const { page, limit } = ctx.query;
|
|
const Op = ctx.fs.dc.ORM.Op;
|
|
let errMsg = { message: '获取设备失败' }
|
|
try {
|
|
let rslt=[]
|
|
let searchWhere = {}
|
|
let option = {
|
|
where: searchWhere,
|
|
order: [["id", "desc"]],
|
|
include: [{ model: models.PointDevice,include:{model:models.Point} }]
|
|
}
|
|
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
|
|
}
|
|
let userInfo = ctx.fs.api.userInfo;
|
|
rslt = await models.Device.findAll(option);
|
|
rslt = rslt.filter(f => f)
|
|
if (userInfo.username != 'SuperAdmin') {
|
|
if (userInfo.structure) {
|
|
rslt = rslt.filter(s =>
|
|
{
|
|
if(s.pointDevices){
|
|
return s.pointDevices.some((item) =>
|
|
userInfo.structure.find((x) => x === item.point.projectId))
|
|
}
|
|
}
|
|
)
|
|
} else {
|
|
rslt = []
|
|
}
|
|
}
|
|
ctx.status = 200;
|
|
ctx.body = rslt;
|
|
} catch (error) {
|
|
ctx.fs.logger.error(`path: ${ctx.path}, error: ${error}`);
|
|
ctx.status = 400;
|
|
ctx.body = errMsg
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
module.exports = {
|
|
getDeviceList,
|
|
addDevice,
|
|
editDevice,
|
|
deleteDevice,
|
|
getDevices
|
|
}
|