package cn.iocoder.yudao.module.basic.service.goodscategory;

import cn.iocoder.yudao.module.basic.controller.admin.goodscategory.vo.GoodsCategoryVO;
import cn.iocoder.yudao.module.basic.dal.dataobject.goodscategory.GoodsCategoryDO;
import cn.iocoder.yudao.module.basic.dal.mysql.goodscategory.GoodsCategoryMapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 商品分类表。分类的编号为 C 开头，如：C100001 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class GoodsCategoryServiceImpl implements GoodsCategoryService {

    @Resource
    private GoodsCategoryMapper goodsCategoryMapper;

    /**
     * 查询所有三级分页数据
     *
     * @return
     */
    @Override
    public List<GoodsCategoryVO> getGoodsCategoryThreeLevelList() {

        // 查询所有一级分类
        List<GoodsCategoryDO> level1CategorieList = goodsCategoryMapper.selectList(new QueryWrapper<GoodsCategoryDO>().eq("level", 1));
        List<GoodsCategoryVO> goodsCategoryVO1List = new ArrayList<>();

        // 查询所有二级分类
        List<GoodsCategoryDO> level2CategorieList = goodsCategoryMapper.selectList(new QueryWrapper<GoodsCategoryDO>().eq("level", 2));
        Map<Long, List<GoodsCategoryDO>> level2CategorieMap = level2CategorieList.stream()
                .collect(Collectors.groupingBy(GoodsCategoryDO::getPid));

        // 查询所有三级分类
        List<GoodsCategoryDO> level3CategorieList = goodsCategoryMapper.selectList(new QueryWrapper<GoodsCategoryDO>().eq("level", 3));
        Map<Long, List<GoodsCategoryDO>> level3CategorieMap = level3CategorieList.stream()
                .collect(Collectors.groupingBy(GoodsCategoryDO::getPid));

        // 构建三级分类VO
        for (GoodsCategoryDO level1Categorie : level1CategorieList) {
            GoodsCategoryVO goodsCategoryVO1 = new GoodsCategoryVO();
            BeanUtils.copyProperties(level1Categorie, goodsCategoryVO1);
            goodsCategoryVO1.setChildren(new ArrayList<>());

            // 构建二级分类VO
            List<GoodsCategoryDO> level2List = level2CategorieMap.get(level1Categorie.getId());
            if (level2List != null) {
                for (GoodsCategoryDO level2Categorie : level2List) {
                    GoodsCategoryVO goodsCategoryVO2 = new GoodsCategoryVO();
                    BeanUtils.copyProperties(level2Categorie, goodsCategoryVO2);
                    goodsCategoryVO2.setChildren(new ArrayList<>());

                    // 构建三级分类VO
                    List<GoodsCategoryDO> level3List = level3CategorieMap.get(level2Categorie.getId());
                    if (level3List != null) {
                        for (GoodsCategoryDO level3Categorie : level3List) {
                            GoodsCategoryVO goodsCategoryVO3 = new GoodsCategoryVO();
                            BeanUtils.copyProperties(level3Categorie, goodsCategoryVO3);
                            goodsCategoryVO2.getChildren().add(goodsCategoryVO3);
                        }
                    }

                    goodsCategoryVO1.getChildren().add(goodsCategoryVO2);
                }
            }

            goodsCategoryVO1List.add(goodsCategoryVO1);
        }

        return goodsCategoryVO1List;
    }
}
