通用-递归树结构
- 通用树结构
/** * 获取区域树 * * @param ydDistrictDto * @return */ public DistrictTree getDistrictTree(YdDistrictDto ydDistrictDto) { //权限逻辑暂时空着 List<YdDistrict> districtList = list(new YdDistrictDto()); DistrictTree districtTree = new DistrictTree(); //文山州为最顶层 YdDistrict topDistrict = new YdDistrict(); int topPid = ydDistrictDto.getParentId() == null ? 1 : ydDistrictDto.getParentId(); if (topPid == 0) { topDistrict = ydDistrictService.queryById(1); } else { topDistrict = ydDistrictService.queryById(topPid); } districtTree.setId(topPid); districtTree.setDistrictLevel(topDistrict.getDistrictLevel()); districtTree.setParentId(topDistrict.getParentId()); districtTree.setDistrictName(topDistrict.getDistrictName()); recursionDicTree(districtTree, districtList); return districtTree; }
- 递归-核心逻辑
/** * 递归生成组织树 * * @param districtTree * @param list */ public void recursionDicTree(DistrictTree districtTree, List<YdDistrict> list) { for (YdDistrict ydDistrict : list) { if (districtTree.getId().equals(ydDistrict.getParentId())) { List<DistrictTree> children = districtTree.getChildren(); if (children == null || children.size() == 0) { children = new ArrayList<>(); districtTree.setChildren(children); } DistrictTree subTree = DistrictTree.convertDicTree(ydDistrict); children.add(subTree); recursionDicTree(subTree, list); } } }
- 递归-核心逻辑
/** * 递归生成组织树 * * @param districtTree * @param list */ public void recursionDicTree(DistrictTree districtTree, List<YdDistrict> list) { for (YdDistrict ydDistrict : list) { if (districtTree.getId().equals(ydDistrict.getParentId())) { List<DistrictTree> children = districtTree.getChildren(); if (children == null || children.size() == 0) { children = new ArrayList<>(); districtTree.setChildren(children); } DistrictTree subTree = DistrictTree.convertDicTree(ydDistrict); children.add(subTree); recursionDicTree(subTree, list); } } }
- 树模型
package com.yiding.saas.ydsaas.vo; import com.yiding.saas.ydsaas.model.YdDistrict; import com.yiding.saas.ydsaas.model.YdSmallFrameLog; import java.io.Serializable; import java.math.BigDecimal; import java.util.List; /** * 行政区树对象 */ public class DistrictTree implements Serializable { private static final long serialVersionUID = -8608004181699249859L; private Integer id; private String districtName; private Integer parentId; private String districtLevel; private BigDecimal currentB; private BigDecimal cumulativeB; private BigDecimal outStockB; /** * 未出发 */ private BigDecimal notStart; /** * 在途中 */ private BigDecimal onWay; /** * 已到达 */ private BigDecimal arrivals; public BigDecimal getNotStart() { return notStart; } public void setNotStart(BigDecimal notStart) { this.notStart = notStart; } public BigDecimal getOnWay() { return onWay; } public void setOnWay(BigDecimal onWay) { this.onWay = onWay; } public BigDecimal getArrivals() { return arrivals; } public void setArrivals(BigDecimal arrivals) { this.arrivals = arrivals; } public BigDecimal getCurrentB() { return currentB; } public void setCurrentB(BigDecimal currentB) { this.currentB = currentB; } public BigDecimal getCumulativeB() { return cumulativeB; } public void setCumulativeB(BigDecimal cumulativeB) { this.cumulativeB = cumulativeB; } public BigDecimal getOutStockB() { return outStockB; } public void setOutStockB(BigDecimal outStockB) { this.outStockB = outStockB; } private List<DistrictTree> children; /** * 库存信息统计 */ private List<KcStatisticalVo> kcInfoList; /** * 运单统计 */ private List<TransprotVo> transprotVoList; public List<TransprotVo> getTransprotVoList() { return transprotVoList; } public void setTransprotVoList(List<TransprotVo> transprotVoList) { this.transprotVoList = transprotVoList; } public List<KcStatisticalVo> getKcInfoList() { return kcInfoList; } public void setKcInfoList(List<KcStatisticalVo> kcInfoList) { this.kcInfoList = kcInfoList; } public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getDistrictName() { return districtName; } public void setDistrictName(String districtName) { this.districtName = districtName; } public Integer getParentId() { return parentId; } public void setParentId(Integer parentId) { this.parentId = parentId; } public List<DistrictTree> getChildren() { return children; } public void setChildren(List<DistrictTree> children) { this.children = children; } public String getDistrictLevel() { return districtLevel; } public void setDistrictLevel(String districtLevel) { this.districtLevel = districtLevel; } /** * 行政区模型转树模型 * * @param ydDistrict * @return */ public static DistrictTree convertDicTree(YdDistrict ydDistrict) { DistrictTree districtTree = new DistrictTree(); districtTree.setId(ydDistrict.getId()); districtTree.setDistrictName(ydDistrict.getDistrictName()); districtTree.setParentId(ydDistrict.getParentId()); districtTree.setDistrictLevel(ydDistrict.getDistrictLevel()); districtTree.setCurrentB(ydDistrict.getCurrentkc()); districtTree.setCumulativeB(ydDistrict.getCumulativeAcquisition()); districtTree.setOutStockB(ydDistrict.getOutStock()); return districtTree; } }