package com.ruoyi.web.modules.home.service.impl;

import java.util.Collections;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.common.exception.ServiceException;
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.vo.CategoryVisibleChildVO;
import com.ruoyi.web.modules.category.vo.CategoryVisibleVO;
import com.ruoyi.web.modules.home.constant.CategoryAppConstants;
import com.ruoyi.web.modules.home.dto.CategoryGoodsQuery;
import com.ruoyi.web.modules.home.mapper.CategoryAppMapper;
import com.ruoyi.web.modules.home.service.ICategoryAppService;
import com.ruoyi.web.modules.home.vo.HomeHotGoodsVO;

/**
 * C 端商品分类 Service 实现
 */
@Service
public class CategoryAppServiceImpl implements ICategoryAppService
{
    @Autowired
    private ICategoryFacade categoryFacade;

    @Autowired
    private BizGoodsCategoryMapper categoryMapper;

    @Autowired
    private CategoryAppMapper categoryAppMapper;

    @Override
    public List<CategoryVisibleVO> listCategoryTree()
    {
        List<CategoryVisibleVO> list = categoryFacade.listVisibleByShopId(null);
        return list != null ? list : Collections.emptyList();
    }

    @Override
    public List<CategoryVisibleChildVO> listLevel2Tabs(Long level1Id)
    {
        assertPlatformLevel1Visible(level1Id);
        List<CategoryVisibleChildVO> list = categoryAppMapper.selectPlatformLevel2Tabs(level1Id);
        return list != null ? list : Collections.emptyList();
    }

    @Override
    public List<HomeHotGoodsVO> listCategoryGoods(CategoryGoodsQuery query)
    {
        if (query == null || query.getCategoryId() == null)
        {
            throw new ServiceException(CategoryAppConstants.MSG_CATEGORY_REQUIRED);
        }
        if (!categoryFacade.isCategoryVisible(query.getCategoryId()))
        {
            throw new ServiceException(CategoryAppConstants.MSG_CATEGORY_INVISIBLE);
        }
        String sortBy = StringUtils.isEmpty(query.getSortBy())
                ? CategoryAppConstants.DEFAULT_SORT
                : query.getSortBy();
        if (!CategoryAppConstants.isValidSortBy(sortBy))
        {
            throw new ServiceException(CategoryAppConstants.MSG_SORT_INVALID);
        }
        List<HomeHotGoodsVO> list = categoryAppMapper.selectGoodsByCategory(query.getCategoryId(), sortBy);
        return list != null ? list : Collections.emptyList();
    }

    private void assertPlatformLevel1Visible(Long level1Id)
    {
        if (level1Id == null)
        {
            throw new ServiceException(CategoryAppConstants.MSG_CATEGORY_INVISIBLE);
        }
        BizGoodsCategory level1 = categoryMapper.selectByIdIgnoreShop(level1Id);
        if (level1 == null
                || level1.getShopId() != null
                || !CategoryConstants.LEVEL_ONE.equals(level1.getCategoryLevel())
                || !CategoryConstants.SHOW_YES.equals(level1.getShowFlag())
                || !CategoryConstants.DEL_FLAG_NORMAL.equals(level1.getDelFlag()))
        {
            throw new ServiceException(CategoryAppConstants.MSG_CATEGORY_INVISIBLE);
        }
    }
}
