
package com.ruoyi.base.service.impl;


import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.ruoyi.base.domain.*;


import com.ruoyi.base.mapper.*;
import com.ruoyi.base.service.ICabinetAndBusbarTreeService;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;




@Service
public class CabinetAndBusbarTreeServiceImpl implements ICabinetAndBusbarTreeService {

    @Autowired
    private BaseCabinetInfoMapper baseCabinetInfoMapper;

    @Autowired
    private BaseBusbarInfoMapper baseBusbarInfoMapper;


    /**
     * 查询电柜信息和母排信息树列表
     * 电柜信息的子节点是母排信息
     *
     * @return 所有电柜信息及下级母排信息
     */
    @Override
    public List<CabinetAndBusbarTree> selectCabinetAndBusbarTree(BaseCabinetInfo baseCabinetInfo) {
        // 创建一个空的List，用于存储查询结果
        List<CabinetAndBusbarTree> tree = new ArrayList<>();
        /*        BaseCabinetInfo allBaseCabinetInfo = new BaseCabinetInfo();*/
        // 根据传入的baseCabinetInfo查询所有符合条件的BaseCabinetInfo
        List<BaseCabinetInfo> cabinets = baseCabinetInfoMapper.selectBaseCabinetInfoList(baseCabinetInfo);

        // 遍历查询结果
        for (BaseCabinetInfo cabinet : cabinets) {
            // 创建一个CabinetAndBusbarTree对象，用于存储查询结果
            CabinetAndBusbarTree cabinetNode = new CabinetAndBusbarTree(cabinet);
            // 创建一个BaseBusbarInfo对象，用于存储查询条件
            BaseBusbarInfo allBaseBusbarInfo = new BaseBusbarInfo();
            // 设置查询条件
            allBaseBusbarInfo.setCabinetCode(cabinet.getCabinetCode());
            // 根据查询条件查询所有符合条件的BaseBusbarInfo
            List<BaseBusbarInfo> busbars = baseBusbarInfoMapper.selectBaseBusbarInfoList(allBaseBusbarInfo);
            for (BaseBusbarInfo busbar : busbars){
                cabinetNode.addChild(new CabinetAndBusbarTree(busbar));
            }
            // 将查询结果添加到tree中
            tree.add(cabinetNode);
        }
        // 返回查询结果
        return tree;
    }


/*

    public List<CabinetAndBusbarTree> selectCabinetAndBusbarTree(BaseCabinetInfo baseCabinetInfo){
        List<BaseCabinetInfo> baseCabinetInfos = baseCabinetInfoMapper.selectBaseCabinetInfoList(baseCabinetInfo);
        return buildCabinetAndBusbarTreeSelects(baseCabinetInfos);
    }

    private List<CabinetAndBusbarTree> buildCabinetAndBusbarTreeSelects(List<BaseCabinetInfo> baseCabinetInfos) {
        List<CabinetAndBusbarTree> cabinetAndBusbarTrees = buildCabinetAndBusbarTree(baseCabinetInfos);
        return cabinetAndBusbarTrees.stream().map(CabinetAndBusbarTree::new).collect(Collectors.toList());
    }

    private List<CabinetAndBusbarTree> buildCabinetAndBusbarTree(List<BaseCabinetInfo> baseCabinetInfos) {
        List<CabinetAndBusbarTree> cabinetAndBusbarTrees = new ArrayList<>();

        for (BaseCabinetInfo baseCabinetInfo : baseCabinetInfos) {
                recursionFn(baseCabinetInfos, baseCabinetInfo);
                cabinetAndBusbarTrees.add(baseCabinetInfo);

        }
        if (cabinetAndBusbarTrees.isEmpty()) {
            cabinetAndBusbarTrees = baseCabinetInfos;
        }
        return cabinetAndBusbarTrees;
    }

    private void recursionFn(List<BaseCabinetInfo> baseCabinetInfos, BaseCabinetInfo baseCabinetInfo) {
        List<BaseBusbarInfo> baseBusbarInfos = getChildList(list, t);
    }
    private List<BaseBusbarInfo> getChildList(List<BaseBusbarInfo> list, BaseBusbarInfo t) {
        List<BaseBusbarInfo> baseBusbarInfos = new ArrayList<>();
        Iterator<BaseBusbarInfo> it = list.iterator();
        while(it.hasNext()) {
            BaseBusbarInfo n = (BaseBusbarInfo)it.next();
            if(n.getParentObjId().longValue()==t.getObjId().longValue()) {
                baseBusbarInfos.add(n);
            }
        }
        return baseBusbarInfos;
    }
*/


}

