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.

253 lines
5.7 KiB

2 years ago
'use strict';
async function projectList (ctx, next) {
try {
const models = ctx.fs.dc.models;
let userInfo = ctx.fs.api.userInfo;
2 years ago
const { limit, page, name, justStructure } = ctx.query;
2 years ago
let options = {
// where: {
// },
// include: [{
// as: 'company',
// model: models.Company,
// attributes: ['id', 'name'],
// },],
2 years ago
}
if (limit) {
options.limit = Number(limit)
}
if (page && limit) {
options.offset = Number(page) * Number(limit)
}
if (name) {
options.where.name = { $like: `%${name}%` }
}
2 years ago
let res = []
if (justStructure) {
res = await models.Project.findAndCountAll({
attributes: ['id', 'name'],
})
} else {
res = await models.Project.findAndCountAll(options)
}
2 years ago
ctx.status = 200;
ctx.body = res
} catch (error) {
ctx.fs.logger.error(`path: ${ctx.path}, error: ${error}`);
ctx.status = 400;
ctx.body = {
"message": "获取结构列表失败"
2 years ago
}
}
}
2 years ago
async function postAddProject (ctx, next) {
try {
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
2 years ago
let errMsg = data.id ? '工程编辑失败' : '工程新增失败'
let project = { img, longitude, latitude, name, type, describe, userId: userInfo.id }
2 years ago
const alikeProject = await models.Project.findOne({
2 years ago
where: {
name: name,
}
})
if ((!data.id && alikeProject) || (alikeProject && alikeProject.id !== data.id)) {
errMsg = '已有相同结构物名称'
2 years ago
throw errMsg
}
if (data && data.id) {
await models.Project.update(project, {
2 years ago
where: {
id: data.id
}
})
} else {
await models.Project.create(project)
2 years ago
}
ctx.status = 204;
} catch (error) {
ctx.fs.logger.error(`path: ${ctx.path}, error: ${error}`)
ctx.status = 400;
ctx.body = {
"message": errMsg
}
}
}
async function delProject (ctx, next) {
try {
const models = ctx.fs.dc.models;
let userInfo = ctx.fs.api.userInfo;
const { id } = ctx.params
await models.Project.destroy({
2 years ago
where: {
id,
}
})
await models.Point.destroy({
where: {
projectId: id
}
})
2 years ago
ctx.status = 204;
} catch (error) {
ctx.fs.logger.error(`path: ${ctx.path}, error: ${error}`)
ctx.status = 400;
ctx.body = {
"message": '删除结构物失败'
2 years ago
}
}
}
async function addPosition (ctx, next) {
2 years ago
try {
const models = ctx.fs.dc.models;
let userInfo = ctx.fs.api.userInfo;
const data = ctx.request.body;
const { longitude, latitude, name, describe, qrCode, projectId, } = data
let errMsg = data.id ? '点位编辑失败' : '点位新增失败'
let pointData = { longitude, latitude, name, describe, qrCode, projectId }
const alikeProject = await models.Project.findOne({
where: {
id: data.id,
}
})
if (data && data.id) {
2 years ago
await models.Point.update({ qrCode }, {
where: {
id: data.id,
}
})
} else {
await models.Point.create(pointData)
}
ctx.status = 204;
} catch (error) {
ctx.fs.logger.error(`path: ${ctx.path}, error: ${error}`)
ctx.status = 400;
ctx.body = {
"message": errMsg
2 years ago
}
}
}
async function position (ctx, next) {
try {
const models = ctx.fs.dc.models;
let userInfo = ctx.fs.api.userInfo;
const { limit, page, projectId } = ctx.query;
let options = {
2 years ago
where: {
id: projectId
},
include: [{
model: models.Point,
},],
}
if (limit) {
options.limit = Number(limit)
}
if (page && limit) {
options.offset = Number(page) * Number(limit)
}
let res = await models.Project.findAndCountAll(options)
ctx.status = 200;
ctx.body = res
} catch (error) {
ctx.fs.logger.error(`path: ${ctx.path}, error: ${error}`);
ctx.status = 400;
ctx.body = {
"message": "获取结构列表失败"
}
}
}
async function delPosition (ctx, next) {
try {
const models = ctx.fs.dc.models;
let userInfo = ctx.fs.api.userInfo;
const { id } = ctx.params
await models.Point.destroy({
where: {
id,
2 years ago
}
})
2 years ago
ctx.status = 204;
} catch (error) {
ctx.fs.logger.error(`path: ${ctx.path}, error: ${error}`)
ctx.status = 400;
ctx.body = {
"message": '删除点位失败'
2 years ago
}
}
}
2 years ago
async function qrCodeShow (ctx, next) {
try {
const models = ctx.fs.dc.models;
let userInfo = ctx.fs.api.userInfo;
const { projectId, name } = ctx.query;
let options = {
where: {
qrCode: { $ne: null }
},
}
if (projectId) {
options.where.projectId = projectId
}
if (name) {
options.where.name = { $like: `%${name}%` }
}
let res = await models.Point.findAndCountAll(options)
ctx.status = 200;
ctx.body = res
} catch (error) {
ctx.fs.logger.error(`path: ${ctx.path}, error: ${error}`);
ctx.status = 400;
ctx.body = {
"message": "获取二维码列表失败"
}
}
}
2 years ago
module.exports = {
projectList,
postAddProject,
delProject,
addPosition,
position,
delPosition,
2 years ago
qrCodeShow
2 years ago
}