'use strict'; const moment = require('moment'); async function record (ctx) { try { const { models } = ctx.fs.dc; const { statusCode, description = '', platform = 'yingshi', serialNo, channelNo } = ctx.request.body; let statusRes = await models.CameraStatus.findOne({ where: { status: statusCode, platform, } }) let alarmRes = null; if (!statusRes) { statusRes = await models.CameraStatus.create({ platform: platform, status: statusCode, describe: description, forbidden: false, }) } else { alarmRes = await models.CameraStatusAlarm.findOne({ where: { statusId: statusRes.id, description, confirm: null, serialNo, channelNo, platform, } }) } if (alarmRes) { await models.CameraStatusAlarm.update({ updateTime: moment().format() }, { where: { id: alarmRes.id } }) } else { alarmRes = await models.CameraStatusAlarm.create({ statusId: statusRes.id, description, createTime: moment().format(), serialNo, channelNo, platform, }) } ctx.status = 204; } catch (error) { ctx.fs.logger.error(`path: ${ctx.path}, error: error`); ctx.status = 400; ctx.body = { message: typeof error == 'string' ? error : undefined } } } async function confirm (ctx) { try { const { models } = ctx.fs.dc; const { alarmId, content } = ctx.request.body; await models.CameraStatusAlarm.findOne({ confirm: content, confirmTime: moment().format(), }, { where: { id: { $in: alarmId }, confirmTime: null } }) ctx.status = 204; } catch (error) { ctx.fs.logger.error(`path: ${ctx.path}, error: ${error}`); ctx.status = 400; ctx.body = { message: typeof error == 'string' ? error : undefined } } } module.exports = { record, confirm, };