
package com.tmerclub.cloud.product.controller.app;

import com.tmerclub.cloud.api.feign.SearchSpuFeignClient;
import com.tmerclub.cloud.common.constant.StatusEnum;
import com.tmerclub.cloud.common.database.dto.PageDTO;
import com.tmerclub.cloud.common.database.vo.PageVO;
import com.tmerclub.cloud.common.exception.LuckException;
import com.tmerclub.cloud.common.i18n.I18nMessage;
import com.tmerclub.cloud.common.product.dto.ProductSearchLimitDTO;
import com.tmerclub.cloud.common.product.vo.app.BrandAppVO;
import com.tmerclub.cloud.common.product.vo.search.SpuSearchVO;
import com.tmerclub.cloud.common.response.ResponseEnum;
import com.tmerclub.cloud.common.response.ServerResponseEntity;
import com.tmerclub.cloud.product.dto.BrandDTO;
import com.tmerclub.cloud.product.service.BrandService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * 品牌信息
 *
 * @author tmerclub
 * @date 2020-10-28 15:27:24
 */
@RestController("appBrandController")
@RequestMapping("/ua/brand")
@Tag(name = "app-品牌信息")
public class BrandController {

    @Autowired
    private BrandService brandService;
    @DubboReference
    private SearchSpuFeignClient searchSpuFeignClient;

    @GetMapping("/page")
    @Operation(summary = "获取品牌信息列表", description = "分页获取品牌信息列表")
    public ServerResponseEntity<PageVO<BrandAppVO>> page(@Valid PageDTO pageDTO, BrandDTO brandDTO) {
        brandDTO.setStatus(StatusEnum.ENABLE.value());
        PageVO<BrandAppVO> brandPage = brandService.appPage(pageDTO, brandDTO);
        loadSpuList(brandPage.getList(), brandDTO.getSpuCount());
        return ServerResponseEntity.success(brandPage);
    }


    @GetMapping("/top_brand_list")
    @Operation(summary = "置顶品牌列表", description = "置顶品牌列表")
    public ServerResponseEntity<List<BrandAppVO>> topBrandList() {
        List<BrandAppVO> brandList = brandService.topBrandList(I18nMessage.getLang());
        return ServerResponseEntity.success(brandList);
    }

    @GetMapping("/list_by_category")
    @Operation(summary = "分类-推荐品牌信息列表", description = "分类-推荐品牌信息列表")
    public ServerResponseEntity<List<BrandAppVO>> getTopBrandList(Long categoryId) {
        List<BrandAppVO> brandPage = brandService.listByCategory(categoryId, I18nMessage.getLang());
        return ServerResponseEntity.success(brandPage);
    }

    /**
     * 添加品牌下的商品数据
     *
     * @param brandList
     * @param size
     */
    private void loadSpuList(List<BrandAppVO> brandList, Integer size) {
        if (Objects.isNull(size) || size == 0) {
            return;
        }
        List<Long> brandIds = brandList.stream().map(BrandAppVO::getBrandId).collect(Collectors.toList());
        ProductSearchLimitDTO productSearchLimitDTO = new ProductSearchLimitDTO(size);
        productSearchLimitDTO.setBrandIds(brandIds);
        ServerResponseEntity<List<SpuSearchVO>> response = searchSpuFeignClient.limitSpuList(productSearchLimitDTO);
        if (!Objects.equals(response.getCode(), ResponseEnum.OK.value())) {
            throw new LuckException(response.getMsg());
        }
        Map<Long, List<SpuSearchVO>> spuMap = response.getData().stream().collect(Collectors.groupingBy(SpuSearchVO::getBrandId));
        for (BrandAppVO brandVO : brandList) {
            brandVO.setSpuList(spuMap.get(brandVO.getBrandId()));
        }
    }
}
