From 76ec2a7c79ca34b96cf928d105040e0992f548d8 Mon Sep 17 00:00:00 2001 From: xuyuxiang Date: Sun, 7 Sep 2025 22:23:33 +0800 Subject: [PATCH] =?UTF-8?q?=E3=80=90=E4=BF=AE=E5=A4=8D=E3=80=91=E4=BF=AE?= =?UTF-8?q?=E5=A4=8D=E5=A4=9A=E4=B8=AA=E6=A0=B9=E8=8A=82=E7=82=B9=E7=BB=84?= =?UTF-8?q?=E7=BB=87=E6=9C=BA=E6=9E=84=E6=83=85=E5=86=B5=E4=B8=8Btree?= =?UTF-8?q?=E6=8E=A5=E5=8F=A3=E6=8E=92=E5=BA=8F=E4=B8=8D=E4=B8=80=E8=87=B4?= =?UTF-8?q?=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../org/service/impl/BizOrgServiceImpl.java | 18 ++++++++++-------- .../org/service/impl/SysOrgServiceImpl.java | 4 ++++ 2 files changed, 14 insertions(+), 8 deletions(-) diff --git a/snowy-plugin/snowy-plugin-biz/src/main/java/vip/xiaonuo/biz/modular/org/service/impl/BizOrgServiceImpl.java b/snowy-plugin/snowy-plugin-biz/src/main/java/vip/xiaonuo/biz/modular/org/service/impl/BizOrgServiceImpl.java index 6545e0f5..a56c9709 100644 --- a/snowy-plugin/snowy-plugin-biz/src/main/java/vip/xiaonuo/biz/modular/org/service/impl/BizOrgServiceImpl.java +++ b/snowy-plugin/snowy-plugin-biz/src/main/java/vip/xiaonuo/biz/modular/org/service/impl/BizOrgServiceImpl.java @@ -114,16 +114,18 @@ public class BizOrgServiceImpl extends ServiceImpl impleme } else { return CollectionUtil.newArrayList(); } - // 先根据排序码排序 - List bizOrgArrayList = CollectionUtil.sort(bizOrgSet, Comparator.comparingInt(BizOrg::getSortCode)); - // 再重置排序码,解决每次相同排序码顺序不一致的问题 - for (int i = 0; i < bizOrgArrayList.size(); i++) { - bizOrgArrayList.get(i).setSortCode(i); - } + + // 修复:使用稳定的排序方式,首先按排序码排序,然后按机构ID排序作为次级条件 + List bizOrgArrayList = new ArrayList<>(bizOrgSet); + bizOrgArrayList.sort(Comparator.comparingInt(BizOrg::getSortCode) + .thenComparing(BizOrg::getId)); // 添加ID作为次级排序条件 + + // 转换为TreeNode并构建树 List> treeNodeList = bizOrgArrayList.stream().map(bizOrg -> - new TreeNode<>(bizOrg.getId(), bizOrg.getParentId(), - bizOrg.getName(), bizOrg.getSortCode()).setExtra(JSONUtil.parseObj(bizOrg))) + new TreeNode<>(bizOrg.getId(), bizOrg.getParentId(), + bizOrg.getName(), bizOrg.getSortCode()).setExtra(JSONUtil.parseObj(bizOrg))) .collect(Collectors.toList()); + return TreeUtil.build(treeNodeList, "0"); } diff --git a/snowy-plugin/snowy-plugin-sys/src/main/java/vip/xiaonuo/sys/modular/org/service/impl/SysOrgServiceImpl.java b/snowy-plugin/snowy-plugin-sys/src/main/java/vip/xiaonuo/sys/modular/org/service/impl/SysOrgServiceImpl.java index d3714047..fdf983b8 100644 --- a/snowy-plugin/snowy-plugin-sys/src/main/java/vip/xiaonuo/sys/modular/org/service/impl/SysOrgServiceImpl.java +++ b/snowy-plugin/snowy-plugin-sys/src/main/java/vip/xiaonuo/sys/modular/org/service/impl/SysOrgServiceImpl.java @@ -50,6 +50,7 @@ import vip.xiaonuo.sys.modular.user.entity.SysUser; import vip.xiaonuo.sys.modular.user.enums.SysUserStatusEnum; import vip.xiaonuo.sys.modular.user.service.SysUserService; +import java.util.Comparator; import java.util.Iterator; import java.util.List; import java.util.stream.Collectors; @@ -100,6 +101,9 @@ public class SysOrgServiceImpl extends ServiceImpl impleme @Override public List> tree() { List sysOrgList = this.getAllOrgList(); + // 使用稳定的排序方式,首先按排序码排序,然后按机构ID排序作为次级条件 + sysOrgList.sort(Comparator.comparingInt(SysOrg::getSortCode) + .thenComparing(SysOrg::getId)); // 添加ID作为次级排序条件 List> treeNodeList = sysOrgList.stream().map(sysOrg -> new TreeNode<>(sysOrg.getId(), sysOrg.getParentId(), sysOrg.getName(), sysOrg.getSortCode()).setExtra(JSONUtil.parseObj(sysOrg)))