From 4d597e8edd61c38cedb0a26130bb4de2bbe90878 Mon Sep 17 00:00:00 2001 From: fengshuonan Date: Fri, 15 Sep 2023 17:26:41 +0800 Subject: [PATCH] =?UTF-8?q?=E3=80=908.0=E3=80=91=E3=80=90menu=E3=80=91?= =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E6=9D=83=E9=99=90=E7=95=8C=E9=9D=A2=E8=8F=9C?= =?UTF-8?q?=E5=8D=95=E5=88=97=E8=A1=A8=E7=9A=84=E6=8E=92=E5=BA=8F=E9=97=AE?= =?UTF-8?q?=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../menu/service/impl/SysMenuServiceImpl.java | 15 ++-- .../modular/menu/util/MenuOrderFixUtil.java | 74 +++++++++++++++++++ 2 files changed, 83 insertions(+), 6 deletions(-) create mode 100644 kernel-s-system/system-business-permission/src/main/java/cn/stylefeng/roses/kernel/sys/modular/menu/util/MenuOrderFixUtil.java diff --git a/kernel-s-system/system-business-permission/src/main/java/cn/stylefeng/roses/kernel/sys/modular/menu/service/impl/SysMenuServiceImpl.java b/kernel-s-system/system-business-permission/src/main/java/cn/stylefeng/roses/kernel/sys/modular/menu/service/impl/SysMenuServiceImpl.java index 069473de3..43d80d8f6 100644 --- a/kernel-s-system/system-business-permission/src/main/java/cn/stylefeng/roses/kernel/sys/modular/menu/service/impl/SysMenuServiceImpl.java +++ b/kernel-s-system/system-business-permission/src/main/java/cn/stylefeng/roses/kernel/sys/modular/menu/service/impl/SysMenuServiceImpl.java @@ -25,6 +25,7 @@ import cn.stylefeng.roses.kernel.sys.modular.menu.mapper.SysMenuMapper; import cn.stylefeng.roses.kernel.sys.modular.menu.pojo.request.SysMenuRequest; import cn.stylefeng.roses.kernel.sys.modular.menu.pojo.response.AppGroupDetail; import cn.stylefeng.roses.kernel.sys.modular.menu.service.SysMenuService; +import cn.stylefeng.roses.kernel.sys.modular.menu.util.MenuOrderFixUtil; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import org.springframework.stereotype.Service; @@ -151,16 +152,13 @@ public class SysMenuServiceImpl extends ServiceImpl impl @Override public List getTotalMenus() { - LambdaQueryWrapper menuLambdaQueryWrapper = new LambdaQueryWrapper<>(); - menuLambdaQueryWrapper.select(SysMenu::getMenuId, SysMenu::getMenuName, SysMenu::getMenuParentId, SysMenu::getAppId); - menuLambdaQueryWrapper.orderByAsc(SysMenu::getMenuSort); - return this.list(menuLambdaQueryWrapper); + return this.getTotalMenus(null); } @Override public List getTotalMenus(Set limitMenuIds) { LambdaQueryWrapper menuLambdaQueryWrapper = new LambdaQueryWrapper<>(); - menuLambdaQueryWrapper.select(SysMenu::getMenuId, SysMenu::getMenuName, SysMenu::getMenuParentId, SysMenu::getAppId); + menuLambdaQueryWrapper.select(SysMenu::getMenuId, SysMenu::getMenuName, SysMenu::getMenuParentId, SysMenu::getAppId, SysMenu::getMenuSort); // 如果限制菜单不为空,则根据限制菜单id进行筛选,否则查询所有菜单 if (ObjectUtil.isNotEmpty(limitMenuIds)) { @@ -168,7 +166,12 @@ public class SysMenuServiceImpl extends ServiceImpl impl } menuLambdaQueryWrapper.orderByAsc(SysMenu::getMenuSort); - return this.list(menuLambdaQueryWrapper); + List list = this.list(menuLambdaQueryWrapper); + + // 对菜单进行再次排序,因为有的菜单是101,有的菜单是10101,需要将位数小的补0,再次排序 + MenuOrderFixUtil.fixOrder(list); + + return list; } @Override diff --git a/kernel-s-system/system-business-permission/src/main/java/cn/stylefeng/roses/kernel/sys/modular/menu/util/MenuOrderFixUtil.java b/kernel-s-system/system-business-permission/src/main/java/cn/stylefeng/roses/kernel/sys/modular/menu/util/MenuOrderFixUtil.java new file mode 100644 index 000000000..ca127799f --- /dev/null +++ b/kernel-s-system/system-business-permission/src/main/java/cn/stylefeng/roses/kernel/sys/modular/menu/util/MenuOrderFixUtil.java @@ -0,0 +1,74 @@ +package cn.stylefeng.roses.kernel.sys.modular.menu.util; + +import cn.hutool.core.util.ObjectUtil; +import cn.stylefeng.roses.kernel.sys.modular.menu.entity.SysMenu; + +import java.math.BigDecimal; +import java.util.Comparator; +import java.util.List; + +/** + * 菜单排序进行修复 + *

+ * 对菜单进行再次排序,因为有的菜单是101,有的菜单是10101,需要将位数小的补0,再次排序 + * + * @author fengshuonan + * @since 2023/9/15 17:14 + */ +public class MenuOrderFixUtil { + + /** + * 修复菜单排序 + * + * @author fengshuonan + * @since 2023/9/15 17:15 + */ + public static void fixOrder(List sysMenus) { + if (ObjectUtil.isEmpty(sysMenus)) { + return; + } + + // 找到最大的数字位数 + int maxDigitCount = 0; + for (SysMenu sysMenu : sysMenus) { + BigDecimal menuSort = sysMenu.getMenuSort(); + if (menuSort == null) { + continue; + } + int digitCount = getPointLeftDigitCount(menuSort); + if (digitCount > maxDigitCount) { + maxDigitCount = digitCount; + } + } + + for (SysMenu sysMenu : sysMenus) { + BigDecimal menuSort = sysMenu.getMenuSort(); + if (menuSort == null) { + menuSort = new BigDecimal(0); + } + int digitCount = getPointLeftDigitCount(menuSort); + if (digitCount < maxDigitCount) { + menuSort = menuSort.multiply(BigDecimal.valueOf(Math.pow(10, maxDigitCount - digitCount))); + } + sysMenu.setMenuSort(menuSort); + } + + sysMenus.sort(Comparator.comparing(SysMenu::getMenuSort)); + } + + /** + * 获取一个数字的小数点左边的位数 + * + * @author fengshuonan + * @since 2023/9/15 17:19 + */ + public static int getPointLeftDigitCount(BigDecimal bigDecimal) { + if (bigDecimal == null) { + return 0; + } + String bigDecimalStr = bigDecimal.toString(); + int decimalIndex = bigDecimalStr.indexOf('.'); + return decimalIndex >= 0 ? decimalIndex : bigDecimalStr.length(); + } + +}