From 70fe70f6186eb464248d67f4a5f97527c00d08ba Mon Sep 17 00:00:00 2001 From: "gao.zhiyuan" Date: Sat, 17 Sep 2022 11:21:01 +0800 Subject: [PATCH] =?UTF-8?q?=E8=8E=B7=E5=8F=96=E5=B7=B2=E7=BB=91=E5=AE=9A?= =?UTF-8?q?=E9=A1=B9=E7=9B=AE=E5=B9=B6=E5=88=86=E9=A1=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- api/app/lib/controllers/alarm/app.js | 17 +++++-- api/app/lib/controllers/project/bind.js | 57 +++++++++++++++++++++-- api/app/lib/controllers/project/index.js | 18 +++++-- api/app/lib/models/project_correlation.js | 27 +++++++---- api/config.js | 3 +- api/sequelize-automate.config.js | 2 +- 6 files changed, 101 insertions(+), 23 deletions(-) diff --git a/api/app/lib/controllers/alarm/app.js b/api/app/lib/controllers/alarm/app.js index 3313e79..3faa464 100644 --- a/api/app/lib/controllers/alarm/app.js +++ b/api/app/lib/controllers/alarm/app.js @@ -43,16 +43,23 @@ async function inspectionList (ctx) { order: [['id', 'DESC']], include: [{ model: models.ProjectApp, - required: Boolean(appId || projectId), + required: true, where: appId ? { id: appId } : undefined, include: { model: models.ProjectCorrelation, - required: Boolean(projectId), - where: projectId ? { - id: projectId - } : undefined, + required: true, + where: Object.assign({}, + { + del: false + }, + projectId ? + { + id: projectId + } : + {} + ), attributes: { exclude: ['anxinProjectId', 'createTime', 'createUser'] } diff --git a/api/app/lib/controllers/project/bind.js b/api/app/lib/controllers/project/bind.js index ffd21e1..13ac5af 100644 --- a/api/app/lib/controllers/project/bind.js +++ b/api/app/lib/controllers/project/bind.js @@ -1,27 +1,76 @@ 'use strict'; async function bindAnxin2pep (ctx) { + const transaction = await ctx.fs.dc.orm.transaction(); try { const models = ctx.fs.dc.models; const { clickHouse } = ctx.app.fs const { bindId, name, pepProjectId, anxinProjectId = [], app = [] } = ctx.request.body + let bindId_ = bindId const existRes = await models.ProjectCorrelation.findOne({ where: { pepProjectId: pepProjectId } }) - if (bindId) { - + let storageData = { + name, pepProjectId, anxinProjectId, + } + if (bindId_) { + if (!existRes) { + throw '尚无已绑定的项企项目' + } + // 修改 + await models.ProjectCorrelation.update(storageData, { + where: { + pepProjectId: pepProjectId + }, + transaction + }) } else { + // 新增 if (existRes) { - throw '当前项企项目已绑定' + // 但是有之前的数据 + if (existRes.del) { + // 不过之前的删除了 + + } else { + // 没有删除 重复添加 + throw '当前项企项目已绑定' + } + } else { + const createRes = await models.ProjectCorrelation.create(storageData, { + transaction + }) + bindId_ = createRes.id + + await models.ProjectApp.bulkCreate(app.map((a, i, arr) => { + if (!a.name || !a.url) { + throw `${a.name} ${a.url} 缺少必要参数` + } + let curUrlArr = a.url.split('://') + if(curUrlArr.length < 2){ + throw `${a.name} ${a.url} url 地址错误` + } + for (let ii = i; ii < arr.length; ii++) { + let + } + + return { + name: a.name, + url: a.url, + projectId: bindId_, + lock: false, + } + })) } } - ctx.status = 20; + await transaction.commit(); + ctx.status = 204; } catch (error) { + await transaction.rollback(); ctx.fs.logger.error(`path: ${ctx.path}, error: ${error}`); ctx.status = 400; ctx.body = { diff --git a/api/app/lib/controllers/project/index.js b/api/app/lib/controllers/project/index.js index 49cf778..6c85141 100644 --- a/api/app/lib/controllers/project/index.js +++ b/api/app/lib/controllers/project/index.js @@ -25,9 +25,12 @@ async function pomsProject (ctx) { const models = ctx.fs.dc.models; const { clickHouse } = ctx.app.fs const { userId } = ctx.fs.api + const { limit, page } = ctx.query - const proRes = await models.ProjectCorrelation.findAll({ - where: {}, + let findOption = { + where: { + del: false + }, include: { model: models.ProjectApp, where: { @@ -37,7 +40,16 @@ async function pomsProject (ctx) { exclude: ['projectId'] } } - }) + } + + if (limit) { + findOption.limit = limit + } + if (page && limit) { + findOption.offset = page * limit + } + + const proRes = await models.ProjectCorrelation.findAll(findOption) let pepProjectIds = new Set() let anxinProjectIds = new Set() diff --git a/api/app/lib/models/project_correlation.js b/api/app/lib/models/project_correlation.js index 3576554..38aaf38 100644 --- a/api/app/lib/models/project_correlation.js +++ b/api/app/lib/models/project_correlation.js @@ -24,15 +24,6 @@ module.exports = dc => { field: "anxin_project_id", autoIncrement: false }, - pepProjectId: { - type: DataTypes.INTEGER, - allowNull: false, - defaultValue: null, - comment: "项目管理的项目id", - primaryKey: false, - field: "pep_project_id", - autoIncrement: false - }, createTime: { type: DataTypes.DATE, allowNull: false, @@ -59,6 +50,24 @@ module.exports = dc => { primaryKey: false, field: "name", autoIncrement: false + }, + pepProjectId: { + type: DataTypes.INTEGER, + allowNull: false, + defaultValue: null, + comment: null, + primaryKey: false, + field: "pep_project_id", + autoIncrement: false + }, + del: { + type: DataTypes.BOOLEAN, + allowNull: true, + defaultValue: null, + comment: null, + primaryKey: false, + field: "del", + autoIncrement: false } }, { tableName: "project_correlation", diff --git a/api/config.js b/api/config.js index 0133b80..56c86eb 100644 --- a/api/config.js +++ b/api/config.js @@ -111,7 +111,8 @@ const product = { exclude: [ { p: '/attachments/:p', o: 'POST' }, { p: '/alarm/application/inspection', o: 'POST' }, - { p: '/project/app_list', o: 'GET' } + { p: '/project/app_list', o: 'GET' }, + { p: '/alarm/application/api', o: 'POST' } ], // 不做认证的路由,也可以使用 exclude: ["*"] 跳过所有路由 redis: { host: IOTA_REDIS_SERVER_HOST, diff --git a/api/sequelize-automate.config.js b/api/sequelize-automate.config.js index 41e326f..69d5cbe 100644 --- a/api/sequelize-automate.config.js +++ b/api/sequelize-automate.config.js @@ -26,7 +26,7 @@ module.exports = { dir: './app/lib/models', // 指定输出 models 文件的目录 typesDir: 'models', // 指定输出 TypeScript 类型定义的文件目录,只有 TypeScript / Midway 等会有类型定义 emptyDir: false, // !!! 谨慎操作 生成 models 之前是否清空 `dir` 以及 `typesDir` - tables: ['project_app'], // 指定生成哪些表的 models,如 ['user', 'user_post'];如果为 null,则忽略改属性 + tables: ['project_correlation'], // 指定生成哪些表的 models,如 ['user', 'user_post'];如果为 null,则忽略改属性 skipTables: [], // 指定跳过哪些表的 models,如 ['user'];如果为 null,则忽略改属性 tsNoCheck: false, // 是否添加 `@ts-nocheck` 注释到 models 文件中 ignorePrefix: [], // 生成的模型名称忽略的前缀,因为 项目中有以下表名是以 t_ 开头的,在实际模型中不需要, 可以添加多个 [ 't_data_', 't_',] ,长度较长的 前缀放前面