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