'use strict'; const moment = require('moment') async function inspection (ctx) { // 巡查 try { const models = ctx.fs.dc.models; const { projectAppId, screenshot = [], } = ctx.request.body const now = moment().format() const storageData = screenshot.map(s => { return { projectAppId, screenshot: s.url, createTime: now, description: s.description, } }) await models.AppInspection.bulkCreate(storageData) 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 inspectionList (ctx) { try { const models = ctx.fs.dc.models; const { clickHouse } = ctx.app.fs const { timeStart, timeEnd, projectId, appId, noted } = ctx.query let findOption = { where: { }, order: [['id', 'DESC']], include: [{ model: models.App, required: true, where: appId ? { id: appId } : undefined, include: { model: models.ProjectCorrelation, required: true, where: Object.assign({}, { del: false }, projectId ? { id: projectId } : {} ), attributes: { exclude: ['anxinProjectId', 'createTime', 'createUser'] } } }] } if (timeStart && timeEnd) { findOption.where.createTime = { $between: [moment(timeStart).format(), moment(timeEnd).format()] } } if (noted) { if (noted == 'noted') { findOption.where.notedTime = { $ne: null } } else if (noted == 'unnote') { findOption.where.notedTime = null } } const inspectionRes = await models.AppInspection.findAll(findOption) let notedUserIds = new Set() for (let ins of inspectionRes) { if (ins.notedPepUserId) { notedUserIds.add(ins.notedPepUserId) } } let userPepRes = notedUserIds.size ? await clickHouse.pepEmis.query(`SELECT DISTINCT user.id AS id, "user"."name" AS name FROM user WHERE user.id IN (${[...notedUserIds].join(',')})`).toPromise() : [] for (let ins of inspectionRes) { if (ins.notedPepUserId) { const corUser = userPepRes.find(up => up.id == ins.notedPepUserId) ins.dataValues.notedPepUser = corUser ? corUser.name : '' } } ctx.status = 200; ctx.body = inspectionRes } catch (error) { ctx.fs.logger.error(`path: ${ctx.path}, error: ${error}`); ctx.status = 400; ctx.body = { message: typeof error == 'string' ? error : undefined } } } async function notedInspection (ctx) { try { const models = ctx.fs.dc.models; const { inspectionId } = ctx.request.body const { userId, pepUserId } = ctx.fs.api await models.AppInspection.update({ notedPepUserId: pepUserId, notedTime: moment().format() }, { where: { id: inspectionId } }) 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 apiError (ctx) { try { const models = ctx.fs.dc.models; const { projectAppId, alarmContent, router, statusCode, screenshot = '' } = ctx.request.body const now = moment().format() let storageData = { projectAppId, alarmContent, router, statusCode } const existRes = await models.AppAlarm.findOne({ where: { projectAppId, alarmContent, router, statusCode, confirm: null } }) if (existRes) { await models.AppAlarm.update({ updateTime: now }, { where: { id: existRes.id } }) } else { const existCount = await models.AppAlarm.count({ where: { } }) storageData.serialNumber = 'WEB' + (existCount < 9 ? '0' + (existCount + 1) : existCount) storageData.createTime = now storageData.screenshot = screenshot await models.AppAlarm.create(storageData) } ctx.status = 200; } catch (error) { ctx.fs.logger.error(`path: ${ctx.path}, error: ${error}`); ctx.status = 400; ctx.body = { message: typeof error == 'string' ? error : undefined } } } async function apiErrorList (ctx) { try { const models = ctx.fs.dc.models; const { clickHouse } = ctx.app.fs const { keyword, errType, confirmState, sustainTimeStart, sustainTimeEnd, limit, page } = ctx.query const pepProjectSql = ` SELECT t_pim_project.id AS id, t_pim_project.project_name AS project_name, t_pim_project_construction.construction_status_id AS construction_status_id, t_pim_project_state.construction_status AS construction_status FROM t_pim_project LEFT JOIN t_pim_project_construction ON t_pim_project.id = t_pim_project_construction.project_id LEFT JOIN t_pim_project_state ON t_pim_project_construction.construction_status_id = t_pim_project_state.id ` let findOption = { where: { $or: [] }, include: [{ model: models.ProjectApp, where: { }, attributes: { exclude: ['projectId'] }, include: [{ model: models.ProjectCorrelation, where: { }, attributes: { exclude: ['createTime', 'createUser', 'anxinProjectId',] }, }] }] } let projectRes = [] if (keyword) { projectRes = await clickHouse.projectManage.query(` ${pepProjectSql} WHERE project_name LIKE '%${keyword}%'` ).toPromise() if (projectRes.length) { findOption.where.$or.push( { '$projectApp.projectCorrelation.pep_project_id$': { $in: projectRes.map(p => p.id) }, '$projectApp.name$': { $like: `%${keyword}%` } } ) } } if (errType) { if (errType = 'element') { findOption.where.screenshot = null } else { findOption.where.statusCode = errType } } if (confirmState) { if (confirmState == 'confirmd') { findOption.where.confirmTime = { $ne: null } } else if (confirmState == 'unconfirmed') { findOption.where.confirmTime = null } } if (sustainTimeStart && sustainTimeEnd) { findOption.where.$or = findOption.where.$or.concat([ { createTime: { $between: [moment(sustainTimeStart).format(), moment(sustainTimeEnd).format()] }, }, { updateTime: { $between: [moment(sustainTimeStart).format(), moment(sustainTimeEnd).format()] } }, { createTime: { $lte: moment(sustainTimeStart).format() }, updateTime: { $gte: moment(sustainTimeEnd).format() }, } ]) } if (limit) { findOption.limit = limit } if (page && limit) { findOption.offset = page * limit } if (!findOption.where.$or.length) { delete findOption.where.$or } const listRes = await models.AppAlarm.findAndCountAll(findOption) if (!keyword) { // 没有关键字筛选 查询关联的项目信息 let pepProjectIds = new Set() for (let lr of listRes.rows) { if (lr.projectApp && lr.projectApp.projectCorrelation) { pepProjectIds.add(lr.projectApp.projectCorrelation.pepProjectId) } } if (pepProjectIds.size) { projectRes = await clickHouse.projectManage.query(` ${pepProjectSql} WHERE id IN (${[...pepProjectIds].join(',')})` ).toPromise() } } for (let lr of listRes.rows) { if (lr.projectApp && lr.projectApp.projectCorrelation) { let corPepProject = projectRes.find(p => p.id == lr.projectApp.projectCorrelation.pepProjectId) if (corPepProject) { lr.projectApp.projectCorrelation.dataValues.pepProject = corPepProject } } } ctx.status = 200; ctx.body = listRes } catch (error) { ctx.fs.logger.error(`path: ${ctx.path}, error: ${error}`); ctx.status = 400; ctx.body = { message: typeof error == 'string' ? error : undefined } } } async function confirmApiError (ctx) { try { const models = ctx.fs.dc.models; const { confirm, appAlarmId = [] } = ctx.request.body await models.AppAlarm.update({ confirm, confirmTime: moment().format() }, { where: { id: { $in: appAlarmId } } }) 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 = { inspection, inspectionList, notedInspection, apiError, apiErrorList, confirmApiError, };