package com.ruoyi.powerdistribution.service.impl;

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

import com.ruoyi.common.core.domain.entity.SysMenu;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.powerdistribution.mapper.PdmExamineRequireMapper;
import com.ruoyi.powerdistribution.domain.PdmExamineRequire;
import com.ruoyi.powerdistribution.service.IPdmExamineRequireService;

/**
 * 时户数要求
Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-01-02
 */
@Service
public class PdmExamineRequireServiceImpl implements IPdmExamineRequireService 
{
    @Autowired
    private PdmExamineRequireMapper pdmExamineRequireMapper;

    /**
     * 查询时户数要求

     * 
     * @param id 时户数要求
主键
     * @return 时户数要求

     */
    @Override
    public PdmExamineRequire selectPdmExamineRequireById(Long id)
    {
        return pdmExamineRequireMapper.selectPdmExamineRequireById(id);
    }

    /**
     * 查询时户数要求
列表
     * 
     * @param pdmExamineRequire 时户数要求

     * @return 时户数要求

     */
    @Override
    public List<PdmExamineRequire> selectPdmExamineRequireList(PdmExamineRequire pdmExamineRequire)
    {
        return pdmExamineRequireMapper.selectPdmExamineRequireList(pdmExamineRequire);
    }

    /**
     * 新增时户数要求

     * 
     * @param pdmExamineRequire 时户数要求

     * @return 结果
     */
    @Override
    public int insertPdmExamineRequire(PdmExamineRequire pdmExamineRequire)
    {
        pdmExamineRequire.setCreateTime(DateUtils.getNowDate());
        return pdmExamineRequireMapper.insertPdmExamineRequire(pdmExamineRequire);
    }

    /**
     * 修改时户数要求

     * 
     * @param pdmExamineRequire 时户数要求

     * @return 结果
     */
    @Override
    public int updatePdmExamineRequire(PdmExamineRequire pdmExamineRequire)
    {
        pdmExamineRequire.setUpdateTime(DateUtils.getNowDate());
        return pdmExamineRequireMapper.updatePdmExamineRequire(pdmExamineRequire);
    }

    /**
     * 批量删除时户数要求

     * 
     * @param ids 需要删除的时户数要求
主键
     * @return 结果
     */
    @Override
    public int deletePdmExamineRequireByIds(Long[] ids)
    {
        return pdmExamineRequireMapper.deletePdmExamineRequireByIds(ids);
    }

    /**
     * 删除时户数要求
信息
     * 
     * @param id 时户数要求
主键
     * @return 结果
     */
    @Override
    public int deletePdmExamineRequireById(Long id)
    {
        return pdmExamineRequireMapper.deletePdmExamineRequireById(id);
    }

    @Override
    public List<PdmExamineRequire> selectExamineRequireTree() {
        List<PdmExamineRequire> returnList=new ArrayList<>();
        List<PdmExamineRequire> pdmExamineRequireList= pdmExamineRequireMapper.selectAllExamineRequireList(new PdmExamineRequire());
        List<Long> tempList = pdmExamineRequireList.stream().map(PdmExamineRequire::getId).collect(Collectors.toList());
        for (Iterator<PdmExamineRequire> iterator = pdmExamineRequireList.iterator(); iterator.hasNext();)
        {
            PdmExamineRequire pdmExamineRequire = (PdmExamineRequire) iterator.next();
            // 如果是顶级节点, 遍历该父节点的所有子节点
            if (!tempList.contains(pdmExamineRequire.getpId()))
            {
                recursionFn(pdmExamineRequireList, pdmExamineRequire);
                returnList.add(pdmExamineRequire);
            }
        }
        if (returnList.isEmpty())
        {
            returnList = pdmExamineRequireList;
        }
        return returnList;
    }
    /**
     * 递归列表
     *
     * @param list 分类表
     * @param t 子节点
     */
    private void recursionFn(List<PdmExamineRequire> list, PdmExamineRequire t)
    {
        // 得到子节点列表
        List<PdmExamineRequire> childList = getChildList(list, t);
        t.setChildren(childList);
        for (PdmExamineRequire tChild : childList)
        {
            if (hasChild(list, tChild))
            {
                recursionFn(list, tChild);
            }
        }
    }
    /**
     * 得到子节点列表
     */
    private List<PdmExamineRequire> getChildList(List<PdmExamineRequire> list, PdmExamineRequire t)
    {
        List<PdmExamineRequire> tlist = new ArrayList<PdmExamineRequire>();
        Iterator<PdmExamineRequire> it = list.iterator();
        while (it.hasNext())
        {
            PdmExamineRequire n = (PdmExamineRequire) it.next();
            if (n.getpId().longValue() == t.getId().longValue())
            {
                tlist.add(n);
            }
        }
        return tlist;
    }
    /**
     * 判断是否有子节点
     */
    private boolean hasChild(List<PdmExamineRequire> list, PdmExamineRequire t)
    {
        return getChildList(list, t).size() > 0;
    }

}
