From daec9f854269f45ca6df66037b855a95c467951c Mon Sep 17 00:00:00 2001 From: "Ray.Hao" <1490493387@qq.com> Date: Sat, 7 Sep 2024 19:52:33 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20=E6=B7=BB=E5=8A=A0=E8=8F=9C?= =?UTF-8?q?=E5=8D=95feign=E5=AE=A2=E6=88=B7=E7=AB=AF=EF=BC=8C=E6=8F=90?= =?UTF-8?q?=E4=BE=9B=E4=BB=A3=E7=A0=81=E7=94=9F=E6=88=90=E6=96=B0=E5=A2=9E?= =?UTF-8?q?=E8=8F=9C=E5=8D=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../youlai/mall/member/MemberApplication.java | 2 +- youlai-codegen/pom.xml | 5 ++ .../youlai/codegen/CodegenApplication.java | 9 ++- .../codegen/controller/CodegenController.java | 8 +- .../codegen/converter/CodegenConverter.java | 5 ++ ...dePreviewVO.java => CodegenPreviewVO.java} | 2 +- .../codegen/service/CodegenService.java | 4 +- .../codegen/service/GenConfigService.java | 1 - .../service/impl/CodegenServiceImpl.java | 12 +-- .../service/impl/GenConfigServiceImpl.java | 11 ++- .../src/main/resources/bootstrap.yml | 4 +- .../java/com/youlai/common/enums/EnvEnum.java | 4 +- .../youlai/system/api/MenuFeignClient.java | 14 ++++ .../youlai/system/api/UserFeignClient.java | 2 +- .../system/controller/MenuController.java | 6 +- .../youlai/system/service/MenuService.java | 2 +- .../system/service/impl/MenuServiceImpl.java | 73 +++++++++++++++++-- 17 files changed, 126 insertions(+), 38 deletions(-) rename youlai-codegen/src/main/java/com/youlai/codegen/model/vo/{CodePreviewVO.java => CodegenPreviewVO.java} (93%) create mode 100644 youlai-system/system-api/src/main/java/com/youlai/system/api/MenuFeignClient.java diff --git a/mall-member/member-boot/src/main/java/com/youlai/mall/member/MemberApplication.java b/mall-member/member-boot/src/main/java/com/youlai/mall/member/MemberApplication.java index a96cd510f..c4c323892 100644 --- a/mall-member/member-boot/src/main/java/com/youlai/mall/member/MemberApplication.java +++ b/mall-member/member-boot/src/main/java/com/youlai/mall/member/MemberApplication.java @@ -13,7 +13,7 @@ import org.springframework.cloud.openfeign.EnableFeignClients; * @since 0.0.1 */ @SpringBootApplication -@EnableDiscoveryClient() +@EnableDiscoveryClient @EnableFeignClients(basePackageClasses = {SkuFeignClient.class}) public class MemberApplication { public static void main(String[] args) { diff --git a/youlai-codegen/pom.xml b/youlai-codegen/pom.xml index 8ee374a70..23ddbfc20 100644 --- a/youlai-codegen/pom.xml +++ b/youlai-codegen/pom.xml @@ -72,6 +72,11 @@ velocity-engine-core + + com.youlai + system-api + + \ No newline at end of file diff --git a/youlai-codegen/src/main/java/com/youlai/codegen/CodegenApplication.java b/youlai-codegen/src/main/java/com/youlai/codegen/CodegenApplication.java index 5fc367f95..af28332f0 100644 --- a/youlai-codegen/src/main/java/com/youlai/codegen/CodegenApplication.java +++ b/youlai-codegen/src/main/java/com/youlai/codegen/CodegenApplication.java @@ -1,15 +1,20 @@ package com.youlai.codegen; +import com.youlai.system.api.MenuFeignClient; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.cloud.client.discovery.EnableDiscoveryClient; +import org.springframework.cloud.openfeign.EnableFeignClients; /** - * 系统服务启动类 + * 代码生成器启动类 * * @author Ray - * @since 0.0.1 + * @since 4.0.0 */ @SpringBootApplication +@EnableDiscoveryClient +@EnableFeignClients(basePackageClasses = {MenuFeignClient.class}) public class CodegenApplication { public static void main(String[] args) { SpringApplication.run(CodegenApplication.class, args); diff --git a/youlai-codegen/src/main/java/com/youlai/codegen/controller/CodegenController.java b/youlai-codegen/src/main/java/com/youlai/codegen/controller/CodegenController.java index ac0303487..a765af6a6 100644 --- a/youlai-codegen/src/main/java/com/youlai/codegen/controller/CodegenController.java +++ b/youlai-codegen/src/main/java/com/youlai/codegen/controller/CodegenController.java @@ -7,7 +7,7 @@ import com.youlai.common.result.Result; import com.youlai.codegen.config.CodegenProperties; import com.youlai.codegen.model.form.GenConfigForm; import com.youlai.codegen.model.query.TablePageQuery; -import com.youlai.codegen.model.vo.CodePreviewVO; +import com.youlai.codegen.model.vo.CodegenPreviewVO; import com.youlai.codegen.model.vo.TablePageVO; import com.youlai.codegen.service.GenConfigService; import com.youlai.codegen.service.CodegenService; @@ -91,14 +91,14 @@ public class CodegenController { @Operation(summary = "获取预览生成代码") @GetMapping("/{tableName}/preview") - public Result> getCodePreviewData(@PathVariable String tableName) { - List list = codegenService.getCodePreviewData(tableName); + public Result> getCodePreviewData(@PathVariable String tableName) { + List list = codegenService.getCodePreviewData(tableName); return Result.success(list); } @Operation(summary = "下载代码") @GetMapping("/{tableName}/download") - public void downloadZip(HttpServletResponse response, @PathVariable String tableName) throws IOException { + public void downloadZip(HttpServletResponse response, @PathVariable String tableName) { String[] tableNames = tableName.split(","); byte[] data = codegenService.downloadCode(tableNames); diff --git a/youlai-codegen/src/main/java/com/youlai/codegen/converter/CodegenConverter.java b/youlai-codegen/src/main/java/com/youlai/codegen/converter/CodegenConverter.java index f65a9f1f8..73f356736 100644 --- a/youlai-codegen/src/main/java/com/youlai/codegen/converter/CodegenConverter.java +++ b/youlai-codegen/src/main/java/com/youlai/codegen/converter/CodegenConverter.java @@ -3,6 +3,7 @@ package com.youlai.codegen.converter; import com.youlai.codegen.model.entity.GenConfig; import com.youlai.codegen.model.entity.GenFieldConfig; import com.youlai.codegen.model.form.GenConfigForm; +import com.youlai.system.dto.CodegenMenuDTO; import org.mapstruct.Mapper; import org.mapstruct.Mapping; @@ -36,4 +37,8 @@ public interface CodegenConverter { GenFieldConfig toGenFieldConfig(GenConfigForm.FieldConfig fieldConfig); + + @Mapping(source = "parentMenuId", target = "parentMenuId") + CodegenMenuDTO toCodegenMenuDTO(Long parentMenuId, GenConfig genConfig); + } \ No newline at end of file diff --git a/youlai-codegen/src/main/java/com/youlai/codegen/model/vo/CodePreviewVO.java b/youlai-codegen/src/main/java/com/youlai/codegen/model/vo/CodegenPreviewVO.java similarity index 93% rename from youlai-codegen/src/main/java/com/youlai/codegen/model/vo/CodePreviewVO.java rename to youlai-codegen/src/main/java/com/youlai/codegen/model/vo/CodegenPreviewVO.java index 52489d8b1..2aa611168 100644 --- a/youlai-codegen/src/main/java/com/youlai/codegen/model/vo/CodePreviewVO.java +++ b/youlai-codegen/src/main/java/com/youlai/codegen/model/vo/CodegenPreviewVO.java @@ -5,7 +5,7 @@ import lombok.Data; @Schema(description = "代码生成代码预览VO") @Data -public class CodePreviewVO { +public class CodegenPreviewVO { @Schema(description = "生成文件路径") private String path; diff --git a/youlai-codegen/src/main/java/com/youlai/codegen/service/CodegenService.java b/youlai-codegen/src/main/java/com/youlai/codegen/service/CodegenService.java index 9deb2bfe9..902f205c2 100644 --- a/youlai-codegen/src/main/java/com/youlai/codegen/service/CodegenService.java +++ b/youlai-codegen/src/main/java/com/youlai/codegen/service/CodegenService.java @@ -2,7 +2,7 @@ package com.youlai.codegen.service; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.youlai.codegen.model.query.TablePageQuery; -import com.youlai.codegen.model.vo.CodePreviewVO; +import com.youlai.codegen.model.vo.CodegenPreviewVO; import com.youlai.codegen.model.vo.TablePageVO; import java.util.List; @@ -29,7 +29,7 @@ public interface CodegenService { * @param tableName 表名 * @return 包含代码预览信息的列表 */ - List getCodePreviewData(String tableName); + List getCodePreviewData(String tableName); /** * 下载代码 diff --git a/youlai-codegen/src/main/java/com/youlai/codegen/service/GenConfigService.java b/youlai-codegen/src/main/java/com/youlai/codegen/service/GenConfigService.java index 1ac50f32a..846c6c013 100644 --- a/youlai-codegen/src/main/java/com/youlai/codegen/service/GenConfigService.java +++ b/youlai-codegen/src/main/java/com/youlai/codegen/service/GenConfigService.java @@ -12,7 +12,6 @@ import com.youlai.codegen.model.form.GenConfigForm; */ public interface GenConfigService extends IService { - /** * 获取代码生成配置 * diff --git a/youlai-codegen/src/main/java/com/youlai/codegen/service/impl/CodegenServiceImpl.java b/youlai-codegen/src/main/java/com/youlai/codegen/service/impl/CodegenServiceImpl.java index 7177b9b46..bbc8ad5f6 100644 --- a/youlai-codegen/src/main/java/com/youlai/codegen/service/impl/CodegenServiceImpl.java +++ b/youlai-codegen/src/main/java/com/youlai/codegen/service/impl/CodegenServiceImpl.java @@ -17,7 +17,7 @@ import com.youlai.codegen.enums.JavaTypeEnum; import com.youlai.codegen.model.entity.GenConfig; import com.youlai.codegen.model.entity.GenFieldConfig; import com.youlai.codegen.model.query.TablePageQuery; -import com.youlai.codegen.model.vo.CodePreviewVO; +import com.youlai.codegen.model.vo.CodegenPreviewVO; import com.youlai.codegen.model.vo.TablePageVO; import com.youlai.codegen.service.DatabaseService; import com.youlai.codegen.service.GenConfigService; @@ -93,9 +93,9 @@ public class CodegenServiceImpl implements CodegenService { * @return 预览数据 */ @Override - public List getCodePreviewData(String tableName) { + public List getCodePreviewData(String tableName) { - List list = new ArrayList<>(); + List list = new ArrayList<>(); GenConfig genConfig = genConfigService.getOne(new LambdaQueryWrapper() .eq(GenConfig::getTableName, tableName) @@ -116,7 +116,7 @@ public class CodegenServiceImpl implements CodegenService { // 遍历模板配置 Map templateConfigs = codegenProperties.getTemplateConfigs(); for (Map.Entry templateConfigEntry : templateConfigs.entrySet()) { - CodePreviewVO previewVO = new CodePreviewVO(); + CodegenPreviewVO previewVO = new CodegenPreviewVO(); CodegenProperties.TemplateConfig templateConfig = templateConfigEntry.getValue(); @@ -308,9 +308,9 @@ public class CodegenServiceImpl implements CodegenService { * @param zip 压缩文件输出流 */ private void generateAndZipCode(String tableName, ZipOutputStream zip) { - List codePreviewList = getCodePreviewData(tableName); + List codePreviewList = getCodePreviewData(tableName); - for (CodePreviewVO codePreview : codePreviewList) { + for (CodegenPreviewVO codePreview : codePreviewList) { String fileName = codePreview.getFileName(); String content = codePreview.getContent(); String path = codePreview.getPath(); diff --git a/youlai-codegen/src/main/java/com/youlai/codegen/service/impl/GenConfigServiceImpl.java b/youlai-codegen/src/main/java/com/youlai/codegen/service/impl/GenConfigServiceImpl.java index dc126f1c6..88a7ef5f2 100644 --- a/youlai-codegen/src/main/java/com/youlai/codegen/service/impl/GenConfigServiceImpl.java +++ b/youlai-codegen/src/main/java/com/youlai/codegen/service/impl/GenConfigServiceImpl.java @@ -21,6 +21,8 @@ import com.youlai.codegen.model.entity.GenFieldConfig; import com.youlai.codegen.model.form.GenConfigForm; import com.youlai.codegen.service.GenConfigService; import com.youlai.codegen.service.GenFieldConfigService; +import com.youlai.system.api.MenuFeignClient; +import com.youlai.system.dto.CodegenMenuDTO; import lombok.RequiredArgsConstructor; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Service; @@ -41,7 +43,7 @@ public class GenConfigServiceImpl extends ServiceImpl genFieldConfigs = codegenConverter.toGenFieldConfig(formData.getFieldConfigs()); diff --git a/youlai-codegen/src/main/resources/bootstrap.yml b/youlai-codegen/src/main/resources/bootstrap.yml index 4f53553fb..704100b7b 100644 --- a/youlai-codegen/src/main/resources/bootstrap.yml +++ b/youlai-codegen/src/main/resources/bootstrap.yml @@ -3,12 +3,12 @@ server: spring: application: - name: youlai-generator + name: youlai-codegen profiles: active: dev # 代码生成器配置 -generator: +codegen: # 下载代码文件名称 downloadFileName: youlai-mall-code.zip # 后端项目名称 diff --git a/youlai-common/common-base/src/main/java/com/youlai/common/enums/EnvEnum.java b/youlai-common/common-base/src/main/java/com/youlai/common/enums/EnvEnum.java index 23e893f55..d1cd333e3 100644 --- a/youlai-common/common-base/src/main/java/com/youlai/common/enums/EnvEnum.java +++ b/youlai-common/common-base/src/main/java/com/youlai/common/enums/EnvEnum.java @@ -12,8 +12,8 @@ import lombok.Getter; @Getter public enum EnvEnum implements IBaseEnum { - DEV("dev", "开发"), - PROD("prod", "生产"); + DEV("dev", "开发环境"), + PROD("prod", "生产环境"); private final String value; diff --git a/youlai-system/system-api/src/main/java/com/youlai/system/api/MenuFeignClient.java b/youlai-system/system-api/src/main/java/com/youlai/system/api/MenuFeignClient.java new file mode 100644 index 000000000..5abba6e36 --- /dev/null +++ b/youlai-system/system-api/src/main/java/com/youlai/system/api/MenuFeignClient.java @@ -0,0 +1,14 @@ +package com.youlai.system.api; + +import com.youlai.common.core.config.FeignDecoderConfig; +import com.youlai.system.dto.CodegenMenuDTO; +import org.springframework.cloud.openfeign.FeignClient; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestBody; + +@FeignClient(value = "youlai-system", contextId = "menu", configuration = {FeignDecoderConfig.class}) +public interface MenuFeignClient { + + @PostMapping("/api/v1/menus/codegen") + void addMenuForCodegen( @RequestBody CodegenMenuDTO data); +} diff --git a/youlai-system/system-api/src/main/java/com/youlai/system/api/UserFeignClient.java b/youlai-system/system-api/src/main/java/com/youlai/system/api/UserFeignClient.java index 47084e29a..c2de0ed0c 100644 --- a/youlai-system/system-api/src/main/java/com/youlai/system/api/UserFeignClient.java +++ b/youlai-system/system-api/src/main/java/com/youlai/system/api/UserFeignClient.java @@ -7,7 +7,7 @@ import org.springframework.cloud.openfeign.FeignClient; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; -@FeignClient(value = "youlai-system", fallback = UserFeignFallbackClient.class, configuration = {FeignDecoderConfig.class}) +@FeignClient(value = "youlai-system", contextId = "user", fallback = UserFeignFallbackClient.class, configuration = {FeignDecoderConfig.class}) public interface UserFeignClient { @GetMapping("/api/v1/users/{username}/authInfo") diff --git a/youlai-system/system-boot/src/main/java/com/youlai/system/controller/MenuController.java b/youlai-system/system-boot/src/main/java/com/youlai/system/controller/MenuController.java index fd8fc84eb..062941ffd 100644 --- a/youlai-system/system-boot/src/main/java/com/youlai/system/controller/MenuController.java +++ b/youlai-system/system-boot/src/main/java/com/youlai/system/controller/MenuController.java @@ -113,16 +113,14 @@ public class MenuController { return Result.judge(result); } - /** * 代码生成器生成代码新增菜单 */ @PostMapping("/codegen") - public Result createCodegenMenu( + public Result addMenuForCodegen( @RequestBody @Valid CodegenMenuDTO data ) { - - boolean result = menuService.createCodegenMenu(data); + boolean result = menuService.addMenuForCodegen(data); return Result.judge(result); } diff --git a/youlai-system/system-boot/src/main/java/com/youlai/system/service/MenuService.java b/youlai-system/system-boot/src/main/java/com/youlai/system/service/MenuService.java index 5a96aa8a4..008fa88be 100644 --- a/youlai-system/system-boot/src/main/java/com/youlai/system/service/MenuService.java +++ b/youlai-system/system-boot/src/main/java/com/youlai/system/service/MenuService.java @@ -64,5 +64,5 @@ public interface MenuService extends IService { /** * 保存生成代码菜单 */ - boolean createCodegenMenu(@Valid CodegenMenuDTO data); + boolean addMenuForCodegen(@Valid CodegenMenuDTO data); } diff --git a/youlai-system/system-boot/src/main/java/com/youlai/system/service/impl/MenuServiceImpl.java b/youlai-system/system-boot/src/main/java/com/youlai/system/service/impl/MenuServiceImpl.java index d7f7c7fa8..a7388bb34 100644 --- a/youlai-system/system-boot/src/main/java/com/youlai/system/service/impl/MenuServiceImpl.java +++ b/youlai-system/system-boot/src/main/java/com/youlai/system/service/impl/MenuServiceImpl.java @@ -338,15 +338,74 @@ public class MenuServiceImpl extends ServiceImpl implements Me } /** - * 保存生成代码菜单 + * 代码生成时添加菜单 * - * @param data - * @return + * @param data {@link CodegenMenuDTO} 代码生成菜单数据传输对象 + * @return 是否成功 */ @Override - public boolean createCodegenMenu(CodegenMenuDTO data) { - return false; + public boolean addMenuForCodegen(CodegenMenuDTO data) { + Long parentMenuId = data.getParentMenuId(); + + Menu parentMenu = this.getById(parentMenuId); + Assert.notNull(parentMenu, "上级菜单不存在"); + String entityName =data.getEntityName(); + + long count = this.count(new LambdaQueryWrapper().eq(Menu::getRouteName, entityName)); + if (count > 0) { + throw new RuntimeException("菜单已存在"); + } + // 业务名称 e.g. 用户管理 + String businessName = data.getBusinessName(); + // 模块名称 e.g. system + String moduleName = data.getModuleName(); + // 获取父级菜单子菜单最带的排序 + Menu maxSortMenu = this.getOne(new LambdaQueryWrapper().eq(Menu::getParentId, parentMenuId) + .orderByDesc(Menu::getSort) + .last("limit 1") + ); + int sort = 1; + if (maxSortMenu != null) { + sort = maxSortMenu.getSort() + 1; + } + + Menu menu = new Menu(); + menu.setParentId(parentMenuId); + menu.setName(businessName); + + menu.setRouteName(entityName); + menu.setRoutePath(StrUtil.toSymbolCase(entityName, '-')); + menu.setComponent(data.getModuleName() + "/" + StrUtil.toSymbolCase(entityName, '-') + "/index"); + menu.setType(MenuTypeEnum.MENU); + menu.setSort(sort); + menu.setVisible(1); + boolean result = this.save(menu); + + if (result) { + // 生成treePath + String treePath = generateMenuTreePath(parentMenuId); + menu.setTreePath(treePath); + this.updateById(menu); + + // 生成CURD按钮权限 + String permPrefix = data.getModuleName() + ":" + StrUtil.lowerFirst(entityName) + ":"; + String[] actions = {"查询", "新增", "编辑", "删除"}; + String[] perms = {"query", "add", "edit", "delete"}; + + for (int i = 0; i < actions.length; i++) { + Menu button = new Menu(); + button.setParentId(menu.getId()); + button.setType(MenuTypeEnum.BUTTON); + button.setName(actions[i]); + button.setPerm(permPrefix + perms[i]); + button.setSort(i + 1); + this.save(button); + + // 生成 treepath + button.setTreePath(treePath + "," + button.getId()); + this.updateById(button); + } + } + return result; } - - }