FindEmergencyChildrenUtil.java 5.0 KB
package com.skua.modules.emergency.util;

import com.skua.core.util.ConvertUtils;
import com.skua.modules.emergency.entity.EmergencyCommandLiaison;
import com.skua.modules.emergency.model.EmergencyCommandLiaisonIdModel;
import com.skua.modules.emergency.model.EmergencyCommandLiaisonTreeModel;
import com.skua.modules.emergency.service.IEmergencyCommandLiaisonUserService;
import com.skua.modules.emergency.vo.EmergencyCommandLiaisonVO;
import com.skua.modules.system.entity.SysDepart;
import com.skua.modules.system.entity.SysUser;
import com.skua.modules.system.model.DepartIdModel;
import com.skua.modules.system.model.SysDepartTreeModel;
import com.skua.modules.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.List;

/**
 * 对应应急的表,处理并查找树级数据
 * @author 11389
 */
public class FindEmergencyChildrenUtil {

    /**
     * queryTreeList的子方法 ====1=====
     * 该方法是将EmergencyCommandLiaison类型的list集合转换成EmergencyCommandLiaisonTreeModel类型的集合
     */
    public static List<EmergencyCommandLiaisonTreeModel> wrapTreeDataToTreeList(List<EmergencyCommandLiaisonVO> recordList) {
        List<EmergencyCommandLiaisonIdModel> idList = new ArrayList<EmergencyCommandLiaisonIdModel>();
        List<EmergencyCommandLiaisonTreeModel> records = new ArrayList<>();
        for (int i = 0; i < recordList.size(); i++) {
            EmergencyCommandLiaisonVO emergencyCommandLiaisonVO = recordList.get(i);
            records.add(new EmergencyCommandLiaisonTreeModel(emergencyCommandLiaisonVO));
        }
        List<EmergencyCommandLiaisonTreeModel> tree = findChildren(records, idList);
        setEmptyChildrenAsNull(tree);
        return tree;
    }

    /**
     * 获取 DepartIdModel
     * @param recordList
     * @return
     */
    public List<EmergencyCommandLiaisonIdModel> wrapTreeDataToDepartIdTreeList(List<EmergencyCommandLiaisonVO> recordList) {
        List<EmergencyCommandLiaisonIdModel> idList = new ArrayList<>();
        List<EmergencyCommandLiaisonTreeModel> records = new ArrayList<>();
        for (int i = 0; i < recordList.size(); i++) {
            EmergencyCommandLiaisonVO emergencyCommandLiaison = recordList.get(i);
            records.add(new EmergencyCommandLiaisonTreeModel(emergencyCommandLiaison));
        }
        findChildren(records, idList);
        return idList;
    }

    /**
     * queryTreeList的子方法 ====2=====
     * 该方法是找到并封装顶级父类的节点到TreeList集合
     */
    private static List<EmergencyCommandLiaisonTreeModel> findChildren(List<EmergencyCommandLiaisonTreeModel> recordList,
                                                         List<EmergencyCommandLiaisonIdModel> idModelList) {

        List<EmergencyCommandLiaisonTreeModel> treeList = new ArrayList<>();
        for (int i = 0; i < recordList.size(); i++) {
            EmergencyCommandLiaisonTreeModel branch = recordList.get(i);
            if (ConvertUtils.isEmpty(branch.getParentId())) {
                treeList.add(branch);
                EmergencyCommandLiaisonIdModel idModel = new EmergencyCommandLiaisonIdModel().convert(branch);
                idModelList.add(idModel);
            }
        }
        getGrandChildren(treeList,recordList,idModelList);
        return treeList;
    }

    /**
     * queryTreeList的子方法====3====
     *该方法是找到顶级父类下的所有子节点集合并封装到TreeList集合
     */
    private static void getGrandChildren(List<EmergencyCommandLiaisonTreeModel> treeList,List<EmergencyCommandLiaisonTreeModel> recordList,List<EmergencyCommandLiaisonIdModel> idList) {

        for (int i = 0; i < treeList.size(); i++) {
            EmergencyCommandLiaisonTreeModel model = treeList.get(i);
            EmergencyCommandLiaisonIdModel idModel = idList.get(i);
            for (int i1 = 0; i1 < recordList.size(); i1++) {
                EmergencyCommandLiaisonTreeModel m = recordList.get(i1);
                if (m.getParentId()!=null && m.getParentId().equals(model.getId())) {
                    model.getChildren().add(m);
                    EmergencyCommandLiaisonIdModel dim = new EmergencyCommandLiaisonIdModel().convert(m);
                    idModel.getChildren().add(dim);
                }
            }
            getGrandChildren(treeList.get(i).getChildren(), recordList, idList.get(i).getChildren());
        }

    }

    /**
     * queryTreeList的子方法 ====4====
     * 该方法是将子节点为空的List集合设置为Null值
     */
    private static void setEmptyChildrenAsNull(List<EmergencyCommandLiaisonTreeModel> treeList) {

        for (int i = 0; i < treeList.size(); i++) {
            EmergencyCommandLiaisonTreeModel model = treeList.get(i);
            if (model.getChildren().size() == 0) {
                model.setChildren(null);
                model.setLeaf(true);
            }else{
                setEmptyChildrenAsNull(model.getChildren());
                model.setLeaf(false);
            }
        }
    }
}