'use strict'; const fs = require('fs'); const moment = require('moment') module.exports = function (app, opts) { function judgeSuper (ctx) { try { const { userInfo = {} } = ctx.fs.api || {}; const { role = [] } = userInfo return role.includes('SuperAdmin') } catch (error) { ctx.fs.logger.error(`path: ${ctx.path}, error: ${error}`); } } async function anxinStrucIdRange ({ ctx, pepProjectId, projectOrStructKeyword }) { try { const { models } = ctx.fs.dc; const { userInfo = {} } = ctx.fs.api || {}; const { clickHouse } = ctx.app.fs const { correlationProject = [] } = userInfo const isSuper = judgeSuper(ctx) let findOption = { where: { del: false } } if (pepProjectId) { findOption.where.pepProjectId = pepProjectId } else if (!isSuper) { findOption.where.id = { $in: correlationProject } } // TODO 这儿也许需要判断传进来的 pepProjectId 在不在当前用户的关注范围内 const bindRes = await models.ProjectCorrelation.findAll(findOption) let pepProjectIds = [] const anxinProjectIds = [ ...bindRes.reduce( (arr, b) => { pepProjectIds.push(b.pepProjectId) for (let sid of b.anxinProjectId) { arr.add(sid); } return arr; }, new Set() ) ] const pepProjectRes = pepProjectIds.length ? await clickHouse.projectManage.query( ` SELECT id, project_name, isdelete FROM t_pim_project WHERE id IN (${pepProjectIds.join(',')}) ` ).toPromise() : [] const undelStrucRes = anxinProjectIds.length ? await clickHouse.anxinyun.query( ` SELECT t_project.id, t_structure.id AS strucId, project_state FROM t_project LEFT JOIN t_project_structure ON t_project_structure.project = t_project.id RIGHT JOIN t_structure ON t_structure.id = t_project_structure.structure WHERE project_state != -1 AND t_project.id IN (${anxinProjectIds.join(',')}) ${projectOrStructKeyword ? `AND (t_project.name LIKE '%${projectOrStructKeyword}%' OR t_structure.name LIKE '%${projectOrStructKeyword}%')` : ''} ` ).toPromise() : [] const undelStrucIds = [...new Set(...undelStrucRes.map(s => s.strucId))] return {} } catch (error) { ctx.fs.logger.error(`path: ${ctx.path}, error: ${error}`); } } return { judgeSuper, anxinStrucIdRange } }