package com.ruoyi.framework.recovery.service.impl;

import com.ruoyi.common.core.domain.TreeSelect;
import com.ruoyi.framework.recovery.domain.RecBrand;
import com.ruoyi.framework.recovery.domain.vo.TreeSelectVo;
import com.ruoyi.framework.recovery.mapper.RecBrandMapper;
import com.ruoyi.framework.recovery.service.IRecBrandService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

/**
 * 回收品牌Service业务层处理
 *
 * @author jin
 * @date 2024-11-16
 */
@Service
public class RecBrandServiceImpl implements IRecBrandService
{
    @Autowired
    private RecBrandMapper recBrandMapper;

    /**
     * 查询回收品牌
     *
     * @param id 回收品牌主键
     * @return 回收品牌
     */
    @Override
    public RecBrand selectRecBrandById(Long id)
    {
        return recBrandMapper.selectRecBrandById(id);
    }

    /**
     * 查询回收品牌列表
     *
     * @param recBrand 回收品牌
     * @return 回收品牌
     */
    @Override
    public List<RecBrand> selectRecBrandList(RecBrand recBrand)
    {
        return recBrandMapper.selectRecBrandList(recBrand);
    }

    /**
     * 新增回收品牌
     *
     * @param recBrand 回收品牌
     * @return 结果
     */
    @Override
    public int insertRecBrand(RecBrand recBrand)
    {
        recBrand.setCreateDate(new Date());
        return recBrandMapper.insertRecBrand(recBrand);
    }

    /**
     * 修改回收品牌
     *
     * @param recBrand 回收品牌
     * @return 结果
     */
    @Override
    public int updateRecBrand(RecBrand recBrand)
    {
        return recBrandMapper.updateRecBrand(recBrand);
    }

    /**
     * 批量删除回收品牌
     *
     * @param ids 需要删除的回收品牌主键
     * @return 结果
     */
    @Override
    public int deleteRecBrandByIds(Long[] ids)
    {
        return recBrandMapper.deleteRecBrandByIds(ids);
    }

    /**
     * 删除回收品牌信息
     *
     * @param id 回收品牌主键
     * @return 结果
     */
    @Override
    public int deleteRecBrandById(Long id)
    {
        return recBrandMapper.deleteRecBrandById(id);
    }

    @Override
    public List<TreeSelectVo> buildBrandSelect(List<RecBrand> list) {
        List<RecBrand> brands = buildMenuTree(list);
        return brands.stream().map(TreeSelectVo::new).collect(Collectors.toList());
    }

    /**
     * 构建前端所需要树结构
     *
     * @param menus 菜单列表
     * @return 树结构列表
     */
    public List<RecBrand> buildMenuTree(List<RecBrand> menus)
    {
        List<RecBrand> returnList = new ArrayList<>();
        List<Long> tempList = menus.stream().map(RecBrand::getId).collect(Collectors.toList());
        for (Iterator<RecBrand> iterator = menus.iterator(); iterator.hasNext();)
        {
            RecBrand menu = iterator.next();
            // 如果是顶级节点, 遍历该父节点的所有子节点
            if (!tempList.contains(menu.getParentId()))
            {
                recursionFn(menus, menu);
                returnList.add(menu);
            }
        }
        if (returnList.isEmpty())
        {
            returnList = menus;
        }
        return returnList;
    }

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

    /**
     * 得到子节点列表
     */
    private List<RecBrand> getChildList(List<RecBrand> list, RecBrand t)
    {
        List<RecBrand> tlist = new ArrayList<>();
        Iterator<RecBrand> it = list.iterator();
        while (it.hasNext())
        {
            RecBrand n = (RecBrand) it.next();
            if (n.getParentId().longValue() == t.getId().longValue())
            {
                tlist.add(n);
            }
        }
        return tlist;
    }

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

}
