| | |
| | | import cn.hutool.core.collection.CollUtil; |
| | | import cn.hutool.core.io.IoUtil; |
| | | import cn.hutool.core.lang.Dict; |
| | | import cn.hutool.core.util.ArrayUtil; |
| | | import cn.hutool.core.util.ObjectUtil; |
| | | import com.baomidou.dynamic.datasource.annotation.DS; |
| | | import com.baomidou.dynamic.datasource.annotation.DSTransactional; |
| | |
| | | import com.baomidou.mybatisplus.extension.plugins.pagination.Page; |
| | | import lombok.RequiredArgsConstructor; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.anyline.metadata.Column; |
| | | import org.anyline.metadata.Table; |
| | | import org.anyline.proxy.ServiceProxy; |
| | | import org.apache.velocity.Template; |
| | | import org.apache.velocity.VelocityContext; |
| | |
| | | Integer pageNum = pageQuery.getPageNum(); |
| | | Integer pageSize = pageQuery.getPageSize(); |
| | | |
| | | LinkedHashMap<String, Table> tablesMap = ServiceProxy.metadata().tables(); |
| | | if (CollUtil.isEmpty(tablesMap)) { |
| | | return TableDataInfo.build(); |
| | | } |
| | | // 过滤并转换表格数据 |
| | | List<GenTable> tables = ServiceProxy.metadata().tables().values().stream() |
| | | List<GenTable> tables = tablesMap.values().stream() |
| | | .filter(x -> { |
| | | boolean nameMatches = true; |
| | | boolean commentMatches = true; |
| | |
| | | @DS("#dataName") |
| | | @Override |
| | | public List<GenTable> selectDbTableListByNames(String[] tableNames, String dataName) { |
| | | return baseMapper.selectDbTableListByNames(tableNames); |
| | | Set<String> tableNameSet = new HashSet<>(List.of(tableNames)); |
| | | LinkedHashMap<String, Table> tablesMap = ServiceProxy.metadata().tables(); |
| | | |
| | | if (CollUtil.isEmpty(tablesMap)) { |
| | | return new ArrayList<>(); |
| | | } |
| | | |
| | | List<Table> tableList = tablesMap.values().stream() |
| | | .filter(x -> tableNameSet.contains(x.getName())).toList(); |
| | | |
| | | if (ArrayUtil.isEmpty(tableList)) { |
| | | return new ArrayList<>(); |
| | | } |
| | | return tableList.stream().map(x -> { |
| | | GenTable gen = new GenTable(); |
| | | gen.setDataName(dataName); |
| | | gen.setTableName(x.getName()); |
| | | gen.setTableComment(x.getComment()); |
| | | gen.setCreateTime(x.getCreateTime()); |
| | | gen.setUpdateTime(x.getUpdateTime()); |
| | | return gen; |
| | | }).toList(); |
| | | } |
| | | |
| | | /** |
| | |
| | | int row = baseMapper.insert(table); |
| | | if (row > 0) { |
| | | // 保存列信息 |
| | | List<GenTableColumn> genTableColumns = genTableColumnMapper.selectDbTableColumnsByName(tableName, dataName); |
| | | List<GenTableColumn> genTableColumns = selectDbTableColumnsByName(tableName, dataName); |
| | | List<GenTableColumn> saveColumns = new ArrayList<>(); |
| | | for (GenTableColumn column : genTableColumns) { |
| | | GenUtils.initColumnField(column, table); |
| | |
| | | } catch (Exception e) { |
| | | throw new ServiceException("导入失败:" + e.getMessage()); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 根据表名称查询列信息 |
| | | * |
| | | * @param tableName 表名称 |
| | | * @param dataName 数据源名称 |
| | | * @return 列信息 |
| | | */ |
| | | @DS("#dataName") |
| | | private List<GenTableColumn> selectDbTableColumnsByName(String tableName, String dataName) { |
| | | LinkedHashMap<String, Column> columns = ServiceProxy.service().metadata().table(tableName).getColumns(); |
| | | List<GenTableColumn> tableColumns = new ArrayList<>(); |
| | | columns.forEach((columnName, column) -> { |
| | | GenTableColumn tableColumn = new GenTableColumn(); |
| | | tableColumn.setIsPk(String.valueOf(column.isPrimaryKey())); |
| | | tableColumn.setColumnName(column.getName()); |
| | | tableColumn.setColumnComment(column.getComment()); |
| | | tableColumn.setColumnType(column.getTypeName().toLowerCase()); |
| | | tableColumn.setSort(column.getPosition()); |
| | | tableColumn.setIsRequired("0"); |
| | | tableColumn.setIsIncrement("0"); |
| | | tableColumns.add(tableColumn); |
| | | }); |
| | | return tableColumns; |
| | | } |
| | | |
| | | /** |
| | |
| | | List<GenTableColumn> tableColumns = table.getColumns(); |
| | | Map<String, GenTableColumn> tableColumnMap = StreamUtils.toIdentityMap(tableColumns, GenTableColumn::getColumnName); |
| | | |
| | | List<GenTableColumn> dbTableColumns = genTableColumnMapper.selectDbTableColumnsByName(table.getTableName(), table.getDataName()); |
| | | List<GenTableColumn> dbTableColumns = selectDbTableColumnsByName(table.getTableName(), table.getDataName()); |
| | | if (CollUtil.isEmpty(dbTableColumns)) { |
| | | throw new ServiceException("同步数据失败,原表结构不存在"); |
| | | } |