From e067004bcffae480f6270cb32302afb475c0c245 Mon Sep 17 00:00:00 2001 From: zhangminghua Date: Tue, 23 May 2023 16:57:00 +0800 Subject: [PATCH] =?UTF-8?q?(+)=E8=A1=A8=E4=B8=9A=E5=8A=A1=E5=85=83?= =?UTF-8?q?=E6=95=B0=E6=8D=AE=E5=A2=9E=E5=88=A0=E6=94=B9=E6=9F=A5=E6=8E=A5?= =?UTF-8?q?=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../lib/controllers/businessMetadata/index.js | 154 ++++++++++++++++++ api/app/lib/index.js | 12 +- .../lib/models/business_metadata_database.js | 2 +- api/app/lib/models/business_metadata_file.js | 2 +- .../lib/models/business_metadata_restapi.js | 2 +- api/app/lib/routes/businessMetadata/index.js | 17 ++ 6 files changed, 185 insertions(+), 4 deletions(-) create mode 100644 api/app/lib/controllers/businessMetadata/index.js create mode 100644 api/app/lib/routes/businessMetadata/index.js diff --git a/api/app/lib/controllers/businessMetadata/index.js b/api/app/lib/controllers/businessMetadata/index.js new file mode 100644 index 0000000..fda2d27 --- /dev/null +++ b/api/app/lib/controllers/businessMetadata/index.js @@ -0,0 +1,154 @@ +'use strict'; + +const moment = require("moment/moment"); + +//获取表业务元数据列表 +async function getBusinessMetadataDatabases(ctx) { + try { + const models = ctx.fs.dc.models; + const { catalog, limit, offset, keywords, orderBy = 'updateAt', orderDirection = 'desc', id = null } = ctx.query; + const where = { type: '表' }; + if (catalog) { + where.catalog = catalog; + } + if (id) { + where.parent = id; + } + if (keywords) { + where['$or'] = [{ name: { $iLike: `%${keywords}%` } }, + { code: { $iLike: `%${keywords}%` } }, + { type: { $iLike: `%${keywords}%` } }] + } + const findObj = { + include: [{ + model: models.BusinessMetadataDatabase, + include: [ + { + model: models.User, + attributes: ['id', 'name', 'username'], + }] + }], + attributes: { exclude: ['catalogKey', 'createAt', 'createBy', 'updateAt'] }, + where: where, + order: [[models.BusinessMetadataDatabase, orderBy, orderDirection + ' NULLS LAST']], + distinct: true + } + if (Number(limit) > 0 && Number(offset) >= 0) { + findObj.offset = Number(offset) * Number(limit); + findObj.limit = Number(limit); + } + const rslt = await models.MetadataDatabase.findAndCountAll(findObj); + let parentIds = rslt.rows.reduce((p, c) => p = p.concat(c.parent), []); + parentIds = [...new Set(parentIds)] + const parentData = await models.MetadataDatabase.findAll({ + where: { id: { $in: parentIds } }, + attributes: ['id', 'name'], + }); + rslt.rows.map(d => { + if (d.parent) { + let data = parentData.filter(p => + p.dataValues.id === d.parent); + if (data && data.length) { + d.dataValues.parentDataName = data[0].dataValues.name; + } + } + }) + ctx.status = 200; + ctx.body = rslt; + } catch (error) { + ctx.fs.logger.error(`path: ${ctx.path}, error: ${error}`); + ctx.status = 400; + ctx.body = { + "message": "获取业务元数据列表失败" + } + } +} + +//新建表业务元数据 +async function postBusinessMetadataDatabases(ctx) { + try { + const { resourceName, resourceAbstract, resourceProvider, resourceCategory, resourceId, metadataId, + createBy, metadataDatabaseId } = ctx.request.body; + const models = ctx.fs.dc.models; + if (!resourceName || !resourceAbstract || !resourceProvider || + !resourceCategory || !resourceId || !metadataId || + !createBy || !metadataDatabaseId) { + ctx.body = { message: '参数不全,请重新配置' } + ctx.status = 400; + } else { + await models.BusinessMetadataDatabase.create({ createAt: moment(), updateAt: moment(), ...ctx.request.body }); + ctx.body = { message: '新建业务元数据成功' } + ctx.status = 200; + } + + } catch (error) { + ctx.fs.logger.error(`path: ${ctx.path}, error: ${error}`); + ctx.status = 400; + ctx.body = { + "message": "新建业务元数据失败" + } + } +} + +//修改表业务元数据 +async function putBusinessMetadataDatabases(ctx) { + try { + const { id } = ctx.params; + const models = ctx.fs.dc.models; + let businessMetadataInfo = await models.BusinessMetadataDatabase.findOne({ where: { id } }); + if (businessMetadataInfo) { + const { resourceName, resourceAbstract, resourceProvider, resourceCategory, resourceId, metadataId } = ctx.request.body; + if (!resourceName || !resourceAbstract || !resourceProvider || + !resourceCategory || !resourceId || !metadataId) { + ctx.body = { message: '参数不全,请重新配置' } + ctx.status = 400; + } else { + await models.BusinessMetadataDatabase.update({ updateAt: moment(), ...ctx.request.body }, { where: { id: id } }); + ctx.status = 200; + ctx.body = { message: '修改业务元数据成功' } + } + } else { + ctx.status = 400; + ctx.body = { message: '该业务元数据不存在' } + } + } catch (error) { + ctx.fs.logger.error(`path: ${ctx.path}, error: ${error}`); + ctx.status = 400; + ctx.body = { + "message": "修改业务元数据失败" + } + } +} +//删除表业务元数据 +async function delBusinessMetadataDatabases(ctx) { + const transaction = await ctx.fs.dc.orm.transaction(); + try { + const models = ctx.fs.dc.models; + const { id } = ctx.params; + let businessMetadataInfo = await models.BusinessMetadataDatabase.findOne({ where: { id } }); + if (businessMetadataInfo) { + await models.BusinessMetadataDatabase.destroy({ + where: { id: id }, + transaction + }) + await transaction.commit(); + ctx.status = 200; + ctx.body = { message: '删除业务元数据成功' } + } else { + ctx.status = 400; + ctx.body = { message: '该业务元数据不存在' } + } + } catch (error) { + await transaction.rollback(); + ctx.fs.logger.error(`path: ${ctx.path}, error: ${error}`); + ctx.status = 400; + ctx.body = { message: '删除业务元数据失败' } + } +} + +module.exports = { + getBusinessMetadataDatabases, + postBusinessMetadataDatabases, + putBusinessMetadataDatabases, + delBusinessMetadataDatabases, +} \ No newline at end of file diff --git a/api/app/lib/index.js b/api/app/lib/index.js index 3b1e32b..58c567e 100644 --- a/api/app/lib/index.js +++ b/api/app/lib/index.js @@ -55,7 +55,8 @@ module.exports.models = function (dc) { }); const { - DataSource, AcquisitionTask, Adapter, User, MetadataDatabase, MetadataFile, MetadataRestapi, AcquisitionLog, ResourceCatalog + DataSource, AcquisitionTask, Adapter, User, MetadataDatabase, MetadataFile, MetadataRestapi, AcquisitionLog, ResourceCatalog, + BusinessMetadataDatabase, BusinessMetadataFile, BusinessMetadataRestapi } = dc.models; AcquisitionTask.belongsTo(DataSource, { foreignKey: 'dataSourceId', targetKey: 'id' }); @@ -72,7 +73,16 @@ module.exports.models = function (dc) { MetadataFile.belongsTo(User, { foreignKey: 'createBy', targetKey: 'id' }); MetadataRestapi.belongsTo(User, { foreignKey: 'createBy', targetKey: 'id' }); + BusinessMetadataDatabase.belongsTo(MetadataDatabase, { foreignKey: 'metadataDatabaseId', targetKey: 'id' }); + MetadataDatabase.hasMany(BusinessMetadataDatabase, { foreignKey: 'metadataDatabaseId', sourceKey: 'id' }); + BusinessMetadataFile.belongsTo(MetadataFile, { foreignKey: 'metadataFileId', targetKey: 'id' }); + MetadataFile.hasMany(BusinessMetadataFile, { foreignKey: 'metadataFileId', sourceKey: 'id' }); + BusinessMetadataRestapi.belongsTo(MetadataRestapi, { foreignKey: 'metadataRestapiId', targetKey: 'id' }); + MetadataRestapi.hasMany(BusinessMetadataRestapi, { foreignKey: 'metadataRestapiId', sourceKey: 'id' }); + BusinessMetadataDatabase.belongsTo(User, { foreignKey: 'createBy', targetKey: 'id' }); + BusinessMetadataFile.belongsTo(User, { foreignKey: 'createBy', targetKey: 'id' }); + BusinessMetadataRestapi.belongsTo(User, { foreignKey: 'createBy', targetKey: 'id' }); }; diff --git a/api/app/lib/models/business_metadata_database.js b/api/app/lib/models/business_metadata_database.js index aca199b..495402c 100644 --- a/api/app/lib/models/business_metadata_database.js +++ b/api/app/lib/models/business_metadata_database.js @@ -101,7 +101,7 @@ module.exports = dc => { field: "update_at", autoIncrement: false }, - metadataDatabase: { + metadataDatabaseId: { type: DataTypes.INTEGER, allowNull: false, defaultValue: null, diff --git a/api/app/lib/models/business_metadata_file.js b/api/app/lib/models/business_metadata_file.js index 350750a..a2bc4ea 100644 --- a/api/app/lib/models/business_metadata_file.js +++ b/api/app/lib/models/business_metadata_file.js @@ -101,7 +101,7 @@ module.exports = dc => { field: "update_at", autoIncrement: false }, - metadataFile: { + metadataFileId: { type: DataTypes.INTEGER, allowNull: false, defaultValue: null, diff --git a/api/app/lib/models/business_metadata_restapi.js b/api/app/lib/models/business_metadata_restapi.js index 749ebeb..8357c07 100644 --- a/api/app/lib/models/business_metadata_restapi.js +++ b/api/app/lib/models/business_metadata_restapi.js @@ -101,7 +101,7 @@ module.exports = dc => { field: "update_at", autoIncrement: false }, - metadataRestapi: { + metadataRestapiId: { type: DataTypes.INTEGER, allowNull: false, defaultValue: null, diff --git a/api/app/lib/routes/businessMetadata/index.js b/api/app/lib/routes/businessMetadata/index.js new file mode 100644 index 0000000..67c1fc3 --- /dev/null +++ b/api/app/lib/routes/businessMetadata/index.js @@ -0,0 +1,17 @@ +'use strict'; + +const businessMetadata = require('../../controllers/businessMetadata'); + +module.exports = function (app, router, opts) { + app.fs.api.logAttr['GET/business/metadata/databases'] = { content: '获取表业务元数据列表', visible: false }; + router.get('/business/metadata/databases', businessMetadata.getBusinessMetadataDatabases); + + app.fs.api.logAttr['POST/business/metadata/databases'] = { content: '新建表业务元数据', visible: true }; + router.post('/business/metadata/databases', businessMetadata.postBusinessMetadataDatabases); + + app.fs.api.logAttr['PUT/business/metadata/databases/:id'] = { content: '修改表业务元数据', visible: true }; + router.put('/business/metadata/databases/:id', businessMetadata.putBusinessMetadataDatabases); + + app.fs.api.logAttr['DEL/business/metadata/databases/:id'] = { content: '删除表业务元数据', visible: true }; + router.delete('/business/metadata/databases/:id', businessMetadata.delBusinessMetadataDatabases); +}; \ No newline at end of file