wuqun
2 years ago
5 changed files with 362 additions and 0 deletions
@ -0,0 +1,209 @@ |
|||
'use strict'; |
|||
|
|||
async function findSingleGraph(ctx, next) { |
|||
let error = { name: 'FindSingleError', message: '查询单一数据失败' }; |
|||
let rslt = null; |
|||
const { projectId } = ctx.params; |
|||
try { |
|||
rslt = await ctx.fs.dc.models.ProjectGraph.findOne({ |
|||
where: { projectId: projectId } |
|||
}); |
|||
error = null; |
|||
} catch (err) { |
|||
ctx.fs.logger.error(`path: ${ctx.path}, error: ${err}`); |
|||
} |
|||
if (error) { |
|||
ctx.status = 400; |
|||
ctx.body = error; |
|||
} else { |
|||
ctx.status = 200; |
|||
ctx.body = rslt; |
|||
} |
|||
} |
|||
|
|||
async function createGraph(ctx, next) { |
|||
let error = { name: 'AddError', message: '添加数据失败' }; |
|||
let graphId = null; |
|||
try { |
|||
const data = ctx.request.body; |
|||
let dataToSave = { |
|||
projectId: data.projectId, |
|||
graph: data.graph, |
|||
} |
|||
const t = await ctx.fs.dc.orm.transaction(); |
|||
try { |
|||
let planarGraph = await ctx.fs.dc.models.ProjectGraph.create(dataToSave); |
|||
graphId = planarGraph.id; |
|||
await t.commit(); |
|||
} catch (e) { |
|||
await t.rollback(); |
|||
throw e; |
|||
} |
|||
error = null; |
|||
// 日志信息
|
|||
ctx.fs.api = ctx.fs.api || {}; |
|||
ctx.fs.api.actionParameter = JSON.stringify(data); |
|||
ctx.fs.api.actionParameterShow = `新增graphId:${graphId}`; |
|||
} catch (err) { |
|||
ctx.fs.logger.error(`path: ${ctx.path}, error: ${err}`); |
|||
} |
|||
if (error) { |
|||
ctx.status = 400; |
|||
ctx.body = error; |
|||
} else { |
|||
ctx.status = 200; |
|||
ctx.body = { id: graphId }; |
|||
} |
|||
} |
|||
|
|||
async function updateGraph(ctx, next) { |
|||
let error = { name: 'UpdateError', message: '修改数据失败' }; |
|||
const { id } = ctx.params; |
|||
const data = ctx.request.body; |
|||
if (id) { |
|||
if (data && Object.keys(data).length) { |
|||
try { |
|||
const models = ctx.fs.dc.models; |
|||
let planarGraph = await models.ProjectGraph.findOne({ where: { id: id } }); |
|||
const dataToSave = {}; |
|||
if (planarGraph) { |
|||
const { projectId, graph } = data; |
|||
if (projectId && !(projectId == planarGraph.projectId)) |
|||
dataToSave.projectId = projectId; |
|||
if (graph && !(graph == planarGraph.graph)) |
|||
dataToSave.graph = graph; |
|||
} |
|||
dataToSave.id = planarGraph.id; |
|||
if (Object.keys(dataToSave).length) { |
|||
await models.ProjectGraph.update(dataToSave, { where: { id: planarGraph.id } }); |
|||
} |
|||
error = null; |
|||
// 日志信息
|
|||
ctx.fs.api = ctx.fs.api || {}; |
|||
ctx.fs.api.actionParameter = JSON.stringify(data); |
|||
ctx.fs.api.actionParameterShow = `结构物平面图id:${data.id}`; |
|||
} catch (err) { |
|||
ctx.fs.logger.error(`path: ${ctx.path}, error: ${err}`); |
|||
} |
|||
} |
|||
} else { |
|||
error = { name: 'UpdateError', message: `不存在{id=${id}}的结构物平面图` }; |
|||
} |
|||
if (error) { |
|||
ctx.status = 400; |
|||
ctx.body = error; |
|||
} else { |
|||
ctx.status = 200; |
|||
ctx.body = { message: "结构物平面图修改成功" }; |
|||
} |
|||
} |
|||
|
|||
async function delProjectGraph(ctx) { |
|||
try { |
|||
const { id } = ctx.params; |
|||
const models = ctx.fs.dc.models; |
|||
let info = await models.ProjectGraph.findOne({ where: { id: id } }); |
|||
if (info) { |
|||
await models.ProjectPointsDeploy.destroy({ where: { graphId: id } }); |
|||
await models.ProjectGraph.destroy({ where: { id } }) |
|||
ctx.status = 204; |
|||
} else { |
|||
ctx.status = 400; |
|||
ctx.body = { message: '数据不存在' } |
|||
} |
|||
} catch (error) { |
|||
ctx.fs.logger.error(`path: ${ctx.path}, error: ${error}`); |
|||
ctx.status = 400; |
|||
ctx.body = { |
|||
"message": "删除结构物布设图失败" |
|||
} |
|||
} |
|||
} |
|||
|
|||
async function getDeployPoints(ctx) { |
|||
try { |
|||
const pictureId = ctx.params.pictureId; |
|||
const models = ctx.fs.dc.models; |
|||
const heatmap = await models.ProjectGraph.findOne({ where: { id: pictureId } }) |
|||
if (heatmap) { |
|||
let allPoints = await models.Point.findAll({ |
|||
attributes: ['id', 'name'], |
|||
where: { projectId: heatmap.dataValues.projectId } |
|||
}) |
|||
let setedPoints = await models.ProjectPointsDeploy.findAll({ |
|||
where: { graphId: pictureId } |
|||
}) |
|||
ctx.status = 200; |
|||
ctx.body = { |
|||
allPoints, |
|||
setedPoints |
|||
}; |
|||
} else { |
|||
throw new Error('pictureId not found'); |
|||
} |
|||
} catch (err) { |
|||
ctx.fs.logger.error(`path: ${ctx.path}, error: ${err}`); |
|||
ctx.status = 400; |
|||
ctx.body = { |
|||
name: 'FindError', |
|||
message: '获取结构物平面图测点布设失败' |
|||
} |
|||
} |
|||
} |
|||
|
|||
async function setDeployPoints(ctx) { |
|||
const pictureId = ctx.params.pictureId; |
|||
const req = ctx.request.body; |
|||
const models = ctx.fs.dc.models; |
|||
const orm = ctx.fs.dc.orm; |
|||
|
|||
try { |
|||
ctx.fs.api = ctx.fs.api || {}; |
|||
ctx.fs.api.actionParameter = req; |
|||
ctx.fs.api.actionParameterShow = '结构物平面图测点部署: ' + JSON.stringify(req); |
|||
|
|||
const t = await orm.transaction(); |
|||
|
|||
try { |
|||
await models.ProjectPointsDeploy.destroy({ where: { graphId: pictureId }, transaction: t }); |
|||
|
|||
const layout = req.spots.map((hotspot, index) => { |
|||
let msg = { |
|||
graphId: pictureId, |
|||
pointId: hotspot.pointId, |
|||
position: JSON.stringify(hotspot.position) |
|||
} |
|||
return msg; |
|||
}); |
|||
|
|||
await models.ProjectPointsDeploy.bulkCreate(layout, { transaction: t }); |
|||
|
|||
await t.commit(); |
|||
} catch (e) { |
|||
await t.rollback(); |
|||
throw e; |
|||
} |
|||
ctx.status = 200; |
|||
ctx.body = { |
|||
name: "CreateSuccess", |
|||
message: "结构物平面图测点部署成功" |
|||
}; |
|||
|
|||
} catch (err) { |
|||
ctx.fs.logger.error(err); |
|||
ctx.status = 400; |
|||
ctx.body = { |
|||
name: 'CreateError', |
|||
message: "结构物平面图测点部署失败" |
|||
} |
|||
} |
|||
} |
|||
|
|||
module.exports = { |
|||
findSingleGraph, |
|||
createGraph, |
|||
updateGraph, |
|||
delProjectGraph, |
|||
getDeployPoints, |
|||
setDeployPoints |
|||
}; |
@ -0,0 +1,44 @@ |
|||
/* eslint-disable*/ |
|||
|
|||
'use strict'; |
|||
|
|||
module.exports = dc => { |
|||
const DataTypes = dc.ORM; |
|||
const sequelize = dc.orm; |
|||
const ProjectGraph = sequelize.define("projectGraph", { |
|||
id: { |
|||
type: DataTypes.INTEGER, |
|||
allowNull: false, |
|||
defaultValue: null, |
|||
comment: null, |
|||
primaryKey: true, |
|||
field: "id", |
|||
autoIncrement: true, |
|||
unique: "project_graph_id_uindex" |
|||
}, |
|||
projectId: { |
|||
type: DataTypes.INTEGER, |
|||
allowNull: false, |
|||
defaultValue: null, |
|||
comment: null, |
|||
primaryKey: false, |
|||
field: "project_id", |
|||
autoIncrement: false |
|||
}, |
|||
graph: { |
|||
type: DataTypes.STRING, |
|||
allowNull: false, |
|||
defaultValue: null, |
|||
comment: null, |
|||
primaryKey: false, |
|||
field: "graph", |
|||
autoIncrement: false |
|||
} |
|||
}, { |
|||
tableName: "project_graph", |
|||
comment: "", |
|||
indexes: [] |
|||
}); |
|||
dc.models.ProjectGraph = ProjectGraph; |
|||
return ProjectGraph; |
|||
}; |
@ -0,0 +1,53 @@ |
|||
/* eslint-disable*/ |
|||
|
|||
'use strict'; |
|||
|
|||
module.exports = dc => { |
|||
const DataTypes = dc.ORM; |
|||
const sequelize = dc.orm; |
|||
const ProjectPointsDeploy = sequelize.define("projectPointsDeploy", { |
|||
id: { |
|||
type: DataTypes.INTEGER, |
|||
allowNull: false, |
|||
defaultValue: null, |
|||
comment: null, |
|||
primaryKey: true, |
|||
field: "id", |
|||
autoIncrement: true, |
|||
unique: "project_points_deploy_id_uindex" |
|||
}, |
|||
pointId: { |
|||
type: DataTypes.INTEGER, |
|||
allowNull: false, |
|||
defaultValue: null, |
|||
comment: null, |
|||
primaryKey: false, |
|||
field: "point_id", |
|||
autoIncrement: false |
|||
}, |
|||
graphId: { |
|||
type: DataTypes.INTEGER, |
|||
allowNull: false, |
|||
defaultValue: null, |
|||
comment: null, |
|||
primaryKey: false, |
|||
field: "graph_id", |
|||
autoIncrement: false |
|||
}, |
|||
position: { |
|||
type: DataTypes.STRING, |
|||
allowNull: false, |
|||
defaultValue: null, |
|||
comment: null, |
|||
primaryKey: false, |
|||
field: "position", |
|||
autoIncrement: false |
|||
} |
|||
}, { |
|||
tableName: "project_points_deploy", |
|||
comment: "", |
|||
indexes: [] |
|||
}); |
|||
dc.models.ProjectPointsDeploy = ProjectPointsDeploy; |
|||
return ProjectPointsDeploy; |
|||
}; |
@ -0,0 +1,23 @@ |
|||
'use strict'; |
|||
|
|||
const pointDeploy = require('../../controllers/pointDeploy'); |
|||
|
|||
module.exports = function (app, router, opts) { |
|||
app.fs.api.logAttr['GET/project/:projectId/planarGraph'] = { content: '获取结构物平面图数据', visible: false }; |
|||
router.get('/project/:projectId/planarGraph', pointDeploy.findSingleGraph); |
|||
|
|||
app.fs.api.logAttr['POST/planarGraph/add'] = { content: '新增结构物平面图', visible: true }; |
|||
router.post('/planarGraph/add', pointDeploy.createGraph); |
|||
|
|||
app.fs.api.logAttr['PUT/planarGraph/:id/modify'] = { content: '修改结构物平面图', visible: true }; |
|||
router.post('/planarGraph/:id/modify', pointDeploy.updateGraph); |
|||
|
|||
app.fs.api.logAttr['DEL/project/graph/:id'] = { content: '删除结构物布设图', visible: false }; |
|||
router.del('/project/graph/:id', pointDeploy.delProjectGraph); |
|||
|
|||
app.fs.api.logAttr['GET/picture/:pictureId/deploy/points'] = { content: '获取点位布设信息', visible: false }; |
|||
router.get('/picture/:pictureId/deploy/points', pointDeploy.getDeployPoints); |
|||
|
|||
app.fs.api.logAttr['POST/set/picture/:pictureId/deploy/points'] = { content: '点位布设', visible: true }; |
|||
router.post('/set/picture/:pictureId/deploy/points', pointDeploy.setDeployPoints); |
|||
}; |
@ -0,0 +1,33 @@ |
|||
/*结构物布设图表*/ |
|||
create table project_graph |
|||
( |
|||
id serial not null, |
|||
project_id int not null, |
|||
graph varchar(255) not null |
|||
); |
|||
|
|||
create unique index project_graph_id_uindex |
|||
on project_graph (id); |
|||
|
|||
alter table project_graph |
|||
add constraint project_graph_pk |
|||
primary key (id); |
|||
|
|||
|
|||
|
|||
|
|||
/*点位布设表*/ |
|||
create table project_points_deploy |
|||
( |
|||
id serial not null, |
|||
point_id int not null, |
|||
graph_id int not null, |
|||
position varchar(1000) not null |
|||
); |
|||
|
|||
create unique index project_points_deploy_id_uindex |
|||
on project_points_deploy (id); |
|||
|
|||
alter table project_points_deploy |
|||
add constraint project_points_deploy_pk |
|||
primary key (id); |
Loading…
Reference in new issue