| | |
| | | import cn.hutool.core.collection.CollUtil; |
| | | import cn.hutool.core.io.IoUtil; |
| | | import cn.hutool.core.lang.Dict; |
| | | import cn.hutool.core.lang.Snowflake; |
| | | import cn.hutool.core.util.IdUtil; |
| | | import cn.hutool.core.util.ObjectUtil; |
| | | import com.baomidou.dynamic.datasource.annotation.DS; |
| | | import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; |
| | |
| | | import com.ruoyi.common.exception.ServiceException; |
| | | import com.ruoyi.common.helper.LoginHelper; |
| | | import com.ruoyi.common.utils.JsonUtils; |
| | | import com.ruoyi.common.utils.StreamUtils; |
| | | import com.ruoyi.common.utils.StringUtils; |
| | | import com.ruoyi.common.utils.file.FileUtils; |
| | | import com.ruoyi.generator.domain.GenTable; |
| | |
| | | import org.apache.velocity.VelocityContext; |
| | | import org.apache.velocity.app.Velocity; |
| | | import org.springframework.stereotype.Service; |
| | | import org.springframework.transaction.annotation.Transactional; |
| | | |
| | | import java.io.ByteArrayOutputStream; |
| | | import java.io.File; |
| | |
| | | import java.io.StringWriter; |
| | | import java.nio.charset.StandardCharsets; |
| | | import java.util.*; |
| | | import java.util.function.Function; |
| | | import java.util.stream.Collectors; |
| | | import java.util.zip.ZipEntry; |
| | | import java.util.zip.ZipOutputStream; |
| | | |
| | |
| | | return TableDataInfo.build(page); |
| | | } |
| | | |
| | | /** |
| | | * 查询业务列表 |
| | | * |
| | | * @param genTable 业务信息 |
| | | * @return 业务集合 |
| | | */ |
| | | @Override |
| | | public List<GenTable> selectGenTableList(GenTable genTable) { |
| | | return baseMapper.selectList(this.buildGenTableQueryWrapper(genTable)); |
| | | } |
| | | |
| | | private QueryWrapper<GenTable> buildGenTableQueryWrapper(GenTable genTable) { |
| | | Map<String, Object> params = genTable.getParams(); |
| | | QueryWrapper<GenTable> wrapper = Wrappers.query(); |
| | |
| | | public TableDataInfo<GenTable> selectPageDbTableList(GenTable genTable, PageQuery pageQuery) { |
| | | Page<GenTable> page = baseMapper.selectPageDbTableList(pageQuery.build(), genTable); |
| | | return TableDataInfo.build(page); |
| | | } |
| | | |
| | | /** |
| | | * 查询据库列表 |
| | | * |
| | | * @param genTable 业务信息 |
| | | * @return 数据库表集合 |
| | | */ |
| | | @Override |
| | | public List<GenTable> selectDbTableList(GenTable genTable) { |
| | | return baseMapper.selectDbTableList(genTable); |
| | | } |
| | | |
| | | /** |
| | |
| | | * @param genTable 业务信息 |
| | | * @return 结果 |
| | | */ |
| | | @Transactional(rollbackFor = Exception.class) |
| | | @Override |
| | | public void updateGenTable(GenTable genTable) { |
| | | String options = JsonUtils.toJsonString(genTable.getParams()); |
| | |
| | | * @param tableIds 需要删除的数据ID |
| | | * @return 结果 |
| | | */ |
| | | @Transactional(rollbackFor = Exception.class) |
| | | @Override |
| | | public void deleteGenTableByIds(Long[] tableIds) { |
| | | List<Long> ids = Arrays.asList(tableIds); |
| | |
| | | * |
| | | * @param tableList 导入表列表 |
| | | */ |
| | | @Transactional(rollbackFor = Exception.class) |
| | | @Override |
| | | public void importGenTable(List<GenTable> tableList) { |
| | | String operName = LoginHelper.getUsername(); |
| | |
| | | Map<String, String> dataMap = new LinkedHashMap<>(); |
| | | // 查询表信息 |
| | | GenTable table = baseMapper.selectGenTableById(tableId); |
| | | Snowflake snowflake = IdUtil.getSnowflake(); |
| | | List<Long> menuIds = new ArrayList<>(); |
| | | for (int i = 0; i < 6; i++) { |
| | | menuIds.add(snowflake.nextId()); |
| | | } |
| | | table.setMenuIds(menuIds); |
| | | // 设置主子表信息 |
| | | setSubTable(table); |
| | | // 设置主键列信息 |
| | |
| | | * |
| | | * @param tableName 表名称 |
| | | */ |
| | | @Transactional(rollbackFor = Exception.class) |
| | | @Override |
| | | public void synchDb(String tableName) { |
| | | GenTable table = baseMapper.selectGenTableByName(tableName); |
| | | List<GenTableColumn> tableColumns = table.getColumns(); |
| | | Map<String, GenTableColumn> tableColumnMap = tableColumns.stream().collect(Collectors.toMap(GenTableColumn::getColumnName, Function.identity())); |
| | | Map<String, GenTableColumn> tableColumnMap = StreamUtils.toIdentityMap(tableColumns, GenTableColumn::getColumnName); |
| | | |
| | | List<GenTableColumn> dbTableColumns = genTableColumnMapper.selectDbTableColumnsByName(tableName); |
| | | if (CollUtil.isEmpty(dbTableColumns)) { |
| | | throw new ServiceException("同步数据失败,原表结构不存在"); |
| | | } |
| | | List<String> dbTableColumnNames = dbTableColumns.stream().map(GenTableColumn::getColumnName).collect(Collectors.toList()); |
| | | List<String> dbTableColumnNames = StreamUtils.toList(dbTableColumns, GenTableColumn::getColumnName); |
| | | |
| | | List<GenTableColumn> saveColumns = new ArrayList<>(); |
| | | dbTableColumns.forEach(column -> { |
| | |
| | | if (CollUtil.isNotEmpty(saveColumns)) { |
| | | genTableColumnMapper.insertBatch(saveColumns); |
| | | } |
| | | |
| | | List<GenTableColumn> delColumns = tableColumns.stream().filter(column -> !dbTableColumnNames.contains(column.getColumnName())).collect(Collectors.toList()); |
| | | List<GenTableColumn> delColumns = StreamUtils.filter(tableColumns, column -> !dbTableColumnNames.contains(column.getColumnName())); |
| | | if (CollUtil.isNotEmpty(delColumns)) { |
| | | List<Long> ids = delColumns.stream().map(GenTableColumn::getColumnId).collect(Collectors.toList()); |
| | | List<Long> ids = StreamUtils.toList(delColumns, GenTableColumn::getColumnId); |
| | | genTableColumnMapper.deleteBatchIds(ids); |
| | | } |
| | | } |
| | |
| | | private void generatorCode(String tableName, ZipOutputStream zip) { |
| | | // 查询表信息 |
| | | GenTable table = baseMapper.selectGenTableByName(tableName); |
| | | Snowflake snowflake = IdUtil.getSnowflake(); |
| | | List<Long> menuIds = new ArrayList<>(); |
| | | for (int i = 0; i < 6; i++) { |
| | | menuIds.add(snowflake.nextId()); |
| | | } |
| | | table.setMenuIds(menuIds); |
| | | // 设置主子表信息 |
| | | setSubTable(table); |
| | | // 设置主键列信息 |