Commit a75bb825d78e48cffbc7f51b4844cb5511e95091

Authored by YunaiV
1 parent a51e9645

代码生成:支持设置主子表的信息

yudao-module-infra/yudao-module-infra-api/src/main/java/cn/iocoder/yudao/module/infra/enums/ErrorCodeConstants.java
... ... @@ -42,6 +42,8 @@ public interface ErrorCodeConstants {
42 42 ErrorCode CODEGEN_SYNC_NONE_CHANGE = new ErrorCode(1_003_001_007, "同步失败,不存在改变");
43 43 ErrorCode CODEGEN_TABLE_INFO_TABLE_COMMENT_IS_NULL = new ErrorCode(1_003_001_008, "数据库的表注释未填写");
44 44 ErrorCode CODEGEN_TABLE_INFO_COLUMN_COMMENT_IS_NULL = new ErrorCode(1_003_001_009, "数据库的表字段({})注释未填写");
  45 + ErrorCode CODEGEN_MASTER_TABLE_NOT_EXISTS = new ErrorCode(1_003_001_010, "主表(id={})定义不存在,请检查");
  46 + ErrorCode CODEGEN_SUB_COLUMN_NOT_EXISTS = new ErrorCode(1_003_001_011, "子表的字段(id={})不存在,请检查");
45 47  
46 48 // ========== 文件配置 1-001-006-000 ==========
47 49 ErrorCode FILE_CONFIG_NOT_EXISTS = new ErrorCode(1_001_006_000, "文件配置不存在");
... ...
yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/controller/admin/codegen/CodegenController.java
... ... @@ -60,10 +60,19 @@ public class CodegenController {
60 60 return success(codegenService.getDatabaseTableList(dataSourceConfigId, name, comment));
61 61 }
62 62  
  63 + @GetMapping("/table/list")
  64 + @Operation(summary = "获得表定义列表")
  65 + @Parameter(name = "dataSourceConfigId", description = "数据源配置的编号", required = true, example = "1")
  66 + @PreAuthorize("@ss.hasPermission('infra:codegen:query')")
  67 + public CommonResult<List<CodegenTableRespVO>> getCodegenTableList(@RequestParam(value = "dataSourceConfigId") Long dataSourceConfigId) {
  68 + List<CodegenTableDO> list = codegenService.getCodegenTableList(dataSourceConfigId);
  69 + return success(CodegenConvert.INSTANCE.convertList05(list));
  70 + }
  71 +
63 72 @GetMapping("/table/page")
64 73 @Operation(summary = "获得表定义分页")
65 74 @PreAuthorize("@ss.hasPermission('infra:codegen:query')")
66   - public CommonResult<PageResult<CodegenTableRespVO>> getCodeGenTablePage(@Valid CodegenTablePageReqVO pageReqVO) {
  75 + public CommonResult<PageResult<CodegenTableRespVO>> getCodegenTablePage(@Valid CodegenTablePageReqVO pageReqVO) {
67 76 PageResult<CodegenTableDO> pageResult = codegenService.getCodegenTablePage(pageReqVO);
68 77 return success(CodegenConvert.INSTANCE.convertPage(pageResult));
69 78 }
... ...
yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/controller/admin/codegen/vo/CodegenUpdateReqVO.java
... ... @@ -5,6 +5,7 @@ import cn.iocoder.yudao.module.infra.controller.admin.codegen.vo.column.CodegenC
5 5 import cn.iocoder.yudao.module.infra.controller.admin.codegen.vo.table.CodegenTableBaseVO;
6 6 import cn.iocoder.yudao.module.infra.enums.codegen.CodegenSceneEnum;
7 7 import cn.iocoder.yudao.module.infra.enums.codegen.CodegenTemplateTypeEnum;
  8 +import com.fasterxml.jackson.annotation.JsonIgnore;
8 9 import io.swagger.v3.oas.annotations.media.Schema;
9 10 import lombok.Data;
10 11 import lombok.EqualsAndHashCode;
... ... @@ -38,17 +39,18 @@ public class CodegenUpdateReqVO {
38 39 private Long id;
39 40  
40 41 @AssertTrue(message = "上级菜单不能为空,请前往 [修改生成配置 -> 生成信息] 界面,设置“上级菜单”字段")
  42 + @JsonIgnore
41 43 public boolean isParentMenuIdValid() {
42 44 // 生成场景为管理后台时,必须设置上级菜单,不然生成的菜单 SQL 是无父级菜单的
43 45 return ObjectUtil.notEqual(getScene(), CodegenSceneEnum.ADMIN.getScene())
44 46 || getParentMenuId() != null;
45 47 }
46 48  
47   - // TODO 芋艿:这里有问题哈;
48   - @AssertTrue(message = "关联的子表与字段不能为空")
  49 + @AssertTrue(message = "关联的父表信息不全")
  50 + @JsonIgnore
49 51 public boolean isSubValid() {
50 52 return ObjectUtil.notEqual(getTemplateType(), CodegenTemplateTypeEnum.SUB)
51   - || (getSubTableId() != null && getSubColumnId() != null);
  53 + || (ObjectUtil.isAllNotEmpty(getMasterTableId(), getSubJoinColumnId(), getSubJoinMany()));
52 54 }
53 55  
54 56 }
... ...
yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/controller/admin/codegen/vo/table/CodegenTableBaseVO.java
... ... @@ -58,9 +58,11 @@ public class CodegenTableBaseVO {
58 58 @Schema(description = "父菜单编号", example = "1024")
59 59 private Long parentMenuId;
60 60  
61   - @Schema(description = "子表的表编号", example = "2048")
62   - private Long subTableId;
63   - @Schema(description = "子表的关联字段编号", example = "4096")
64   - private Long subColumnId;
  61 + @Schema(description = "主表的编号", example = "2048")
  62 + private Long masterTableId;
  63 + @Schema(description = "子表关联主表的字段编号", example = "4096")
  64 + private Long subJoinColumnId;
  65 + @Schema(description = "主表与子表是否一对多", example = "4096")
  66 + private Boolean subJoinMany;
65 67  
66 68 }
... ...
yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/convert/codegen/CodegenConvert.java
... ... @@ -56,7 +56,7 @@ public interface CodegenConvert {
56 56  
57 57 // ========== CodegenTableDO 相关 ==========
58 58  
59   -// List<CodegenTableRespVO> convertList02(List<CodegenTableDO> list);
  59 + List<CodegenTableRespVO> convertList05(List<CodegenTableDO> list);
60 60  
61 61 CodegenTableRespVO convert(CodegenTableDO bean);
62 62  
... ...
yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/dal/dataobject/codegen/CodegenTableDO.java
... ... @@ -6,7 +6,6 @@ import cn.iocoder.yudao.module.infra.enums.codegen.CodegenFrontTypeEnum;
6 6 import cn.iocoder.yudao.module.infra.enums.codegen.CodegenSceneEnum;
7 7 import cn.iocoder.yudao.module.infra.enums.codegen.CodegenTemplateTypeEnum;
8 8 import com.baomidou.mybatisplus.annotation.KeySequence;
9   -import com.baomidou.mybatisplus.annotation.TableField;
10 9 import com.baomidou.mybatisplus.annotation.TableId;
11 10 import com.baomidou.mybatisplus.annotation.TableName;
12 11 import com.baomidou.mybatisplus.generator.config.po.TableInfo;
... ... @@ -124,14 +123,12 @@ public class CodegenTableDO extends BaseDO {
124 123 *
125 124 * 关联 {@link CodegenTableDO#getId()}
126 125 */
127   - @TableField(exist = false)
128 126 private Long masterTableId;
129 127 /**
130 128 * 【自己】子表关联主表的字段编号
131 129 *
132 130 * 关联 {@link CodegenColumnDO#getId()}
133 131 */
134   - @TableField(exist = false)
135 132 private Long subJoinColumnId;
136 133 /**
137 134 * 主表与子表是否一对多
... ... @@ -139,7 +136,6 @@ public class CodegenTableDO extends BaseDO {
139 136 * true:一对多
140 137 * false:一对一
141 138 */
142   - @TableField(exist = false)
143 139 private Boolean subJoinMany;
144 140  
145 141 }
... ...
yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/enums/codegen/CodegenTemplateTypeEnum.java
1 1 package cn.iocoder.yudao.module.infra.enums.codegen;
2 2  
  3 +import cn.iocoder.yudao.framework.common.util.object.ObjectUtils;
3 4 import lombok.AllArgsConstructor;
4 5 import lombok.Getter;
5 6  
... ... @@ -26,4 +27,15 @@ public enum CodegenTemplateTypeEnum {
26 27 */
27 28 private final Integer type;
28 29  
  30 + /**
  31 + * 是否为主表
  32 + *
  33 + * @param type 类型
  34 + * @return 是否主表
  35 + */
  36 + public static boolean isMaster(Integer type) {
  37 + return ObjectUtils.equalsAny(type,
  38 + MASTER_NORMAL.type, MASTER_ERP.type, MASTER_INNER.type);
  39 + }
  40 +
29 41 }
... ...
yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/service/codegen/CodegenService.java
... ... @@ -49,6 +49,14 @@ public interface CodegenService {
49 49 void deleteCodegen(Long tableId);
50 50  
51 51 /**
  52 + * 获得表定义列表
  53 + *
  54 + * @param dataSourceConfigId 数据源配置的编号
  55 + * @return 表定义列表
  56 + */
  57 + List<CodegenTableDO> getCodegenTableList(Long dataSourceConfigId);
  58 +
  59 + /**
52 60 * 获得表定义分页
53 61 *
54 62 * @param pageReqVO 分页条件
... ...
yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/service/codegen/CodegenServiceImpl.java
... ... @@ -14,6 +14,7 @@ import cn.iocoder.yudao.module.infra.dal.dataobject.codegen.CodegenTableDO;
14 14 import cn.iocoder.yudao.module.infra.dal.mysql.codegen.CodegenColumnMapper;
15 15 import cn.iocoder.yudao.module.infra.dal.mysql.codegen.CodegenTableMapper;
16 16 import cn.iocoder.yudao.module.infra.enums.codegen.CodegenSceneEnum;
  17 +import cn.iocoder.yudao.module.infra.enums.codegen.CodegenTemplateTypeEnum;
17 18 import cn.iocoder.yudao.module.infra.framework.codegen.config.CodegenProperties;
18 19 import cn.iocoder.yudao.module.infra.service.codegen.inner.CodegenBuilder;
19 20 import cn.iocoder.yudao.module.infra.service.codegen.inner.CodegenEngine;
... ... @@ -25,10 +26,7 @@ import org.springframework.stereotype.Service;
25 26 import org.springframework.transaction.annotation.Transactional;
26 27  
27 28 import javax.annotation.Resource;
28   -import java.util.ArrayList;
29   -import java.util.List;
30   -import java.util.Map;
31   -import java.util.Set;
  29 +import java.util.*;
32 30 import java.util.function.BiPredicate;
33 31 import java.util.stream.Collectors;
34 32  
... ... @@ -129,6 +127,16 @@ public class CodegenServiceImpl implements CodegenService {
129 127 if (codegenTableMapper.selectById(updateReqVO.getTable().getId()) == null) {
130 128 throw exception(CODEGEN_TABLE_NOT_EXISTS);
131 129 }
  130 + // 校验主表字段存在
  131 + if (Objects.equals(updateReqVO.getTable().getTemplateType(), CodegenTemplateTypeEnum.SUB.getType())) {
  132 + if (codegenTableMapper.selectById(updateReqVO.getTable().getMasterTableId()) == null) {
  133 + throw exception(CODEGEN_MASTER_TABLE_NOT_EXISTS, updateReqVO.getTable().getMasterTableId());
  134 + }
  135 + if (CollUtil.findOne(updateReqVO.getColumns(), // 关联主表的字段不存在
  136 + column -> column.getId().equals(updateReqVO.getTable().getSubJoinColumnId())) == null) {
  137 + throw exception(CODEGEN_SUB_COLUMN_NOT_EXISTS, updateReqVO.getTable().getSubJoinColumnId());
  138 + }
  139 + }
132 140  
133 141 // 更新 table 表定义
134 142 CodegenTableDO updateTableObj = CodegenConvert.INSTANCE.convert(updateReqVO.getTable());
... ... @@ -209,6 +217,11 @@ public class CodegenServiceImpl implements CodegenService {
209 217 }
210 218  
211 219 @Override
  220 + public List<CodegenTableDO> getCodegenTableList(Long dataSourceConfigId) {
  221 + return codegenTableMapper.selectListByDataSourceConfigId(dataSourceConfigId);
  222 + }
  223 +
  224 + @Override
212 225 public PageResult<CodegenTableDO> getCodegenTablePage(CodegenTablePageReqVO pageReqVO) {
213 226 return codegenTableMapper.selectPage(pageReqVO);
214 227 }
... ... @@ -235,23 +248,22 @@ public class CodegenServiceImpl implements CodegenService {
235 248 throw exception(CODEGEN_COLUMN_NOT_EXISTS);
236 249 }
237 250  
238   - // 校验子表是否已经存在
239   - CodegenTableDO subTable = null;
240   - List<CodegenColumnDO> subColumns = null;
241   - if (table.getMasterTableId() != null) {
  251 + // 如果是主子表,则加载对应的子表信息
  252 + List<CodegenTableDO> subTables = null;
  253 + List<List<CodegenColumnDO>> subColumnsList = null;
  254 + if (CodegenTemplateTypeEnum.isMaster(table.getTemplateType())) {
  255 + subTables = codegenTableMapper.selectListByMasterTableId(tableId);
242 256 subTable = codegenTableMapper.selectById(table.getMasterTableId());
243 257 subColumns = codegenColumnMapper.selectListByTableId(table.getMasterTableId());
244 258 }
245 259  
246 260 // 执行生成
247   -// return codegenEngine.execute(table, columns, subTable, subColumns);
248   - return codegenEngine.execute(table, columns, null, null);
  261 + return codegenEngine.execute(table, columns, subTable, subColumns);
249 262 }
250 263  
251 264 @Override
252 265 public List<DatabaseTableRespVO> getDatabaseTableList(Long dataSourceConfigId, String name, String comment) {
253 266 List<TableInfo> tables = databaseTableService.getTableList(dataSourceConfigId, name, comment);
254   - // 移除已经生成的表
255 267 // 移除在 Codegen 中,已经存在的
256 268 Set<String> existsTables = CollectionUtils.convertSet(
257 269 codegenTableMapper.selectListByDataSourceConfigId(dataSourceConfigId), CodegenTableDO::getTableName);
... ...