通用-遞迴樹結構

万树飞花發表於2024-03-27
  • 通用樹結構
     /**
         * 獲取區域樹
         *
         * @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;
    }
}

相關文章