package cn.cecep.talroad.service.impl;

import cn.cecep.talroad.common.core.utils.StringUtils;
import cn.cecep.talroad.domain.BEnterpriseIrEesType;
import cn.cecep.talroad.mapper.BEnterpriseIrEesTypeMapper;
import cn.cecep.talroad.service.IBEnterpriseIrEesTypeService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

/**
 * 知识库-生态环境标准-分类Service业务层处理
 *
 * @author szhpt
 * @date 2023-03-14*/
@Service
public class BEnterpriseIrEesTypeServiceImpl extends ServiceImpl<BEnterpriseIrEesTypeMapper,BEnterpriseIrEesType> implements IBEnterpriseIrEesTypeService
{
    @Autowired
    private BEnterpriseIrEesTypeMapper bEnterpriseIrEesTypeMapper;

    /**
     * 查询知识库-生态环境标准-分类列表
     *
     * @param bEnterpriseIrEesType 知识库-生态环境标准-分类
     * @return 知识库-生态环境标准-分类
     */
    @Override
    public List<BEnterpriseIrEesType> selectBEnterpriseIrEesTypeList(BEnterpriseIrEesType bEnterpriseIrEesType)
    {
        return bEnterpriseIrEesTypeMapper.selectBEnterpriseIrEesTypeList(bEnterpriseIrEesType);
    }

    @Override
    public List<BEnterpriseIrEesType> selectTreeList(String eesClassify) {
        List<BEnterpriseIrEesType> bEnterpriseIrEesTypes = bEnterpriseIrEesTypeMapper.selectList(new QueryWrapper<>());
        return StringUtils.isNotEmpty(eesClassify) ? getChildPerms(bEnterpriseIrEesTypes, eesClassify) : buildIrEesTree(bEnterpriseIrEesTypes);
    }

    /**
     * 根据父节点的ID获取所有子节点
     *
     * @param list 分类表
     * @param parentId 传入的父节点ID
     * @return String
     */
    public List<BEnterpriseIrEesType> getChildPerms(List<BEnterpriseIrEesType> list, String parentId)
    {
        List<BEnterpriseIrEesType> returnList = new ArrayList<BEnterpriseIrEesType>();
        for (Iterator<BEnterpriseIrEesType> iterator = list.iterator(); iterator.hasNext();)
        {
            BEnterpriseIrEesType t = (BEnterpriseIrEesType) iterator.next();
            // 一、根据传入的某个父节点ID,遍历该父节点的所有子节点
            if (StringUtils.equals(t.getEesTitle(), parentId))
            {
                recursionFn(list, t);
                returnList.add(t);
            }
        }
        return returnList;
    }

    /**
     * 构建前端所需要树结构
     *
     * @param irEesList 列表
     * @return 树结构列表
     */
    public List<BEnterpriseIrEesType> buildIrEesTree(List<BEnterpriseIrEesType> irEesList)
    {
        List<BEnterpriseIrEesType> returnList = new ArrayList<>();
        List<String> tempList = irEesList.stream().map(BEnterpriseIrEesType::getEesClassify).collect(Collectors.toList());

        for (BEnterpriseIrEesType irEesType : irEesList) {
            // 如果是顶级节点, 遍历该父节点的所有子节点
            if (!tempList.contains(irEesType.getEesTitle())) {
                recursionFn(irEesList, irEesType);
                returnList.add(irEesType);
            }
        }

        if (returnList.isEmpty()) {
            returnList = irEesList;
        }
        return returnList;
    }

    /**
     * 递归列表
     */
    private void recursionFn(List<BEnterpriseIrEesType> list, BEnterpriseIrEesType t)
    {
        // 得到子节点列表
        List<BEnterpriseIrEesType> childList = getChildList(list, t);
        t.setChildren(childList);
        for (BEnterpriseIrEesType tChild : childList)
        {
            if (hasChild(list, tChild))
            {
                recursionFn(list, tChild);
            }
        }
    }

    /**
     * 得到子节点列表
     */
    private List<BEnterpriseIrEesType> getChildList(List<BEnterpriseIrEesType> list, BEnterpriseIrEesType t) {
        List<BEnterpriseIrEesType> tlist = new ArrayList<>();
        for (BEnterpriseIrEesType n : list) {
            if (StringUtils.isNotNull(n.getEesTitle()) && StringUtils.equals(n.getEesTitle(), t.getEesClassify())) {
                tlist.add(n);
            }
        }
        return tlist;
    }

    private boolean hasChild(List<BEnterpriseIrEesType> list, BEnterpriseIrEesType t) {
        return getChildList(list, t).size() > 0;
    }

}
