'use strict'; const moment = require('moment') async function edit (ctx) { const transaction = await ctx.fs.dc.orm.transaction(); try { const models = ctx.fs.dc.models; const { userId, token } = ctx.fs.api const { configId, name, pushWay, noticeWay, timing, receiver, cameraId } = ctx.request.body; let configId_ = configId let receiverSorted = receiver.sort() let cameraIdSorted = cameraId.sort() let noticeWaySorted = noticeWay.sort() let editDataExist = false // 判重 const configAllRes = await models.CameraStatusPushConfig.findAll({ include: [{ model: models.CameraStatusPushMonitor, attributes: ['cameraId'], required: false, duplicating: true, }, { model: models.CameraStatusPushReceiver, attributes: ['receiver'], duplicating: false, required: false, },] }) for (let c of configAllRes) { if (configId_ && c.id == configId_) { editDataExist = true continue } else if (c.name == name) { throw '已有同名称配置信息' } const cReceiver = c.cameraStatusPushReceivers.map(r => r.receiver) const cCameraId = c.cameraStatusPushMonitors.map(m => m.cameraId) const cReceiverSorted = cReceiver.sort() const cCameraIdSorted = cCameraId.sort() if ( receiverSorted.join(',') == cReceiverSorted.join(',') && cameraIdSorted.join(',') == cCameraIdSorted.join(',') && c.pushWay == pushWay && c.noticeWay.sort().join(',') == noticeWaySorted.join(',') && ( (!c.timing && !timing) || c.timing == timing ) ) { throw '已有相同配置信息' } } let config = { name, pushWay, noticeWay, timing } if (configId) { if (!editDataExist) { throw '参数错误' } else { // DO UPDATE await models.CameraStatusPushConfig.update(config, { where: { id: configId }, transaction }) await models.CameraStatusPushReceiver.destroy({ where: { configId }, transaction }) await models.CameraStatusPushMonitor.destroy({ where: { configId }, transaction }) } } else { // DO CREATE config.createUser = userId config.forbidden = false const createConfigRes = await models.CameraStatusPushConfig.create(config, { transaction }) configId_ = createConfigRes.id } if (receiver && receiver.length) { await models.CameraStatusPushReceiver.bulkCreate( receiver.map(item => ({ configId: configId_, receiver: item, })), { transaction } ) } if (cameraId && cameraId.length) { await models.CameraStatusPushMonitor.bulkCreate( cameraId.map(item => ({ configId: configId_, cameraId: item, })), { transaction } ) } await transaction.commit(); ctx.status = 204; } catch (error) { await transaction.rollback(); ctx.fs.logger.error(`path: ${ctx.path}, error: ${error}`); ctx.status = 400; ctx.body = { message: typeof error == 'string' ? error : undefined } } } async function get (ctx) { try { const models = ctx.fs.dc.models; const { userId, token } = ctx.fs.api const { limit, page, orderBy, orderDirection, name, pushWay } = ctx.query let findOption = { where: {}, order: [ [orderBy || 'id', orderDirection || 'DESC'] ] } if (limit) { findOption.limit = limit } if (page && limit) { findOption.offset = page * limit } if (name) { findOption.where['$or'] = { name: { $like: `%${name}%` }, } } if (pushWay) { findOption.where.pushWay = pushWay } const configLimitRes = await models.CameraStatusPushConfig.findAll(findOption) delete findOption.limit delete findOption.offset const configIds = configLimitRes.map(c => c.id) findOption.where.id = { $in: configIds } findOption.order = [ [orderBy || 'id', orderDirection || 'DESC'] ] findOption.include = [ { model: models.CameraStatusPushMonitor, attributes: ['cameraId'], required: false, duplicating: true, include: [{ model: models.Camera, attributes: ['name'], }] }, { model: models.CameraStatusPushLog, attributes: ['id'], duplicating: false, required: false, }, { model: models.CameraStatusPushReceiver, attributes: ['receiver'], duplicating: false, required: false, }, ] const configRes = await models.CameraStatusPushConfig.findAll(findOption) delete findOption.order delete findOption.include delete findOption.where.id const count = await models.CameraStatusPushConfig.count(findOption) for (let { dataValues: c } of configRes) { c.monitorCount = c.cameraStatusPushMonitors.length; c.logCount = c.cameraStatusPushLogs.length; delete c.cameraStatusPushLogs } ctx.status = 200; ctx.body = { count: count, rows: configRes } } catch (error) { ctx.fs.logger.error(`path: ${ctx.path}, error: ${error}`); ctx.status = 400; ctx.body = {} } } async function banned (ctx) { try { const models = ctx.fs.dc.models; const { userId, token } = ctx.fs.api const data = ctx.request.body; await models.CameraStatusPushConfig.update({ forbidden: data.forbidden }, { where: { id: data.configId } }) ctx.status = 204; } catch (error) { ctx.fs.logger.error(`path: ${ctx.path}, error: ${error}`); ctx.status = 400; ctx.body = {} } } async function del (ctx) { const transaction = await ctx.fs.dc.orm.transaction(); try { const models = ctx.fs.dc.models; const { userId, token } = ctx.fs.api const { configId } = ctx.params; await models.CameraStatusPushReceiver.destroy({ where: { configId }, transaction }) await models.CameraStatusPushMonitor.destroy({ where: { configId }, transaction }) await models.CameraStatusPushConfig.destroy({ where: { id: configId }, transaction }) await transaction.commit(); ctx.status = 204; } catch (error) { await transaction.rollback(); ctx.fs.logger.error(`path: ${ctx.path}, error: ${error}`); ctx.status = 400; ctx.body = {} } } async function copy (ctx) { const transaction = await ctx.fs.dc.orm.transaction(); try { const models = ctx.fs.dc.models; const { userId, token } = ctx.fs.api const { configId } = ctx.params; const configRes = await models.CameraStatusPushConfig.findOne({ where: { id: configId }, include: [ { model: models.CameraStatusPushMonitor, attributes: ['cameraId'], required: false, duplicating: true }, { model: models.CameraStatusPushLog, attributes: [], duplicating: false, required: false, }, { model: models.CameraStatusPushReceiver, attributes: ['receiver'], duplicating: false, required: false, }, ], }) if (!configRes) { throw '参数错误' } let copyConfig = JSON.parse(JSON.stringify(configRes.dataValues)) let returnConfig = JSON.parse(JSON.stringify(configRes.dataValues)) delete copyConfig.id let cameraStatusPushMonitors = copyConfig.cameraStatusPushMonitors let cameraStatusPushReceiver = copyConfig.cameraStatusPushReceivers delete copyConfig.cameraStatusPushMonitors delete copyConfig.cameraStatusPushReceivers copyConfig.name += '-副本' copyConfig.createUser = userId copyConfig.forbidden = true let newConfig = await models.CameraStatusPushConfig.create(copyConfig, { transaction }) if (cameraStatusPushMonitors && cameraStatusPushMonitors.length) { await models.CameraStatusPushMonitor.bulkCreate( cameraStatusPushMonitors.map(item => ({ configId: newConfig.id, cameraId: item.cameraId, })), { transaction } ) } if (cameraStatusPushReceiver && cameraStatusPushReceiver.length) { await models.CameraStatusPushReceiver.bulkCreate( cameraStatusPushReceiver.map(item => ({ configId: newConfig.id, receiver: item.receiver, })), { transaction } ) } returnConfig.id = newConfig.id await transaction.commit(); ctx.status = 200; ctx.body = returnConfig } catch (error) { await transaction.rollback(); ctx.fs.logger.error(`path: ${ctx.path}, error: ${error}`); ctx.status = 400; ctx.body = { message: typeof error == 'string' ? error : undefined } } } async function pushLog (ctx) { try { const models = ctx.fs.dc.models; const { userId, token } = ctx.fs.api const { configId } = ctx.params; const sequelize = ctx.fs.dc.ORM; const logRes = await models.CameraStatusPushLog.findAll({ where: { pushConfigId: configId }, order: [['time', 'DESC']], }) let cameraIds = new Set() for (let lr of logRes) { for (let c of lr.camera) { cameraIds.add(c) } } let cameraRes = await models.Camera.findAll({ attributes: ['id', 'name'], where: { id: { $in: Array.from(cameraIds) } } }) for (let lr of logRes) { let camera = cameraRes.filter(c => lr.camera.some(lrc => lrc == c.id)) lr.camera = camera } ctx.status = 200; ctx.body = logRes } catch (error) { ctx.fs.logger.error(`path: ${ctx.path}, error: ${error}`); ctx.status = 400; ctx.body = { message: typeof error == 'string' ? error : undefined } } } module.exports = { edit, get, banned, del, copy, pushLog };