|
|
|
'use strict';
|
|
|
|
const moment = require('moment')
|
|
|
|
|
|
|
|
async function edit (ctx, next) {
|
|
|
|
const transaction = await ctx.fs.dc.orm.transaction();
|
|
|
|
try {
|
|
|
|
const models = ctx.fs.dc.models;
|
|
|
|
const { userId } = ctx.fs.api
|
|
|
|
const data = ctx.request.body;
|
|
|
|
|
|
|
|
// 或取其他服务信息
|
|
|
|
const nvrData = {
|
|
|
|
channelCount: 8,
|
|
|
|
port: 8080,
|
|
|
|
}
|
|
|
|
|
|
|
|
if (data.id) {
|
|
|
|
// 修改
|
|
|
|
const storageData = Object.assign({}, data, nvrData)
|
|
|
|
await models.Nvr.update(storageData, {
|
|
|
|
where: {
|
|
|
|
id: data.id
|
|
|
|
},
|
|
|
|
transaction
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
// 添加
|
|
|
|
const storageData = Object.assign({}, data, nvrData, {
|
|
|
|
createTime: moment().format(),
|
|
|
|
createUserId: userId,
|
|
|
|
delete: false,
|
|
|
|
})
|
|
|
|
await models.Nvr.create(storageData, { 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 get (ctx) {
|
|
|
|
const models = ctx.fs.dc.models;
|
|
|
|
try {
|
|
|
|
const { userId, token } = ctx.fs.api
|
|
|
|
const { limit, page, orderBy, orderDirection, keyword, venderId } = ctx.query
|
|
|
|
let findOption = {
|
|
|
|
attributes: { exclude: ['delete'] },
|
|
|
|
where: {
|
|
|
|
createUserId: userId,
|
|
|
|
delete: false,
|
|
|
|
},
|
|
|
|
order: [
|
|
|
|
[orderBy || 'id', orderDirection || 'DESC']
|
|
|
|
]
|
|
|
|
}
|
|
|
|
if (limit) {
|
|
|
|
findOption.limit = limit
|
|
|
|
}
|
|
|
|
if (page && limit) {
|
|
|
|
findOption.offset = page * limit
|
|
|
|
}
|
|
|
|
if (keyword) {
|
|
|
|
findOption.where.name = { $like: `%${keyword}%` }
|
|
|
|
}
|
|
|
|
if (venderId) {
|
|
|
|
findOption.where.venderId = venderId
|
|
|
|
}
|
|
|
|
|
|
|
|
const res = await models.Nvr.findAll(findOption)
|
|
|
|
const total = await models.Nvr.count({
|
|
|
|
where: findOption.where
|
|
|
|
})
|
|
|
|
|
|
|
|
ctx.status = 200;
|
|
|
|
ctx.body = {
|
|
|
|
total: total,
|
|
|
|
data: res
|
|
|
|
}
|
|
|
|
} catch (error) {
|
|
|
|
ctx.fs.logger.error(`path: ${ctx.path}, error: ${error}`);
|
|
|
|
ctx.status = 400;
|
|
|
|
ctx.body = {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async function del (ctx, next) {
|
|
|
|
const transaction = await ctx.fs.dc.orm.transaction();
|
|
|
|
try {
|
|
|
|
const models = ctx.fs.dc.models;
|
|
|
|
const { userId, token } = ctx.fs.api
|
|
|
|
const { nvrId } = ctx.params
|
|
|
|
|
|
|
|
await models.Nvr.destroy({
|
|
|
|
where: {
|
|
|
|
id: nvrId
|
|
|
|
},
|
|
|
|
transaction
|
|
|
|
})
|
|
|
|
|
|
|
|
const cameraRes = await models.Camera.findAll({
|
|
|
|
where: {
|
|
|
|
nvrId
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
const cameraIds = cameraRes.map(c => c.id)
|
|
|
|
|
|
|
|
await models.Camera.destroy({
|
|
|
|
where: {
|
|
|
|
nvrId,
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
await ctx.app.fs.axyRequest.delete('vcmp/camera/project', { query: { token, cameraId: cameraIds.join(',') } })
|
|
|
|
|
|
|
|
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 = {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
edit,
|
|
|
|
get,
|
|
|
|
del,
|
|
|
|
};
|