package com.njcn.device.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.njcn.common.pojo.dto.SimpleDTO;
import com.njcn.common.pojo.enums.common.ServerEnum;
import com.njcn.common.utils.EnumUtils;
import com.njcn.device.enums.LineBaseEnum;
import com.njcn.device.pojo.bo.BaseLineInfo;
import com.njcn.device.pojo.bo.DeviceType;
import com.njcn.device.pojo.dto.GeneralDeviceDTO;
import com.njcn.device.pojo.param.DeviceInfoParam;
import com.njcn.device.pojo.po.DeptLine;
import com.njcn.device.pojo.po.Line;
import com.njcn.device.service.DeptLineService;
import com.njcn.device.service.TerminalBaseService;
import com.njcn.redis.utils.RedisUtil;
import com.njcn.system.api.DicDataFeignClient;
import com.njcn.system.enums.DicDataTypeEnum;
import com.njcn.system.pojo.enums.StatisticsEnum;
import com.njcn.system.pojo.po.DictData;
import com.njcn.user.api.DeptFeignClient;
import com.njcn.user.pojo.dto.DeptDTO;
import com.njcn.web.utils.WebUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 终端信息处理器，根据需求返回笼统的台账信息。
 * 包括：类别名称、类别索引、监测点索引集合、终端索引集合、变电站索引集合、供电公司索引集合。
 * PS:若后期需要比如：省会、项目时再动态添加。
 *
 * @author hongawen
 * @version 1.0.0
 * @date 2022年02月11日 09:29
 */
@Slf4j
@Component
@RequiredArgsConstructor
public class GeneralDeviceService {

    private final DeptFeignClient deptFeignClient;

    private final DeptLineService deptLineService;

    private final TerminalBaseService terminalBaseService;

    private final DicDataFeignClient dicDataFeignClient;

    private final RedisUtil redisUtil;


    /**
     * 根据部门id、远程服务名、远程客户端类型，以部门的方式
     *
     * @param runFlag  终端状态
     * @param devModel 终端模型
     * @return 部门分类终端信息
     */
    public List<GeneralDeviceDTO> getDeviceInfoAsDept(DeviceInfoParam deviceInfoParam, List<Integer> runFlag, List<Integer> devModel) {
        List<GeneralDeviceDTO> deviceInfos = new ArrayList<>();
        List<Integer> deptType = WebUtil.filterDeptType();
        //获取包括当前部门的后代所有部门信息
        List<DeptDTO> deptInfos = deptFeignClient.getDeptDescendantIndexes(deviceInfoParam.getDeptIndex(), deptType).getData();

        DeviceType deviceType = new DeviceType();
        if (CollectionUtil.isEmpty(devModel)) {
            deviceType.setDevModel(null);
        } else {
            deviceType.setDevModel(devModel);
        }
        if (CollectionUtil.isEmpty(runFlag)) {
            deviceType.setRunFlag(null);
        } else {
            deviceType.setRunFlag(runFlag);
        }
        filterDataType(deviceType, deviceInfoParam.getServerName());

        List<DeptDTO> directDeptInfos = deptInfos.stream()
                .filter(deptDTO -> deptDTO.getPid().equals(deviceInfoParam.getDeptIndex()))
                .collect(Collectors.toList());
        if (CollectionUtil.isEmpty(directDeptInfos)) {
            //没有直接子部门，获取当前部门所有信息
            List<DeptDTO> dept = deptInfos.stream()
                    .filter(deptDTO -> deptDTO.getId().equals(deviceInfoParam.getDeptIndex()))
                    .collect(Collectors.toList());
            deviceInfos.add(getGeneralDeviceInfoAsDept(dept.get(0), deviceType, Collections.singletonList(deviceInfoParam.getDeptIndex()), deviceInfoParam));
        } else {
            for (DeptDTO deptDTO : directDeptInfos) {
                //筛选上级部门pids包含该id的所有部门
                List<DeptDTO> descendantDeptDTO = deptInfos.stream()
                        .filter(d -> d.getPids().contains(deptDTO.getId()))
                        .collect(Collectors.toList());
                //形成需要查询监测点的部门索引
                List<String> indexes = descendantDeptDTO.stream()
                        .map(DeptDTO::getId)
                        .distinct()
                        .collect(Collectors.toList());
                indexes.add(deptDTO.getId());
                deviceInfos.add(getGeneralDeviceInfoAsDept(deptDTO, deviceType, indexes, deviceInfoParam));
            }
        }
        return deviceInfos;

    }

    private GeneralDeviceDTO getGeneralDeviceInfoAsDept(DeptDTO deptDTO, DeviceType deviceType, List<String> ids, DeviceInfoParam deviceInfoParam) {
        GeneralDeviceDTO generalDeviceDTO = new GeneralDeviceDTO();
        generalDeviceDTO.setIndex(deptDTO.getId());
        if (deptDTO.getType() == 0) {
            generalDeviceDTO.setName(deptDTO.getArea());
        } else {
            generalDeviceDTO.setName(deptDTO.getName());
        }
        List<DeptLine> deptLines = deptLineService.selectDeptBindLines(ids);
        //返回空数据
        if (CollectionUtil.isEmpty(deptLines)) {
            return generalDeviceDTO;
        }
        //获取line详细数据
        List<Line> lines = terminalBaseService.getLineByCondition(deptLines.stream().map(DeptLine::getLineId).collect(Collectors.toList()), deviceInfoParam);
        //返回空数据
        if (CollectionUtil.isEmpty(lines)) {
            return generalDeviceDTO;
        }
        //筛选出终端id，理论上监测点的pids中第五个id为终端id
        List<String> devIds = lines.stream().map(line -> {
            String[] idsArray = line.getPids().split(",");
            return idsArray[4];
        }).collect(Collectors.toList());
        //再根据终端条件筛选合法终端信息
        List<Line> devices = terminalBaseService.getDeviceById(devIds, deviceType);
        //筛选最终的数据
        dealDeviceData(generalDeviceDTO, lines, devices);
        return generalDeviceDTO;
    }

    /**
     * 处理各节点索引集合
     *
     * @param generalDeviceDTO 终端信息综合体
     * @param lines            监测点信息
     * @param devices          终端信息
     */
    private void dealDeviceData(GeneralDeviceDTO generalDeviceDTO, List<Line> lines, List<Line> devices) {
        List<String> gdIndexes = new ArrayList<>(), subIndexes = new ArrayList<>(), deviceIndexes = new ArrayList<>(), voltageIndexes = new ArrayList<>(), lineIndexes = new ArrayList<>();
        //筛选出供电公司、变电站、终端索引集合
        for (Line device : devices) {
            String[] ids = device.getPids().split(",");
            gdIndexes.add(ids[2]);
            subIndexes.add(ids[3]);
            deviceIndexes.add(device.getId());
        }
        //筛选出母线、监测点集合
        for (Line line : lines) {
            String[] ids = line.getPids().split(",");
            if (deviceIndexes.contains(ids[4])) {
                lineIndexes.add(line.getId());
                voltageIndexes.add(ids[5]);
            }
        }
        //排重，入参到终端综合体
        generalDeviceDTO.setGdIndexes(gdIndexes.stream().distinct().collect(Collectors.toList()));
        generalDeviceDTO.setSubIndexes(subIndexes.stream().distinct().collect(Collectors.toList()));
        generalDeviceDTO.setDeviceIndexes(deviceIndexes.stream().distinct().collect(Collectors.toList()));
        generalDeviceDTO.setVoltageIndexes(voltageIndexes.stream().distinct().collect(Collectors.toList()));
        generalDeviceDTO.setLineIndexes(lineIndexes.stream().distinct().collect(Collectors.toList()));

    }

    /**
     * 根据条件获取按变电站分类的终端信息
     *
     * @param deviceInfoParam 终端查询条件
     * @param runFlag         终端状态
     * @param devModel        终端模型
     * @return 终端信息
     */
    public List<GeneralDeviceDTO> getDeviceInfoAsSubstation(DeviceInfoParam deviceInfoParam, List<Integer> runFlag, List<Integer> devModel) {
        List<GeneralDeviceDTO> deviceInfoAsSubstation = new ArrayList<>();
        List<GeneralDeviceDTO> deviceInfoAsDept = getDeviceInfo(deviceInfoParam, runFlag, devModel);
        List<String> substationIds = new ArrayList<>();
        List<String> lineIds = new ArrayList<>();
        if (!CollectionUtil.isEmpty(deviceInfoAsDept)) {
            for (GeneralDeviceDTO generalDeviceDTO : deviceInfoAsDept) {
                substationIds.addAll(generalDeviceDTO.getSubIndexes());
                lineIds.addAll(generalDeviceDTO.getLineIndexes());
            }
        } else {
            return deviceInfoAsSubstation;
        }
        substationIds = substationIds.stream().distinct().collect(Collectors.toList());
        lineIds = lineIds.stream().distinct().collect(Collectors.toList());
        if (!CollectionUtil.isEmpty(substationIds)) {
            List<Line> substations = terminalBaseService.getSubstationByIds(substationIds);
            List<Line> lines = terminalBaseService.getLineById(lineIds);
            for (Line substation : substations) {
                deviceInfoAsSubstation.add(mergeDeviceInfoAsSubstation(substation, lines));
            }
        }
        return deviceInfoAsSubstation;
    }

    /**
     * 根据变电站筛选数据
     *
     * @param substation 变电站
     * @param lines      监测点信息
     * @return 按变电站分类的终端信息
     */
    private GeneralDeviceDTO mergeDeviceInfoAsSubstation(Line substation, List<Line> lines) {
        List<String> gdIndexes = new ArrayList<>(), subIndexes = new ArrayList<>(), deviceIndexes = new ArrayList<>(), voltageIndexes = new ArrayList<>(), lineIndexes = new ArrayList<>();
        GeneralDeviceDTO generalDeviceDTO = new GeneralDeviceDTO();
        generalDeviceDTO.setName(substation.getName());
        generalDeviceDTO.setIndex(substation.getId());
        gdIndexes.add(substation.getPid());
        subIndexes.add(substation.getId());
        generalDeviceDTO.setGdIndexes(gdIndexes);
        generalDeviceDTO.setSubIndexes(subIndexes);
        //筛选终端、母线、监测点索引
        for (Line line : lines) {
            String[] ids = line.getPids().split(",");
            if (ids[3].equals(substation.getId())) {
                deviceIndexes.add(ids[4]);
                voltageIndexes.add(ids[5]);
                lineIndexes.add(line.getId());
            }
        }
        generalDeviceDTO.setDeviceIndexes(deviceIndexes.stream().distinct().collect(Collectors.toList()));
        generalDeviceDTO.setVoltageIndexes(voltageIndexes.stream().distinct().collect(Collectors.toList()));
        generalDeviceDTO.setLineIndexes(lineIndexes.stream().distinct().collect(Collectors.toList()));
        return generalDeviceDTO;
    }


    /**
     * 筛选数据类型
     */
    private void filterDataType(DeviceType deviceType, String serverName) {
        ServerEnum serverEnum = EnumUtils.getServerEnumByName(serverName);
        List<Integer> dataType = new ArrayList<>();
        dataType.add(2);
        switch (serverEnum) {
            case EVENT:
                dataType.add(0);
                break;
            case HARMONIC:
                dataType.add(1);
                break;
            default:
                dataType.add(0);
                dataType.add(1);
                break;
        }
        deviceType.setDataType(dataType);
    }

    /**
     * 根据部门id、远程服务名、远程客户端类型，以部门的方式
     *
     * @param deviceInfoParam 终端查询条件
     * @param runFlag         终端状态
     * @param devModel        终端模型
     * @return 部门分类终端信息
     */
    public List<GeneralDeviceDTO> getDeviceInfo(DeviceInfoParam deviceInfoParam, List<Integer> runFlag, List<Integer> devModel) {
        //定义待返回终端信息
        List<GeneralDeviceDTO> deviceInfos = new ArrayList<>();
        //初始化终端查询条件
        DeviceType deviceType = new DeviceType();
        if (CollectionUtil.isEmpty(devModel)) {
            deviceType.setDevModel(null);
        } else {
            deviceType.setDevModel(devModel);
        }
        if (CollectionUtil.isEmpty(runFlag)) {
            deviceType.setRunFlag(null);
        } else {
            deviceType.setRunFlag(runFlag);
        }
        filterDataType(deviceType, deviceInfoParam.getServerName());

        //初始化部门筛选条件
        List<Integer> deptType = WebUtil.filterDeptType();
        //获取包括当前部门的后代所有部门信息
        List<DeptDTO> deptInfos = deptFeignClient.getDeptDescendantIndexes(deviceInfoParam.getDeptIndex(), deptType).getData();
        List<DeptDTO> directDeptInfos = deptInfos.stream()
                .filter(deptDTO -> deptDTO.getPid().equals(deviceInfoParam.getDeptIndex()))
                .collect(Collectors.toList());
        if (CollectionUtil.isEmpty(directDeptInfos)) {
            //没有直接子部门，获取当前部门所有信息
            List<DeptDTO> dept = deptInfos.stream()
                    .filter(deptDTO -> deptDTO.getId().equals(deviceInfoParam.getDeptIndex()))
                    .collect(Collectors.toList());
            deviceInfos.add(getGeneralDeviceInfo(dept.get(0), deviceType, Collections.singletonList(deviceInfoParam.getDeptIndex()), deviceInfoParam));
        } else {
            for (DeptDTO deptDTO : directDeptInfos) {
                //筛选上级部门pids包含该id的所有部门
                List<DeptDTO> descendantDeptDTO = deptInfos.stream()
                        .filter(d -> d.getPids().contains(deptDTO.getId()))
                        .collect(Collectors.toList());
                //形成需要查询监测点的部门索引
                List<String> indexes = descendantDeptDTO.stream()
                        .map(DeptDTO::getId)
                        .distinct()
                        .collect(Collectors.toList());
                indexes.add(deptDTO.getId());
                deviceInfos.add(getGeneralDeviceInfo(deptDTO, deviceType, indexes, deviceInfoParam));
            }
        }


        //判断统计类型
        StatisticsEnum statisticsEnum = StatisticsEnum.getStatisticsEnumByCode(deviceInfoParam.getStatisticalType().getCode());
        switch (statisticsEnum) {
            case VOLTAGE_LEVEL:
                return filterDataByScale(deviceInfos, deviceInfoParam.getScale());
            case LOAD_TYPE:
                return filterDataByLoadType(deviceInfos, deviceInfoParam.getLoadType());
            case MANUFACTURER:
                return filterDataByManufacturer(deviceInfos, deviceInfoParam.getManufacturer());
            default:
                return deviceInfos;
        }
    }


    private GeneralDeviceDTO getGeneralDeviceInfo(DeptDTO deptDTO, DeviceType deviceType, List<String> ids, DeviceInfoParam deviceInfoParam) {
        GeneralDeviceDTO generalDeviceDTO = new GeneralDeviceDTO();
        generalDeviceDTO.setIndex(deptDTO.getId());
        if (deptDTO.getType() == 0) {
            generalDeviceDTO.setName(deptDTO.getArea());
        } else {
            generalDeviceDTO.setName(deptDTO.getName());
        }
        List<DeptLine> deptLines = deptLineService.selectDeptBindLines(ids);
        //返回空数据
        if (CollectionUtil.isEmpty(deptLines)) {
            return generalDeviceDTO;
        }
        //获取line详细数据
        List<Line> lines = terminalBaseService.getLineByCondition(deptLines.stream().map(DeptLine::getLineId).collect(Collectors.toList()), deviceInfoParam);
        //返回空数据
        if (CollectionUtil.isEmpty(lines)) {
            return generalDeviceDTO;
        }

        //筛选出终端id，理论上监测点的pids中第五个id为终端id
        List<String> devIds = lines.stream().map(line -> {
            String[] idsArray = line.getPids().split(",");
            return idsArray[4];
        }).collect(Collectors.toList());
        //再根据终端条件筛选合法终端信息
        List<Line> devices = terminalBaseService.getDeviceByCondition(devIds, deviceType, deviceInfoParam.getManufacturer());

        //筛选出母线id，理论上监测点的pids中第六个id为母线id
        List<String> voltageIds = lines.stream().map(line -> {
            String[] idsArray = line.getPids().split(",");
            return idsArray[5];
        }).collect(Collectors.toList());
        //再根据电压等级筛选合法母线信息
        List<Line> voltages = terminalBaseService.getVoltageByCondition(voltageIds, deviceInfoParam.getScale());
        //筛选最终的数据
        dealDeviceData(generalDeviceDTO, lines, devices, voltages);
        return generalDeviceDTO;
    }

    /**
     * 取多条件筛选后的交集索引，填充到部门统计中
     *
     * @param generalDeviceDTO 部门信息
     * @param lines            筛选后的监测点信息
     * @param devices          筛选后的终端信息
     * @param voltages         筛选后的母线信息
     */
    private void dealDeviceData(GeneralDeviceDTO generalDeviceDTO, List<Line> lines, List<Line> devices, List<Line> voltages) {
        List<String> gdIndexes = new ArrayList<>(), subIndexes = new ArrayList<>(), deviceIndexes = new ArrayList<>(), voltageIndexes = new ArrayList<>(), lineIndexes = new ArrayList<>();
        List<String> devIds = devices.stream().map(Line::getId).distinct().collect(Collectors.toList());
        List<String> volIds = voltages.stream().map(Line::getId).distinct().collect(Collectors.toList());
        for (Line line : lines) {
            String[] idsArray = line.getPids().split(",");
            //监测点同时满足条件筛选后的终端、母线信息，才是最终的结果
            if (devIds.contains(idsArray[LineBaseEnum.DEVICE_LEVEL.getCode()]) && volIds.contains(idsArray[LineBaseEnum.SUB_V_LEVEL.getCode()])) {
                gdIndexes.add(idsArray[LineBaseEnum.GD_LEVEL.getCode()]);
                subIndexes.add(idsArray[LineBaseEnum.SUB_LEVEL.getCode()]);
                deviceIndexes.add(idsArray[LineBaseEnum.DEVICE_LEVEL.getCode()]);
                voltageIndexes.add(idsArray[LineBaseEnum.SUB_V_LEVEL.getCode()]);
                lineIndexes.add(line.getId());
            }
        }
        //排重，入参到终端综合体
        generalDeviceDTO.setGdIndexes(gdIndexes.stream().distinct().collect(Collectors.toList()));
        generalDeviceDTO.setSubIndexes(subIndexes.stream().distinct().collect(Collectors.toList()));
        generalDeviceDTO.setDeviceIndexes(deviceIndexes.stream().distinct().collect(Collectors.toList()));
        generalDeviceDTO.setVoltageIndexes(voltageIndexes.stream().distinct().collect(Collectors.toList()));
        generalDeviceDTO.setLineIndexes(lineIndexes.stream().distinct().collect(Collectors.toList()));
    }

    private List<GeneralDeviceDTO> filterDataByScale(List<GeneralDeviceDTO> deviceInfos, List<SimpleDTO> scales) {
        List<GeneralDeviceDTO> generalDeviceDTOS = new ArrayList<>();
        List<String> voltageIds = new ArrayList<>(), lineIds = new ArrayList<>();
        for (GeneralDeviceDTO generalDeviceDTO : deviceInfos) {
            voltageIds.addAll(generalDeviceDTO.getVoltageIndexes());
            lineIds.addAll(generalDeviceDTO.getLineIndexes());
        }
        //如果电压等级集合为空，则查询所有的电压等级
        if (CollectionUtil.isEmpty(scales)) {
            List<DictData> scaleDictData = dicDataFeignClient.getDicDataByTypeName(DicDataTypeEnum.DEV_VOLTAGE.getName()).getData();
            scales = scaleDictData.stream().map(dictData -> {
                SimpleDTO simpleDTO = new SimpleDTO();
                BeanUtil.copyProperties(dictData,simpleDTO);
                return simpleDTO;
            }).collect(Collectors.toList());
        }
        //监测点为空，则返回空的分类数据
        if (CollectionUtil.isEmpty(lineIds)) {
            return assembleCommonData(scales);
        }
        List<Line> lines = terminalBaseService.getLineById(lineIds);
        for (SimpleDTO simpleDTO : scales) {
            List<String> voltageScaleIds = terminalBaseService.getVoltageIdByScale(voltageIds, simpleDTO.getId());
            generalDeviceDTOS.add(assembleDataByLine(simpleDTO, lines, voltageScaleIds, LineBaseEnum.SUB_V_LEVEL.getCode()));
        }
        return generalDeviceDTOS;
    }


    private List<GeneralDeviceDTO> filterDataByLoadType(List<GeneralDeviceDTO> deviceInfos, List<SimpleDTO> loadType) {
        List<GeneralDeviceDTO> generalDeviceDTOS = new ArrayList<>();
        List<String> lineIds = new ArrayList<>();
        for (GeneralDeviceDTO generalDeviceDTO : deviceInfos) {
            lineIds.addAll(generalDeviceDTO.getLineIndexes());
        }
        //如果干扰源集合为空，则查询所有的电压等级
        if (CollectionUtil.isEmpty(loadType)) {
            List<DictData> scaleDictData = dicDataFeignClient.getDicDataByTypeName(DicDataTypeEnum.INTERFERENCE_SOURCE_TYPE.getName()).getData();
            loadType = scaleDictData.stream().map(dictData -> {
                SimpleDTO simpleDTO = new SimpleDTO();
                BeanUtil.copyProperties(dictData,simpleDTO);
                return simpleDTO;
            }).collect(Collectors.toList());
        }
        //监测点为空，则返回空的分类数据
        if (CollectionUtil.isEmpty(lineIds)) {
            return assembleCommonData(loadType);
        }
        List<Line> lines = terminalBaseService.getLineById(lineIds);
        for (SimpleDTO simpleDTO : loadType) {
            List<String> lineLoadTypeIds = terminalBaseService.getLineIdByLoadType(lineIds, simpleDTO.getId());
            generalDeviceDTOS.add(assembleDataByLine(simpleDTO, lines, lineLoadTypeIds, LineBaseEnum.LINE_LEVEL.getCode()));
        }
        return generalDeviceDTOS;
    }

    private List<GeneralDeviceDTO> filterDataByManufacturer(List<GeneralDeviceDTO> deviceInfos, List<SimpleDTO> manufacturer) {
        List<GeneralDeviceDTO> generalDeviceDTOS = new ArrayList<>();
        List<String> deviceIds = new ArrayList<>(), lineIds = new ArrayList<>();
        for (GeneralDeviceDTO generalDeviceDTO : deviceInfos) {
            deviceIds.addAll(generalDeviceDTO.getDeviceIndexes());
            lineIds.addAll(generalDeviceDTO.getLineIndexes());
        }
        //如果终端厂家集合为空，则查询所有的电压等级
        if (CollectionUtil.isEmpty(manufacturer)) {
            List<DictData> scaleDictData = dicDataFeignClient.getDicDataByTypeName(DicDataTypeEnum.DEV_MANUFACTURER.getName()).getData();
            manufacturer = scaleDictData.stream().map(dictData -> {
                SimpleDTO simpleDTO = new SimpleDTO();
                BeanUtil.copyProperties(dictData,simpleDTO);
                return simpleDTO;
            }).collect(Collectors.toList());
        }
        //监测点为空，则返回空的分类数据
        if (CollectionUtil.isEmpty(lineIds)) {
            return assembleCommonData(manufacturer);
        }
        List<Line> lines = terminalBaseService.getLineById(lineIds);
        for (SimpleDTO simpleDTO : manufacturer) {
            List<String> voltageScaleIds = terminalBaseService.getDeviceIdByManufacturer(deviceIds, simpleDTO.getId());
            generalDeviceDTOS.add(assembleDataByLine(simpleDTO, lines, voltageScaleIds, LineBaseEnum.DEVICE_LEVEL.getCode()));
        }
        return generalDeviceDTOS;
    }


    /**
     * 当该部门不存在监测点时，返回空的分类数据
     *
     * @param simpleDTOS 分类类别
     * @return .
     */
    private List<GeneralDeviceDTO> assembleCommonData(List<SimpleDTO> simpleDTOS) {
        return simpleDTOS.stream().map(this::assembleData).collect(Collectors.toList());
    }

    /**
     * 当该部门不存在监测点时，返回空的分类数据
     *
     * @param simpleDTO 基础数据
     * @return .
     */
    private GeneralDeviceDTO assembleData(SimpleDTO simpleDTO) {
        GeneralDeviceDTO generalDeviceDTO = new GeneralDeviceDTO();
        generalDeviceDTO.setName(simpleDTO.getName());
        generalDeviceDTO.setIndex(simpleDTO.getId());
        return generalDeviceDTO;
    }

    /**
     * 筛选对应等级的id
     *
     * @param simpleDTO 分类信息
     * @param lines     所有监测点
     * @param keyIds    待筛选的id
     * @param level     待筛选的层级
     */
    private GeneralDeviceDTO assembleDataByLine(SimpleDTO simpleDTO, List<Line> lines, List<String> keyIds, Integer level) {
        GeneralDeviceDTO generalDeviceDTO = assembleData(simpleDTO);
        if (CollectionUtil.isNotEmpty(keyIds)) {
            List<Line> tempLines = lines.stream().filter(line -> {
                String[] idsArray = line.getPids().split(",");
                if (level.equals(LineBaseEnum.LINE_LEVEL.getCode())) {
                    return keyIds.contains(line.getId());
                } else {
                    return keyIds.contains(idsArray[level]);
                }
            }).collect(Collectors.toList());
            List<String> gdIndexes = new ArrayList<>(), subIndexes = new ArrayList<>(), deviceIndexes = new ArrayList<>(), voltageIndexes = new ArrayList<>(), lineIndexes = new ArrayList<>();
            for (Line line : tempLines) {
                String[] idsArray = line.getPids().split(",");
                gdIndexes.add(idsArray[LineBaseEnum.GD_LEVEL.getCode()]);
                subIndexes.add(idsArray[LineBaseEnum.SUB_LEVEL.getCode()]);
                deviceIndexes.add(idsArray[LineBaseEnum.DEVICE_LEVEL.getCode()]);
                voltageIndexes.add(idsArray[LineBaseEnum.SUB_V_LEVEL.getCode()]);
                lineIndexes.add(line.getId());
            }
            //排重，入参到终端综合体
            generalDeviceDTO.setGdIndexes(gdIndexes.stream().distinct().collect(Collectors.toList()));
            generalDeviceDTO.setSubIndexes(subIndexes.stream().distinct().collect(Collectors.toList()));
            generalDeviceDTO.setDeviceIndexes(deviceIndexes.stream().distinct().collect(Collectors.toList()));
            generalDeviceDTO.setVoltageIndexes(voltageIndexes.stream().distinct().collect(Collectors.toList()));
            generalDeviceDTO.setLineIndexes(lineIndexes.stream().distinct().collect(Collectors.toList()));
        }
        return generalDeviceDTO;
    }


}
