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.
 
 
 
 
 

371 lines
10 KiB

'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 config = {
name, pushWay, noticeWay, timing
}
if (configId) {
const configRes = await models.CameraStatusPushConfig.findOne({
where: {
id: configId
}
})
if (!configRes) {
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, keyword, pushWay } = ctx.query
const sequelize = ctx.fs.dc.ORM;
let findOption = {
where: {},
order: [
[orderBy || 'id', orderDirection || 'DESC']
]
}
if (limit) {
findOption.limit = limit
}
if (page && limit) {
findOption.offset = page * limit
}
if (keyword) {
findOption.where['$or'] = {
name: {
$like: `%${keyword}%`
},
}
}
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.attributes = {
include: [
[sequelize.fn('COUNT', sequelize.col('cameraStatusPushMonitors.id')), 'monitorCount'],
[sequelize.fn('COUNT', sequelize.col('cameraStatusPushLogs.id')), 'logCount']
],
}
findOption.distinct = true
findOption.subQuery = false
findOption.group = [
'cameraStatusPushConfig.id',
'cameraStatusPushMonitors.id',
'cameraStatusPushLogs.id',
'cameraStatusPushReceivers.id',
]
findOption.order = [
[orderBy || 'id', orderDirection || 'DESC']
]
findOption.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,
},
]
const configRes = await models.CameraStatusPushConfig.findAll(findOption)
delete findOption.attributes
delete findOption.group
delete findOption.order
delete findOption.distinct
delete findOption.subQuery
delete findOption.include
delete findOption.where.id
const count = await models.CameraStatusPushConfig.count(findOption)
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
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
};