'use strict'; const moment = require('moment') async function groupList (ctx) { try { const { models } = ctx.fs.dc; const { userId } = ctx.fs.api const res = await models.ProjectGroup.findAll({ where: { pomsUserId: userId }, order: [['id', 'DESC']] }) ctx.status = 200; ctx.body = res } catch (error) { ctx.fs.logger.error(`path: ${ctx.path}, error: ${error}`); ctx.status = 400; ctx.body = { message: typeof error == 'string' ? error : undefined } } } async function editGroup (ctx) { try { const { models } = ctx.fs.dc; const { userId } = ctx.fs.api const { id, name, pomsProjectIds = [] } = ctx.request.body if (!name || !pomsProjectIds || !pomsProjectIds.length) { throw '参数错误!' } let repeatNameRes = await models.ProjectGroup.findOne({ where: { pomsUserId: userId, name, } }) let repeatProjectRes = await models.ProjectGroup.findOne({ where: { pomsUserId: userId, pomsProjectIds } }) if (repeatNameRes && (!id || (id && repeatNameRes.id != id))) { throw '已有相同名称的分组信息!' } if (repeatProjectRes && (!id || (id && repeatProjectRes.id != id))) { throw '已有相同项目的分组信息!' } if (id) { await models.ProjectGroup.update({ name, pomsProjectIds, }, { where: { id } }) } else { await models.ProjectGroup.create({ name, pomsProjectIds, pomsUserId: userId, }, { where: { id } }) } 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 delGroup (ctx) { try { const { models } = ctx.fs.dc; await models.ProjectGroup.destroy({ where: { id: ctx.query.groupId } }) 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 groupStatic (ctx) { try { const { models } = ctx.fs.dc; const { userId } = ctx.fs.api const { clickHouse } = ctx.app.fs const sequelize = ctx.fs.dc.orm const progectGroupList = await models.ProjectGroup.findAll({ where: { pomsUserId: userId } }) // 获取全部的 poms 项目id 并构建关系 let pomsProjectIds = new Set() let groupMap = { } for (let group of progectGroupList) { groupMap[group.id] = groupMap[group.id] || {} for (let projectId of group.pomsProjectIds) { pomsProjectIds.add(projectId) } } let pomsProjectIdArr = Array.from(pomsProjectIds) const groupProjectRes = await models.ProjectCorrelation.findAll({ where: { id: { $in: pomsProjectIdArr } } }) // 获取所有的 安心云项目id let anxinProjectIds = new Set() for (let project of groupProjectRes) { for (let projectId of project.anxinProjectId) { anxinProjectIds.add(projectId) } groupMap[group.id].anxinProjectCount = anxinProjectCount } let anxinProjectIdArr = Array.from(anxinProjectIds) // 统计安心云项目下的结构物个数 const strucCountRes = await clickHouse.anxinyun.query( ` SELECT project, COUNT(*) AS count FROM t_project_structure WHERE project IN (${[...anxinProjectIdArr].join(',')}, -1) GROUP BY project ` ).toPromise() let rslt = [] for (let pg of progectGroupList) { let anxinProjectCount = 0 for (let projectId of pg.pomsProjectIds) { if (anxinProjectIdArr.includes(projectId)) { anxinProjectCount++ } } let strucCount = 0 } ctx.status = 200; ctx.body = [] } catch (error) { ctx.fs.logger.error(`path: ${ctx.path}, error: error`); ctx.status = 400; ctx.body = { message: typeof error == 'string' ? error : undefined } } } module.exports = { groupList, editGroup, delGroup, groupStatic, };