Browse Source

获取已绑定项目并分页

dev
巴林闲侠 2 years ago
parent
commit
70fe70f618
  1. 17
      api/app/lib/controllers/alarm/app.js
  2. 57
      api/app/lib/controllers/project/bind.js
  3. 18
      api/app/lib/controllers/project/index.js
  4. 27
      api/app/lib/models/project_correlation.js
  5. 3
      api/config.js
  6. 2
      api/sequelize-automate.config.js

17
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']
}

57
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 = {

18
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()

27
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",

3
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,

2
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_',] ,长度较长的 前缀放前面

Loading…
Cancel
Save