diff --git a/code/VideoAccess-VCMP/api/app/lib/controllers/camera/index.js b/code/VideoAccess-VCMP/api/app/lib/controllers/camera/index.js index f5e967a..f55dd01 100644 --- a/code/VideoAccess-VCMP/api/app/lib/controllers/camera/index.js +++ b/code/VideoAccess-VCMP/api/app/lib/controllers/camera/index.js @@ -1,4 +1,6 @@ 'use strict'; +const fs = require('fs'); +const moment = require('moment') async function getCameraProject (ctx, next) { try { @@ -238,21 +240,130 @@ async function del (ctx) { } } -async function cameraExport(ctx){ +async function cameraExport (ctx) { try { const { models } = ctx.fs.dc const { userId, token } = ctx.fs.api const { utils: { simpleExcelDown } } = ctx.app.fs + const header = [{ + title: "设备名称", + key: "name", + }, { + title: "设备厂家", + key: "vender", + }, { + title: "接入类型", + key: "type", + }, { + title: "设备状态", + key: "state", + }, { + title: "云台支持", + key: "cloudControl", + }, { + title: "内存卡信息", + key: "memoryCard", + }, { + title: "设备创建时间", + key: "createTime", + }, { + title: "设备添加账号", + key: "createUser", + }, { + title: "项目名称", + key: "projectName", + }, { + title: "pcode", + key: "pcode", + }, { + title: "结构物", + key: "structure", + }, { + title: "测点", + key: "stationName", + }, { + title: "监测因素", + key: "factor", + },]; + + const cameraRes = await models.Camera.findAll({ + where: { + createUserId: userId, + recycleTime: null, + delete: false + }, + include: [{ + model: models.CameraAbility + }, { + model: models.CameraKind + }, { + model: models.Vender + }] + }) - - // const filePath = await simpleExcelDown({ data: exportData, header, fileName: `NVR信息列表_${userId}_${moment().format('YYYYMMDDHHmmss')}` }) - // const fileData = fs.readFileSync(filePath); - // let fileName = filePath.split('/').pop() - // ctx.status = 200; - // ctx.set('Content-Type', 'application/x-xls'); - // ctx.set('Content-disposition', 'attachment; filename=' + encodeURI(fileName)); - // ctx.body = fileData; + let cameraIds = [] + let createUserIds = new Set() + + for (let c of cameraRes) { + cameraIds.push(c.dataValues.id) + createUserIds.add(c.dataValues.createUserId) + } + + // 查在安心云绑定的数据 + const axbindCameraRes = await ctx.app.fs.axyRequest.get('vcmp/camera/project', { query: { token, cameraId: cameraIds.join(',') } }) + + // 查对应创建者信息 + const corUsers = await ctx.app.fs.authRequest.get(`user/${[...createUserIds].join(',') || -1}/message`, { query: { token } }) + + let exportData = [] + let typeMap = { + yingshi: '萤石云平台摄像头', + nvr: 'NVR摄像头', + ipc: 'IPC 网络摄像头', + cascade: '不明厂家', + } + for (let { dataValues: camera } of cameraRes) { + camera.vender = camera.vender ? camera.vender.name : '' + camera.type = typeMap[camera.type] + + const corUser = corUsers.find(u => u.id == camera.createUserId) + camera.createUser = corUser ? corUser.username : '' + + let stationName = new Set(), + projectName = new Set(), + pcode = new Set(), + structure = new Set(), + factor = new Set() + const corBindCamera = axbindCameraRes.find(b => b.cameraId == camera.id) + if (corBindCamera) { + for (let station of corBindCamera.stations) { + stationName.add(station.name) + factor.add(station.factor.name) + structure.add(station.structure.name) + for (let project of station.structure.projects) { + projectName.add(project.name) + pcode.add(project.url) + } + } + } + camera.stationName = [...stationName].join('\r\n') + camera.factor = [...factor].join('\r\n') + camera.projectName = [...projectName].join('\r\n') + camera.pcode = [...pcode].join('\r\n') + camera.structure = [...structure].join('\r\n') + + exportData.push(camera) + } + + + const filePath = await simpleExcelDown({ data: exportData, header, fileName: `摄像头信息列表_${userId}_${moment().format('YYYYMMDDHHmmss')}` }) + const fileData = fs.readFileSync(filePath); + let fileName = filePath.split('/').pop() + ctx.status = 200; + ctx.set('Content-Type', 'application/x-xls'); + ctx.set('Content-disposition', 'attachment; filename=' + encodeURI(fileName)); + ctx.body = fileData; } catch (error) { ctx.fs.logger.error(`path: ${ctx.path}, error: ${error}`); ctx.status = 400;