|
|
@ -2,15 +2,86 @@ |
|
|
|
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.CameraStatusConfig.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 = {} |
|
|
|
ctx.body = { |
|
|
|
message: typeof error == 'string' ? error : undefined |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
@ -18,8 +89,67 @@ 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 |
|
|
|
|
|
|
|
ctx.status = 204; |
|
|
|
const sequelize = ctx.fs.dc.ORM; |
|
|
|
let findOption = { |
|
|
|
attributes: { |
|
|
|
include: [ |
|
|
|
[sequelize.fn('COUNT', sequelize.col('cameraStatusPushMonitors.id')), 'monitorCount'], |
|
|
|
[sequelize.fn('COUNT', sequelize.col('cameraStatusPushLogs.id')), 'logCount'] |
|
|
|
], |
|
|
|
}, |
|
|
|
where: {}, |
|
|
|
order: [ |
|
|
|
[orderBy || 'id', orderDirection || 'DESC'] |
|
|
|
], |
|
|
|
distinct: true, |
|
|
|
subQuery: false, |
|
|
|
group: [ |
|
|
|
'cameraStatusPushConfig.id', |
|
|
|
'cameraStatusPushMonitors.id', |
|
|
|
'cameraStatusPushLogs.id', |
|
|
|
'cameraStatusPushReceivers.id', |
|
|
|
], |
|
|
|
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 (limit) { |
|
|
|
findOption.limit = limit |
|
|
|
} |
|
|
|
if (page && limit) { |
|
|
|
findOption.offset = page * limit |
|
|
|
} |
|
|
|
if (keyword) { |
|
|
|
findOption.where['$or'] = { |
|
|
|
name: { |
|
|
|
$like: `%${keyword}%` |
|
|
|
}, |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
const configRes = await models.CameraStatusPushConfig.findAll(findOption) |
|
|
|
|
|
|
|
ctx.status = 200; |
|
|
|
ctx.body = configRes |
|
|
|
} catch (error) { |
|
|
|
ctx.fs.logger.error(`path: ${ctx.path}, error: ${error}`); |
|
|
|
ctx.status = 400; |
|
|
@ -31,6 +161,15 @@ 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) { |
|
|
@ -41,12 +180,35 @@ async function banned (ctx) { |
|
|
|
} |
|
|
|
|
|
|
|
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 = {} |
|
|
@ -54,32 +216,118 @@ async function del (ctx) { |
|
|
|
} |
|
|
|
|
|
|
|
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; |
|
|
|
|
|
|
|
ctx.status = 204; |
|
|
|
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 |
|
|
|
delete copyConfig.id |
|
|
|
let cameraStatusPushMonitors = copyConfig.cameraStatusPushMonitors |
|
|
|
let cameraStatusPushReceiver = copyConfig.cameraStatusPushReceivers |
|
|
|
delete copyConfig.cameraStatusPushMonitors |
|
|
|
delete copyConfig.cameraStatusPushReceivers |
|
|
|
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 = {} |
|
|
|
ctx.body = { |
|
|
|
message: typeof error == 'string' ? error : undefined |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
async function detail (ctx) { |
|
|
|
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']], |
|
|
|
}) |
|
|
|
|
|
|
|
ctx.status = 204; |
|
|
|
ctx.status = 200; |
|
|
|
ctx.body = logRes |
|
|
|
} catch (error) { |
|
|
|
ctx.fs.logger.error(`path: ${ctx.path}, error: ${error}`); |
|
|
|
ctx.status = 400; |
|
|
|
ctx.body = {} |
|
|
|
ctx.body = { |
|
|
|
message: typeof error == 'string' ? error : undefined |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
module.exports = { |
|
|
|
edit, get, banned, del, copy, detail |
|
|
|
edit, get, banned, del, copy, pushLog |
|
|
|
}; |