From 033599ab61cd73f83a80c634e059426bf15c9743 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=99=88=E8=A3=95=E8=B4=A2?= Date: Fri, 18 Feb 2022 02:01:16 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E8=8A=82=E7=82=B9=E7=B1=BB?= =?UTF-8?q?=E5=9E=8B=E3=80=81=E6=A0=87=E7=AD=BE=E7=AD=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/xm/core/ctrl/XmMenuController.java | 2 + .../com/xm/core/service/XmMenuService.java | 99 ++++++++++++++++++- 2 files changed, 96 insertions(+), 5 deletions(-) diff --git a/xm-core/src/main/java/com/xm/core/ctrl/XmMenuController.java b/xm-core/src/main/java/com/xm/core/ctrl/XmMenuController.java index ae437a31..fd0bce77 100644 --- a/xm-core/src/main/java/com/xm/core/ctrl/XmMenuController.java +++ b/xm-core/src/main/java/com/xm/core/ctrl/XmMenuController.java @@ -170,6 +170,7 @@ public class XmMenuController { xmMenu.setMmUserid(user.getUserid()); xmMenu.setMmUsername(user.getUsername()); } + xmMenuService.parentIdPathsCalcBeforeSave(xmMenu); xmMenuService.insert(xmMenu); m.put("data",xmMenu); @@ -334,6 +335,7 @@ public class XmMenuController { try{ if(xmMenus.size()>0) { + this.xmMenuService.parentIdPathsCalcBeforeSave(xmMenus); this.xmMenuService.doBatchInsert(xmMenus); }else { tips.setFailureMsg("没有数据可以新增,请上送数据"); diff --git a/xm-core/src/main/java/com/xm/core/service/XmMenuService.java b/xm-core/src/main/java/com/xm/core/service/XmMenuService.java index e3c4920e..82419eeb 100644 --- a/xm-core/src/main/java/com/xm/core/service/XmMenuService.java +++ b/xm-core/src/main/java/com/xm/core/service/XmMenuService.java @@ -1,5 +1,6 @@ package com.xm.core.service; +import com.mdp.core.entity.Tips; import com.mdp.core.service.BaseService; import com.xm.core.entity.XmMenu; import com.xm.core.vo.XmMenuVo; @@ -7,9 +8,7 @@ import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import org.springframework.util.StringUtils; -import java.util.ArrayList; -import java.util.List; -import java.util.Map; +import java.util.*; import java.util.stream.Collectors; /** @@ -58,9 +57,10 @@ public class XmMenuService extends BaseService { } } if(addList.size()>0) { - this.batchInsert(addList); + List adds=this.parentIdPathsCalcBeforeSave(addList.stream().map(i->(XmMenu)i).collect(Collectors.toList())); + this.batchInsert(adds); - List list= addList.stream().filter(i->!addList.stream().filter(k->k.getMenuId().equals(i.getPmenuId())).findAny().isPresent()).collect(Collectors.toList()); + List list= adds.stream().filter(i->!adds.stream().filter(k->k.getMenuId().equals(i.getPmenuId())).findAny().isPresent()).collect(Collectors.toList()); list=list.stream().filter(i-> StringUtils.hasText(i.getPmenuId())).collect(Collectors.toList()); if(list.size()>0){ this.updateChildrenCntByIds(list.stream().map(i->i.getPmenuId()).collect(Collectors.toSet()).stream().collect(Collectors.toList())); @@ -80,6 +80,95 @@ public class XmMenuService extends BaseService { return this.selectList("selectExistIterationMenus",map("menuIds",menuIds)); } + public List parentIdPathsCalcBeforeSave(List nodes) { + List noExistsList=nodes.stream().filter(i->!nodes.stream().filter(k->k.getMenuId().equals(i.getPmenuId())).findAny().isPresent()).collect(Collectors.toList()); + noExistsList=noExistsList.stream().filter(i->StringUtils.hasText(i.getPmenuId())).collect(Collectors.toList()); + Map hadCalcMap=new HashMap<>(); + for (XmMenu node : noExistsList) { + if(hadCalcMap.containsKey(node.getPmenuId())){ + String idPaths=hadCalcMap.get(node.getPmenuId()); + node.setPidPaths(idPaths+node.getMenuId()+","); + }else{ + this.parentIdPathsCalcBeforeSave(node); + String idPaths=node.getPidPaths(); + idPaths=idPaths.substring(0,idPaths.length()-node.getMenuId().length()-1); + hadCalcMap.put(node.getPmenuId(),idPaths); + } + } + for (XmMenu node : nodes) { + if(StringUtils.hasText(node.getPmenuId())){ + node.setPidPaths("0,"); + continue; + } + if(hadCalcMap.containsKey(node.getPmenuId())){ + String idPaths=hadCalcMap.get(node.getPmenuId()); + node.setPidPaths(idPaths+node.getMenuId()+","); + }else{ + List pnodeList=this.getParentList(node,nodes); + XmMenu topParent=pnodeList.get(pnodeList.size()-1); + String idPath="0,"; + if(hadCalcMap.containsKey(topParent.getPmenuId())){ + idPath=hadCalcMap.get(topParent.getPmenuId()); + } + for (int i = pnodeList.size() - 1; i >= 0; i--) { + idPath=idPath+pnodeList.get(i).getMenuId()+","; + } + node.setPidPaths(idPath+node.getMenuId()+","); + } + } + return nodes; + } + + public Tips parentIdPathsCalcBeforeSave(XmMenu currNode) { + Tips tips = new Tips("成功"); + if (!StringUtils.hasText(currNode.getPmenuId()) || "0".equals(currNode.getPmenuId())) { + currNode.setPidPaths("0," + currNode.getMenuId() + ","); + return tips; + } else { + List parentList=this.getParentList(currNode); + String idPath="0,"; + for (int i = parentList.size() - 1; i >= 0; i--) { + idPath=idPath+parentList.get(i).getMenuId()+","; + } + currNode.setPidPaths(idPath+currNode.getMenuId()+","); + } + return tips; + } + + private List getParentList(XmMenu currNode){ + List parentList=new ArrayList<>(); + XmMenu current=currNode; + while (true){ + if(!StringUtils.hasText(currNode.getPmenuId()) || "0".equals(currNode.getPmenuId())){ + return parentList; + } + XmMenu query=new XmMenu(); + query.setMenuId(current.getPmenuId()); + current=this.selectOneObject(query); + if(current==null){ + return parentList; + } + parentList.add(current); + } + } + + private List getParentList(XmMenu currNode,List nodes){ + List parentList=new ArrayList<>(); + XmMenu current=currNode; + while (true){ + if(!StringUtils.hasText(currNode.getPmenuId()) || "0".equals(currNode.getPmenuId())){ + return parentList; + } + XmMenu query=new XmMenu(); + query.setMenuId(current.getPmenuId()); + current=nodes.stream().filter(i->i.getMenuId().equals(query.getMenuId())).findFirst().get(); + if(current==null){ + return parentList; + } + parentList.add(current); + } + } + @Transactional public int insert(XmMenu xmMenu) { int i= super.insert(xmMenu);