FindsChildrenUtil.java 4.5 KB
package com.skua.modules.assessment.utils;

import com.skua.core.util.ConvertUtils;
import com.skua.modules.assessment.model.DataAssessmentItemIdModel;
import com.skua.modules.assessment.model.DataAssessmentItemTreeModel;
import com.skua.modules.assessment.vo.DataAssessmentItemVO;

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

/**
 * 查找对应评分项的表,处理并查找树级数据
 *
 * @author 11389
 */
public class FindsChildrenUtil {

    /**
     * queryTreeList的子方法 ====1=====
     * 该方法是s将SysDepart类型的list集合转换成SysDepartTreeModel类型的集合
     */
    public static List<DataAssessmentItemTreeModel> wrapTreeDataToTreeList(List<DataAssessmentItemVO> recordList) {
        // 在该方法每请求一次,都要对全局list集合进行一次清理
        List<DataAssessmentItemIdModel> idList = new ArrayList<>();
        List<DataAssessmentItemTreeModel> records = new ArrayList<>();
        for (int i = 0; i < recordList.size(); i++) {
            DataAssessmentItemVO dataAssessmentItem = recordList.get(i);
            records.add(new DataAssessmentItemTreeModel(dataAssessmentItem));
        }
        List<DataAssessmentItemTreeModel> tree = findChildren(records, idList);
        setEmptyChildrenAsNull(tree);
        return tree;
    }

//    /**
//     * 获取 DepartIdModel
//     *
//     * @param recordList
//     * @return
//     */
//    public static List<DataAssessmentItemIdModel> wrapTreeDataToDepartIdTreeList(List<DataAssessmentItem> recordList) {
//        // 在该方法每请求一次,都要对全局list集合进行一次清理
//        List<DataAssessmentItemIdModel> idList = new ArrayList<>();
//        List<DataAssessmentItemTreeModel> records = new ArrayList<>();
//        for (int i = 0; i < recordList.size(); i++) {
//            DataAssessmentItem item = recordList.get(i);
//            records.add(new DataAssessmentItemTreeModel(item));
//        }
//        findChildren(records, idList);
//        return idList;
//    }

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

        List<DataAssessmentItemTreeModel> treeList = new ArrayList<>();
        for (int i = 0; i < recordList.size(); i++) {
            DataAssessmentItemTreeModel branch = recordList.get(i);
            if (ConvertUtils.isEmpty(branch.getPid())) {
                treeList.add(branch);
                DataAssessmentItemIdModel itemIdModel = new DataAssessmentItemIdModel().convert(branch);
                itemIdModels.add(itemIdModel);
            }
        }
        getGrandChildren(treeList, recordList, itemIdModels);
        return treeList;
    }

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

        for (int i = 0; i < treeList.size(); i++) {
            DataAssessmentItemTreeModel model = treeList.get(i);
            DataAssessmentItemIdModel idModel = idList.get(i);
            for (int i1 = 0; i1 < recordList.size(); i1++) {
                DataAssessmentItemTreeModel m = recordList.get(i1);
                if (m.getPid() != null && m.getPid().equals(model.getId())) {
                    model.getChildren().add(m);
                    DataAssessmentItemIdModel dim = new DataAssessmentItemIdModel().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<DataAssessmentItemTreeModel> treeList) {

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