package com.ruoyi.interfaces.service.impl;

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

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.uuid.IdUtils;
import com.ruoyi.interfaces.domain.MdDataCate;
import com.ruoyi.interfaces.domain.TreeSelectString;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.interfaces.mapper.PtTreeCateMapper;
import com.ruoyi.interfaces.domain.PtTreeCate;
import com.ruoyi.interfaces.service.IPtTreeCateService;

/**
 * 树状目录Service业务层处理
 * 
 * @author 朱得糠
 * @date 2025-07-29
 */
@Service
public class PtTreeCateServiceImpl implements IPtTreeCateService 
{
    @Autowired
    private PtTreeCateMapper ptTreeCateMapper;

    /**
     * 查询树状目录
     * 
     * @param treeId 树状目录主键
     * @return 树状目录
     */
    @Override
    public PtTreeCate selectPtTreeCateByTreeId(String treeId)
    {
        return ptTreeCateMapper.selectPtTreeCateByTreeId(treeId);
    }

    /**
     * 查询树状目录列表
     * 
     * @param ptTreeCate 树状目录
     * @return 树状目录
     */
    @Override
    public List<PtTreeCate> selectPtTreeCateList(PtTreeCate ptTreeCate)
    {
        return ptTreeCateMapper.selectPtTreeCateList(ptTreeCate);
    }

    /**
     * 新增树状目录
     * 
     * @param ptTreeCate 树状目录
     * @return 结果
     */
    @Override
    public int insertPtTreeCate(PtTreeCate ptTreeCate)
    {
        ptTreeCate.setTreeId(IdUtils.fastUUID());
        ptTreeCate.setCreateby(DateUtils.getNowDate());
        return ptTreeCateMapper.insertPtTreeCate(ptTreeCate);
    }

    /**
     * 修改树状目录
     * 
     * @param ptTreeCate 树状目录
     * @return 结果
     */
    @Override
    public int updatePtTreeCate(PtTreeCate ptTreeCate)
    {
        ptTreeCate.setModifyby(DateUtils.getNowDate());
        return ptTreeCateMapper.updatePtTreeCate(ptTreeCate);
    }

    /**
     * 批量删除树状目录
     * 
     * @param treeIds 需要删除的树状目录主键
     * @return 结果
     */
    @Override
    public int deletePtTreeCateByTreeIds(String[] treeIds)
    {
        return ptTreeCateMapper.deletePtTreeCateByTreeIds(treeIds);
    }

    /**
     * 删除树状目录信息
     * 
     * @param treeId 树状目录主键
     * @return 结果
     */
    @Override
    public int deletePtTreeCateByTreeId(String treeId)
    {
        return ptTreeCateMapper.deletePtTreeCateByTreeId(treeId);
    }

    @Override
    public List<TreeSelectString> buildDataTreeSelect(List<PtTreeCate> menus) {

        List<PtTreeCate> menuTrees = buildDataTree(menus);
        List<TreeSelectString> collect = menuTrees.stream().map(TreeSelectString::new).collect(Collectors.toList());
        return collect;
    }

    private List<PtTreeCate> buildDataTree(List<PtTreeCate> menus) {
        List<PtTreeCate> returnList = new ArrayList<PtTreeCate>();
        List<String> tempList = menus.stream().map(PtTreeCate::getTreeId).collect(Collectors.toList());
        for (Iterator<PtTreeCate> iterator = menus.iterator(); iterator.hasNext();)
        {
            PtTreeCate menu = (PtTreeCate) iterator.next();
            // 如果是顶级节点, 遍历该父节点的所有子节点
            if (!tempList.contains(menu.getTreePid()))
            {
                recursionFn(menus, menu);
                returnList.add(menu);
            }
        }
        if (returnList.isEmpty())
        {
            returnList = menus;
        }
        return returnList;
    }
    private void recursionFn(List<PtTreeCate> list, PtTreeCate t)
    {
        // 得到子节点列表
        List<PtTreeCate> childList = getChildList(list, t);
        t.setChildren(childList);
        for (PtTreeCate tChild : childList)
        {
            if (hasChild(list, tChild))
            {
                recursionFn(list, tChild);
            }
        }
    }
    /**
     * 得到子节点列表
     */
    private List<PtTreeCate> getChildList(List<PtTreeCate> list, PtTreeCate t)
    {
        List<PtTreeCate> tlist = new ArrayList<PtTreeCate>();
        Iterator<PtTreeCate> it = list.iterator();
        while (it.hasNext())
        {
            PtTreeCate n = (PtTreeCate) it.next();
            if (n.getTreePid().equals(t.getTreeId()))
            {
                tlist.add(n);
            }
        }
        return tlist;
    }

    /**
     * 判断是否有子节点
     */
    private boolean hasChild(List<PtTreeCate> list, PtTreeCate t)
    {
        return getChildList(list, t).size() > 0;
    }

}
