package com.njcn.device.pq.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.njcn.common.pojo.response.HttpResult;
import com.njcn.device.pq.enums.LineBaseEnum;
import com.njcn.device.pq.mapper.DeptLineMapper;
import com.njcn.device.pq.mapper.LineMapper;
import com.njcn.device.pq.mapper.TreeMapper;
import com.njcn.device.pq.pojo.dto.GeneralDeviceDTO;
import com.njcn.device.pq.pojo.param.DeviceInfoParam;
import com.njcn.device.pq.pojo.vo.DeptLineTreeVO;
import com.njcn.device.pq.pojo.vo.TerminalTree;
import com.njcn.device.pq.service.TerminalTreeService;
import com.njcn.system.api.AreaFeignClient;
import com.njcn.system.api.DicDataFeignClient;
import com.njcn.system.pojo.enums.StatisticsEnum;
import com.njcn.system.pojo.po.Area;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * pqs
 * 终端设备树
 *
 * @author cdf
 * @date 2021/7/19
 */
@Service
@AllArgsConstructor
public class TerminalTreeServiceImpl implements TerminalTreeService {

    private final LineMapper lineMapper;

    private final AreaFeignClient areaFeignClient;

    private final DicDataFeignClient dicDataFeignClient;

    private final DeptLineMapper deptLineMapper;

    private final GeneralDeviceService generalDeviceService;

    private final TreeMapper treeMapper;

    /**
     * 台账页面终端树
     *
     * @author cdf
     * @date 2022/1/13
     */
    @Override
    public List<TerminalTree> getTerminalTree() {
        List<TerminalTree> taiZhang = new ArrayList<>();
        List<TerminalTree> allList = lineMapper.getAllList();
        List<TerminalTree> projectList = allList.stream().filter(item -> item.getLevel().equals(LineBaseEnum.PROJECT_LEVEL.getCode())).sorted(Comparator.comparing(TerminalTree::getSort)).collect(Collectors.toList());
        List<TerminalTree> provinceList = lineMapper.getProvinceList(null, 0);
        List<TerminalTree> gdList = allList.stream().filter(item -> item.getLevel().equals(LineBaseEnum.GD_LEVEL.getCode())).sorted(Comparator.comparing(TerminalTree::getSort)).collect(Collectors.toList());
        List<TerminalTree> subList = allList.stream().filter(item -> item.getLevel().equals(LineBaseEnum.SUB_LEVEL.getCode())).sorted(Comparator.comparing(TerminalTree::getSort)).collect(Collectors.toList());
        List<TerminalTree> devList = allList.stream().filter(item -> item.getLevel().equals(LineBaseEnum.DEVICE_LEVEL.getCode())).sorted(Comparator.comparing(TerminalTree::getSort)).collect(Collectors.toList());
        List<TerminalTree> subvList = allList.stream().filter(item -> item.getLevel().equals(LineBaseEnum.SUB_V_LEVEL.getCode())).sorted(Comparator.comparing(TerminalTree::getSort)).collect(Collectors.toList());
        List<TerminalTree> lineList = allList.stream().filter(item -> item.getLevel().equals(LineBaseEnum.LINE_LEVEL.getCode())).sorted(Comparator.comparing(TerminalTree::getSort)).collect(Collectors.toList());


        subvList.forEach(subv -> subv.setChildren(getChildren(subv, lineList)));
        devList.forEach(dev -> dev.setChildren(getChildren(dev, subvList)));
        subList.forEach(sub -> sub.setChildren(getChildren(sub, devList)));
        gdList.forEach(gd -> gd.setChildren(getChildren(gd, subList)));
        provinceList.forEach(province -> province.setChildren(getChildren(province, gdList)));
        projectList.forEach(project -> project.setChildren(getChildren(project, provinceList)));

        if (CollectionUtil.isNotEmpty(allList)) {
            TerminalTree terminalTree = new TerminalTree();
            terminalTree.setId("9999999");
            terminalTree.setLevel(0);
            terminalTree.setName("台账管理");
            if (CollectionUtil.isNotEmpty(projectList)) {
                terminalTree.setChildren(projectList);
            }
            taiZhang.add(terminalTree);
        }
        return taiZhang;
    }

    /**
     * 5层树排除设备 母线监测点合并
     *
     * @author cdf
     * @date 2022/1/13
     */
    @Override
    public List<TerminalTree> getTerminalTreeForFive(DeviceInfoParam deviceInfoParam) {
        //deviceInfoParam.setDeptIndex(RequestUtil.getDeptIndex());
        // 获取所有数据
        List<GeneralDeviceDTO> generalDeviceDTOList = generalDeviceService.getDeviceInfo(deviceInfoParam, Stream.of(0).collect(Collectors.toList()), Stream.of(1).collect(Collectors.toList()));
        // 判断所有数据集合状态
        if (CollectionUtil.isNotEmpty(generalDeviceDTOList)) {
            // 创建集合
            List<TerminalTree> taiZhang = new ArrayList<>();
            // 遍历集合
            for (GeneralDeviceDTO generalDeviceDTO : generalDeviceDTOList) {
                // 创建实体类
                TerminalTree terminalTree = new TerminalTree();
                // 判断监测点索引集合状态
                if (CollectionUtils.isEmpty(generalDeviceDTO.getLineIndexes())) {
                    continue;
                }
                // 通过供电公司索引查询省会
                List<TerminalTree> proList = treeMapper.getProvinceList(generalDeviceDTO.getGdIndexes());
                // 通过供电公司索引查询供电公司信息
                List<TerminalTree> gdList = treeMapper.getGdList(generalDeviceDTO.getGdIndexes());
                // 通过供电站索引查询供电站信息
                List<TerminalTree> subList = treeMapper.getSubList(generalDeviceDTO.getSubIndexes());
                // 通过监测点索引查询监测点信息
                List<TerminalTree> lineList = treeMapper.getLineList(generalDeviceDTO.getLineIndexes());

                //处理变电站
                dealChildrenData(subList, lineList, true);

                //监测点前面加序号,后面不需要删除下面两行就行
                //Integer[] arr = {1};
                //subList.forEach(item->item.getChildren().forEach(it->it.setName((arr[0]++ +"_"+it.getName()))));
                //处理供电公司
                dealChildrenData(gdList, subList, false);

                if (deviceInfoParam.getStatisticalType().getCode().equalsIgnoreCase(StatisticsEnum.POWER_NETWORK.getCode())) {
                    terminalTree.setChildren(gdList);
                } else {
                    //还需要额外处理省会
                    dealChildrenData(proList, gdList, false);
                    terminalTree.setChildren(proList);
                }
                terminalTree.setId(generalDeviceDTO.getIndex());
                terminalTree.setName(generalDeviceDTO.getName());
                terminalTree.setLevel(0);
                taiZhang.add(terminalTree);
            }
            return taiZhang;
        } else {
            return new ArrayList<>();
        }
    }

    /**
     * 处理变电站
     * @param targetData
     * @param childrenData
     * @param isLine
     */
    private void dealChildrenData(List<TerminalTree> targetData, List<TerminalTree> childrenData, boolean isLine) {
        // 创建一个map集合，用于封装对象
        Map<String, List<TerminalTree>> groupLine;
        if (isLine) {
            // 通过stream流分组
            groupLine = childrenData.stream().collect(Collectors.groupingBy(terminalTree -> {
                // 获取父id字符串，通过 逗号 分割 成一个数组
                String[] pid = terminalTree.getPids().split(",");
                return pid[LineBaseEnum.SUB_LEVEL.getCode()];
            }));
        } else {
            groupLine = childrenData.stream().collect(Collectors.groupingBy(TerminalTree::getPid));
        }
        //变电站
        targetData = targetData.stream().peek(terminalTree -> {
            if(isLine){
                //变电站集合
                List<TerminalTree> terminalTrees = groupLine.get(terminalTree.getId());

                int size = terminalTrees.stream().map(x -> {
                    // 获取父id字符串，通过 逗号 分割 成一个数组
                    String[] pid = x.getPids().split(",");
                    return pid[LineBaseEnum.DEVICE_LEVEL.getCode()];
                }).distinct().collect(Collectors.toList()).size();

                terminalTree.setName(terminalTree.getName()+"（"+size+"台装置）");

                terminalTree.setChildren(groupLine.get(terminalTree.getId()));
            }else{
                terminalTree.setChildren(groupLine.get(terminalTree.getId()));
            }
        }).collect(Collectors.toList());
    }


    @Override
    public List<DeptLineTreeVO> getDeptBindDeptLineTree(String deptId, Integer deptType, String id, Integer type) {
        List<DeptLineTreeVO> deptLineTreeVOS = new ArrayList<>();
        /**
         * 非自定义树
         */
        if (deptType.equals(0)) {
            DeptLineTreeVO deptLineTreeVO = new DeptLineTreeVO();
            deptLineTreeVO.setNothingBindTree(getNothingDeptBindDeptLineTree(id, type));
            deptLineTreeVO.setOtherBindTree(getOtherDeptBindDeptLineTree(deptId, id, type));
            deptLineTreeVO.setMyselfBindTree(getMyselfDeptBindDeptLineTree(deptId, id, type));
            deptLineTreeVOS.add(deptLineTreeVO);
        } else {
            /**
             * 自定义树
             */
            DeptLineTreeVO deptLineTreeVO = new DeptLineTreeVO();
            deptLineTreeVO.setNothingBindTree(setNothingBindComDeptTree(id, type));
            deptLineTreeVO.setMyselfBindTree(getMyselfDeptBindDeptLineTree(deptId, id, type));
            deptLineTreeVOS.add(deptLineTreeVO);
        }
        return deptLineTreeVOS;
    }


    /**
     * 获取全部子节点
     *
     * @author cdf
     * @date 2021/7/19
     */
    public List<TerminalTree> getChildren(TerminalTree item, List<TerminalTree> all) {
        return all.stream().filter(allItem -> allItem.getPid().equals(item.getId())).collect(Collectors.toList());
    }

    /**
     * 未绑定监测点树
     *
     * @param id
     * @param type
     * @return
     */
    public List<TerminalTree> getNothingDeptBindDeptLineTree(String id, Integer type) {

        List<TerminalTree> nothingBindTree = new ArrayList<>();
        List<TerminalTree> allList = lineMapper.getAllList();
        HttpResult<Area> area = areaFeignClient.areaPro(id, type);
        Area areaDetail = area.getData();
        String areaId = areaDetail.getId();

        if (CollectionUtil.isNotEmpty(allList)) {
            List<TerminalTree> projectList;
            List<TerminalTree> provinceList;
            if(!"0".equals(areaId)){
                projectList=allList.stream().filter(item -> item.getLevel().equals(LineBaseEnum.PROJECT_LEVEL.getCode()) && lineMapper.selectProject(areaId).contains(item.getId())).collect(Collectors.toList());
                provinceList = allList.stream().filter(item -> item.getLevel().equals(LineBaseEnum.PROVINCE_LEVEL.getCode()) && item.getName().equals(areaId)).collect(Collectors.toList());
            }else{
                projectList = allList.stream().filter(item -> item.getLevel().equals(LineBaseEnum.PROJECT_LEVEL.getCode())).collect(Collectors.toList());
                provinceList = allList.stream().filter(item -> item.getLevel().equals(LineBaseEnum.PROVINCE_LEVEL.getCode())).collect(Collectors.toList());
            }
            for (TerminalTree terminalTree : provinceList) {
                terminalTree.setName(lineMapper.getProviceName(terminalTree.getName()));
            }
            List<TerminalTree> gdList = allList.stream().filter(item -> item.getLevel().equals(LineBaseEnum.GD_LEVEL.getCode())).collect(Collectors.toList());
            List<TerminalTree> subList = allList.stream().filter(item -> item.getLevel().equals(LineBaseEnum.SUB_LEVEL.getCode())).collect(Collectors.toList());
            List<TerminalTree> devList = allList.stream().filter(item -> item.getLevel().equals(LineBaseEnum.DEVICE_LEVEL.getCode())).collect(Collectors.toList());
            List<TerminalTree> subvList = allList.stream().filter(item -> item.getLevel().equals(LineBaseEnum.SUB_V_LEVEL.getCode())).collect(Collectors.toList());
            List<TerminalTree> lineList = allList.stream().filter(item -> item.getLevel().equals(LineBaseEnum.LINE_LEVEL.getCode())).collect(Collectors.toList());
            List<String> deptLineList = deptLineMapper.getAllBindLine();
            /**
             * 取出未绑定的监测点
             */
            List<TerminalTree> lineNoBindList = new ArrayList<>();
            for (int i = 0; i < lineList.size(); i++) {
                if (!deptLineList.contains(lineList.get(i).getId())) {
                    lineNoBindList.add(lineList.get(i));
                }
            }
            for (TerminalTree sub : subList) {
                List<TerminalTree> lineTemp = new ArrayList<>();
                List<TerminalTree> devTemp = devList.stream().filter(d -> d.getPid().equals(sub.getId())).collect(Collectors.toList());

                for (TerminalTree dev : devTemp) {
                    List<TerminalTree> subvTemp = subvList.stream().filter(d -> d.getPid().equals(dev.getId())).collect(Collectors.toList());

                    for (TerminalTree subv : subvTemp) {
                        List<TerminalTree> lineRes = lineNoBindList.stream().filter(d -> d.getPid().equals(subv.getId())).map(
                                (item) ->
                                {
                                    String name = subv.getName() + "_" + item.getName();
                                    item.setName(name);
                                    item.setPid(sub.getId());
                                    return item;
                                }).collect(Collectors.toList());
                        lineTemp.addAll(lineRes);

                    }
                }
                sub.setChildren(lineTemp);
            }

            gdList.stream().peek(gd -> {
                gd.setChildren(getChildren(gd, subList));
                gd.setLevel(2);
            }).collect(Collectors.toList());
            provinceList.stream().peek(province -> {
                province.setChildren(getChildren(province, gdList));
                province.setLevel(1);
            }).collect(Collectors.toList());
            nothingBindTree = projectList.stream().peek(project -> {
                project.setChildren(getChildren(project, provinceList));
                project.setLevel(0);
            }).collect(Collectors.toList());

        }
        return nothingBindTree;
    }

    /**
     * 未绑定监测点树(自定义)
     *
     * @param id
     * @param type
     * @return
     */
    public List<TerminalTree> setNothingBindComDeptTree(String id, Integer type) {
        List<TerminalTree> nothingBindTree = new ArrayList<>();
        List<TerminalTree> allList = lineMapper.getAllList();
        HttpResult<Area> area = areaFeignClient.areaPro(id, type);
        Area areaDetail = area.getData();
        String areaId = areaDetail.getId();

        if (CollectionUtil.isNotEmpty(allList)) {
            List<TerminalTree> projectList;
            List<TerminalTree> provinceList;
            if(!"0".equals(areaId)){
                projectList=allList.stream().filter(item -> item.getLevel().equals(LineBaseEnum.PROJECT_LEVEL.getCode()) && lineMapper.selectProject(areaId).contains(item.getId())).collect(Collectors.toList());
                provinceList = allList.stream().filter(item -> item.getLevel().equals(LineBaseEnum.PROVINCE_LEVEL.getCode()) && item.getName().equals(areaId)).collect(Collectors.toList());
            }else{
                projectList = allList.stream().filter(item -> item.getLevel().equals(LineBaseEnum.PROJECT_LEVEL.getCode())).collect(Collectors.toList());
                provinceList = allList.stream().filter(item -> item.getLevel().equals(LineBaseEnum.PROVINCE_LEVEL.getCode())).collect(Collectors.toList());
            }
            for (TerminalTree terminalTree : provinceList) {
                terminalTree.setName(lineMapper.getProviceName(terminalTree.getName()));
            }
            List<TerminalTree> gdList = allList.stream().filter(item -> item.getLevel().equals(LineBaseEnum.GD_LEVEL.getCode())).collect(Collectors.toList());
            List<TerminalTree> subList = allList.stream().filter(item -> item.getLevel().equals(LineBaseEnum.SUB_LEVEL.getCode())).collect(Collectors.toList());
            List<TerminalTree> devList = allList.stream().filter(item -> item.getLevel().equals(LineBaseEnum.DEVICE_LEVEL.getCode())).collect(Collectors.toList());
            List<TerminalTree> subvList = allList.stream().filter(item -> item.getLevel().equals(LineBaseEnum.SUB_V_LEVEL.getCode())).collect(Collectors.toList());
            List<TerminalTree> lineList = allList.stream().filter(item -> item.getLevel().equals(LineBaseEnum.LINE_LEVEL.getCode())).collect(Collectors.toList());
            for (TerminalTree sub : subList) {
                List<TerminalTree> lineTemp = new ArrayList<>();
                List<TerminalTree> devTemp = devList.stream().filter(d -> d.getPid().equals(sub.getId())).collect(Collectors.toList());

                for (TerminalTree dev : devTemp) {
                    List<TerminalTree> subvTemp = subvList.stream().filter(d -> d.getPid().equals(dev.getId())).collect(Collectors.toList());

                    for (TerminalTree subv : subvTemp) {
                        List<TerminalTree> lineRes = lineList.stream().filter(d -> d.getPid().equals(subv.getId())).map(
                                (item) ->
                                {
                                    String name = subv.getName() + "_" + item.getName();
                                    item.setPid(sub.getId());
                                    item.setName(name);
                                    return item;
                                }).collect(Collectors.toList());
                        lineTemp.addAll(lineRes);

                    }
                }
                sub.setChildren(lineTemp);
            }

            gdList.stream().peek(gd -> {
                gd.setChildren(getChildren(gd, subList));
                gd.setLevel(2);
            }).collect(Collectors.toList());
            provinceList.stream().peek(province -> {
                province.setChildren(getChildren(province, gdList));
                province.setLevel(1);
            }).collect(Collectors.toList());
            nothingBindTree = projectList.stream().peek(project -> {
                project.setChildren(getChildren(project, provinceList));
                project.setLevel(0);
            }).collect(Collectors.toList());

        }
        return nothingBindTree;
    }

    /**
     * 其他绑定绑定监测点树
     *
     * @param id
     * @param type
     * @return
     */
    public List<TerminalTree> getOtherDeptBindDeptLineTree(String deptId, String id, Integer type) {
        //其他绑定监测点树
        List<TerminalTree> otherBindTree = new ArrayList<>();
        List<TerminalTree> allList = lineMapper.getAllList();
        HttpResult<Area> area = areaFeignClient.areaPro(id, type);
        Area areaDetail = area.getData();
        String areaId = areaDetail.getId();

        if (CollectionUtil.isNotEmpty(allList)) {
            List<TerminalTree> projectList;
            List<TerminalTree> provinceList;
            if(!"0".equals(areaId)){
                projectList=allList.stream().filter(item -> item.getLevel().equals(LineBaseEnum.PROJECT_LEVEL.getCode()) && lineMapper.selectProject(areaId).contains(item.getId())).collect(Collectors.toList());
                provinceList = allList.stream().filter(item -> item.getLevel().equals(LineBaseEnum.PROVINCE_LEVEL.getCode()) && item.getName().equals(areaId)).collect(Collectors.toList());
            }else{
                projectList = allList.stream().filter(item -> item.getLevel().equals(LineBaseEnum.PROJECT_LEVEL.getCode())).collect(Collectors.toList());
                provinceList = allList.stream().filter(item -> item.getLevel().equals(LineBaseEnum.PROVINCE_LEVEL.getCode())).collect(Collectors.toList());
            }
            for (TerminalTree terminalTree : provinceList) {
                terminalTree.setName(lineMapper.getProviceName(terminalTree.getName()));
            }
            List<TerminalTree> gdList = allList.stream().filter(item -> item.getLevel().equals(LineBaseEnum.GD_LEVEL.getCode())).collect(Collectors.toList());
            List<TerminalTree> subList = allList.stream().filter(item -> item.getLevel().equals(LineBaseEnum.SUB_LEVEL.getCode())).collect(Collectors.toList());
            List<TerminalTree> devList = allList.stream().filter(item -> item.getLevel().equals(LineBaseEnum.DEVICE_LEVEL.getCode())).collect(Collectors.toList());
            List<TerminalTree> subvList = allList.stream().filter(item -> item.getLevel().equals(LineBaseEnum.SUB_V_LEVEL.getCode())).collect(Collectors.toList());
            List<TerminalTree> lineList = allList.stream().filter(item -> item.getLevel().equals(LineBaseEnum.LINE_LEVEL.getCode())).collect(Collectors.toList());
            List<String> deptLineList = deptLineMapper.getOtherBindLine(deptId);

            List<TerminalTree> lineOther = new ArrayList<>();
            for (int i = 0; i < lineList.size(); i++) {
                if (deptLineList.contains(lineList.get(i).getId())) {
                    lineOther.add(lineList.get(i));
                }
            }
            for (TerminalTree sub : subList) {
                List<TerminalTree> lineTemp = new ArrayList<>();
                List<TerminalTree> devTemp = devList.stream().filter(d -> d.getPid().equals(sub.getId())).collect(Collectors.toList());

                for (TerminalTree dev : devTemp) {
                    List<TerminalTree> subvTemp = subvList.stream().filter(d -> d.getPid().equals(dev.getId())).collect(Collectors.toList());

                    for (TerminalTree subv : subvTemp) {
                        List<TerminalTree> lineRes = lineOther.stream().filter(d -> d.getPid().equals(subv.getId())).map(
                                (item) ->
                                {
                                    String name = subv.getName() + "_" + item.getName();
                                    item.setName(name);
                                    item.setPid(sub.getId());
                                    return item;
                                }).collect(Collectors.toList());
                        lineTemp.addAll(lineRes);

                    }
                }
                sub.setChildren(lineTemp);

            }

            gdList.stream().peek(gd -> {
                gd.setChildren(getChildren(gd, subList));
                gd.setLevel(2);
            }).collect(Collectors.toList());
            provinceList.stream().peek(province -> {
                province.setChildren(getChildren(province, gdList));
                province.setLevel(1);
            }).collect(Collectors.toList());
            otherBindTree = projectList.stream().peek(project -> {
                project.setChildren(getChildren(project, provinceList));
                project.setLevel(0);
            }).collect(Collectors.toList());
        }
        return otherBindTree;
    }

    /**
     * 本身绑定绑定监测点树
     *
     * @param id
     * @param type
     * @return
     */
    public List<TerminalTree> getMyselfDeptBindDeptLineTree(String deptId, String id, Integer type) {
        //本身绑定监测点树
        List<TerminalTree> myselfBindTree = new ArrayList<>();
        List<TerminalTree> allList = lineMapper.getAllList();
        HttpResult<Area> area = areaFeignClient.areaPro(id, type);
        Area areaDetail = area.getData();
        String areaId = areaDetail.getId();

        if (CollectionUtil.isNotEmpty(allList)) {
            List<TerminalTree> projectList;
            List<TerminalTree> provinceList;
            if(!"0".equals(areaId)){
                projectList=allList.stream().filter(item -> item.getLevel().equals(LineBaseEnum.PROJECT_LEVEL.getCode()) && lineMapper.selectProject(areaId).contains(item.getId())).collect(Collectors.toList());
                provinceList = allList.stream().filter(item -> item.getLevel().equals(LineBaseEnum.PROVINCE_LEVEL.getCode()) && item.getName().equals(areaId)).collect(Collectors.toList());
            }else{
                projectList = allList.stream().filter(item -> item.getLevel().equals(LineBaseEnum.PROJECT_LEVEL.getCode())).collect(Collectors.toList());
                provinceList = allList.stream().filter(item -> item.getLevel().equals(LineBaseEnum.PROVINCE_LEVEL.getCode())).collect(Collectors.toList());
            }
            for (TerminalTree terminalTree : provinceList) {
                terminalTree.setName(lineMapper.getProviceName(terminalTree.getName()));
            }
            List<TerminalTree> gdList = allList.stream().filter(item -> item.getLevel().equals(LineBaseEnum.GD_LEVEL.getCode())).collect(Collectors.toList());
            List<TerminalTree> subList = allList.stream().filter(item -> item.getLevel().equals(LineBaseEnum.SUB_LEVEL.getCode())).collect(Collectors.toList());
            List<TerminalTree> devList = allList.stream().filter(item -> item.getLevel().equals(LineBaseEnum.DEVICE_LEVEL.getCode())).collect(Collectors.toList());
            List<TerminalTree> subvList = allList.stream().filter(item -> item.getLevel().equals(LineBaseEnum.SUB_V_LEVEL.getCode())).collect(Collectors.toList());
            List<TerminalTree> lineList = allList.stream().filter(item -> item.getLevel().equals(LineBaseEnum.LINE_LEVEL.getCode())).collect(Collectors.toList());
            List<String> deptLineList = deptLineMapper.getMyselfBindLine(deptId);

            List<TerminalTree> lineMyself = new ArrayList<>();
            for (int i = 0; i < lineList.size(); i++) {
                if (deptLineList.contains(lineList.get(i).getId())) {
                    lineMyself.add(lineList.get(i));
                }
            }


            for (TerminalTree sub : subList) {
                List<TerminalTree> lineTemp = new ArrayList<>();
                List<TerminalTree> devTemp = devList.stream().filter(d -> d.getPid().equals(sub.getId())).collect(Collectors.toList());

                for (TerminalTree dev : devTemp) {
                    List<TerminalTree> subvTemp = subvList.stream().filter(d -> d.getPid().equals(dev.getId())).collect(Collectors.toList());

                    for (TerminalTree subv : subvTemp) {
                        List<TerminalTree> lineRes = lineMyself.stream().filter(d -> d.getPid().equals(subv.getId())).map(
                                (item) ->
                                {
                                    String name = subv.getName() + "_" + item.getName();
                                    item.setName(name);
                                    item.setPid(sub.getId());
                                    return item;
                                }).collect(Collectors.toList());
                        lineTemp.addAll(lineRes);

                    }
                }
                sub.setChildren(lineTemp);

            }

            gdList.stream().peek(gd -> {
                gd.setChildren(getChildren(gd, subList));
                gd.setLevel(2);
            }).collect(Collectors.toList());
            provinceList.stream().peek(province -> {
                province.setChildren(getChildren(province, gdList));
                province.setLevel(1);
            }).collect(Collectors.toList());
            myselfBindTree = projectList.stream().peek(project -> {
                project.setChildren(getChildren(project, provinceList));
                project.setLevel(0);
            }).collect(Collectors.toList());
        }
        return myselfBindTree;
    }


}
