From 6d2cc6e87d609589a23ea8e95331bb64a1a9a88c Mon Sep 17 00:00:00 2001 From: 疯狂的狮子Li <15040126243@163.com> Date: 星期五, 21 六月 2024 14:59:50 +0800 Subject: [PATCH] update 优化 替换过期方法 --- ruoyi-modules/ruoyi-generator/src/main/java/org/dromara/generator/service/GenTableServiceImpl.java | 87 ++++++++++++++++++++++++------------------- 1 files changed, 49 insertions(+), 38 deletions(-) diff --git a/ruoyi-modules/ruoyi-generator/src/main/java/org/dromara/generator/service/GenTableServiceImpl.java b/ruoyi-modules/ruoyi-generator/src/main/java/org/dromara/generator/service/GenTableServiceImpl.java index 09ecb05..02a9fa7 100644 --- a/ruoyi-modules/ruoyi-generator/src/main/java/org/dromara/generator/service/GenTableServiceImpl.java +++ b/ruoyi-modules/ruoyi-generator/src/main/java/org/dromara/generator/service/GenTableServiceImpl.java @@ -5,13 +5,18 @@ import cn.hutool.core.lang.Dict; import cn.hutool.core.util.ObjectUtil; import com.baomidou.dynamic.datasource.annotation.DS; +import com.baomidou.dynamic.datasource.annotation.DSTransactional; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.core.incrementer.IdentifierGenerator; import com.baomidou.mybatisplus.core.toolkit.Wrappers; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; +import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; +import org.apache.velocity.Template; +import org.apache.velocity.VelocityContext; +import org.apache.velocity.app.Velocity; import org.dromara.common.core.constant.Constants; -import org.dromara.generator.constant.GenConstants; import org.dromara.common.core.exception.ServiceException; import org.dromara.common.core.utils.StreamUtils; import org.dromara.common.core.utils.StringUtils; @@ -20,6 +25,7 @@ import org.dromara.common.mybatis.core.page.PageQuery; import org.dromara.common.mybatis.core.page.TableDataInfo; import org.dromara.common.satoken.utils.LoginHelper; +import org.dromara.generator.constant.GenConstants; import org.dromara.generator.domain.GenTable; import org.dromara.generator.domain.GenTableColumn; import org.dromara.generator.mapper.GenTableColumnMapper; @@ -27,11 +33,6 @@ import org.dromara.generator.util.GenUtils; import org.dromara.generator.util.VelocityInitializer; import org.dromara.generator.util.VelocityUtils; -import lombok.RequiredArgsConstructor; -import lombok.extern.slf4j.Slf4j; -import org.apache.velocity.Template; -import org.apache.velocity.VelocityContext; -import org.apache.velocity.app.Velocity; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; @@ -40,7 +41,11 @@ import java.io.IOException; import java.io.StringWriter; import java.nio.charset.StandardCharsets; -import java.util.*; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; import java.util.zip.ZipEntry; import java.util.zip.ZipOutputStream; @@ -49,7 +54,6 @@ * * @author Lion Li */ -@DS("#header.datasource") @Slf4j @RequiredArgsConstructor @Service @@ -94,16 +98,19 @@ private QueryWrapper<GenTable> buildGenTableQueryWrapper(GenTable genTable) { Map<String, Object> params = genTable.getParams(); QueryWrapper<GenTable> wrapper = Wrappers.query(); - wrapper.like(StringUtils.isNotBlank(genTable.getTableName()), "lower(table_name)", StringUtils.lowerCase(genTable.getTableName())) + wrapper + .eq(StringUtils.isNotEmpty(genTable.getDataName()),"data_name", genTable.getDataName()) + .like(StringUtils.isNotBlank(genTable.getTableName()), "lower(table_name)", StringUtils.lowerCase(genTable.getTableName())) .like(StringUtils.isNotBlank(genTable.getTableComment()), "lower(table_comment)", StringUtils.lowerCase(genTable.getTableComment())) .between(params.get("beginTime") != null && params.get("endTime") != null, "create_time", params.get("beginTime"), params.get("endTime")); return wrapper; } - + @DS("#genTable.dataName") @Override public TableDataInfo<GenTable> selectPageDbTableList(GenTable genTable, PageQuery pageQuery) { + genTable.getParams().put("genTableNames",baseMapper.selectTableNameList(genTable.getDataName())); Page<GenTable> page = baseMapper.selectPageDbTableList(pageQuery.build(), genTable); return TableDataInfo.build(page); } @@ -112,10 +119,12 @@ * 鏌ヨ鎹簱鍒楄〃 * * @param tableNames 琛ㄥ悕绉扮粍 + * @param dataName 鏁版嵁婧愬悕绉� * @return 鏁版嵁搴撹〃闆嗗悎 */ + @DS("#dataName") @Override - public List<GenTable> selectDbTableListByNames(String[] tableNames) { + public List<GenTable> selectDbTableListByNames(String[] tableNames, String dataName) { return baseMapper.selectDbTableListByNames(tableNames); } @@ -133,7 +142,6 @@ * 淇敼涓氬姟 * * @param genTable 涓氬姟淇℃伅 - * @return 缁撴灉 */ @Transactional(rollbackFor = Exception.class) @Override @@ -152,13 +160,12 @@ * 鍒犻櫎涓氬姟瀵硅薄 * * @param tableIds 闇�瑕佸垹闄ょ殑鏁版嵁ID - * @return 缁撴灉 */ @Transactional(rollbackFor = Exception.class) @Override public void deleteGenTableByIds(Long[] tableIds) { List<Long> ids = Arrays.asList(tableIds); - baseMapper.deleteBatchIds(ids); + baseMapper.deleteByIds(ids); genTableColumnMapper.delete(new LambdaQueryWrapper<GenTableColumn>().in(GenTableColumn::getTableId, ids)); } @@ -166,19 +173,21 @@ * 瀵煎叆琛ㄧ粨鏋� * * @param tableList 瀵煎叆琛ㄥ垪琛� + * @param dataName 鏁版嵁婧愬悕绉� */ - @Transactional(rollbackFor = Exception.class) + @DSTransactional @Override - public void importGenTable(List<GenTable> tableList) { - String operName = LoginHelper.getUsername(); + public void importGenTable(List<GenTable> tableList, String dataName) { + Long operId = LoginHelper.getUserId(); try { for (GenTable table : tableList) { String tableName = table.getTableName(); - GenUtils.initTable(table, operName); + GenUtils.initTable(table, operId); + table.setDataName(dataName); int row = baseMapper.insert(table); if (row > 0) { // 淇濆瓨鍒椾俊鎭� - List<GenTableColumn> genTableColumns = genTableColumnMapper.selectDbTableColumnsByName(tableName); + List<GenTableColumn> genTableColumns = genTableColumnMapper.selectDbTableColumnsByName(tableName, dataName); List<GenTableColumn> saveColumns = new ArrayList<>(); for (GenTableColumn column : genTableColumns) { GenUtils.initColumnField(column, table); @@ -231,14 +240,14 @@ /** * 鐢熸垚浠g爜锛堜笅杞芥柟寮忥級 * - * @param tableName 琛ㄥ悕绉� + * @param tableId 琛ㄥ悕绉� * @return 鏁版嵁 */ @Override - public byte[] downloadCode(String tableName) { + public byte[] downloadCode(Long tableId) { ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); ZipOutputStream zip = new ZipOutputStream(outputStream); - generatorCode(tableName, zip); + generatorCode(tableId, zip); IoUtil.close(zip); return outputStream.toByteArray(); } @@ -246,12 +255,12 @@ /** * 鐢熸垚浠g爜锛堣嚜瀹氫箟璺緞锛� * - * @param tableName 琛ㄥ悕绉� + * @param tableId 琛ㄥ悕绉� */ @Override - public void generatorCode(String tableName) { + public void generatorCode(Long tableId) { // 鏌ヨ琛ㄤ俊鎭� - GenTable table = baseMapper.selectGenTableByName(tableName); + GenTable table = baseMapper.selectGenTableById(tableId); // 璁剧疆涓婚敭鍒椾俊鎭� setPkColumn(table); @@ -262,7 +271,7 @@ // 鑾峰彇妯℃澘鍒楄〃 List<String> templates = VelocityUtils.getTemplateList(table.getTplCategory()); for (String template : templates) { - if (!StringUtils.containsAny(template, "sql.vm", "api.js.vm", "index.vue.vm", "index-tree.vue.vm")) { + if (!StringUtils.containsAny(template, "sql.vm", "api.ts.vm", "types.ts.vm", "index.vue.vm", "index-tree.vue.vm")) { // 娓叉煋妯℃澘 StringWriter sw = new StringWriter(); Template tpl = Velocity.getTemplate(template, Constants.UTF8); @@ -280,16 +289,16 @@ /** * 鍚屾鏁版嵁搴� * - * @param tableName 琛ㄥ悕绉� + * @param tableId 琛ㄥ悕绉� */ - @Transactional(rollbackFor = Exception.class) + @DSTransactional @Override - public void synchDb(String tableName) { - GenTable table = baseMapper.selectGenTableByName(tableName); + public void synchDb(Long tableId) { + GenTable table = baseMapper.selectGenTableById(tableId); List<GenTableColumn> tableColumns = table.getColumns(); Map<String, GenTableColumn> tableColumnMap = StreamUtils.toIdentityMap(tableColumns, GenTableColumn::getColumnName); - List<GenTableColumn> dbTableColumns = genTableColumnMapper.selectDbTableColumnsByName(tableName); + List<GenTableColumn> dbTableColumns = genTableColumnMapper.selectDbTableColumnsByName(table.getTableName(), table.getDataName()); if (CollUtil.isEmpty(dbTableColumns)) { throw new ServiceException("鍚屾鏁版嵁澶辫触锛屽師琛ㄧ粨鏋勪笉瀛樺湪"); } @@ -322,22 +331,24 @@ List<GenTableColumn> delColumns = StreamUtils.filter(tableColumns, column -> !dbTableColumnNames.contains(column.getColumnName())); if (CollUtil.isNotEmpty(delColumns)) { List<Long> ids = StreamUtils.toList(delColumns, GenTableColumn::getColumnId); - genTableColumnMapper.deleteBatchIds(ids); + if (CollUtil.isNotEmpty(ids)) { + genTableColumnMapper.deleteByIds(ids); + } } } /** * 鎵归噺鐢熸垚浠g爜锛堜笅杞芥柟寮忥級 * - * @param tableNames 琛ㄦ暟缁� + * @param tableIds 琛↖D鏁扮粍 * @return 鏁版嵁 */ @Override - public byte[] downloadCode(String[] tableNames) { + public byte[] downloadCode(String[] tableIds) { ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); ZipOutputStream zip = new ZipOutputStream(outputStream); - for (String tableName : tableNames) { - generatorCode(tableName, zip); + for (String tableId : tableIds) { + generatorCode(Long.parseLong(tableId), zip); } IoUtil.close(zip); return outputStream.toByteArray(); @@ -346,9 +357,9 @@ /** * 鏌ヨ琛ㄤ俊鎭苟鐢熸垚浠g爜 */ - private void generatorCode(String tableName, ZipOutputStream zip) { + private void generatorCode(Long tableId, ZipOutputStream zip) { // 鏌ヨ琛ㄤ俊鎭� - GenTable table = baseMapper.selectGenTableByName(tableName); + GenTable table = baseMapper.selectGenTableById(tableId); List<Long> menuIds = new ArrayList<>(); for (int i = 0; i < 6; i++) { menuIds.add(identifierGenerator.nextId(null).longValue()); -- Gitblit v1.9.3