'use strict'; const moment = require('moment'); async function dataList (ctx) { try { const { models } = ctx.fs.dc; const { userId, pepUserId, userInfo = {}, pepUserInfo } = ctx.fs.api const { clickHouse } = ctx.app.fs const { utils: { judgeSuper, anxinStrucIdRange, pomsProjectRange } } = ctx.app.fs const { database: anxinyun } = clickHouse.anxinyun.opts.config const { pepProjectId } = ctx.request.query let anxinStruc = await anxinStrucIdRange({ ctx, pepProjectId }) let pomsProject = await pomsProjectRange({ ctx, pepProjectId, }) const pomsProjectIds = pomsProject.map(p => p.id) if (anxinStruc.length) { const anxinStrucIds = anxinStruc.map(a => a.strucId) || [] const dataAlarm = await clickHouse.dataAlarm.query(` SELECT formatDateTime(alarmData.StartTime,'%F %H') hours, count(AlarmId) count FROM ( SELECT AlarmId,State,StartTime FROM alarms WHERE alarms.StructureId IN (${anxinStrucIds.join(",")}) AND AlarmGroup = 3) AS alarmData GROUP BY hours `).toPromise(); // const confirmedAlarm = dataAlarm // // TODO: 开发临时注释 // .filter(ar => ar.State && ar.State > 2) // .map(ar => "'" + ar.AlarmId + "'") // // formatDateTime(Time,'%F %H') hours, count(AlarmId) count // const dataConfirme = confirmedAlarm.length ? // await clickHouse.dataAlarm.query(` // SELECT // max(Time) AS Time, AlarmId // FROM // alarm_details // WHERE // AlarmId IN (${confirmedAlarm.join(',')}) // GROUP BY AlarmId // `).toPromise() : // []; // dataAlarm.forEach(ar => { // ar.confirme = // dataConfirme.find(as => as.AlarmId == ar.AlarmId) || {} // }) ctx.status = 200 ctx.body = dataAlarm } else { ctx.status = 200 ctx.body = { dataAlarm: 0, } } } catch (error) { ctx.fs.logger.error(`path: ${ctx.path}, error: ${error}`); ctx.status = 400; ctx.body = { message: typeof error == 'string' ? error : undefined } } } async function userlist (ctx) { try { const models = ctx.fs.dc.models; const { clickHouse } = ctx.app.fs const sequelize = ctx.fs.dc.orm; const { pepId } = ctx.query const excludeField = ['lastInTime', 'inTimes', 'onlineDuration', 'lastInAddress', 'deleted', 'updateTime'] let findOption = { attributes: { exclude: excludeField, // include: [[sequelize.fn('array_length', sequelize.col('role')), 'roleCount']] }, where: { deleted: false, $or: [{ $not: { role: { $contained: ['SuperAdmin', 'admin'] } } }, sequelize.where(sequelize.fn('cardinality', sequelize.col('role')), 0)], // $not: { // role: { $contained: ['SuperAdmin', 'admin'] } // } }, order: [['updateTime', 'DESC']] } const userRes = await models.User.findAndCountAll(findOption) const adminRes = await models.User.findAll({ where: { role: { $contains: ['admin'] } }, attributes: { exclude: excludeField, }, order: [['updateTime', 'DESC']] }) let userIds = new Set() let pomsProjectIds = new Set() for (let u of userRes.rows.concat(adminRes)) { userIds.add(u.pepUserId) for (let pid of u.correlationProject) { pomsProjectIds.add(pid) } } // 查用户所属的项企pep的部门、人员信息 let userPepRes = userIds.size ? await clickHouse.pepEmis.query(` SELECT DISTINCT user.id AS id, "user"."name" AS name, department.name AS depName, department.id AS depId FROM department_user LEFT JOIN user ON department_user.user=user.id LEFT JOIN department ON department.id=department_user.department WHERE user.id IN (${[...userIds].join(',')}) AND department.delete=false` ).toPromise() : [] // 查用户绑定的当前系统的项目 id let pomsProjectRes = await models.ProjectCorrelation.findAll({ where: { id: { $in: [...pomsProjectIds] }, // del: false } }) // 获取响应的绑定的 项企项目的 id let pepPojectIds = new Set() for (let p of pomsProjectRes) { if (p.pepProjectId) { pepPojectIds.add(p.pepProjectId) } } // 查对应的项企项目信息 let pepProjectRes = pepPojectIds.size ? await clickHouse.projectManage.query(` SELECT id, project_name, isdelete FROM t_pim_project WHERE id IN (${[...pepPojectIds]}) `).toPromise() : [] // 遍历用户并将查到的信息拼合 for (let u of userRes.rows.concat(adminRes)) { // 用户信息 const corUsers = userPepRes.filter(up => up.id == u.pepUserId) u.dataValues.name = corUsers.length ? corUsers[0].name : '' u.dataValues.departments = corUsers.length ? corUsers.map(cu => { return { name: cu.depName, id: cu.depId } }) : [] // pep项目信息 u.dataValues.correlationProject = u.dataValues.correlationProject.map(cpid => { let returnData = { id: cpid, } const corPomsProject = pomsProjectRes.find(ppr => ppr.id == cpid) if (corPomsProject) { returnData.name = corPomsProject.name returnData.del = corPomsProject.del if (corPomsProject.pepProjectId) { returnData.pepProjectId = corPomsProject.pepProjectId const corPepProject = pepProjectRes.find(ppr => ppr.id == corPomsProject.pepProjectId) if (corPepProject) { returnData.pepProjectName = corPepProject.project_name returnData.pepIsdelete = corPepProject.isdelete } } } return returnData }) } let personnel = userRes.rows.filter(r => r.correlationProject.length > 0) if (pepId) { personnel = personnel.filter(r => r.dataValues.correlationProject.map(v => v.id).includes(Number(pepId))) } ctx.status = 200 ctx.body = { personnel: personnel.map(v => v.dataValues.name) } } catch (error) { ctx.fs.logger.error(`path: ${ctx.path}, error: ${error}`); ctx.status = 400; ctx.body = { } } } module.exports = { dataList, userlist };