package org.jeecg.modules.mdc.util; import org.jeecg.common.util.oConvertUtils; import org.jeecg.modules.mdc.model.MdcEquipmentTree; import org.jeecg.modules.system.entity.SysDepart; import org.jeecg.modules.system.model.SysDepartTreeModel; import java.util.ArrayList; import java.util.List; /** * 部门设备树 * @author: LiuS * @create: 2023-03-31 14:53 */ public class FindsEquipmentDepartUtil { /** * 获取 MdcEquipmentTree */ public static List wrapEquipmentDepartTreeList(List recordList) { List idList = new ArrayList<>(); List records = new ArrayList<>(); for (SysDepart sysDepart : recordList) { records.add(new SysDepartTreeModel(sysDepart)); } getChildren(records, idList); setEmptyChildrenAsNull(idList); return idList; } /** * 该方法是找到并封装顶级父类的节点到TreeList集合 */ private static void getChildren(List recordList, List idList) { List treeList = new ArrayList<>(); for (SysDepartTreeModel sysDepartTreeModel : recordList) { if (oConvertUtils.isEmpty(sysDepartTreeModel.getParentId())) { treeList.add(sysDepartTreeModel); idList.add(new MdcEquipmentTree().convertByDepart(sysDepartTreeModel)); } } getGrandChildren(treeList, recordList, idList); } /** *该方法是找到顶级父类下的所有子节点集合并封装到TreeList集合 */ private static void getGrandChildren(List treeList, List recordList, List idList) { for (int i = 0; i < treeList.size(); i++) { SysDepartTreeModel model = treeList.get(i); MdcEquipmentTree mdcEquipmentTree = idList.get(i); for (int i1 = 0; i1 < recordList.size(); i1++) { SysDepartTreeModel m = recordList.get(i1); if (m.getParentId() != null && m.getParentId().equals(model.getId())) { model.getChildren().add(m); mdcEquipmentTree.getChildren().add(new MdcEquipmentTree().convertByDepart(m)); } } getGrandChildren(treeList.get(i).getChildren(), recordList, idList.get(i).getChildren()); } } /** * 该方法是将子节点为空的List集合设置为Null值 */ private static void setEmptyChildrenAsNull(List idList) { for (MdcEquipmentTree mdcEquipmentTree : idList) { if (mdcEquipmentTree.getChildren().isEmpty()) { mdcEquipmentTree.setLeaf(true); } else { setEmptyChildrenAsNull(mdcEquipmentTree.getChildren()); mdcEquipmentTree.setLeaf(false); } } } }