package com.ruoyi.system.service.impl;

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

import com.ruoyi.common.annotation.DataScope;
import com.ruoyi.common.core.domain.TreeSelect;
import com.ruoyi.common.core.domain.entity.CategoryProposal;
import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.spring.SpringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.CategoryProposalMapper;
import com.ruoyi.system.service.ICategoryProposalService;

/**
 * 提案类别Service业务层处理
 * 
 * @author boman
 * @date 2024-03-07
 */
@Service
public class CategoryProposalServiceImpl implements ICategoryProposalService 
{
    @Autowired
    private CategoryProposalMapper categoryProposalMapper;

    /**
     * 查询提案类别
     * 
     * @param categoryId 提案类别主键
     * @return 提案类别
     */
    @Override
    public CategoryProposal selectCategoryProposalByCategoryId(Long categoryId)
    {
        return categoryProposalMapper.selectCategoryProposalByCategoryId(categoryId);
    }

    /**
     * 查询提案类别列表
     * 
     * @param categoryProposal 提案类别
     * @return 提案类别
     */
    @Override
    @DataScope(deptAlias = "d")
    public List<CategoryProposal> selectCategoryProposalList(CategoryProposal categoryProposal)
    {
        return categoryProposalMapper.selectCategoryProposalList(categoryProposal);
    }

    /**
     * 新增提案类别
     * 
     * @param categoryProposal 提案类别
     * @return 结果
     */
    @Override
    public int insertCategoryProposal(CategoryProposal categoryProposal)
    {
        categoryProposal.setCreateTime(DateUtils.getNowDate());
        return categoryProposalMapper.insertCategoryProposal(categoryProposal);
    }

    /**
     * 修改提案类别
     * 
     * @param categoryProposal 提案类别
     * @return 结果
     */
    @Override
    public int updateCategoryProposal(CategoryProposal categoryProposal)
    {
        categoryProposal.setUpdateTime(DateUtils.getNowDate());
        return categoryProposalMapper.updateCategoryProposal(categoryProposal);
    }

    /**
     * 批量删除提案类别
     * 
     * @param categoryIds 需要删除的提案类别主键
     * @return 结果
     */
    @Override
    public int deleteCategoryProposalByCategoryIds(Long[] categoryIds)
    {
        return categoryProposalMapper.deleteCategoryProposalByCategoryIds(categoryIds);
    }

    /**
     * 删除提案类别信息
     * 
     * @param categoryId 提案类别主键
     * @return 结果
     */
    @Override
    public int deleteCategoryProposalByCategoryId(Long categoryId)
    {
        return categoryProposalMapper.deleteCategoryProposalByCategoryId(categoryId);
    }

    @Override
    public List<TreeSelect> selectCategoryTreeList(CategoryProposal categoryProposal) {
        List<CategoryProposal> categoryProposals = SpringUtils.getAopProxy(this).selectCategoryProposalList(categoryProposal);
        return buildCategoryTreeSelect(categoryProposals);
    }

    @Override
    public List<TreeSelect> buildCategoryTreeSelect(List<CategoryProposal> categoryProposals) {
        List<CategoryProposal> categoryTrees = buildCategoryTree(categoryProposals);
        return categoryTrees.stream().map(TreeSelect::new).collect(Collectors.toList());
    }

    /**
     * 构建前端所需要树结构
     *
     * @param
     * @return 树结构列表
     */
    private List<CategoryProposal> buildCategoryTree(List<CategoryProposal> categoryProposals) {
        List<CategoryProposal> returnList = new ArrayList<CategoryProposal>();
        List<Long> tempList = categoryProposals.stream().map(CategoryProposal::getCategoryId).collect(Collectors.toList());
        for (CategoryProposal category : categoryProposals)
        {
            // 如果是顶级节点, 遍历该父节点的所有子节点
            if (!tempList.contains(category.getParentId()))
            {
                recursionFn(categoryProposals, category);
                returnList.add(category);
            }
        }
        if (returnList.isEmpty())
        {
            returnList = categoryProposals;
        }
        return returnList;
    }

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

    /**
     * 得到子节点列表
     */
    private List<CategoryProposal> getChildList(List<CategoryProposal> categoryProposals, CategoryProposal category) {
        List<CategoryProposal> tlist = new ArrayList<CategoryProposal>();
        Iterator<CategoryProposal> it = categoryProposals.iterator();
        while (it.hasNext())
        {
            CategoryProposal n = (CategoryProposal) it.next();
            if (StringUtils.isNotNull(n.getParentId()) && n.getParentId().longValue() == category.getCategoryId().longValue())
            {
                tlist.add(n);
            }
        }
        return tlist;
    }


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



}
