
package com.tmerclub.cloud.search.controller.admin;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.tmerclub.cloud.api.auth.constant.SysTypeEnum;
import com.tmerclub.cloud.api.dto.EsPageDTO;
import com.tmerclub.cloud.api.product.feign.CategoryShopFeignClient;
import com.tmerclub.cloud.api.product.feign.SkuFeignClient;
import com.tmerclub.cloud.api.product.feign.SkuStockFeignClient;
import com.tmerclub.cloud.api.product.feign.SpuFeignClient;
import com.tmerclub.cloud.api.supplier.feign.SupplierDetailFeignClient;
import com.tmerclub.cloud.api.supplier.vo.SupplierApiDetailVO;
import com.tmerclub.cloud.api.vo.EsPageVO;
import com.tmerclub.cloud.common.constant.Constant;
import com.tmerclub.cloud.common.constant.StatusEnum;
import com.tmerclub.cloud.common.exception.LuckException;
import com.tmerclub.cloud.common.i18n.I18nMessage;
import com.tmerclub.cloud.common.i18n.LanguageEnum;
import com.tmerclub.cloud.common.product.bo.EsBrandBO;
import com.tmerclub.cloud.common.product.constant.SpuMold;
import com.tmerclub.cloud.common.product.constant.SpuType;
import com.tmerclub.cloud.common.product.constant.SupplierSpuType;
import com.tmerclub.cloud.common.product.dto.ProductSearchDTO;
import com.tmerclub.cloud.common.product.vo.SkuVO;
import com.tmerclub.cloud.common.product.vo.SpuVO;
import com.tmerclub.cloud.common.product.vo.search.ProductSearchVO;
import com.tmerclub.cloud.common.product.vo.search.SpuAdminVO;
import com.tmerclub.cloud.common.response.ServerResponseEntity;
import com.tmerclub.cloud.common.security.AuthUserContext;
import com.tmerclub.cloud.search.constant.DataType;
import com.tmerclub.cloud.search.constant.EsConstant;
import com.tmerclub.cloud.search.manager.ProductSearchManager;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import org.apache.commons.compress.utils.Lists;
import org.apache.dubbo.config.annotation.DubboReference;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
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.*;
import java.util.stream.Collectors;

/**
 * 商品搜索
 *
 * @author tmerclub
 * @date 2020/11/16
 */
@RestController("adminSearchSpuController")
@RequestMapping("/mp/search")
@Tag(name = "admin-spu管理列表接口")
public class ProductSearchController {
    private static final Logger LOGGER = LoggerFactory.getLogger(ProductSearchController.class);

    @Autowired
    private ProductSearchManager productSearchManager;
    @DubboReference
    private SkuFeignClient skuFeignClient;
    @DubboReference
    private SkuStockFeignClient skuStockFeignClient;
    @DubboReference
    private CategoryShopFeignClient categoryShopFeignClient;
    @DubboReference
    private SpuFeignClient spuFeignClient;
    @DubboReference
    private SupplierDetailFeignClient supplierDetailFeignClient;

    @GetMapping("/page")
    @Operation(summary = "商品信息列表", description = "商品信息列表")
    public ServerResponseEntity<EsPageVO<SpuAdminVO>> page(@Valid EsPageDTO pageDTO, ProductSearchDTO productSearchDTO) {
        Long shopId = AuthUserContext.get().getTenantId();
        productSearchDTO.setFetchSource(EsConstant.SHOP_FETCH_SOURCE);
        productSearchDTO.setShopId(shopId);
        if (Objects.nonNull(productSearchDTO.getSupplierDeliveryType())) {
            productSearchDTO.setSpuMold(0);
        }
        EsPageVO<SpuAdminVO> searchPage = productSearchManager.adminPage(pageDTO, productSearchDTO);
        List<SpuAdminVO> spuList = searchPage.getList();
        List<Long> spuIds = spuList.stream().map(SpuAdminVO::getSpuId).collect(Collectors.toList());
        List<Long> supplierSpuIds = Lists.newArrayList();
        List<Long> comboSpuList = Lists.newArrayList();
        if (CollUtil.isNotEmpty(spuList)) {
            for (SpuAdminVO item : spuList) {
                long spuId = item.getSpuId();
                if (Objects.equals(item.getSupplierSpuType(), SupplierSpuType.CONSIGNMENT.value()) && Objects.nonNull(item.getSupplierSpuId())) {
                    supplierSpuIds.add(item.getSupplierSpuId());
                    spuId = item.getSupplierSpuId();
                }
                if(Objects.equals(item.getSpuMold(), SpuMold.COMBO.value())) {
                    comboSpuList.add(spuId);
                }
            }
        }
        if (CollUtil.isNotEmpty(supplierSpuIds)) {
            //代销商品 供应商商品更新后代销商品是否已同步更新 值
            List<SpuVO> spuIsSyn = spuFeignClient.getSpuIsSynUpdate(spuIds).getData();
            Map<Long, Integer> isSynUpdate = spuIsSyn.stream().collect(Collectors.toMap(SpuVO::getSpuId, SpuVO::getIsSynUpdate));
            for (SpuAdminVO spuAdminVO : searchPage.getList()) {
                spuAdminVO.setIsSynUpdate(isSynUpdate.get(spuAdminVO.getSpuId()));
            }
        }
        // 如果是组合商品,需要重新计算一下库存
        if (CollUtil.isNotEmpty(comboSpuList)) {
            ServerResponseEntity<Map<Long, Integer>> spuIdStockMapResp = skuStockFeignClient.calculateStockByComboSpuList(comboSpuList);
            Map<Long, Integer> spuStockMap = spuIdStockMapResp.getData();
            for (SpuAdminVO spuAdminVO : spuList) {
                if(!spuStockMap.containsKey(spuAdminVO.getSpuId()) && !spuStockMap.containsKey(spuAdminVO.getSupplierSpuId())){
                    continue;
                }
                spuAdminVO.setStock(spuStockMap.get(spuAdminVO.getSpuId()));
                if (Objects.equals(spuAdminVO.getSupplierSpuType(), SupplierSpuType.CONSIGNMENT.value()) && Objects.nonNull(spuAdminVO.getSupplierSpuId())) {
                    spuAdminVO.setStock(spuStockMap.get(spuAdminVO.getSupplierSpuId()));
                }
            }
        }
        //检查代销商品是否改变了原有得供应商图片和名字，如果改变了显示商家改变后的图片和名字，没有改变则显示供应商信息
        List<SpuAdminVO> data = spuFeignClient.checkCommissionSpuIsUpdateMsg(spuList).getData();
        searchPage.setList(data);
        // 加载商品总库存
        this.loadSpuStock(searchPage);
        return ServerResponseEntity.success(searchPage);
    }

    @GetMapping("/pageDistributionSpu")
    @Operation(summary = "分销商品列表", description = "分销商品列表")
    public ServerResponseEntity<EsPageVO<SpuAdminVO>> pageDistributionSpu(@Valid EsPageDTO pageDTO, ProductSearchDTO productSearchDTO) {
        Long shopId = AuthUserContext.get().getTenantId();
        if (!Objects.equals(shopId, Constant.PLATFORM_SHOP_ID)) {
            productSearchDTO.setShopId(shopId);
        }
        productSearchDTO.setFetchSource(EsConstant.DISTRIBUTION_SPU_SOURCE);
        EsPageVO<SpuAdminVO> searchPage = productSearchManager.adminPage(pageDTO, productSearchDTO);
        // 加载商品总库存
        this.loadSpuStock(searchPage);
        return ServerResponseEntity.success(searchPage);
    }

    @GetMapping("/pageSupplySup")
    @Operation(summary = "商家搜索供应商品列表", description = "商家搜索供应商品列表")
    public ServerResponseEntity<EsPageVO<SpuAdminVO>> pageSupplySup(@Valid EsPageDTO pageDTO, ProductSearchDTO productSearchDTO) {
        Long shopId = AuthUserContext.get().getTenantId();
        Integer sysType = AuthUserContext.get().getSysType();
        ServerResponseEntity<List<Long>> oneCategoryIdByShopId = categoryShopFeignClient.getOneCategoryIdByShopId(shopId, sysType, I18nMessage.getLang());
        if (Objects.isNull(oneCategoryIdByShopId.getData())) {
            return ServerResponseEntity.success();
        }
        // 查询出已经代销的商品
        ProductSearchDTO productSearchConsignment = new ProductSearchDTO();
        productSearchConsignment.setSupplierSpuType(SupplierSpuType.CONSIGNMENT.value());
        productSearchConsignment.setShopId(shopId);
        productSearchConsignment.setFetchSource(EsConstant.CONSIGNMENT_SPU);
        productSearchConsignment.setShopQuerySupplierSpu(true);
        EsPageDTO pageConsignment = new EsPageDTO();
        pageConsignment.setPageSize(EsPageDTO.MAX_PAGE_SIZE);
        pageConsignment.setPageNum(1);
        EsPageVO<SpuAdminVO> spuAdminEsPage = productSearchManager.adminPage(pageConsignment, productSearchConsignment);
        List<Long> spuIdList = spuAdminEsPage.getList().stream().map(SpuAdminVO::getSupplierSpuId).toList();
        //根据分类查找对应的供应商id
        ServerResponseEntity<List<Long>> supplyIdByCategoryIds = categoryShopFeignClient.getSupplyIdByCategoryIds(oneCategoryIdByShopId.getData());
        //es搜索商品
        productSearchDTO.setSupplierIds(supplyIdByCategoryIds.getData());
        productSearchDTO.setCategoryIds(oneCategoryIdByShopId.getData());
        productSearchDTO.setFetchSource(EsConstant.SUPPLIER_SPU);
        productSearchDTO.setSupplierSpuType(1);
        productSearchDTO.setShopQuerySupplierSpu(true);
        EsPageVO<SpuAdminVO> spuAdminPage = productSearchManager.adminPage(pageConsignment, productSearchDTO);
        List<SpuAdminVO> list = spuAdminPage.getList();
        // 代销商品过滤
        Set<Long> filterSpuIdList = new HashSet<>(Constant.INITIAL_CAPACITY);
        if (!Objects.isNull(productSearchDTO.getIsCommission())) {
            if (productSearchDTO.getIsCommission() == 0) {
                filterSpuIdList.addAll(spuIdList);
            } else {
                List<Long> includeSpuIds = new ArrayList<>(spuIdList);
                productSearchDTO.setSpuIds(includeSpuIds);
            }
        }
        // 需要过滤的商品id(过滤掉没有导入，且供应商商品状态不是上架状态的商品)
        for (SpuAdminVO spuAdminVO : list) {
            boolean contains = spuIdList.contains(spuAdminVO.getSpuId());
            if (!contains) {
                if (!Objects.equals(spuAdminVO.getSpuStatus(), StatusEnum.ENABLE.value())) {
                    filterSpuIdList.add(spuAdminVO.getSpuId());
                }
            }
        }
        productSearchDTO.setSpuIdsExclude(new ArrayList<>(filterSpuIdList));
        spuAdminPage = productSearchManager.adminPage(pageDTO, productSearchDTO);
        list = spuAdminPage.getList();
        ServerResponseEntity<List<SupplierApiDetailVO>> supplierResp = supplierDetailFeignClient.listSupplierDetailBySupplierIds(supplyIdByCategoryIds.getData());
        Map<Long, String> supplierNameMap = supplierResp.getData().stream().collect(Collectors.toMap(SupplierApiDetailVO::getSupplierId, SupplierApiDetailVO::getSupplierName));
        for (SpuAdminVO spuAdminVO : list) {
            spuAdminVO.setSupplyPriceFee(spuAdminVO.getPriceFee());
            //品牌名称
            setBrandName(spuAdminVO);
            spuAdminVO.setSupplierName(supplierNameMap.get(spuAdminVO.getSupplierId()) == null ? spuAdminVO.getShopName() : supplierNameMap.get(spuAdminVO.getSupplierId()));
        }
        //验证是否代销了供应商商品
        ServerResponseEntity<List<SpuAdminVO>> result = spuFeignClient.isCommissionByShopIdAndSpuId(list, shopId);
        spuAdminPage.setList(result.getData());
        // 加载商品总库存
        this.loadSpuStock(spuAdminEsPage);
        return ServerResponseEntity.success(spuAdminPage);
    }

    @GetMapping("/pageSupplierSpuList")
    @Operation(summary = "获取某个供应商或商家（不含代销商品）的商品列表", description = "获取某个供应商或商家（不含代销商品）的商品列表")
    public ServerResponseEntity<EsPageVO<SpuAdminVO>> pageSupplierSpuList(@Valid EsPageDTO pageDTO, ProductSearchDTO productSearchDTO) {
        Long shopId = AuthUserContext.get().getTenantId();
        if (Objects.isNull(productSearchDTO.getCategoryId()) || Objects.isNull(productSearchDTO.getSupplierIds())) {
            ServerResponseEntity<List<Long>> categoryIdsResp = categoryShopFeignClient.getCategoryIdsByShopId(shopId, AuthUserContext.get().getSysType());
            if (!categoryIdsResp.isSuccess()) {
                throw new LuckException(categoryIdsResp.getMsg());
            }
            // 根据分类过滤商品
            if (Objects.isNull(productSearchDTO.getCategoryId())) {
                productSearchDTO.setCategoryIds(categoryIdsResp.getData());
            }
            // 根据供应商过滤商品
            if (CollUtil.isEmpty(productSearchDTO.getSupplierIds())) {
                //根据分类查找对应的供应商id
                ServerResponseEntity<List<Long>> supplyIdByCategoryIds = categoryShopFeignClient.getSupplyIdByCategoryIds(categoryIdsResp.getData());
                //es搜索商品
                productSearchDTO.setSupplierIds(supplyIdByCategoryIds.getData());
            }
        }
        productSearchDTO.setFetchSource(EsConstant.CONSIGNMENT_SPU);
        productSearchDTO.setHasStock(1);
        productSearchDTO.setSpuStatus(StatusEnum.ENABLE.value());
        EsPageVO<SpuAdminVO> searchPage = productSearchManager.adminPage(pageDTO, productSearchDTO);
        // 加载商品总库存
        this.loadSpuStock(searchPage);
        if (CollUtil.isEmpty(searchPage.getList())) {
            return ServerResponseEntity.success(searchPage);
        }
        List<Long> spuIds = searchPage.getList().stream().map(SpuAdminVO::getSpuId).collect(Collectors.toList());
        // 商家端查询供应商库存时，stock为商家库存，supplierStock为供应商库存
        List<SkuVO> skuListResp;
        boolean isQueryShopStock = productSearchDTO.getShopQuerySupplierSpu() != null && productSearchDTO.getShopQuerySupplierSpu();
        if (isQueryShopStock) {
            skuListResp = skuFeignClient.listSupplierBySpuIds(spuIds, shopId).getData();
        } else {
            skuListResp = skuFeignClient.listBySpuIds(spuIds);
        }
        Map<Long, List<SkuVO>> skuMap = skuListResp.stream().collect(Collectors.groupingBy(SkuVO::getSpuId));
        // 去掉库存为0的sku
        for (SpuAdminVO spuAdminVO : searchPage.getList()) {
            List<SkuVO> skuList = skuMap.get(spuAdminVO.getSpuId());
            skuList.removeIf(skuVO -> (isQueryShopStock && skuVO.getSupplierStock() == 0)
                    || (!isQueryShopStock && Objects.equals(skuVO.getStock(), 0)));
            spuAdminVO.setSkuList(skuList);
        }
        return ServerResponseEntity.success(searchPage);
    }

    @GetMapping("/pageShopSpuList")
    @Operation(summary = "获取商家的商品列表", description = "获取商家的商品列表")
    public ServerResponseEntity<EsPageVO<SpuAdminVO>> pageShopSpuList(@Valid EsPageDTO pageDTO, ProductSearchDTO productSearchDTO) {
        Long shopId = AuthUserContext.get().getTenantId();
        ServerResponseEntity<List<Long>> categoryIdsResp = categoryShopFeignClient.getCategoryIdsByShopId(shopId, SysTypeEnum.MULTISHOP.value());
        if (!categoryIdsResp.isSuccess()) {
            throw new LuckException(categoryIdsResp.getMsg());
        }
        productSearchDTO.setCategoryIds(categoryIdsResp.getData());
        productSearchDTO.setShopId(shopId);
        productSearchDTO.setFetchSource(EsConstant.SHOP_FETCH_SOURCE);
        productSearchDTO.setDataType(DataType.ALL_PROD.value());
        productSearchDTO.setSupplierSpuType(null);
        EsPageVO<SpuAdminVO> searchPage = productSearchManager.adminPage(pageDTO, productSearchDTO);
        // 加载商品总库存
        this.loadSpuStock(searchPage);
        if (CollectionUtil.isEmpty(searchPage.getList())) {
            return ServerResponseEntity.success(searchPage);
        }
        List<Long> spuIds = searchPage.getList().stream().map(SpuAdminVO::getSpuId).collect(Collectors.toList());
        List<SkuVO> skuListResp = skuFeignClient.listBySpuIds(spuIds);
        Map<Long, List<SkuVO>> skuMap = skuListResp.stream().collect(Collectors.groupingBy(SkuVO::getSpuId));
        for (SpuAdminVO spuAdminVO : searchPage.getList()) {
            spuAdminVO.setSkuList(skuMap.get(spuAdminVO.getSpuId()));
        }
        return ServerResponseEntity.success(searchPage);
    }

    @GetMapping("/pageConsignmentSpu")
    @Operation(summary = "代销商品列表", description = "代销商品列表")
    public ServerResponseEntity<EsPageVO<SpuAdminVO>> pageConsignmentSpu(@Valid EsPageDTO pageDTO, ProductSearchDTO productSearchDTO) {
        Long shopId = AuthUserContext.get().getTenantId();
        productSearchDTO.setSupplierSpuType(SupplierSpuType.CONSIGNMENT.value());
        productSearchDTO.setShopId(shopId);
        productSearchDTO.setFetchSource(EsConstant.CONSIGNMENT_SPU);
        productSearchDTO.setShopQuerySupplierSpu(false);
        EsPageVO<SpuAdminVO> spuAdminEsPage = productSearchManager.adminPage(pageDTO, productSearchDTO);
        if (spuAdminEsPage.getList().size() == 0) {
            //查询为空直接返回
            LOGGER.info("代销商品列表查询列表为空，条件为{}", productSearchDTO);
        } else {
            List<SpuAdminVO> commissionSpu = spuAdminEsPage.getList();
            //供商品id
            List<Long> supplierSpuIds = commissionSpu.stream().map(SpuAdminVO::getSupplierSpuId).collect(Collectors.toList());
            //代销商品id
            List<Long> spuIds = commissionSpu.stream().map(SpuAdminVO::getSpuId).collect(Collectors.toList());
            //供应商品信息
            ServerResponseEntity<List<SpuVO>> supplierEntity = spuFeignClient.listSpuBySpuIds(supplierSpuIds);
            //代销商品 供应商商品更新后代销商品是否已同步更新 值
            List<SpuVO> supplierSpu = supplierEntity.getData();
            List<SpuVO> spuIsSyn = spuFeignClient.getSpuIsSynUpdate(spuIds).getData();
            Map<Long, Integer> isSynUpdate = spuIsSyn.stream().collect(Collectors.toMap(SpuVO::getSpuId, SpuVO::getIsSynUpdate));
            //供应商品供货价
            Map<Long, Long> supplierSpuFee = supplierSpu.stream().collect(Collectors.toMap(SpuVO::getSpuId, SpuVO::getPriceFee));
            for (SpuAdminVO spuAdminVO : commissionSpu) {
                spuAdminVO.setIsSynUpdate(isSynUpdate.get(spuAdminVO.getSpuId()));
                //品牌名称
                setBrandName(spuAdminVO);
                //设置供货价
                spuAdminVO.setSupplyPriceFee(supplierSpuFee.get(spuAdminVO.getSupplierSpuId()));
            }

            //检查代销商品是否改变了原有得供应商图片和名字，如果改变了显示商家改变后的图片和名字，没有改变则显示供应商信息
            List<SpuAdminVO> result = spuFeignClient.checkCommissionSpuIsUpdateMsg(commissionSpu).getData();
            spuAdminEsPage.setList(result);
        }
        // 加载商品总库存
        this.loadSpuStock(spuAdminEsPage);
        return ServerResponseEntity.success(spuAdminEsPage);
    }

    @GetMapping("/pageCanComboSpu")
    @Operation(summary = "获取可以参加组合商品的商品列表", description = "获取可以参加组合商品的商品列表,商家关联商品范围：商家商品+代销但商家发货商品；供应商关联商品范围：供应商商品")
    public ServerResponseEntity<EsPageVO<SpuAdminVO>> pageCanComboSpu(@Valid EsPageDTO pageDTO, ProductSearchDTO productSearchDTO) {
        Long shopId = AuthUserContext.get().getTenantId();
//        if (Objects.isNull(productSearchDTO.getCategoryId()) || Objects.isNull(productSearchDTO.getSupplierIds())) {
//            ServerResponseEntity<List<Long>> categoryIdsResp = categoryShopFeignClient.getCategoryIdsByShopId(shopId, SysTypeEnum.MULTISHOP.value());
//            if (!categoryIdsResp.isSuccess()) {
//                throw new LuckException(categoryIdsResp.getMsg());
//            }
//            // 根据分类过滤商品
//            if (Objects.isNull(productSearchDTO.getCategoryId())) {
//                productSearchDTO.setCategoryIds(categoryIdsResp.getData());
//            }
//        }
        productSearchDTO.setFetchSource(EsConstant.CONSIGNMENT_SPU);
        productSearchDTO.setIsGiveaway(1);
        productSearchDTO.setCanDeposit(1);
        productSearchDTO.setIsVirtual(null);
        Integer sysType = AuthUserContext.get().getSysType();
        if(Objects.equals(sysType,SysTypeEnum.MULTISHOP.value())) {
            productSearchDTO.setIsSupplierDelivery(1);
            productSearchDTO.setShopId(AuthUserContext.get().getTenantId());
        }
        if(Objects.equals(sysType,SysTypeEnum.SUPPLIER.value())){
            productSearchDTO.setSupplierSpuType(1);
            productSearchDTO.setSupplierId(AuthUserContext.get().getTenantId());
        }
        productSearchDTO.setSpuMold(SpuMold.REAL.value());
//        productSearchDTO.setHasStock(1);
        productSearchDTO.setSpuStatus(StatusEnum.ENABLE.value());
        EsPageVO<SpuAdminVO> searchPage = productSearchManager.adminPage(pageDTO, productSearchDTO);
        // 加载商品总库存
        this.loadSpuStock(searchPage);
        if (CollUtil.isEmpty(searchPage.getList())) {
            return ServerResponseEntity.success(searchPage);
        }
        List<Long> spuIds = searchPage.getList().stream().map(SpuAdminVO::getSpuId).collect(Collectors.toList());
        // 商家端查询供应商库存时，stock为商家库存，supplierStock为供应商库存
        List<SkuVO> skuListResp;
        boolean isQueryShopStock = productSearchDTO.getShopQuerySupplierSpu() != null && productSearchDTO.getShopQuerySupplierSpu();
        if (isQueryShopStock) {
            skuListResp = skuFeignClient.listSupplierBySpuIds(spuIds, shopId).getData();
        } else {
            skuListResp = skuFeignClient.listBySpuIds(spuIds);
        }
        Map<Long, List<SkuVO>> skuMap = skuListResp.stream().collect(Collectors.groupingBy(SkuVO::getSpuId));
        // 去掉库存为0的sku
        for (SpuAdminVO spuAdminVO : searchPage.getList()) {
            List<SkuVO> skuList = skuMap.get(spuAdminVO.getSpuId());
//            skuList.removeIf(skuVO -> (isQueryShopStock && skuVO.getSupplierStock() == 0)
//                    || (!isQueryShopStock && Objects.equals(skuVO.getStock(), 0)));
            spuAdminVO.setSkuList(skuList);
        }
        return ServerResponseEntity.success(searchPage);
    }

    public void setBrandName(SpuAdminVO spuAdminVO) {
        //品牌名称
        Integer lang = I18nMessage.getLang();
        EsBrandBO brand = spuAdminVO.getBrand();
        if (!Objects.isNull(brand)) {
            if (Objects.equals(lang, LanguageEnum.LANGUAGE_EN.getLang())) {
                spuAdminVO.setBrandName(brand.getBrandNameEn());
            }
            if (Objects.equals(lang, LanguageEnum.LANGUAGE_ZH_CN.getLang())) {
                spuAdminVO.setBrandName(brand.getBrandNameZh());
            }
        }
    }


    @GetMapping("/pageComboAddSpu")
    @Operation(summary = "套餐以及赠品选择商品列表", description = "套餐以及赠品选择商品列表")
    public ServerResponseEntity<EsPageVO<SpuAdminVO>> pageComboAddSpu(@Valid EsPageDTO pageDTO, ProductSearchDTO productSearchDTO) {
        Long shopId = AuthUserContext.get().getTenantId();
        productSearchDTO.setFetchSource(EsConstant.SHOP_FETCH_SOURCE);
        productSearchDTO.setShopId(shopId);
        productSearchDTO.setSpuType(SpuType.NORMAL.value());
        productSearchDTO.setNotSpuMold(SpuMold.COMBO.value());
        productSearchDTO.setKeyword(productSearchDTO.getSpuName());
        EsPageVO<SpuAdminVO> searchPage = productSearchManager.adminPage(pageDTO, productSearchDTO);
        List<SpuAdminVO> spuList = searchPage.getList();
        // 插入sku列表 - 套餐是可以选择供应商商品的，所以供应商发货的sku的库存要换成供应商sku库存
        List<SpuAdminVO> spuAdminList = skuFeignClient.setSpuSku(spuList).getData();
        searchPage.setList(spuAdminList);
        // 加载商品总库存
        this.loadSpuStock(searchPage);
        return ServerResponseEntity.success(searchPage);
    }

    @GetMapping("/renovationPage")
    @Operation(summary = "商品信息列表(商家装修商品列表)", description = "商品信息列表(商家装修商品列表)")
    public ServerResponseEntity<EsPageVO<ProductSearchVO>> renovationPage(@Valid EsPageDTO pageDTO, ProductSearchDTO productSearchDTO) {
        productSearchDTO.setShopId(AuthUserContext.get().getTenantId());
        EsPageVO<ProductSearchVO> searchPage = productSearchManager.showSpuTypeRenovationPage(pageDTO, productSearchDTO);
        return ServerResponseEntity.success(searchPage);
    }

    private void loadSpuStock(EsPageVO<SpuAdminVO> searchPage) {
        ServerResponseEntity<List<SpuAdminVO>> listServerResponseEntity = skuStockFeignClient.loadProdStock(searchPage.getList());
        if (!listServerResponseEntity.isSuccess()) {
            throw new LuckException(listServerResponseEntity.getMsg());
        }
        searchPage.setList(listServerResponseEntity.getData());
    }
}
