'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": '删除点位失败' } } } 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": "获取二维码列表失败" } } } async function q(ctx) { let error = { name: 'FindError', message: "获取失败!" }; const models = ctx.fs.dc.models; const { devices } = ctx.request.body const attachment = ctx.app.fs.qn_attachment try { if (!Array.isArray(devices)) { error = { name: 'paramsError', message: '参数不能为空' }; ctx.throw(400); } const devicesArr = await models.Device.findAll({ attributes: ['deviceNo', 'periodCode', 'qrSrc'], where: { deviceNo: { $in: devices } } }) let ids = [], idsMap = {}, qnImages = [] devicesArr.forEach(d => { const qrSrc = d.qrSrc const deviceNo = d.deviceNo const periodCode = d.periodCode if (qrSrc) { if (/^\d+$/.test(qrSrc)) { ids.push(qrSrc) idsMap[qrSrc] = { deviceNo, periodCode } } else { let domain = globalCache.getQnDomain() let imgUrl = `${domain}/${qrSrc}` qnImages.push({ src: imgUrl, deviceNo, periodCode }) } } }) const docs = await models.QrcodePng.findAll({ where: { id: { $in: ids } }, attributes: ["id", "base64"] }) let pics = [] if (docs.length > 0) { pics = docs.map((d) => { let { deviceNo, periodCode } = idsMap[d.id] || {} let base64 = d.base64.replace(/^data:image\/\w+;base64,/, '') return { url: Buffer.from(base64, 'base64'), name: deviceNo, periodCode } }) } if (qnImages.length > 0) { let qns = await downloadImgsAsBase64(qnImages) pics = pics.concat(qns) } let fileUrl = await downLoadImageBiz(pics, { zipName: "二维码_" + moment().format("YYYY-MM-DD-HH-mm-ss"), attachment }) add2CleanCache(fileUrl, attachment) ctx.status = 200 ctx.body = { fileUrl } } catch (err) { ctx.fs.logger.error(err); ctx.status = 400; ctx.body = error; } } module.exports = { projectList, postAddProject, delProject, addPosition, position, delPosition, qrCodeShow, q }