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.

410 lines
11 KiB

3 years ago
'use strict';
const moment = require('moment')
async function edit (ctx) {
3 years ago
const transaction = await ctx.fs.dc.orm.transaction();
3 years ago
try {
const models = ctx.fs.dc.models;
const { userId, token } = ctx.fs.api
3 years ago
const {
configId, name, pushWay, noticeWay, timing,
receiver, cameraId
} = ctx.request.body;
3 years ago
3 years ago
let configId_ = configId
3 years ago
let receiverSorted = receiver.sort()
let cameraIdSorted = cameraId.sort()
let noticeWaySorted = noticeWay.sort()
let editDataExist = false
3 years ago
// 判重
3 years ago
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 '已有相同配置信息'
}
}
3 years ago
let config = {
name, pushWay, noticeWay, timing
}
if (configId) {
3 years ago
if (!editDataExist) {
3 years ago
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();
3 years ago
ctx.status = 204;
} catch (error) {
3 years ago
await transaction.rollback();
3 years ago
ctx.fs.logger.error(`path: ${ctx.path}, error: ${error}`);
ctx.status = 400;
3 years ago
ctx.body = {
message: typeof error == 'string' ? error : undefined
}
3 years ago
}
}
async function getStatusPushList (ctx) {
3 years ago
try {
const models = ctx.fs.dc.models;
2 years ago
const sequelize = ctx.fs.dc.ORM;
3 years ago
const { userId, token } = ctx.fs.api
3 years ago
const { limit, page, orderBy, orderDirection, name, pushWay } = ctx.query
3 years ago
3 years ago
let findOption = {
where: {},
order: [
[orderBy || 'id', orderDirection || 'DESC']
3 years ago
]
3 years ago
}
if (limit) {
findOption.limit = limit
}
if (page && limit) {
findOption.offset = page * limit
}
3 years ago
if (name) {
3 years ago
findOption.where['$or'] = {
name: {
3 years ago
$like: `%${name}%`
3 years ago
},
}
}
3 years ago
if (pushWay) {
findOption.where.pushWay = pushWay
}
3 years ago
3 years ago
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']
]
2 years ago
3 years ago
findOption.include = [
{
model: models.CameraStatusPushMonitor,
attributes: ['cameraId'],
required: false,
duplicating: true,
3 years ago
include: [{
model: models.Camera,
attributes: ['name'],
}]
3 years ago
},
{
model: models.CameraStatusPushReceiver,
attributes: ['receiver'],
duplicating: false,
required: false,
},
]
3 years ago
const configRes = await models.CameraStatusPushConfig.findAll(findOption)
3 years ago
delete findOption.order
delete findOption.include
delete findOption.where.id
const count = await models.CameraStatusPushConfig.count(findOption)
2 years ago
// 获取所有id
const pushLogCountRes = await models.CameraStatusPushLog.findAll({
attributes: [
'pushConfigId',
[sequelize.fn('COUNT', sequelize.col('id')), 'count']
],
where: {
pushConfigId: { $in: configIds }
},
group: ['pushConfigId']
})
for (let { dataValues: c } of configRes) {
c.monitorCount = c.cameraStatusPushMonitors.length;
2 years ago
let corLofCount = pushLogCountRes.find(p => p.dataValues.pushConfigId == c.id)
c.logCount = corLofCount ? corLofCount.dataValues.count : 0
delete c.cameraStatusPushLogs
}
3 years ago
ctx.status = 200;
3 years ago
ctx.body = {
count: count,
rows: configRes
}
3 years ago
} 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
3 years ago
const data = ctx.request.body;
await models.CameraStatusPushConfig.update({
forbidden: data.forbidden
}, {
where: {
id: data.configId
}
})
3 years ago
ctx.status = 204;
} catch (error) {
ctx.fs.logger.error(`path: ${ctx.path}, error: ${error}`);
ctx.status = 400;
ctx.body = {}
}
}
async function del (ctx) {
3 years ago
const transaction = await ctx.fs.dc.orm.transaction();
3 years ago
try {
const models = ctx.fs.dc.models;
const { userId, token } = ctx.fs.api
3 years ago
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
})
3 years ago
3 years ago
await transaction.commit();
3 years ago
ctx.status = 204;
} catch (error) {
3 years ago
await transaction.rollback();
3 years ago
ctx.fs.logger.error(`path: ${ctx.path}, error: ${error}`);
ctx.status = 400;
ctx.body = {}
}
}
async function copy (ctx) {
3 years ago
const transaction = await ctx.fs.dc.orm.transaction();
3 years ago
try {
const models = ctx.fs.dc.models;
const { userId, token } = ctx.fs.api
3 years ago
const { configId } = ctx.params;
3 years ago
3 years ago
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
3 years ago
copyConfig.name += '-副本'
3 years ago
copyConfig.createUser = userId
copyConfig.forbidden = true
3 years ago
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
3 years ago
} catch (error) {
3 years ago
await transaction.rollback();
3 years ago
ctx.fs.logger.error(`path: ${ctx.path}, error: ${error}`);
ctx.status = 400;
3 years ago
ctx.body = {
message: typeof error == 'string' ? error : undefined
}
3 years ago
}
}
3 years ago
async function pushLog (ctx) {
3 years ago
try {
const models = ctx.fs.dc.models;
const { userId, token } = ctx.fs.api
3 years ago
const { configId } = ctx.params;
const sequelize = ctx.fs.dc.ORM;
const logRes = await models.CameraStatusPushLog.findAll({
where: {
pushConfigId: configId
},
order: [['time', 'DESC']],
})
3 years ago
3 years ago
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
}
3 years ago
ctx.status = 200;
ctx.body = logRes
3 years ago
} catch (error) {
ctx.fs.logger.error(`path: ${ctx.path}, error: ${error}`);
ctx.status = 400;
3 years ago
ctx.body = {
message: typeof error == 'string' ? error : undefined
}
3 years ago
}
}
module.exports = {
edit, getStatusPushList, banned, del, copy, pushLog
3 years ago
};