diff --git a/api/app/lib/controllers/metadataSearch/index.js b/api/app/lib/controllers/metadataSearch/index.js index 9b2b205..a8b5a74 100644 --- a/api/app/lib/controllers/metadataSearch/index.js +++ b/api/app/lib/controllers/metadataSearch/index.js @@ -74,22 +74,27 @@ function getTableData(opts) { const models = ctx.fs.dc.models; try { const { id } = ctx.query; - let { user, host, database, password, port } = ctx.request.body; - const pool = new Pool({ - user: user, - host: host, - database: database, - password: password, - port: port, - }) - const client = await pool.connect() + const metaData = await models.MetadataDatabase.findOne({ where: { id: id } }) + let rslt = [], metaDataChildren = [] + if (metaData && metaData.datasourceConfig) { + metaDataChildren = await models.MetadataDatabase.findAll({ where: { parent: id, type: '字段' } }) + let { user, host, database, password, port } = metaData.datasourceConfig; + const pool = new Pool({ + user: user, + host: host, + database: database, + password: password, + port: port, + }) + const client = await pool.connect() - const tableName = "user" - const ress = await client.query(`SELECT * from "${tableName}"`, []) - console.log(ress.rows) + const tableName = metaData.name; + const ress = await client.query(`SELECT * from "${tableName}"`, []) + rslt = ress.rows + } ctx.status = 200; - ctx.body = { rslt: ress.rows } + ctx.body = { rslt: rslt, metaDataChildren: metaDataChildren.map(s => s.code) } } catch (error) { ctx.fs.logger.error(`path: ${ctx.path}, error: ${error}`); ctx.status = 200; diff --git a/web/client/src/sections/resourceRetrieval/containers/retrieval.js b/web/client/src/sections/resourceRetrieval/containers/retrieval.js index 63011ec..b80e07f 100644 --- a/web/client/src/sections/resourceRetrieval/containers/retrieval.js +++ b/web/client/src/sections/resourceRetrieval/containers/retrieval.js @@ -5,6 +5,7 @@ import { useFsRequest, ApiTable } from '$utils'; import { connect } from 'react-redux'; import { downloadImg, markRedKeywords } from '../utils/index' import KeyModal from '../components/keyModal'; +import xlsx from 'xlsx'; const METADTA_TYPE = { '库': 'databases', '表': 'databases', @@ -26,6 +27,8 @@ function Retrieval(props) { const [firstInput, setFirstInput] = useState() const [page, setPage] = useState(1) const [currentData, setCurrentData] = useState(null) + const [searchDataId, setSearchDataId] = useState(null) + const formRef = React.createRef(); // const { data: catalogs = [] } = useFsRequest({ // url: ApiTable.getResourceCatalog, @@ -54,6 +57,58 @@ function Retrieval(props) { ready: !!(user?.id) }); + //检索元数据 + const { data: tableData = {} } = useFsRequest({ + url: 'meta/table/data', + query: { + id: searchDataId + }, + refreshDeps: [searchDataId], + ready: !!searchDataId + }); + + useEffect(() => { + if (tableData?.rslt && tableData?.metaDataChildren) handleTableExport() + }, [tableData]) + + const handleTableExport = () => { + const metaData = result?.rows?.find(s => s.id == searchDataId) + if (!metaData) return; + let excelTitle = tableData?.metaDataChildren.map(s => { + return { k: s, v: s } + }); + let workBook = { + SheetNames: [], //sheet名称 + Sheets: {} //根据SheetNames名称顺序依次添加每个sheet数据 + }; + if (tableData?.rslt && tableData?.rslt?.length > 0) { + let sheetName = '元数据列表'; + let sheetDataMap = new Map(); + let sheetData = [excelTitle]; + let index = 1; + tableData?.rslt.map(data => { + const arr = [] + tableData?.metaDataChildren.map(s => { + arr.push( + { k: s, v: JSON.stringify(data[s]) }, + ); + }); + sheetData.push(arr) + index = index + 1; + }) + + sheetDataMap.set(sheetName, sheetData); + sheetDataMap.forEach((values, key) => { + // 写入excel + workBook.Sheets[key] = xlsx.utils.aoa_to_sheet(values); + workBook.Sheets[key]['!cols'] = [{ wpx: 50 }, { wpx: 150 }, { wpx: 180 }, { wpx: 230 }, { wpx: 230 }, { wpx: 230 }] + }) + workBook.SheetNames = [sheetName]; + } + // 将workBook写入文件 + xlsx.writeFile(workBook, `${metaData?.name}-data.xlsx`); + } + const renderIcon = (type) => { switch (type) { case '库': @@ -106,7 +161,13 @@ function Retrieval(props) { window.open('/assets/files/common/' + s.fileName) } } else { - alert('库表下载待开发') + if (s.datasourceConfig) { + setSearchDataId(s.id) + if (tableData?.rslt && tableData?.metaDataChildren) handleTableExport() + } else { + message.warning('数据源信息读取失败,无法下载元数据!') + } + } }