package com.ruoyi.web.modules.category.facade.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.web.modules.category.constant.CategoryConstants;
import com.ruoyi.web.modules.category.domain.BizGoodsCategory;
import com.ruoyi.web.modules.category.facade.ICategoryFacade;
import com.ruoyi.web.modules.category.mapper.BizGoodsCategoryMapper;
import com.ruoyi.web.modules.category.service.IPlatformCategoryService;
import com.ruoyi.web.modules.category.vo.CategoryLevel2OptionVO;
import com.ruoyi.web.modules.category.vo.CategoryVisibleChildVO;
import com.ruoyi.web.modules.category.vo.CategoryVisibleVO;

/**
 * 分类 Facade 实现
 */
@Service
public class CategoryFacadeImpl implements ICategoryFacade
{
    @Autowired
    private BizGoodsCategoryMapper categoryMapper;

    @Autowired
    private IPlatformCategoryService platformCategoryService;

    @Override
    public boolean isCategoryVisible(Long categoryId)
    {
        if (categoryId == null)
        {
            return false;
        }
        BizGoodsCategory level2 = categoryMapper.selectByIdIgnoreShop(categoryId);
        if (level2 == null || !CategoryConstants.LEVEL_TWO.equals(level2.getCategoryLevel()))
        {
            return false;
        }
        if (!CategoryConstants.SHOW_YES.equals(level2.getShowFlag()))
        {
            return false;
        }
        BizGoodsCategory parent = categoryMapper.selectByIdIgnoreShop(level2.getParentId());
        if (parent == null || !CategoryConstants.SHOW_YES.equals(parent.getShowFlag()))
        {
            return false;
        }
        return true;
    }

    @Override
    public String getCategoryPath(Long categoryId)
    {
        BizGoodsCategory level2 = categoryMapper.selectByIdIgnoreShop(categoryId);
        if (level2 == null)
        {
            return StringUtils.EMPTY;
        }
        if (CategoryConstants.LEVEL_ONE.equals(level2.getCategoryLevel()))
        {
            return level2.getCategoryName();
        }
        BizGoodsCategory parent = categoryMapper.selectByIdIgnoreShop(level2.getParentId());
        if (parent == null)
        {
            return level2.getCategoryName();
        }
        return parent.getCategoryName() + " > " + level2.getCategoryName();
    }

    @Override
    public List<CategoryLevel2OptionVO> listLevel2ByShopId(Long shopId)
    {
        return buildLevel2Options(shopId);
    }

    @Override
    public List<CategoryLevel2OptionVO> listPlatformLevel2Options()
    {
        return platformCategoryService.selectPlatformLevel2Options();
    }

    private List<CategoryLevel2OptionVO> buildLevel2Options(Long shopId)
    {
        BizGoodsCategory query = new BizGoodsCategory();
        query.setShopId(shopId);
        List<BizGoodsCategory> all = categoryMapper.selectList(query);
        Map<Long, String> level1Names = all.stream()
                .filter(c -> CategoryConstants.LEVEL_ONE.equals(c.getCategoryLevel()))
                .collect(Collectors.toMap(BizGoodsCategory::getCategoryId, BizGoodsCategory::getCategoryName, (a, b) -> a));
        List<CategoryLevel2OptionVO> list = new ArrayList<>();
        for (BizGoodsCategory c : all)
        {
            if (CategoryConstants.LEVEL_TWO.equals(c.getCategoryLevel()))
            {
                String parentName = level1Names.getOrDefault(c.getParentId(), "");
                String label = StringUtils.isEmpty(parentName) ? c.getCategoryName()
                        : parentName + " > " + c.getCategoryName();
                list.add(new CategoryLevel2OptionVO(c.getCategoryId(), label, c.getCategoryName()));
            }
        }
        return list;
    }

    @Override
    public List<CategoryVisibleVO> listVisibleByShopId(Long shopId)
    {
        BizGoodsCategory query = new BizGoodsCategory();
        query.setShopId(shopId);
        List<BizGoodsCategory> all = categoryMapper.selectList(query);
        Map<Long, List<BizGoodsCategory>> childrenMap = all.stream()
                .filter(c -> CategoryConstants.LEVEL_TWO.equals(c.getCategoryLevel()))
                .collect(Collectors.groupingBy(BizGoodsCategory::getParentId));
        List<CategoryVisibleVO> result = new ArrayList<>();
        for (BizGoodsCategory l1 : all)
        {
            if (!CategoryConstants.LEVEL_ONE.equals(l1.getCategoryLevel()))
            {
                continue;
            }
            if (!CategoryConstants.SHOW_YES.equals(l1.getShowFlag()))
            {
                continue;
            }
            CategoryVisibleVO vo = new CategoryVisibleVO();
            vo.setCategoryId(l1.getCategoryId());
            vo.setCategoryName(l1.getCategoryName());
            vo.setCategoryPic(l1.getCategoryPic());
            vo.setHotFlag(l1.getHotFlag());
            vo.setSortNo(l1.getSortNo());
            List<BizGoodsCategory> children = childrenMap.getOrDefault(l1.getCategoryId(), new ArrayList<>());
            for (BizGoodsCategory l2 : children)
            {
                if (isCategoryVisible(l2.getCategoryId()))
                {
                    CategoryVisibleChildVO child = new CategoryVisibleChildVO();
                    child.setCategoryId(l2.getCategoryId());
                    child.setCategoryName(l2.getCategoryName());
                    child.setCategoryPic(l2.getCategoryPic());
                    child.setHotFlag(l2.getHotFlag());
                    child.setSortNo(l2.getSortNo());
                    vo.getChildren().add(child);
                }
            }
            if (!vo.getChildren().isEmpty())
            {
                result.add(vo);
            }
        }
        return result;
    }
}
