Преглед изворни кода

代码生成时,如果是管理后台,必须设置菜单

YunaiV пре 3 година
родитељ
комит
6f8baa3110

+ 11 - 0
yudao-module-infra/yudao-module-infra-impl/src/main/java/cn/iocoder/yudao/module/infra/controller/admin/codegen/vo/CodegenUpdateReqVO.java

@@ -1,7 +1,9 @@
 package cn.iocoder.yudao.module.infra.controller.admin.codegen.vo;
 
+import cn.hutool.core.util.ObjectUtil;
 import cn.iocoder.yudao.module.infra.controller.admin.codegen.vo.column.CodegenColumnBaseVO;
 import cn.iocoder.yudao.module.infra.controller.admin.codegen.vo.table.CodegenTableBaseVO;
+import cn.iocoder.yudao.module.infra.enums.codegen.CodegenSceneEnum;
 import io.swagger.annotations.ApiModel;
 import io.swagger.annotations.ApiModelProperty;
 import lombok.Data;
@@ -9,6 +11,7 @@ import lombok.EqualsAndHashCode;
 import lombok.ToString;
 
 import javax.validation.Valid;
+import javax.validation.constraints.AssertTrue;
 import javax.validation.constraints.NotNull;
 import java.util.List;
 
@@ -28,11 +31,19 @@ public class CodegenUpdateReqVO {
     @Data
     @EqualsAndHashCode(callSuper = true)
     @ToString(callSuper = true)
+    @Valid
     public static class Table extends CodegenTableBaseVO {
 
         @ApiModelProperty(value = "编号", required = true, example = "1")
         private Long id;
 
+        @AssertTrue(message = "生成失败,必须设置上级菜单")
+        public boolean isParentMenuIdValid() {
+            // 生成场景为管理后台时,必须设置上级菜单,不然生成的菜单 SQL 是无父级菜单的
+            return ObjectUtil.notEqual(getScene(), CodegenSceneEnum.ADMIN.getScene())
+                    || getParentMenuId() != null;
+        }
+
     }
 
     @ApiModel("更新表定义")

+ 4 - 25
yudao-module-infra/yudao-module-infra-impl/src/main/java/cn/iocoder/yudao/module/infra/service/codegen/CodegenServiceImpl.java

@@ -2,6 +2,7 @@ package cn.iocoder.yudao.module.infra.service.codegen;
 
 import cn.hutool.core.collection.CollUtil;
 import cn.iocoder.yudao.framework.common.pojo.PageResult;
+import cn.iocoder.yudao.framework.common.util.collection.CollectionUtils;
 import cn.iocoder.yudao.module.infra.controller.admin.codegen.vo.CodegenUpdateReqVO;
 import cn.iocoder.yudao.module.infra.controller.admin.codegen.vo.table.CodegenTablePageReqVO;
 import cn.iocoder.yudao.module.infra.convert.codegen.CodegenConvert;
@@ -14,21 +15,18 @@ import cn.iocoder.yudao.module.infra.dal.mysql.codegen.CodegenTableMapper;
 import cn.iocoder.yudao.module.infra.dal.mysql.codegen.SchemaColumnMapper;
 import cn.iocoder.yudao.module.infra.dal.mysql.codegen.SchemaTableMapper;
 import cn.iocoder.yudao.module.infra.enums.codegen.CodegenImportTypeEnum;
+import cn.iocoder.yudao.module.infra.enums.codegen.CodegenSceneEnum;
 import cn.iocoder.yudao.module.infra.framework.codegen.config.CodegenProperties;
 import cn.iocoder.yudao.module.infra.service.codegen.inner.CodegenBuilder;
 import cn.iocoder.yudao.module.infra.service.codegen.inner.CodegenEngine;
 import cn.iocoder.yudao.module.infra.service.codegen.inner.CodegenSQLParser;
 import cn.iocoder.yudao.module.system.api.user.AdminUserApi;
-import cn.iocoder.yudao.framework.common.util.collection.CollectionUtils;
 import org.apache.commons.collections4.KeyValue;
 import org.springframework.stereotype.Service;
 import org.springframework.transaction.annotation.Transactional;
 
 import javax.annotation.Resource;
-import java.util.ArrayList;
-import java.util.List;
-import java.util.Map;
-import java.util.Set;
+import java.util.*;
 import java.util.stream.Collectors;
 
 import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
@@ -266,27 +264,8 @@ public class CodegenServiceImpl implements CodegenService {
         // TODO 强制移除 Quartz 的表,未来做成可配置
         tables.removeIf(table -> table.getTableName().startsWith("QRTZ_"));
         tables.removeIf(table -> table.getTableName().startsWith("ACT_"));
+        tables.removeIf(table -> table.getTableName().startsWith("FLW_"));
         return tables;
     }
 
-//    /**
-//     * 修改保存参数校验
-//     *
-//     * @param genTable 业务信息
-//     */
-//    @Override
-//    public void validateEdit(GenTable genTable) {
-//        if (GenConstants.TPL_TREE.equals(genTable.getTplCategory())) {
-//            String options = JSON.toJSONString(genTable.getParams());
-//            JSONObject paramsObj = JSONObject.parseObject(options);
-//            if (StringUtils.isEmpty(paramsObj.getString(GenConstants.TREE_CODE))) {
-//                throw new CustomException("树编码字段不能为空");
-//            } else if (StringUtils.isEmpty(paramsObj.getString(GenConstants.TREE_PARENT_CODE))) {
-//                throw new CustomException("树父编码字段不能为空");
-//            } else if (StringUtils.isEmpty(paramsObj.getString(GenConstants.TREE_NAME))) {
-//                throw new CustomException("树名称字段不能为空");
-//            }
-//        }
-//    }
-
 }

+ 4 - 5
yudao-module-infra/yudao-module-infra-impl/src/main/java/cn/iocoder/yudao/module/infra/service/codegen/inner/CodegenBuilder.java

@@ -121,14 +121,13 @@ public class CodegenBuilder {
     private void initTableDefault(CodegenTableDO table) {
         // 以 system_dept 举例子。moduleName 为 system、businessName 为 dept、className 为 SystemDept
         // 如果不希望 System 前缀,则可以手动在【代码生成 - 修改生成配置 - 基本信息】,将实体类名称改为 Dept 即可
-        table.setModuleName(StrUtil.subBefore(table.getTableName(),
-                '_', false)); // 第一个 _ 前缀的前面,作为 module 名字
+        table.setModuleName(subBefore(table.getTableName(), '_', false)); // 第一个 _ 前缀的前面,作为 module 名字
         table.setBusinessName(toCamelCase(subAfter(table.getTableName(),
                 '_', false))); // 第一步,第一个 _ 前缀的后面,作为 module 名字; 第二步,可能存在多个 _ 的情况,转换成驼峰
-        table.setClassName(upperFirst(toCamelCase(table.getTableName()))); // 驼峰 + 首字母大写
+        table.setClassName(upperFirst(toCamelCase( // 驼峰 + 首字母大写
+                subAfter(table.getTableName(), '_', false)))); // 第一个 _ 前缀的前面,作为 class 名字
         table.setClassComment(subBefore(table.getTableComment(), // 去除结尾的表,作为类描述
                 '表', true));
-        table.setAuthor("芋艿"); // TODO 稍后改成创建人
         table.setTemplateType(CodegenTemplateTypeEnum.CRUD.getType());
     }
 
@@ -157,7 +156,7 @@ public class CodegenBuilder {
         column.setJavaField(toCamelCase(column.getColumnName()));
         // 处理 dictType 字段,暂无
         // 处理 javaType 字段
-        String dbType = StrUtil.subBefore(column.getColumnType(), '(', false);
+        String dbType = subBefore(column.getColumnType(), '(', false);
         javaTypeMappings.entrySet().stream()
                 .filter(entry -> entry.getValue().contains(dbType))
                 .findFirst().ifPresent(entry -> column.setJavaType(entry.getKey()));

+ 7 - 1
yudao-ui-admin/src/views/infra/codegen/basicInfoForm.vue

@@ -13,7 +13,13 @@
       </el-col>
 
       <el-col :span="12">
-        <el-form-item label="实体类名称" prop="className">
+        <el-form-item prop="className">
+          <span slot="label">
+            实体类名称
+            <el-tooltip content="默认去除表名的前缀。如果存在重复,则需要手动添加前缀,避免 MyBatis 报 Alias 重复的问题。" placement="top">
+              <i class="el-icon-question"></i>
+            </el-tooltip>
+          </span>
           <el-input placeholder="请输入" v-model="info.className" />
         </el-form-item>
       </el-col>