'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' } = ctx.query; const where = { type: '表' }; if (catalog) { where.catalog = catalog; } if (keywords) { where['$or'] = [{ name: { $iLike: `%${keywords}%` } }, { code: { $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: '删除业务元数据失败' } } } //获取文件业务元数据列表 async function getBusinessMetadataFiles(ctx) { try { const models = ctx.fs.dc.models; const { catalog, limit, offset, keywords, orderBy = 'updateAt', orderDirection = 'desc' } = ctx.query; const where = {}; if (catalog) { where.catalog = catalog; } if (keywords) { where.name = { $iLike: `%${keywords}%` }; } const findObj = { include: [{ model: models.BusinessMetadataFile, include: [ { model: models.User, attributes: ['id', 'name', 'username'], }] }], attributes: { exclude: ['catalogKey', 'createAt', 'createBy', 'updateAt'] }, where: where, order: [[models.BusinessMetadataFile, 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.MetadataFile.findAndCountAll(findObj); 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 postBusinessMetadataFiles(ctx) { try { const { resourceName, resourceAbstract, resourceProvider, resourceCategory, resourceId, metadataId, createBy, metadataFileId } = ctx.request.body; const models = ctx.fs.dc.models; if (!resourceName || !resourceAbstract || !resourceProvider || !resourceCategory || !resourceId || !metadataId || !createBy || !metadataFileId) { ctx.body = { message: '参数不全,请重新配置' } ctx.status = 400; } else { await models.BusinessMetadataFile.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 putBusinessMetadataFiles(ctx) { try { const { id } = ctx.params; const models = ctx.fs.dc.models; let businessMetadataInfo = await models.BusinessMetadataFile.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.BusinessMetadataFile.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 delBusinessMetadataFiles(ctx) { const transaction = await ctx.fs.dc.orm.transaction(); try { const models = ctx.fs.dc.models; const { id } = ctx.params; let businessMetadataInfo = await models.BusinessMetadataFile.findOne({ where: { id } }); if (businessMetadataInfo) { await models.BusinessMetadataFile.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: '删除业务元数据失败' } } } //获取接口业务元数据列表 async function getBusinessMetadataRestapis(ctx) { try { const models = ctx.fs.dc.models; const { catalog, limit, offset, keywords, orderBy = 'updateAt', orderDirection = 'desc' } = ctx.query; const where = {}; if (catalog) { where.catalog = catalog; } if (keywords) { where.name = { $iLike: `%${keywords}%` }; } const findObj = { include: [{ model: models.BusinessMetadataRestapi, include: [ { model: models.User, attributes: ['id', 'name', 'username'], }] }], attributes: { exclude: ['catalogKey', 'createAt', 'createBy', 'updateAt'] }, where: where, order: [[models.BusinessMetadataRestapi, 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.MetadataRestapi.findAndCountAll(findObj); 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 postBusinessMetadataRestapis(ctx) { try { const { resourceName, resourceAbstract, resourceProvider, resourceCategory, resourceId, metadataId, createBy, metadataRestapiId } = ctx.request.body; const models = ctx.fs.dc.models; if (!resourceName || !resourceAbstract || !resourceProvider || !resourceCategory || !resourceId || !metadataId || !createBy || !metadataRestapiId) { ctx.body = { message: '参数不全,请重新配置' } ctx.status = 400; } else { await models.BusinessMetadataRestapi.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 putBusinessMetadataRestapis(ctx) { try { const { id } = ctx.params; const models = ctx.fs.dc.models; let businessMetadataInfo = await models.BusinessMetadataRestapi.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.BusinessMetadataRestapi.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 delBusinessMetadataRestapis(ctx) { const transaction = await ctx.fs.dc.orm.transaction(); try { const models = ctx.fs.dc.models; const { id } = ctx.params; let businessMetadataInfo = await models.BusinessMetadataRestapi.findOne({ where: { id } }); if (businessMetadataInfo) { await models.BusinessMetadataRestapi.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, getBusinessMetadataFiles, postBusinessMetadataFiles, putBusinessMetadataFiles, delBusinessMetadataFiles, getBusinessMetadataRestapis, postBusinessMetadataRestapis, putBusinessMetadataRestapis, delBusinessMetadataRestapis, }