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

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.njcn.common.pojo.enums.common.DataStateEnum;
import com.njcn.common.pojo.exception.BusinessException;
import com.njcn.device.line.mapper.LineMapper;
import com.njcn.device.pq.enums.LineBaseEnum;
import com.njcn.device.pq.enums.PvDeviceResponseEnum;
import com.njcn.device.pq.mapper.PqsTflgassMapper;
import com.njcn.device.pq.mapper.PqsTransformerMapper;
import com.njcn.device.pq.pojo.dto.transformer.EntityLogicDTO;
import com.njcn.device.pq.pojo.param.PqsTransformerParam;
import com.njcn.device.pq.pojo.po.Line;
import com.njcn.device.pq.pojo.po.PqsTflgass;
import com.njcn.device.pq.pojo.po.PqsTransformer;
import com.njcn.device.pq.pojo.vo.PqsTflgassVO;
import com.njcn.device.pq.pojo.vo.PqsTransformerVO;
import com.njcn.device.pq.pojo.vo.TerminalTree;
import com.njcn.device.pq.service.IPqsTflgassService;
import com.njcn.device.pq.service.IPqsTransformerService;
import com.njcn.user.api.UserFeignClient;
import com.njcn.user.pojo.po.User;
import com.njcn.web.pojo.param.BaseParam;

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

import com.njcn.web.pojo.vo.LineDataVO;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author wr
 * @since 2023-07-19
 */
@Service
@RequiredArgsConstructor
public class PqsTransformerServiceImpl extends ServiceImpl<PqsTransformerMapper, PqsTransformer> implements IPqsTransformerService {

    private final IPqsTflgassService iPqsTflgassService;

    private final PqsTflgassMapper pqsTflgassMapper;
    private final LineMapper lineMapper;

    private final UserFeignClient userFeignClient;

    @Override
    public Page<PqsTransformerVO> transformerPage(BaseParam param) {
        Page<PqsTransformer> page = this.page(new Page<>(param.getPageNum(), param.getPageSize()), new LambdaQueryWrapper<PqsTransformer>()
                .eq(StrUtil.isNotBlank(param.getSearchValue()), PqsTransformer::getSubIndex, param.getSearchValue())
                .or()
                .eq(StrUtil.isNotBlank(param.getSearchValue()), PqsTransformer::getTfIndex, param.getSearchValue())
                .orderByDesc(PqsTransformer::getUpdateTime)
        );

        Page<PqsTransformerVO> voPage = BeanUtil.copyProperties(page, Page.class);
        List<PqsTransformer> records = page.getRecords();

        if (CollUtil.isNotEmpty(records)) {
            //初始化属性
            List<PqsTransformerVO> transformerVOList = new ArrayList<>();
            PqsTransformerVO vo;
            //查询变电站信息
            List<String> subIds = records.stream().map(PqsTransformer::getSubIndex).collect(Collectors.toList());
            List<LineDataVO> lineDetail = lineMapper.getLineDetail(subIds);
            Map<String, String> lineMap = lineDetail.stream().collect(Collectors.toMap(LineDataVO::getId, LineDataVO::getName));

            //查询用户信息
            List<String> userIds = records.stream().map(PqsTransformer::getUpdateBy).collect(Collectors.toList());
            List<User> userList = userFeignClient.getUserByIdList(userIds).getData();
            Map<String, String> userMap = userList.stream().collect(Collectors.toMap(User::getId, User::getName));


            for (PqsTransformer record : records) {
                vo = BeanUtil.copyProperties(record, PqsTransformerVO.class);
                if (lineMap.containsKey(record.getSubIndex())) {
                    vo.setSubIndexName(lineMap.get(record.getSubIndex()));
                }
                if (userMap.containsKey(record.getUpdateBy())) {
                    vo.setUpdateBy(userMap.get(record.getUpdateBy()));
                }
                transformerVOList.add(vo);
            }
            voPage.setRecords(transformerVOList);
        }
        return voPage;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean insertTransformer(PqsTransformerParam.TransformerAddParam param) {
        List<PqsTransformer> list = this.list(new LambdaQueryWrapper<PqsTransformer>()
                .eq(PqsTransformer::getTfName, param.getTfName())
        );
        if (CollectionUtil.isNotEmpty(list)) {
            throw new BusinessException(PvDeviceResponseEnum.TRANSFORMER_NAME_REPEAT);
        }
        PqsTransformer pqsTransformer = BeanUtil.copyProperties(param, PqsTransformer.class);
        pqsTransformer.setSort(this.baseMapper.sortTransformer() + 1);
        pqsTransformer.setStatus(1);
        boolean save = this.save(pqsTransformer);
        return save;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean updateTransformer(PqsTransformerParam.TransformerUpdateParam param) {
        List<PqsTransformer> list = this.list(new LambdaQueryWrapper<PqsTransformer>()
                .ne(PqsTransformer::getTfIndex, param.getTfIndex())
                .and(wrapper ->
                        wrapper.eq(PqsTransformer::getTfName, param.getTfName())
                )
        );
        if (CollectionUtil.isNotEmpty(list)) {
            throw new BusinessException(PvDeviceResponseEnum.TRANSFORMER_NAME_REPEAT);
        }
        PqsTransformer pqsTransformer = BeanUtil.copyProperties(param, PqsTransformer.class);
        boolean update = this.updateById(pqsTransformer);
        return update;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean delTransformer(List<String> ids) {
        boolean b = this.removeByIds(ids);
        LambdaQueryWrapper<PqsTflgass> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.in(PqsTflgass::getTfIndex, ids);
        return iPqsTflgassService.remove(lambdaQueryWrapper);
    }

    @Override
    public PqsTflgassVO selectFlgAss(String ids) {
        PqsTflgassVO pqsTflgassVO = new PqsTflgassVO();
        PqsTflgass one = iPqsTflgassService.getOne(new LambdaQueryWrapper<PqsTflgass>()
                .eq(PqsTflgass::getTfIndex, ids));
        if (ObjectUtil.isNotNull(one)) {
            BeanUtil.copyProperties(one, pqsTflgassVO);
            Line logicBefore = lineMapper.selectById(one.getLogicBefore());
            Line logicNext = lineMapper.selectById(one.getLogicNext());
            pqsTflgassVO.setLogicBeforeName(logicBefore.getName());
            pqsTflgassVO.setLogicNextName(logicNext.getName());

        }
        return pqsTflgassVO;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean nodeMaintenance(PqsTransformerParam param) {
        boolean fly;
        //根据变压器台账id查询是否存在节点信息，存在则修改，不存则新增
        PqsTflgass one = iPqsTflgassService.getOne(new LambdaQueryWrapper<PqsTflgass>()
                .eq(PqsTflgass::getTfIndex, param.getTfIndex()));
        if (ObjectUtil.isNull(one)) {
            PqsTflgass ass = new PqsTflgass();
            ass.setTfIndex(param.getTfIndex());
            ass.setLogicBefore(param.getUpNode());
            ass.setLogicNext(param.getDownNode());
            PqsTflgass tflgass = iPqsTflgassService.getOne(new LambdaQueryWrapper<PqsTflgass>()
                    .eq(PqsTflgass::getTfIndex, param.getTfIndex())
                    .or(wrapper ->
                            wrapper.eq(PqsTflgass::getLogicBefore, param.getUpNode())
                                    .eq(PqsTflgass::getLogicNext, param.getDownNode())
                    )
            );
            if (ObjectUtil.isNotNull(tflgass)) {
                throw new BusinessException(PvDeviceResponseEnum.TRANSFORMER_NAME_REPEAT);
            } else {
                fly = iPqsTflgassService.save(ass);
            }


        } else {
            one.setLogicBefore(param.getUpNode());
            one.setLogicNext(param.getDownNode());
            fly = iPqsTflgassService.updateById(one);
        }
        return fly;
    }

    @Override
    public Map<String, Object> getGeneratrix(String subId, String id) {
        Map<String, Object> voltage = new HashMap<>();
        //先判断当前，变电站是否已添加了节点
        PqsTflgass one = iPqsTflgassService.getOne(new LambdaQueryWrapper<PqsTflgass>()
                .eq(PqsTflgass::getTfIndex, id));

        List<String> arr = new ArrayList<>();
        if (Objects.nonNull(one)) {
            arr.add(one.getLogicBefore());
            arr.add(one.getLogicNext());
        }
        voltage.put("bind", arr);

        //1.获取当前变电站下，所拥有的母线条数
        List<TerminalTree> downVoltage = lineMapper.getvoltage(subId, 1);
        if(CollUtil.isNotEmpty(arr)) {
            downVoltage.forEach(item->{
                if(Objects.equals(item.getId(),arr.get(1))){
                    item.setLevel(LineBaseEnum.SUB_V_LEVEL.getCode());
                }
            });
        }
        //获取所有母线层级
        List<Line> UpBusList = lineMapper.selectList(new LambdaQueryWrapper<Line>().eq(Line::getLevel, LineBaseEnum.SUB_V_LEVEL.getCode()).eq(Line::getState, DataStateEnum.ENABLE.getCode()).orderByAsc(Line::getSort));
        List<TerminalTree> upTreeList = UpBusList.stream().map(it -> {
            TerminalTree terminalTree = new TerminalTree();
            terminalTree.setId(it.getId());
            terminalTree.setName(it.getName());
            terminalTree.setPid(it.getPids().split(StrUtil.COMMA)[LineBaseEnum.SUB_LEVEL.getCode()]);
            terminalTree.setSort(it.getSort());
            if(CollUtil.isNotEmpty(arr)) {
                if (it.getId().equals(arr.get(0))) {
                    terminalTree.setLevel(LineBaseEnum.SUB_V_LEVEL.getCode());
                }
            }
            return terminalTree;
        }).collect(Collectors.toList());


        //获取母线id
        //List<String> ids = downVoltage.stream().map(TerminalTree::getId).collect(Collectors.toList());

        List<TerminalTree> upList = TreedChildren(upTreeList, "变压器上节点设备树", subId);
        voltage.put("upNode", upList);
        List<TerminalTree> downList = TreedChildren(downVoltage, "变压器下节点设备树", subId);
        voltage.put("downNode", downList);
        return voltage;

    }




public Map<String, Object> getGeneratrixOld(String subId, String id) {
    Map<String, Object> voltage = new HashMap<>();
    //先判断当前，变电站是否已添加了节点
    PqsTflgass one = iPqsTflgassService.getOne(new LambdaQueryWrapper<PqsTflgass>()
            .eq(PqsTflgass::getTfIndex, id));

    //1.获取当前变电站下，所拥有的母线条数
    List<TerminalTree> downVoltage = lineMapper.getvoltage(subId, 1);
    //获取母线id
    List<String> ids = downVoltage.stream().map(TerminalTree::getId).collect(Collectors.toList());
    //查询节点是否绑定
    List<PqsTflgass> pqsTflgasses = pqsTflgassMapper.selectFlgass(subId, ids);


    String[][] arr = new String[pqsTflgasses.size()][2];
    if (CollUtil.isNotEmpty(pqsTflgasses) && ids.size() > 2) {
        for (int i = 0; i < pqsTflgasses.size(); i++) {
            arr[i][0] = pqsTflgasses.get(i).getLogicBefore();
            arr[i][1] = pqsTflgasses.get(i).getLogicNext();
        }

    }
    voltage.put("bind", arr);

    Stream<String> beforeStream = pqsTflgasses.stream().map(PqsTflgass::getLogicBefore);
    Stream<String> nextStream = pqsTflgasses.stream().map(PqsTflgass::getLogicNext);
    //合并节点，要是母线都存在，说明是跨变电站，反之是本变电站
    List<String> merge = Stream.concat(beforeStream, nextStream)
            .distinct()
            .collect(Collectors.toList());
    //查看已经绑定的母线
    List<String> bindList = merge.stream().filter(x -> ids.contains(x)).collect(Collectors.toList());

    //绑定母线，如有绑定的母线下不存在这个母线，说明是有跨变电站数据，则新增母线节点，为最底层节点往后，拼接
    List<String> straddleList = merge.stream().filter(x -> !ids.contains(x)).collect(Collectors.toList());
    //判断当前母线下是否还存在，没绑定节点
    List<String> not = ids.stream().filter(x -> !bindList.contains(x)).collect(Collectors.toList());


    /**
     * 当母线节点+1等于去重之后的绑定节点说明，节点已经全部绑定了
     *
     * 跨变电站
     *      1.1当母线小于俩条时为跨
     *      1.2当都所有节点都绑定时，只有最起始节点为跨
     * 本变电站
     *      1.1当有俩条时或者大于俩条时时为本
     *      1.2当已绑定有跨变电站时，则上节点为最底层节点母线，下节点为没绑定的母线信息
     */


    if (ObjectUtil.isNotNull(one)) {
        List<String> collect = ids.stream().filter(x -> x.equals(one.getLogicBefore())).collect(Collectors.toList());
        /**
         * 本
         *  1.当母线大于俩条
         *  2.当所有母线没有绑定是
         */
        if (collect.size() > 0) {
            List<TerminalTree> upVOList;
            if (straddleList.size() > 0 && not.size() > 0) {
                String s = mostNext(pqsTflgasses);
                upVOList = downVoltage.stream().filter(x -> s.equals(x.getId())).collect(Collectors.toList());
            } else {
                //上节点id
                List<String> upIDs = pqsTflgasses.stream().map(PqsTflgass::getLogicBefore).collect(Collectors.toList());
                upVOList = downVoltage.stream().filter(x -> !upIDs.contains(x.getId())).collect(Collectors.toList());
            }

            //下节点id
            List<String> downIDs = pqsTflgasses.stream().map(PqsTflgass::getLogicNext).collect(Collectors.toList());
            List<TerminalTree> downVOList = downVoltage.stream().filter(x -> !downIDs.contains(x.getId())).collect(Collectors.toList());

            if (ObjectUtil.isNotNull(one)) {
                TerminalTree beforeTree = addTree(one.getLogicBefore());
                TerminalTree nextTree = addTree(one.getLogicNext());
                upVOList.add(beforeTree);
                downVOList.add(nextTree);
            }
            List<TerminalTree> upList = TreedChildren(upVOList, "变压器上节点设备树", subId);
            voltage.put("upNode", upList);
            List<TerminalTree> downList = TreedChildren(downVOList, "变压器下节点设备树", subId);
            voltage.put("downNode", downList);
            return voltage;
        } else {
            //查询所有变电站下母线信息
            List<TerminalTree> upVoltage = lineMapper.getvoltage(subId, 0);
            if (merge.size() == ids.size()) {
                String s = mostBefore(pqsTflgasses);
                downVoltage = downVoltage.stream().filter(x -> x.getId().equals(s)).collect(Collectors.toList());

            }
            if (ObjectUtil.isNotNull(one)) {
                TerminalTree beforeTree = addTree(one.getLogicBefore());
                TerminalTree nextTree = addTree(one.getLogicNext());
                upVoltage = upVoltage.stream().filter(x -> !x.getId().equals(beforeTree.getId())).collect(Collectors.toList());
                upVoltage.add(beforeTree);

                downVoltage.clear();
                downVoltage.add(nextTree);
            }
            List<TerminalTree> upList = TreedChildren(upVoltage, "变压器上节点设备树", null);
            voltage.put("upNode", upList);
            List<TerminalTree> downList = TreedChildren(downVoltage, "变压器下节点设备树", subId);
            voltage.put("downNode", downList);
            return voltage;
        }

    } else {
        //判断是否已经全部绑定
        String chain = chain(pqsTflgasses);
        String chain2 = chain2(pqsTflgasses);
        if (StrUtil.isNotBlank(chain) && not.size() == 0 && pqsTflgasses.size() + 1 != ids.size() && straddleList.size() == 0
                || StrUtil.isNotBlank(chain2) && not.size() == 0 && pqsTflgasses.size() + 1 != ids.size() && straddleList.size() == 0) {
            List<String> logicBefore = pqsTflgasses.stream().map(PqsTflgass::getLogicBefore).collect(Collectors.toList());
            List<String> logicNext = pqsTflgasses.stream().map(PqsTflgass::getLogicNext).collect(Collectors.toList());
            List<TerminalTree> up = downVoltage.stream().filter(x -> !logicBefore.contains(x.getId())).collect(Collectors.toList());
            List<TerminalTree> upList = TreedChildren(up, "变压器上节点设备树", subId);
            voltage.put("upNode", upList);

            List<TerminalTree> down = downVoltage.stream().filter(x -> !logicNext.contains(x.getId())).collect(Collectors.toList());
            List<TerminalTree> downList = TreedChildren(down, "变压器下节点设备树", subId);
            voltage.put("downNode", downList);
            return voltage;
        } else {
            if (downVoltage.size() > 1 && straddleList.size() == 0 && bindList.size() != ids.size()
                    || straddleList.size() > 0 && not.size() > 0

            ) {
                List<TerminalTree> upVOList;
                if (straddleList.size() > 0 && not.size() > 0) {
                    String s = mostNext(pqsTflgasses);
                    upVOList = downVoltage.stream().filter(x -> s.equals(x.getId())).collect(Collectors.toList());
                } else {
                    //上节点id
                    List<String> upIDs = pqsTflgasses.stream().map(PqsTflgass::getLogicBefore).collect(Collectors.toList());
                    upVOList = downVoltage.stream().filter(x -> !upIDs.contains(x.getId())).collect(Collectors.toList());
                }

                //下节点id
                List<String> downIDs = pqsTflgasses.stream().map(PqsTflgass::getLogicNext).collect(Collectors.toList());

                List<TerminalTree> downVOList = downVoltage.stream().filter(x -> !downIDs.contains(x.getId())).collect(Collectors.toList());
                if (not.size() == 1) {
                    List<String> logicBefore = pqsTflgasses.stream().map(PqsTflgass::getLogicBefore).collect(Collectors.toList());
                    downVOList = downVOList.stream().filter(x -> !logicBefore.contains(x.getId())).collect(Collectors.toList());
                }
                List<TerminalTree> upList = TreedChildren(upVOList, "变压器上节点设备树", subId);
                voltage.put("upNode", upList);
                List<TerminalTree> downList = TreedChildren(downVOList, "变压器下节点设备树", subId);
                voltage.put("downNode", downList);
            }

            /**
             * 跨
             *  1.当母线小于2条跨
             *  2.当所有母线都绑定为跨
             */
            else {
                //查询所有变电站下母线信息
                List<TerminalTree> upVoltage = lineMapper.getvoltage(subId, 0);
                if (merge.size() == ids.size()) {
                    String s = mostBefore(pqsTflgasses);
                    downVoltage = downVoltage.stream().filter(x -> x.getId().equals(s)).collect(Collectors.toList());

                }
                //下节点id
                List<String> downIDs = pqsTflgasses.stream().map(PqsTflgass::getLogicNext).collect(Collectors.toList());
                List<TerminalTree> downVOList = downVoltage.stream().filter(x -> !downIDs.contains(x.getId())).collect(Collectors.toList());

                List<TerminalTree> upList = TreedChildren(upVoltage, "变压器上节点设备树", null);
                voltage.put("upNode", upList);
                List<TerminalTree> downList = TreedChildren(downVOList, "变压器下节点设备树", subId);
                voltage.put("downNode", downList);
                return voltage;
            }
        }
    }
    return voltage;
}


@Override
public List<TerminalTree> getTransformerTree() {
    List<TerminalTree> terminalTrees = this.baseMapper.listTransformer();
    return TreedChildren(terminalTrees, "变压器设备树", null);
}

@Override
public List<EntityLogicDTO> getLogic() {
    return pqsTflgassMapper.getLogic();
}

/**
 * 获取全部子节点
 *
 * @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 children 子节点
 * @param name     节点名称
 * @param subId    不为空则只展示，当前变电站节点的信息，为空则展示所有信息
 * @return
 */
public List<TerminalTree> TreedChildren(List<TerminalTree> children, String name, String subId) {
    List<TerminalTree> taiZhang = new ArrayList<>();
    List<TerminalTree> allList = lineMapper.getAllList();

    List<TerminalTree> gdList;
    List<TerminalTree> subList;
    List<TerminalTree> provinceList;
    if (StrUtil.isBlank(subId)) {
        subList = allList.stream().filter(item -> item.getLevel().equals(LineBaseEnum.SUB_LEVEL.getCode())).sorted(Comparator.comparing(TerminalTree::getSort)).collect(Collectors.toList());
        gdList = allList.stream().filter(item -> item.getLevel().equals(LineBaseEnum.GD_LEVEL.getCode())).sorted(Comparator.comparing(TerminalTree::getSort)).collect(Collectors.toList());
        provinceList = lineMapper.getProvinceList(null, 0);
    } else {
        subList = allList.stream().filter(item -> item.getLevel().equals(LineBaseEnum.SUB_LEVEL.getCode())
                && item.getId().equals(subId)
        ).sorted(Comparator.comparing(TerminalTree::getSort)).collect(Collectors.toList());
        gdList = allList.stream().filter(item -> item.getLevel().equals(LineBaseEnum.GD_LEVEL.getCode())
                && item.getId().equals(subList.get(0).getPid())
        ).sorted(Comparator.comparing(TerminalTree::getSort)).collect(Collectors.toList());
        provinceList = lineMapper.getProvinceList(Arrays.asList(gdList.get(0).getPid()), 1);
    }
    List<TerminalTree> terminal = children.stream().sorted(Comparator.comparing(TerminalTree::getSort)).collect(Collectors.toList());

    subList.forEach(sub -> sub.setChildren(getChildren(sub, terminal)));
    gdList.forEach(gd -> gd.setChildren(getChildren(gd, subList)));
    provinceList.forEach(province -> province.setChildren(getChildren(province, gdList)));

    if (CollectionUtil.isNotEmpty(allList)) {
        TerminalTree terminalTree = new TerminalTree();
        terminalTree.setId("9999999");
        terminalTree.setLevel(0);
        terminalTree.setName(name);
        if (CollectionUtil.isNotEmpty(provinceList)) {
            terminalTree.setChildren(provinceList);
        }
        taiZhang.add(terminalTree);
    }
    return taiZhang;
}

/**
 * 查找最顶层节点信息
 *
 * @param pqsTflgasses
 * @return
 */
private static String mostBefore(List<PqsTflgass> pqsTflgasses) {
    String logicBefore = null;
    for (PqsTflgass pqsTflgass : pqsTflgasses) {
        //根据获取的的上节点判断是否存在，这上节点是否是尾节点
        if (ObjectUtil.isNull(logicBefore)) {
            logicBefore = pqsTflgass.getLogicBefore();
        } else {
            while (true) {
                for (int i1 = 0; i1 < pqsTflgasses.size(); i1++) {
                    String logicNext = pqsTflgasses.get(i1).getLogicNext();
                    if (logicBefore.equals(logicNext)) {
                        logicBefore = pqsTflgasses.get(i1).getLogicBefore();
                        break;
                    } else {
                        if (i1 == pqsTflgasses.size()) {
                            break;
                        }
                    }
                }
                break;
            }
        }
    }
    return logicBefore;
}

/**
 * 查找最底层节点信息
 *
 * @param pqsTflgasses
 * @return
 */
private static String mostNext(List<PqsTflgass> pqsTflgasses) {
    String logicNext = null;
    for (PqsTflgass pqsTflgass : pqsTflgasses) {
        //根据获取的的上节点判断是否存在，这上节点是否是尾节点
        if (ObjectUtil.isNull(logicNext)) {
            logicNext = pqsTflgass.getLogicNext();
        } else {
            while (true) {
                for (int i1 = 0; i1 < pqsTflgasses.size(); i1++) {
                    String logicBefore = pqsTflgasses.get(i1).getLogicBefore();
                    if (logicNext.equals(logicBefore)) {
                        logicNext = pqsTflgasses.get(i1).getLogicNext();
                        break;
                    } else {
                        if (i1 == pqsTflgasses.size()) {
                            break;
                        }
                    }
                }
                break;
            }
        }
    }
    return logicNext;
}

/**
 * 判断是否已经成为链行
 *
 * @param pqsTflgasses
 * @return
 */
private static String chain(List<PqsTflgass> pqsTflgasses) {
    if (pqsTflgasses.size() < 2) {
        return null;
    }
    String logicNext = null;
    for (PqsTflgass pqsTflgass : pqsTflgasses) {
        //根据获取的的下节点判断是否存在，这下节点是否是首节点
        if (ObjectUtil.isNull(logicNext)) {
            logicNext = pqsTflgass.getLogicNext();
        } else {
            while (true) {
                for (int i1 = 0; i1 < pqsTflgasses.size(); i1++) {
                    String logicBefore = pqsTflgasses.get(i1).getLogicBefore();
                    if (logicNext.equals(logicBefore)) {
                        logicNext = pqsTflgasses.get(i1).getLogicNext();
                        break;
                    } else {
                        if (i1 == pqsTflgasses.size() - 1) {
                            return logicNext;
                        }
                    }
                }
                break;
            }

        }
    }
    return logicNext;
}

private static String chain2(List<PqsTflgass> pqsTflgasses) {
    if (pqsTflgasses.size() < 2) {
        return null;
    }
    String logicBefore = null;
    for (PqsTflgass pqsTflgass : pqsTflgasses) {
        //根据获取的的下节点判断是否存在，这下节点是否是首节点
        if (ObjectUtil.isNull(logicBefore)) {
            logicBefore = pqsTflgass.getLogicBefore();
        } else {
            while (true) {
                for (int i1 = 0; i1 < pqsTflgasses.size(); i1++) {
                    String logicNext = pqsTflgasses.get(i1).getLogicNext();
                    if (logicBefore.equals(logicNext)) {
                        logicBefore = pqsTflgasses.get(i1).getLogicBefore();
                        break;
                    } else {
                        if (i1 == pqsTflgasses.size() - 1) {
                            return logicBefore;
                        }
                    }
                }
                break;
            }

        }
    }
    return logicBefore;
}

/**
 * 根据母线获取信息
 *
 * @param id
 * @return
 */
public TerminalTree addTree(String id) {
    List<TerminalTree> getvoltage = lineMapper.getvoltage(id, 2);
    getvoltage.get(0).setLevel(5);
    return getvoltage.get(0);
}



}
