'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 { limit, page, orderBy, orderDirection } = ctx.query let findOption = { attributes: { exclude: ['delete'] }, where: { delete: false, }, order: [ [orderBy || 'id', orderDirection || 'DESC'] ] } if (limit) { findOption.limit = limit } if (page && limit) { findOption.offset = page * limit } const res = await models.Nvr.findAll(findOption) const total = await models.Nvr.count() 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) { try { const models = ctx.fs.dc.models; const { nvrId } = ctx.params await models.Nvr.destroy({ where: { id: nvrId } }) ctx.status = 204; } catch (error) { ctx.fs.logger.error(`path: ${ctx.path}, error: ${error}`); ctx.status = 400; ctx.body = {} } } module.exports = { edit, get, del, };