疯狂的狮子li
2021-10-15 a6fb88d74c20cc28043d75e8a5097fce49cf9a78
ruoyi-generator/src/main/java/com/ruoyi/generator/service/GenTableServiceImpl.java
@@ -3,7 +3,6 @@
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.convert.Convert;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.constant.GenConstants;
import com.ruoyi.common.core.mybatisplus.core.ServicePlusImpl;
@@ -42,7 +41,7 @@
/**
 * 业务 服务层实现
 *
 * @author ruoyi
 * @author Lion Li
 */
@Slf4j
@Service
@@ -131,18 +130,7 @@
        int row = baseMapper.updateById(genTable);
        if (row > 0) {
            for (GenTableColumn cenTableColumn : genTable.getColumns()) {
                genTableColumnMapper.update(cenTableColumn,
                        new LambdaUpdateWrapper<GenTableColumn>()
                     .set(StringUtils.isBlank(cenTableColumn.getColumnComment()), GenTableColumn::getColumnComment, null)
                     .set(StringUtils.isBlank(cenTableColumn.getIsPk()), GenTableColumn::getIsPk, null)
                     .set(StringUtils.isBlank(cenTableColumn.getIsIncrement()), GenTableColumn::getIsIncrement, null)
                     .set(StringUtils.isBlank(cenTableColumn.getIsInsert()), GenTableColumn::getIsInsert, null)
                     .set(StringUtils.isBlank(cenTableColumn.getIsEdit()), GenTableColumn::getIsEdit, null)
                     .set(StringUtils.isBlank(cenTableColumn.getIsList()), GenTableColumn::getIsList, null)
                     .set(StringUtils.isBlank(cenTableColumn.getIsQuery()), GenTableColumn::getIsQuery, null)
                     .set(StringUtils.isBlank(cenTableColumn.getIsRequired()), GenTableColumn::getIsRequired, null)
                     .set(StringUtils.isBlank(cenTableColumn.getDictType()), GenTableColumn::getDictType, "")
                     .eq(GenTableColumn::getColumnId,cenTableColumn.getColumnId()));
                genTableColumnMapper.updateById(cenTableColumn);
            }
        }
    }
@@ -175,18 +163,18 @@
                String tableName = table.getTableName();
                GenUtils.initTable(table, operName);
                int row = baseMapper.insert(table);
            if (row > 0) {
               // 保存列信息
               List<GenTableColumn> genTableColumns = genTableColumnMapper.selectDbTableColumnsByName(tableName);
               List<GenTableColumn> saveColumns = new ArrayList<>();
               for (GenTableColumn column : genTableColumns) {
                  GenUtils.initColumnField(column, table);
                  saveColumns.add(column);
               }
               if (CollUtil.isNotEmpty(saveColumns)) {
                  genTableColumnMapper.insertAll(saveColumns);
               }
            }
                if (row > 0) {
                    // 保存列信息
                    List<GenTableColumn> genTableColumns = genTableColumnMapper.selectDbTableColumnsByName(tableName);
                    List<GenTableColumn> saveColumns = new ArrayList<>();
                    for (GenTableColumn column : genTableColumns) {
                        GenUtils.initColumnField(column, table);
                        saveColumns.add(column);
                    }
                    if (CollUtil.isNotEmpty(saveColumns)) {
                        genTableColumnMapper.insertAll(saveColumns);
                    }
                }
            }
        } catch (Exception e) {
            throw new ServiceException("导入失败:" + e.getMessage());
@@ -265,12 +253,12 @@
                StringWriter sw = new StringWriter();
                Template tpl = Velocity.getTemplate(template, Constants.UTF8);
                tpl.merge(context, sw);
            try {
                   String path = getGenPath(table, template);
                   FileUtils.writeUtf8String(sw.toString(), path);
            } catch (Exception e) {
               throw new ServiceException("渲染模板失败,表名:" + table.getTableName());
            }
                try {
                    String path = getGenPath(table, template);
                    FileUtils.writeUtf8String(sw.toString(), path);
                } catch (Exception e) {
                    throw new ServiceException("渲染模板失败,表名:" + table.getTableName());
                }
            }
        }
    }
@@ -293,16 +281,16 @@
        }
        List<String> dbTableColumnNames = dbTableColumns.stream().map(GenTableColumn::getColumnName).collect(Collectors.toList());
      List<GenTableColumn> saveColumns = new ArrayList<>();
      dbTableColumns.forEach(column -> {
         if (!tableColumnNames.contains(column.getColumnName())) {
            GenUtils.initColumnField(column, table);
            saveColumns.add(column);
         }
      });
      if (CollUtil.isNotEmpty(saveColumns)) {
         genTableColumnMapper.insertAll(saveColumns);
      }
        List<GenTableColumn> saveColumns = new ArrayList<>();
        dbTableColumns.forEach(column -> {
            if (!tableColumnNames.contains(column.getColumnName())) {
                GenUtils.initColumnField(column, table);
                saveColumns.add(column);
            }
        });
        if (CollUtil.isNotEmpty(saveColumns)) {
            genTableColumnMapper.insertAll(saveColumns);
        }
        List<GenTableColumn> delColumns = tableColumns.stream().filter(column -> !dbTableColumnNames.contains(column.getColumnName())).collect(Collectors.toList());
        if (CollUtil.isNotEmpty(delColumns)) {
@@ -371,7 +359,7 @@
    @Override
    public void validateEdit(GenTable genTable) {
        if (GenConstants.TPL_TREE.equals(genTable.getTplCategory())) {
         Map<String, Object> paramsObj = genTable.getParams();
            Map<String, Object> paramsObj = genTable.getParams();
            if (StringUtils.isEmpty(paramsObj.get(GenConstants.TREE_CODE))) {
                throw new ServiceException("树编码字段不能为空");
            } else if (StringUtils.isEmpty(paramsObj.get(GenConstants.TREE_PARENT_CODE))) {
@@ -434,7 +422,7 @@
     * @param genTable 设置后的生成对象
     */
    public void setTableFromOptions(GenTable genTable) {
      Map<String, Object> paramsObj = JsonUtils.parseMap(genTable.getOptions());
        Map<String, Object> paramsObj = JsonUtils.parseMap(genTable.getOptions());
        if (StringUtils.isNotNull(paramsObj)) {
            String treeCode = Convert.toStr(paramsObj.get(GenConstants.TREE_CODE));
            String treeParentCode = Convert.toStr(paramsObj.get(GenConstants.TREE_PARENT_CODE));
@@ -453,7 +441,7 @@
    /**
     * 获取代码生成地址
     *
     * @param table 业务表信息
     * @param table    业务表信息
     * @param template 模板文件路径
     * @return 生成地址
     */