package com.njcn.product.terminal.mysqlTerminal.service;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.njcn.common.pojo.dto.SimpleDTO;
import com.njcn.common.pojo.enums.common.DataStateEnum;
import com.njcn.common.pojo.enums.common.ServerEnum;
import com.njcn.common.utils.EnumUtils;
import com.njcn.product.cnuser.user.pojo.dto.DeptDTO;
import com.njcn.product.system.dict.mapper.DictDataMapper;
import com.njcn.product.system.dict.pojo.enums.DicDataTypeEnum;
import com.njcn.product.system.dict.pojo.po.DictData;
import com.njcn.product.terminal.mysqlTerminal.mapper.DeptLineMapper;
import com.njcn.product.cnuser.user.mapper.DeptMapper;
import com.njcn.product.terminal.mysqlTerminal.pojo.dto.DeviceType;
import com.njcn.product.terminal.mysqlTerminal.pojo.dto.GeneralDeviceDTO;
import com.njcn.product.terminal.mysqlTerminal.pojo.enums.LineBaseEnum;
import com.njcn.product.terminal.mysqlTerminal.pojo.enums.PowerFlagEnum;
import com.njcn.product.terminal.mysqlTerminal.pojo.enums.StatisticsEnum;
import com.njcn.product.terminal.mysqlTerminal.pojo.param.DeviceInfoParam;
import com.njcn.product.terminal.mysqlTerminal.pojo.param.LargeScreenCountParam;
import com.njcn.product.cnuser.user.pojo.po.Dept;
import com.njcn.product.terminal.mysqlTerminal.pojo.po.DeptLine;
import com.njcn.product.terminal.mysqlTerminal.pojo.po.Line;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;


/**
 * @Author: cdf
 * @CreateTime: 2025-06-25
 * @Description:
 */
@Service
@RequiredArgsConstructor
public class CommGeneralService {

    private final DeptMapper deptMapper;

    private final DeptLineMapper deptLineMapper;

    private final DeptLineService deptLineService;

    private final DictDataMapper dictDataMapper;

    private final TerminalBaseService terminalBaseService;


    /**
     * 根据部门id获取部门所拥有的监测点
     * @param deptId
     * @return
     */
    public List<String> getRunLineIdsByDept(String deptId){
        LambdaQueryWrapper<Dept> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.like(Dept::getPids,deptId).eq(Dept::getState, DataStateEnum.ENABLE.getCode());
        List<Dept> deptAndChildren = deptMapper.selectList(lambdaQueryWrapper);
        List<String> deptIds = deptAndChildren.stream().map(Dept::getId).distinct().collect(Collectors.toList());
        deptIds.add(deptId);

        List<String> deptLineList = deptLineMapper.getLineIdByDeptIds(deptIds, Stream.of(0).collect(Collectors.toList()));
        return deptLineList;
    }

    /**
     * 根据部门id获取部门所拥有的监测点
     * @param param
     * @return
     */
    public List<String> getRunLineIdsByDept(LargeScreenCountParam param){
        LambdaQueryWrapper<Dept> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.like(Dept::getPids,param.getDeptId()).eq(Dept::getState, DataStateEnum.ENABLE.getCode());
        List<Dept> deptAndChildren = deptMapper.selectList(lambdaQueryWrapper);
        List<String> deptIds = deptAndChildren.stream().map(Dept::getId).distinct().collect(Collectors.toList());
        deptIds.add(param.getDeptId());

        List<String> deptLineList = deptLineMapper.getLineIdByDeptIds(deptIds, Stream.of(0).collect(Collectors.toList()));
        return deptLineList;
    }


    /**
     * 根据部门id获取部门所拥有的监测点
     * @param deptId
     * @return
     */
    public List<String> getAllLineIdsByDept(String deptId){
        LambdaQueryWrapper<Dept> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.like(Dept::getPids,deptId).eq(Dept::getState, DataStateEnum.ENABLE.getCode());
        List<Dept> deptAndChildren = deptMapper.selectList(lambdaQueryWrapper);
        List<String> deptIds = deptAndChildren.stream().map(Dept::getId).distinct().collect(Collectors.toList());
        deptIds.add(deptId);

        List<String> deptLineList = deptLineMapper.getLineIdByDeptIds(deptIds, Stream.of(0,1,2).collect(Collectors.toList()));
        return deptLineList;
    }


    /**
     * 根据部门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)) {
            /**
             * 终端模型（0：虚拟设备；1：实际设备；2：离线设备；）默认是实际设备
             */
            deviceType.setDevModel(null);
        } else {
            deviceType.setDevModel(devModel);
        }
        if (CollectionUtil.isEmpty(runFlag)) {
            /**
             * 终端状态（0：投运；1：热备用；2：停运）
             */
            deviceType.setRunFlag(null);
        } else {
            deviceType.setRunFlag(runFlag);
        }
        if(ObjectUtil.isNotNull(deviceInfoParam.getComFlagStatus())){
            deviceType.setComFlag(Arrays.asList(deviceInfoParam.getComFlagStatus()));
        }
        filterDataType(deviceType, deviceInfoParam.getServerName());

        // 初始化部门筛选条件
        List<Integer> deptType = Stream.of(0,1,2,3).collect(Collectors.toList());
        // 获取包括当前部门的后代所有部门信息
        List<DeptDTO> deptInfos = deptMapper.getDeptDescendantIndexes(deviceInfoParam.getDeptIndex(), deptType);
        // 过滤非直接后代部门，集合直接子部门
        List<DeptDTO> directDeptInfos = deptInfos.stream()
                .filter(deptDTO -> deptDTO.getPid().equals(deviceInfoParam.getDeptIndex())).sorted(Comparator.comparing(DeptDTO::getSort))
                .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 directDeptDTO : directDeptInfos) {
                //筛选pids包含该id的所有部门 直接子部门下属所有部门
                List<DeptDTO> descendantDeptDTO = deptInfos.stream()
                        .filter(d -> d.getPids().contains(directDeptDTO.getId()))
                        .collect(Collectors.toList());
                //形成需要查询监测点的部门索引
                List<String> indexes = descendantDeptDTO.stream()
                        .map(DeptDTO::getId)
                        .distinct()
                        .collect(Collectors.toList());
                indexes.add(directDeptDTO.getId());
                GeneralDeviceDTO generalDeviceInfo = getGeneralDeviceInfo(directDeptDTO, deviceType, indexes, deviceInfoParam);
                deviceInfos.add(generalDeviceInfo);
            }
        }


        //判断统计类型
        if (deviceInfoParam.getStatisticalType() == null) {
            deviceInfoParam.setStatisticalType(new SimpleDTO());
        }
        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());
            case POWER_FLAG:
                return filterDataByPowerFlag(deviceInfos, deviceInfoParam.getManufacturer());
            default:
                return deviceInfos;
        }
    }


    /**
     * 筛选数据类型
     */
    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;
        }
        /**
         * 数据类型（0：暂态系统；1：稳态系统；2：两个系统）
         */
        deviceType.setDataType(dataType);
    }



    /**
     * 根据部门id集合获取监测点信息
     *
     * @param directDeptDTO   入参deptIndex的直接子部门
     * @param deviceType
     * @param ids             直接子部门以及后代部门id集合
     * @param deviceInfoParam
     * @return
     */
    private GeneralDeviceDTO getGeneralDeviceInfo(DeptDTO directDeptDTO,
                                                  DeviceType deviceType,
                                                  List<String> ids,
                                                  DeviceInfoParam deviceInfoParam) {
        GeneralDeviceDTO generalDeviceDTO = new GeneralDeviceDTO();
        generalDeviceDTO.setIndex(directDeptDTO.getId());
        // type:部门类型 0-非自定义；1-web自定义；2-App自定义；3-web测试
        if (directDeptDTO.getType() == 0) {
            generalDeviceDTO.setName(directDeptDTO.getArea());
        } else {
            generalDeviceDTO.setName(directDeptDTO.getName());
        }
        // 根据部门ids集合查询是否绑定监测点 部门和监测点关联关系中间表：pq_dept_line 可以一对多
        List<DeptLine> deptLines = deptLineService.selectDeptBindLines(ids);
        // 返回空数据
        if (CollectionUtil.isEmpty(deptLines)) {
            return generalDeviceDTO;
        }
        // 提取该部门及其子部门所有监测点id
        List<String> lineIds = deptLines.stream().map(DeptLine::getLineId).collect(Collectors.toList());
        // 获取line详细数据 ：根据监测点id，获取所有监测点 联查 pq_line、pq_line_detail
        List<Line> lines = terminalBaseService.getLineByCondition(lineIds, deviceInfoParam);
        // 返回空数据
        if (CollectionUtil.isEmpty(lines)) {
            return generalDeviceDTO;
        }

        //1.筛选出母线id，理论上监测点的pids中第六个id为母线id 联查： pq_line t1 ,pq_voltage t2
        List<String> voltageIds=lines.stream().map(Line::getPid).collect(Collectors.toList());
        //再根据电压等级筛选合法母线信息
        List<Line> voltages = terminalBaseService.getVoltageByCondition(voltageIds, deviceInfoParam.getScale());

        //2.筛选出终端id，理论上监测点的pids中第五个id为终端id
        List<String> devIds=voltages.stream().map(Line::getPid).collect(Collectors.toList());
        // 再根据终端条件筛选合法终端信息 联查：pq_line t1,pq_device t2
        List<Line> devices = terminalBaseService.getDeviceByCondition(devIds, deviceType, deviceInfoParam.getManufacturer());

        //3.筛选出变电站id，理论上监测点的pids中第四个id为变电站id 联查： pq_line t1 ,pq_substation t2
        List<String> subIds=devices.stream().map(Line::getPid).collect(Collectors.toList());
        List<Line> sub = terminalBaseService.getSubByCondition(subIds, new ArrayList<>());

        //筛选最终的数据
        dealDeviceData(generalDeviceDTO, lines, devices, voltages, sub);
        return generalDeviceDTO;
    }


    private List<GeneralDeviceDTO> filterDataByScale(List<GeneralDeviceDTO> deviceInfos, List<SimpleDTO> scales) {
        List<GeneralDeviceDTO> generalDeviceDTOS = new ArrayList<>();
        List<String> subIds = new ArrayList<>(), lineIds = new ArrayList<>();
        for (GeneralDeviceDTO generalDeviceDTO : deviceInfos) {
            subIds.addAll(generalDeviceDTO.getSubIndexes());
            lineIds.addAll(generalDeviceDTO.getLineIndexes());
        }
        //如果电压等级集合为空，则查询所有的电压等级
        if (CollectionUtil.isEmpty(scales)) {
            List<DictData> scaleDictData = dictDataMapper.getDicDataByTypeCode(DicDataTypeEnum.DEV_VOLTAGE_STAND.getCode());
            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.getSubIdByScale(subIds, simpleDTO.getId());
            generalDeviceDTOS.add(assembleDataByLine(simpleDTO, lines, voltageScaleIds, LineBaseEnum.SUB_LEVEL.getCode()));
        }
        return generalDeviceDTOS;
    }


    /**
     * 处理各节点索引集合
     *
     * @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 generalDeviceDTO 部门信息
     * @param lines            筛选后的监测点信息
     * @param devices          筛选后的终端信息
     * @param voltages         筛选后的母线信息
     */
    private void dealDeviceData(GeneralDeviceDTO generalDeviceDTO, List<Line> lines, List<Line> devices, List<Line> voltages, List<Line> sub) {
        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());
        List<String> subIds = sub.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()])&&
                    subIds.contains(idsArray[LineBaseEnum.SUB_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> 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 = dictDataMapper.getDicDataByTypeCode(DicDataTypeEnum.INTERFERENCE_SOURCE_TYPE.getCode());
            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 = dictDataMapper.getDicDataByTypeCode(DicDataTypeEnum.DEV_MANUFACTURER.getCode());
            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;
    }

    private List<GeneralDeviceDTO> filterDataByPowerFlag(List<GeneralDeviceDTO> deviceInfos, List<SimpleDTO> manufacturer) {
        List<GeneralDeviceDTO> generalDeviceDTOS = new ArrayList<>();
        List<String> deviceIds = deviceInfos.stream().flatMap(x->x.getLineIndexes().stream()).collect(Collectors.toList());
        List<String> lineIds = deviceInfos.stream().flatMap(x->x.getLineIndexes().stream()).collect(Collectors.toList());
        //监测点为空，则返回空的分类数据
        if (CollectionUtil.isEmpty(lineIds)) {
            return assembleCommonData(manufacturer);
        }
        SimpleDTO dto;
        List<Line> lines = terminalBaseService.getLineById(lineIds);
        for (int i = 0; i < 6; i++) {
            List<String> powerFlagIds = terminalBaseService.getDeviceIdByPowerFlag(deviceIds, i);
            dto=new SimpleDTO();
            PowerFlagEnum enumByCode = PowerFlagEnum.getPowerFlagEnumByCode(i);
            dto.setId(enumByCode.getCode().toString());
            dto.setName(enumByCode.getMessage());
            generalDeviceDTOS.add(assembleDataByLine(dto, lines, powerFlagIds, LineBaseEnum.LINE_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;
    }
}
