package com.ruoyi.mall.service.impl;

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

import com.ruoyi.common.core.domain.RegionTreeSelect;
import com.ruoyi.common.core.domain.TreeSelect;
import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.mall.domain.MallRegion;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.mall.mapper.MallRegionMapper;
import com.ruoyi.mall.service.IMallRegionService;

/**
 * 省市区Service业务层处理
 *
 * @author ruoyi
 * @date 2022-07-15
 */
@Service
public class MallRegionServiceImpl implements IMallRegionService
{
    @Autowired
    private MallRegionMapper mallRegionMapper;

    /**
     * 查询省市区
     *
     * @param id 省市区主键
     * @return 省市区
     */
    @Override
    public MallRegion selectMallRegionById(Integer id)
    {
        return mallRegionMapper.selectMallRegionById(id);
    }

    /**
     * 查询省市区列表
     *
     * @param mallRegion 省市区
     * @return 省市区
     */
    @Override
    public List<MallRegion> selectMallRegionList(MallRegion mallRegion)
    {
        return mallRegionMapper.selectMallRegionList(mallRegion);
    }

    /**
     * 查询省市区列表
     *
     * @return 省市区集合
     */
    @Override
    public List<RegionTreeSelect> buildRegionTreeList(){

        List<MallRegion> mallRegions = buildTree(mallRegionMapper.selectMallRegionList(new MallRegion()));
        return mallRegions.stream().map(RegionTreeSelect::new).collect(Collectors.toList());
    }

    public List<MallRegion> buildTree(List<MallRegion> regions){

        List<MallRegion> returnList = new ArrayList<MallRegion>();
        List<Integer> tempList = new ArrayList<Integer>();
        for (MallRegion region : regions) {
            tempList.add(region.getId());
        }
        for (Iterator<MallRegion> iterator = regions.iterator(); iterator.hasNext();){
            MallRegion reg = iterator.next();
            // 如果是顶级节点, 遍历该父节点的所有子节点
            if (!tempList.contains(reg.getPid())){
                recursionFn(regions, reg);
                returnList.add(reg);
            }
        }
        return returnList.size() > 0 ? returnList : regions;
    }

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

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

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

    /**
     * 查询省市区列表
     *
     * @param pid 省市区
     * @return 省市区
     */
    @Override
    public List<MallRegion> selectList(Integer pid)
    {
        return mallRegionMapper.selectList(pid);
    }

    /**
     * 新增省市区
     *
     * @param mallRegion 省市区
     * @return 结果
     */
    @Override
    public int insertMallRegion(MallRegion mallRegion)
    {
        return mallRegionMapper.insertMallRegion(mallRegion);
    }

    /**
     * 修改省市区
     *
     * @param mallRegion 省市区
     * @return 结果
     */
    @Override
    public int updateMallRegion(MallRegion mallRegion)
    {
        return mallRegionMapper.updateMallRegion(mallRegion);
    }

    /**
     * 批量删除省市区
     *
     * @param ids 需要删除的省市区主键
     * @return 结果
     */
    @Override
    public int deleteMallRegionByIds(Integer[] ids)
    {
        return mallRegionMapper.deleteMallRegionByIds(ids);
    }

    /**
     * 删除省市区信息
     *
     * @param id 省市区主键
     * @return 结果
     */
    @Override
    public int deleteMallRegionById(Integer id)
    {
        return mallRegionMapper.deleteMallRegionById(id);
    }

    /**
     * 根据上级id获取省市区列表（0-省级）
     *
     * @return 行政区域集合
     */
    @Override
    public List<MallRegion> getRegionByPid(Integer pid) {
        return mallRegionMapper.getRegionByPid(pid);
    }
}
