feat: 代码生成支持生成菜单路由

This commit is contained in:
ray
2024-08-02 00:41:36 +08:00
parent b267264b63
commit 65e3f717ef
4 changed files with 42 additions and 18 deletions

View File

@@ -47,8 +47,11 @@ public class SysMenuController {
@Operation(summary = "菜单下拉列表")
@GetMapping("/options")
public Result listMenuOptions() {
List<Option> menus = menuService.listMenuOptions();
public Result listMenuOptions(
@Parameter(description = "是否只查询父级菜单")
@RequestParam(required = false, defaultValue = "false") boolean onlyParent
) {
List<Option> menus = menuService.listMenuOptions(onlyParent);
return Result.success(menus);
}

View File

@@ -1,6 +1,7 @@
package com.youlai.system.service;
import com.baomidou.mybatisplus.extension.service.IService;
import com.youlai.system.model.entity.GenConfig;
import com.youlai.system.model.form.MenuForm;
import com.youlai.system.common.model.Option;
import com.youlai.system.model.entity.SysMenu;
@@ -24,11 +25,12 @@ public interface SysMenuService extends IService<SysMenu> {
*/
List<MenuVO> listMenus(MenuQuery queryParams);
/**
* 获取菜单下拉列表
*
* @param onlyParent 是否只查询父级菜单
*/
List<Option> listMenuOptions();
List<Option> listMenuOptions(boolean onlyParent);
/**
* 新增菜单
@@ -68,7 +70,7 @@ public interface SysMenuService extends IService<SysMenu> {
* 为代码生成添加菜单
*
* @param parentMenuId 父菜单ID
* @param entityName 实体名
* @param genConfig 实体名
*/
void addMenuForCodeGeneration(Long parentMenuId,String businessName, String entityName);
void saveMenu(Long parentMenuId, GenConfig genConfig);
}

View File

@@ -101,7 +101,8 @@ public class GeneratorServiceImpl implements GeneratorService {
genConfig.setEntityName(entityName);
String packageName = SystemApplication.class.getPackageName();
genConfig.setPackageName(packageName);
genConfig.setPackageName( StrUtil.subBefore(packageName, ".", true));
genConfig.setModuleName(StrUtil.subAfter(packageName, ".", true));
genConfig.setAuthor(generatorProperties.getDefaultConfig().getAuthor());
@@ -177,7 +178,7 @@ public class GeneratorServiceImpl implements GeneratorService {
// 如果选择上级菜单
Long parentMenuId = formData.getParentMenuId();
if (parentMenuId != null) {
menuService.addMenuForCodeGeneration(parentMenuId,genConfig.getBusinessName(),genConfig.getEntityName());
menuService.saveMenu(parentMenuId,genConfig);
}
List<GenFieldConfig> genFieldConfigs = genConfigConverter.toGenFieldConfig(formData.getFieldConfigs());

View File

@@ -18,6 +18,7 @@ import com.youlai.system.common.model.Option;
import com.youlai.system.converter.MenuConverter;
import com.youlai.system.mapper.SysMenuMapper;
import com.youlai.system.model.bo.RouteBO;
import com.youlai.system.model.entity.GenConfig;
import com.youlai.system.model.entity.SysMenu;
import com.youlai.system.model.form.MenuForm;
import com.youlai.system.model.query.MenuQuery;
@@ -102,11 +103,15 @@ public class SysMenuServiceImpl extends ServiceImpl<SysMenuMapper, SysMenu> impl
/**
* 菜单下拉数据
*
* @param onlyParent 是否只查询父级菜单 如果为true排除按钮
*/
@Override
public List<Option> listMenuOptions() {
public List<Option> listMenuOptions(boolean onlyParent) {
List<SysMenu> menuList = this.list(new LambdaQueryWrapper<SysMenu>()
.orderByAsc(SysMenu::getSort));
.in(onlyParent, SysMenu::getType, MenuTypeEnum.CATALOG.getValue(), MenuTypeEnum.MENU.getValue())
.orderByAsc(SysMenu::getSort)
);
return buildMenuOptions(SystemConstants.ROOT_NODE_ID, menuList);
}
@@ -355,27 +360,40 @@ public class SysMenuServiceImpl extends ServiceImpl<SysMenuMapper, SysMenu> impl
* 为代码生成添加菜单
*
* @param parentMenuId 父菜单ID
* @param entityName 实体名称
* @param genConfig 实体名称
*/
@Override
public void addMenuForCodeGeneration(Long parentMenuId, String businessName, String entityName) {
public void saveMenu(Long parentMenuId, GenConfig genConfig) {
SysMenu parentMenu = this.getById(parentMenuId);
Assert.notNull(parentMenu, "菜单不存在");
Assert.notNull(parentMenu, "上级菜单不存在");
String entityName = genConfig.getEntityName();
long count = this.count(new LambdaQueryWrapper<SysMenu>().eq(SysMenu::getRouteName, entityName));
if (count > 0) {
return;
}
// 获取父级菜单子菜单最带的排序
SysMenu maxSortMenu = this.getOne(new LambdaQueryWrapper<SysMenu>().eq(SysMenu::getParentId, parentMenuId)
.orderByDesc(SysMenu::getSort)
.last("limit 1")
);
int sort = 1;
if (maxSortMenu != null) {
sort = maxSortMenu.getSort() + 1;
}
SysMenu menu = new SysMenu();
menu.setParentId(parentMenuId);
menu.setName(businessName);
menu.setName(genConfig.getBusinessName());
menu.setRouteName(entityName);
menu.setRoutePath(StrUtil.toUnderlineCase(entityName));
menu.setComponent(StrUtil.toUnderlineCase(entityName) + "/index");
menu.setType(MenuTypeEnum.CATALOG);
menu.setSort(0);
menu.setRoutePath(StrUtil.toSymbolCase(entityName, '-'));
menu.setComponent(genConfig.getModuleName() + "/" + StrUtil.toSymbolCase(entityName, '-') + "/index");
menu.setType(MenuTypeEnum.MENU);
menu.setSort(sort);
menu.setVisible(1);
boolean result = this.save(menu);