feat: 代码生成添加菜单路由生成

This commit is contained in:
ray
2024-08-01 07:53:02 +08:00
parent 06fcb7f01b
commit 67ceb660cb
6 changed files with 58 additions and 11 deletions

View File

@@ -63,4 +63,12 @@ public interface SysMenuService extends IService<SysMenu> {
* @param id 菜单ID
*/
boolean deleteMenu(Long id);
/**
* 为代码生成添加菜单
*
* @param parentMenuId 父菜单ID
* @param entityName 实体名
*/
void addMenuForCodeGeneration(Long parentMenuId,String businessName, String entityName);
}

View File

@@ -30,6 +30,7 @@ import com.youlai.system.model.vo.TablePageVO;
import com.youlai.system.service.GeneratorService;
import com.youlai.system.service.GenConfigService;
import com.youlai.system.service.GenFieldConfigService;
import com.youlai.system.service.SysMenuService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
@@ -52,6 +53,7 @@ public class GeneratorServiceImpl implements GeneratorService {
private final GenConfigService genConfigService;
private final GenFieldConfigService genFieldConfigService;
private final GenConfigConverter genConfigConverter;
private final SysMenuService menuService;
/**
* 数据表分页列表
@@ -98,9 +100,6 @@ public class GeneratorServiceImpl implements GeneratorService {
String entityName = StrUtil.toCamelCase(StrUtil.removePrefix(tableName, tableName.split("_")[0]));
genConfig.setEntityName(entityName);
String packageName = SystemApplication.class.getPackageName();
genConfig.setPackageName(packageName);
genConfig.setAuthor(generatorProperties.getDefaultConfig().getAuthor());
}
@@ -125,7 +124,7 @@ public class GeneratorServiceImpl implements GeneratorService {
.filter(item -> StrUtil.equals(item.getColumnName(), columnName))
.findFirst()
.orElseGet(() -> createDefaultFieldConfig(tableColumn));
if (genFieldConfig.getFieldSort() == null){
if (genFieldConfig.getFieldSort() == null) {
genFieldConfig.setFieldSort(++maxSort);
}
// 根据列类型设置字段类型
@@ -172,6 +171,12 @@ public class GeneratorServiceImpl implements GeneratorService {
GenConfig genConfig = genConfigConverter.toGenConfig(formData);
genConfigService.saveOrUpdate(genConfig);
// 如果选择上级菜单
Long parentMenuId = formData.getParentMenuId();
if (parentMenuId != null) {
menuService.addMenuForCodeGeneration(parentMenuId,genConfig.getBusinessName(),genConfig.getEntityName());
}
List<GenFieldConfig> genFieldConfigs = genConfigConverter.toGenFieldConfig(formData.getFieldConfigs());
if (CollectionUtil.isEmpty(genFieldConfigs)) {

View File

@@ -351,4 +351,41 @@ public class SysMenuServiceImpl extends ServiceImpl<SysMenuMapper, SysMenu> impl
}
/**
* 为代码生成添加菜单
*
* @param parentMenuId 父菜单ID
* @param entityName 实体名称
*/
@Override
public void addMenuForCodeGeneration(Long parentMenuId, String businessName, String entityName) {
SysMenu parentMenu = this.getById(parentMenuId);
Assert.notNull(parentMenu, "父菜单不存在");
long count = this.count(new LambdaQueryWrapper<SysMenu>().eq(SysMenu::getRouteName, entityName));
if (count > 0) {
return;
}
SysMenu menu = new SysMenu();
menu.setParentId(parentMenuId);
menu.setName(businessName);
menu.setRouteName(entityName);
menu.setRoutePath(StrUtil.toUnderlineCase(entityName));
menu.setComponent(StrUtil.toUnderlineCase(entityName) + "/index");
menu.setType(MenuTypeEnum.CATALOG);
menu.setSort(0);
menu.setVisible(1);
boolean result = this.save(menu);
if (result) {
// 生成treePath
String treePath = generateMenuTreePath(parentMenuId);
menu.setTreePath(treePath);
this.updateById(menu);
}
}
}