陈裕财 4 years ago
parent
commit
ae6c1d5007
  1. 16
      xm-core/src/main/java/com/xm/core/ctrl/XmFuncController.java
  2. 128
      xm-core/src/main/java/com/xm/core/service/XmFuncService.java

16
xm-core/src/main/java/com/xm/core/ctrl/XmFuncController.java

@ -20,8 +20,7 @@ import springfox.documentation.annotations.ApiIgnore;
import java.util.*; import java.util.*;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import static com.mdp.core.utils.BaseUtils.fromMap;
import static com.mdp.core.utils.BaseUtils.toMap;
import static com.mdp.core.utils.BaseUtils.*;
import static com.mdp.core.utils.ResponseHelper.failed; import static com.mdp.core.utils.ResponseHelper.failed;
/** /**
@ -90,6 +89,7 @@ public class XmFuncController {
return failed("pk-exists","编号重复,请修改编号再提交"); return failed("pk-exists","编号重复,请修改编号再提交");
} }
} }
xmFuncService.parentIdPathsCalcBeforeSave(xmFunc);
xmFuncService.insert(xmFunc); xmFuncService.insert(xmFunc);
m.put("data",xmFunc); m.put("data",xmFunc);
}catch (BizException e) { }catch (BizException e) {
@ -119,6 +119,10 @@ public class XmFuncController {
if( xmFuncDb == null ){ if( xmFuncDb == null ){
return failed("data-not-exists","数据不存在,无法删除"); return failed("data-not-exists","数据不存在,无法删除");
} }
Long childcnt=xmFuncService.countByWhere(map("pid",xmFuncDb.getId()));
if(childcnt>0){
return failed("childcnt-not-0","至少还有"+childcnt+"个子节点,请先删除子节点,再删除父节点");
}
xmFuncService.deleteByPk(xmFunc); xmFuncService.deleteByPk(xmFunc);
}catch (BizException e) { }catch (BizException e) {
tips=e.getTips(); tips=e.getTips();
@ -177,6 +181,7 @@ public class XmFuncController {
Set<String> fields=new HashSet<>(); Set<String> fields=new HashSet<>();
fields.add("id"); fields.add("id");
fields.add("pidPaths");
for (String fieldName : xmFuncMap.keySet()) { for (String fieldName : xmFuncMap.keySet()) {
if(fields.contains(fieldName)){ if(fields.contains(fieldName)){
return failed(fieldName+"-no-edit",fieldName+"不允许修改"); return failed(fieldName+"-no-edit",fieldName+"不允许修改");
@ -205,6 +210,13 @@ public class XmFuncController {
} }
} }
if(can.size()>0){ if(can.size()>0){
if(xmFuncMap.containsKey("pid")){
if(can.size()>1){
return failed("pid-1","修改上级归属只能一个个节点修改,不能批量修改");
}
xmFuncService.parentIdPathsCalcBeforeSave(can.get(0));
xmFuncMap.put("pidPaths",can.get(0).getPidPaths());
}
xmFuncMap.put("ids",can.stream().map(i->i.getId()).collect(Collectors.toList())); xmFuncMap.put("ids",can.stream().map(i->i.getId()).collect(Collectors.toList()));
xmFuncService.editSomeFields(xmFuncMap); xmFuncService.editSomeFields(xmFuncMap);
} }

128
xm-core/src/main/java/com/xm/core/service/XmFuncService.java

@ -1,16 +1,16 @@
package com.xm.core.service; package com.xm.core.service;
import com.mdp.core.entity.Tips;
import com.mdp.core.service.BaseService;
import com.xm.core.entity.XmFunc;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import java.util.List;
import java.util.Map;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import com.mdp.core.service.BaseService;
import static com.mdp.core.utils.BaseUtils.*;
import com.mdp.core.entity.Tips;
import com.mdp.core.err.BizException;
import org.springframework.util.StringUtils;
import java.util.*;
import java.util.stream.Collectors;
import com.xm.core.entity.XmFunc;
/** /**
* 父类已经支持增删改查操作,因此,即使本类什么也不写,也已经可以满足一般的增删改查操作了.<br> * 父类已经支持增删改查操作,因此,即使本类什么也不写,也已经可以满足一般的增删改查操作了.<br>
* 组织 com 顶级模块 xm 大模块 core 小模块 <br> * 组织 com 顶级模块 xm 大模块 core 小模块 <br>
@ -20,5 +20,119 @@ import com.xm.core.entity.XmFunc;
public class XmFuncService extends BaseService { public class XmFuncService extends BaseService {
static Logger logger =LoggerFactory.getLogger(XmFuncService.class); static Logger logger =LoggerFactory.getLogger(XmFuncService.class);
public List<XmFunc> parentIdPathsCalcBeforeSave(List<XmFunc> nodes) {
List<XmFunc> noExistsList = nodes.stream().filter(i -> !nodes.stream().filter(k -> k.getId().equals(i.getPid())).findAny().isPresent()).collect(Collectors.toList());
noExistsList = noExistsList.stream().filter(i -> StringUtils.hasText(i.getPid())).collect(Collectors.toList());
Map<String, String> hadCalcMap = new HashMap<>();
for (XmFunc node : noExistsList) {
if (hadCalcMap.containsKey(node.getPid())) {
String idPaths = hadCalcMap.get(node.getPid());
node.setPidPaths(idPaths + node.getId() + ",");
} else {
this.parentIdPathsCalcBeforeSave(node);
String idPaths = node.getPidPaths();
idPaths = idPaths.substring(0, idPaths.length() - node.getId().length() - 1);
hadCalcMap.put(node.getPid(), idPaths);
}
}
for (XmFunc node : nodes) {
if (!StringUtils.hasText(node.getPid())) {
node.setPidPaths("0," + node.getId() + ",");
continue;
}
if (hadCalcMap.containsKey(node.getPid())) {
String idPaths = hadCalcMap.get(node.getPid());
node.setPidPaths(idPaths + node.getId() + ",");
} else {
List<XmFunc> pnodeList = this.getParentList(node, nodes);
if (pnodeList == null || pnodeList.size() == 0) {
node.setPidPaths("0," + node.getId() + ",");
node.setPid(null);
continue;
}
XmFunc topParent = pnodeList.get(pnodeList.size() - 1);
String idPath = "0,";
if (hadCalcMap.containsKey(topParent.getPid())) {
idPath = hadCalcMap.get(topParent.getPid());
}
for (int i = pnodeList.size() - 1; i >= 0; i--) {
idPath = idPath + pnodeList.get(i).getId() + ",";
}
node.setPidPaths(idPath + node.getId() + ",");
}
}
for (XmFunc node : nodes) {
String idPaths = node.getPidPaths();
String[] idpss = idPaths.split(",");
node.setLvl(idpss.length - 1);
}
return nodes;
}
public Tips parentIdPathsCalcBeforeSave(XmFunc currNode) {
Tips tips = new Tips("成功");
if (!StringUtils.hasText(currNode.getPid()) || "0".equals(currNode.getPid())) {
currNode.setPidPaths("0," + currNode.getId() + ",");
currNode.setLvl(1);
return tips;
} else {
List<XmFunc> parentList = this.getParentList(currNode);
if (parentList == null || parentList.size() == 0) {
currNode.setPidPaths("0," + currNode.getId() + ",");
currNode.setPid(null);
currNode.setLvl(1);
return tips;
}
String idPath = "0,";
for (int i = parentList.size() - 1; i >= 0; i--) {
idPath = idPath + parentList.get(i).getId() + ",";
}
currNode.setPidPaths(idPath + currNode.getId() + ",");
String idPaths = currNode.getPidPaths();
String[] idpss = idPaths.split(",");
currNode.setLvl(idpss.length - 1);
}
return tips;
}
private List<XmFunc> getParentList(XmFunc currNode) {
List<XmFunc> parentList = new ArrayList<>();
XmFunc current = currNode;
while (true) {
if (!StringUtils.hasText(current.getPid()) || "0".equals(current.getPid()) || current.getId().equals(current.getPid())) {
return parentList;
}
XmFunc query = new XmFunc();
query.setId(current.getPid());
current = this.selectOneObject(query);
if (current == null) {
return parentList;
}
parentList.add(current);
}
}
private List<XmFunc> getParentList(XmFunc currNode, List<XmFunc> nodes) {
List<XmFunc> parentList = new ArrayList<>();
XmFunc current = currNode;
while (true) {
if (!StringUtils.hasText(current.getPid()) || "0".equals(current.getPid()) || current.getId().equals(current.getPid())) {
return parentList;
}
XmFunc query = new XmFunc();
query.setId(current.getPid());
Optional<XmFunc> optional = nodes.stream().filter(i -> i.getId().equals(query.getId())).findFirst();
if (optional.isPresent()) {
current = optional.get();
parentList.add(current);
} else {
return parentList;
}
}
}
} }
Loading…
Cancel
Save