
package com.tmerclub.cloud.product.feign;

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.multishop.feign.ShopSubstituteSalesFeignClient;
import com.tmerclub.cloud.api.product.bo.SpuSimpleBO;
import com.tmerclub.cloud.api.product.feign.SpuFeignClient;
import com.tmerclub.cloud.api.product.util.ProductLangUtil;
import com.tmerclub.cloud.common.cache.constant.ProductCacheNames;
import com.tmerclub.cloud.common.cache.util.RedisUtil;
import com.tmerclub.cloud.common.constant.Constant;
import com.tmerclub.cloud.common.constant.StatusEnum;
import com.tmerclub.cloud.common.product.bo.EsProductBO;
import com.tmerclub.cloud.common.product.constant.*;
import com.tmerclub.cloud.common.product.dto.SpuDTO;
import com.tmerclub.cloud.common.product.vo.*;
import com.tmerclub.cloud.common.product.vo.search.ProductSearchVO;
import com.tmerclub.cloud.common.product.vo.search.SpuAdminVO;
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.common.util.BeanUtil;
import com.tmerclub.cloud.common.util.Json;
import com.tmerclub.cloud.product.bo.CategoryRateBO;
import com.tmerclub.cloud.product.model.SkuStock;
import com.tmerclub.cloud.product.model.Spu;
import com.tmerclub.cloud.product.model.SpuConsignmentChange;
import com.tmerclub.cloud.product.service.*;
import com.tmerclub.cloud.product.service.*;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.dubbo.config.annotation.DubboService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @author tmerclub
 * @date 2020/12/8
 */
@DubboService
public class SpuFeignController implements SpuFeignClient {
    private static final Logger logger = LoggerFactory.getLogger(SpuFeignController.class);

    @Autowired
    private SpuService spuService;
    @Autowired
    private SkuService skuService;
    @Autowired
    private CategoryShopService categoryShopService;

    @Autowired
    private SpuExtensionService spuExtensionService;
    @DubboReference
    private ShopSubstituteSalesFeignClient shopSubstituteSalesFeignClient;
    @Autowired
    private SupplierSpuUpdateLaterOperateService supplierSpuUpdateLaterOperateService;
    @Autowired
    private SpuConsignmentChangeService spuConsignmentChangeService;
    @Autowired
    private SkuStockService skuStockService;


    @Override
    public ServerResponseEntity<SpuVO> getById(Long spuId) {
        SpuVO spuVO = spuService.getBySpuId(spuId);
        ProductLangUtil.spu(spuVO);
        SpuVO spu = new SpuVO();
        spu.setSpuId(spuVO.getSpuId());
        spu.setName(spuVO.getName());
        spu.setSellingPoint(spuVO.getSellingPoint());
        spu.setMainImgUrl(spuVO.getMainImgUrl());
        spu.setStatus(spuVO.getStatus());
        spu.setSupplierDeliveryType(spuVO.getSupplierDeliveryType());
        spu.setSupplierSpuType(spuVO.getSupplierSpuType());
        spu.setSupplierSpuId(spuVO.getSupplierSpuId());
        spu.setSpuType(spuVO.getSpuType());
        spu.setSpuMold(spuVO.getSpuMold());
        spu.setIsRefund(spuVO.getIsRefund());
        spu.setWriteOffNum(spuVO.getWriteOffNum());
        spu.setWriteOffMultipleCount(spuVO.getWriteOffMultipleCount());
        spu.setWriteOffTime(spuVO.getWriteOffTime());
        spu.setWriteOffStart(spuVO.getWriteOffStart());
        spu.setWriteOffEnd(spuVO.getWriteOffEnd());
        return ServerResponseEntity.success(spu);
    }

    @Override
    public ServerResponseEntity<SpuVO> getDetailById(Long spuId) {
        SpuVO spuVO = spuService.getBySpuId(spuId);
        ProductLangUtil.spu(spuVO);
        return ServerResponseEntity.success(spuVO);
    }

    @Override
    public ServerResponseEntity<SpuAndSkuVO> getSpuAndSkuAndRateById(Long spuId, Long skuId, Boolean supplierSpuType) {
        SpuVO spu = spuService.getBySpuId(spuId);
        SkuVO sku = skuService.getSkuBySkuId(skuId);
        // 当商品状态不正常时，不能添加到购物车
        boolean spuIsNotExist = isSpuIsNotExist(spu, sku);
        if (spuIsNotExist) {
            // 当返回商品不存在时，前端应该将商品从购物车界面移除
            return ServerResponseEntity.fail(ResponseEnum.SPU_NOT_EXIST);
        }
        List<CategoryRateBO> shopCategoryRates = categoryShopService.listRateByShopId(spu.getShopId(), SysTypeEnum.MULTISHOP.value());
        spu.setRate(0.0);
        for (CategoryRateBO shopCategoryRate : shopCategoryRates) {
            if (Objects.equals(shopCategoryRate.getCategoryId(), spu.getCategoryId())) {
                spu.setRate(shopCategoryRate.getRate());
            }
        }
        //获取代销商品基本信息是否改变
        SpuConsignmentChange spuConsignmentChange = spuConsignmentChangeService.getBySpuId(spuId);
        // 判断下是否为供应商商品,如果是商家代销供应商商品且为供应商发货设置供应商信息
        if (!Objects.equals(spu.getSupplierId(), 0L) && Objects.equals(spu.getSupplierDeliveryType(), 1)) {
            SpuVO supplierSpu = spuService.getBySpuId(spu.getSupplierSpuId());
            SkuVO supplierSku = skuService.getSkuBySkuId(sku.getSupplierSkuId());
            //名称
            if (Objects.nonNull(spuConsignmentChange) && Objects.equals(spuConsignmentChange.getNameChange(), 0)) {
                if (Objects.equals(spu.getSupplierDeliveryType(), SupplierDeliveryType.SUPPLIER_DELIVERY.value())) {
                    spu.setName(supplierSpu.getName());
                }
            }
            spu.setDeliveryTemplateId(supplierSpu.getDeliveryTemplateId());
            spu.setDeliveryAmount(supplierSpu.getDeliveryAmount());
            sku.setSupplyPriceFee(supplierSku.getPriceFee());
            List<CategoryRateBO> supplierCategoryRates = categoryShopService.listRateByShopId(spu.getSupplierId(), SysTypeEnum.SUPPLIER.value());
            spu.setSupplierRate(0.0);
            for (CategoryRateBO supplierCategoryRate : supplierCategoryRates) {
                if (Objects.equals(supplierCategoryRate.getCategoryId(), spu.getCategoryId())) {
                    spu.setSupplierRate(supplierCategoryRate.getRate());
                }
            }
        }
        SpuAndSkuVO spuAndSku = new SpuAndSkuVO();
        spuAndSku.setSku(sku);
        spuAndSku.setSpu(spu);
        return ServerResponseEntity.success(spuAndSku);
    }

    private static boolean isSpuIsNotExist(SpuVO spu, SkuVO sku) {
        return Objects.isNull(spu) || Objects.isNull(sku) || !Objects.equals(spu.getStatus(), StatusEnum.ENABLE.value()) || !Objects.equals(sku.getStatus(), StatusEnum.ENABLE.value()) || !Objects.equals(sku.getSpuId(), spu.getSpuId());
    }

    @Override
    public ServerResponseEntity<List<SpuVO>> listCanSeckillProd(Long spuId, Long shopId) {
        return ServerResponseEntity.success(spuService.listCanSeckillProd(spuId, shopId));
    }

    @Override
    public ServerResponseEntity<List<PurchaseSpuVO>> listPurchaseSpuByPartyCodeList(List<String> partyCodeList, Long shopId) {
        List<SkuVO> skuList = skuService.listByPartyCodeList(partyCodeList);
        // 查询商家对应供应商商品的库存信息
        List<SkuVO> shopSkuStockVO = skuStockService.listShopStockByPartyCodesAndShopId(partyCodeList, shopId);
        Map<String, Integer> shopSkuStockMap = shopSkuStockVO.stream().collect(Collectors.toMap(SkuVO::getPartyCode, SkuVO::getStock));
        List<PurchaseSpuVO> purchaseSpuList = new ArrayList<>(Constant.INITIAL_CAPACITY);
        for (SkuVO skuVO : skuList) {
            PurchaseSpuVO purchaseSpuVO = new PurchaseSpuVO();
            SpuVO spuVO = spuService.getBySpuId(skuVO.getSpuId());
            ProductLangUtil.spu(spuVO);
            ProductLangUtil.sku(skuVO);
            SkuStock skuStock = skuStockService.getBySkuId(skuVO.getSkuId(), spuVO.getSpuMold());
            purchaseSpuVO.setSkuId(skuVO.getSkuId());
            purchaseSpuVO.setPartyCode(skuVO.getPartyCode());
            purchaseSpuVO.setPriceFee(skuVO.getPriceFee());
            purchaseSpuVO.setImgUrl(skuVO.getImgUrl());
            purchaseSpuVO.setSpuId(skuVO.getSpuId());
            purchaseSpuVO.setName(spuVO.getName());
            purchaseSpuVO.setSkuName(skuVO.getSkuName());
            purchaseSpuVO.setMainImgUrl(spuVO.getMainImgUrl());
            purchaseSpuVO.setStock(shopSkuStockMap.getOrDefault(skuVO.getPartyCode(), 0));
            purchaseSpuVO.setSupplierStock(skuStock.getStock());
            purchaseSpuVO.setSupplierId(spuVO.getSupplierId());
            purchaseSpuList.add(purchaseSpuVO);
        }
        return ServerResponseEntity.success(purchaseSpuList);
    }

    @Override
    public ServerResponseEntity<SpuActivityAppVO> spuActivityBySpuId(Long shopId, Long spuId) {
        return ServerResponseEntity.success(spuService.spuActivityBySpuId(shopId, spuId));
    }

    @Override
    public ServerResponseEntity<Void> offlineSpuBySupplierId(Long supplierId) {
        spuService.offlineSpuBySupplierId(supplierId);
        return ServerResponseEntity.success();
    }

    @Override
    public ServerResponseEntity<Void> offlineSpuByShopId(Long shopId) {
        spuService.offlineSpuByShopId(shopId);
        return ServerResponseEntity.success();
    }

    @Override
    public ServerResponseEntity<Void> offlineSpuByShopIds(List<Long> shopIds) {
        spuService.offlineSpuByShopIds(3, shopIds);
        return ServerResponseEntity.success();
    }

    @Override
    public ServerResponseEntity<SpuVO> getSpuAndSkuBySpuId(Long spuId) {
        SpuVO spuVO = spuService.getBySpuId(spuId);
        ProductLangUtil.spu(spuVO);
        // sku信息
        if (Objects.equals(spuVO.getSupplierSpuType(), SupplierSpuType.CONSIGNMENT.value())) {
            //代销商品
            spuVO.setSkus(skuService.listConsignmentSku(spuVO, false));
        } else {
            //其他商品（商家商品，供应商商品）
            spuVO.setSkus(skuService.listSkuAllInfoBySpuId(spuId, spuVO, false));
        }
        // 计算商品的库存和销量
        int totalStock = 0;
        int saleNum = 0;
        for (SkuVO skuVO : spuVO.getSkus()) {
            skuVO.setStock(Objects.isNull(skuVO.getStock()) ? 0 : skuVO.getStock());
            totalStock += skuVO.getStock();
            saleNum += Objects.isNull(skuVO.getSaleNum()) ? 0 : skuVO.getSaleNum();
        }
        spuVO.setTotalStock(totalStock);
        spuVO.setSaleNum(saleNum);
        spuVO.setDeliveryModeVO(Json.parseObject(spuVO.getDeliveryMode(), SpuVO.DeliveryModeVO.class));
        return ServerResponseEntity.success(spuVO);
    }

    @Override
    public ServerResponseEntity<Void> changeSpuType(SpuDTO spuDTO) {
        Spu spu = BeanUtil.map(spuDTO, Spu.class);
        spuService.updateSpu(spu);
        return ServerResponseEntity.success();
    }

    @Override
    public ServerResponseEntity<Integer> countByTransportId(Long transportId) {

        return ServerResponseEntity.success(spuService.countByTransportId(transportId));
    }

    @Override
    public void removeSpuActivityCache(Long shopId, List<Long> spuIds) {
        if (Objects.equals(shopId, Constant.PLATFORM_SHOP_ID)) {
            shopId = null;
        }
        if (CollUtil.isEmpty(spuIds) && !Objects.equals(shopId, Constant.PLATFORM_SHOP_ID)) {
            spuIds = spuService.listSpuIdsByShopId(shopId);
        }
        spuService.batchRemoveSpuActivityCache(spuIds);
    }

    @Override
    public void changeToNormalSpu(List<Long> spuIds) {
        spuService.changeToNormalSpu(spuIds);
    }

    @Override
    public ServerResponseEntity<Integer> countByUserId(Long userId) {
        return ServerResponseEntity.success(spuService.countByUserId(userId));
    }

    @Override
    public ServerResponseEntity<List<SpuVO>> listSpuNameBySpuIds(List<Long> spuIds) {
        return ServerResponseEntity.success(spuService.listSpuNameBySpuIds(spuIds));
    }

    @Override
    public ServerResponseEntity<List<SpuVO>> listSpuBySpuIds(List<Long> spuIds) {
        return ServerResponseEntity.success(spuService.listSpuBySpuIds(spuIds));
    }

    @Override
    public ServerResponseEntity<Void> updateSpuUpdateTimeByShopIds(List<Long> shopIds, Integer sysType) {
        spuService.updateSpuUpdateTime(null, null, shopIds, sysType);
        return ServerResponseEntity.success();
    }

    @Override
    public void updateSpuUpdateTimeBySpuIds(List<Long> spuIds) {
        spuService.updateSpuUpdateTime(spuIds, null, null, null);
    }

    @Override
    public ServerResponseEntity<Void> offlineSpuActivityByShopIds(List<Long> shopIds) {
        spuService.offlineSpuByShopIds(1, shopIds);
        return ServerResponseEntity.success();
    }

    @Override
    public ServerResponseEntity<List<SpuSimpleBO>> listSimple(SpuSimpleBO spuSimpleBO) {
        List<SpuSimpleBO> spus = spuService.listSimple(spuSimpleBO);
        return ServerResponseEntity.success(spus);
    }

    @Override
    public ServerResponseEntity<List<SpuSimpleBO>> listSimples(SpuSimpleBO spuSimpleBO) {
        List<SpuSimpleBO> spus = spuService.listSimples(spuSimpleBO);
        return ServerResponseEntity.success(spus);
    }

    @Override
    public ServerResponseEntity<List<SkuVO>> listSkuPriceByIds(List<Long> skuIds) {
        return ServerResponseEntity.success(skuService.listSkuPriceByIds(skuIds));
    }

    @Override
    public ServerResponseEntity<List<Long>> listSpuIdsByShopId(Long shopId) {
        return ServerResponseEntity.success(spuService.listSpuIdsByShopIds(Collections.singletonList(shopId)));
    }


    @Override
    public ServerResponseEntity<?> checkSpuStatus(List<Long> spuIds) {
        return spuService.checkSpuStatus(spuIds);
    }

    @Override
    public ServerResponseEntity<List<SpuAndSkuVO>> listSpuAndSkuById(List<SkuVO> skuList, Long supplierId) {
        List<SpuAndSkuVO> spuAndSkuList = new ArrayList<>(Constant.INITIAL_CAPACITY);
        for (SkuVO skuVO : skuList) {
            SpuVO spu = spuService.getBySpuId(skuVO.getSpuId());
            SkuVO sku = skuService.getSkuBySkuId(skuVO.getSkuId());
            // 当商品状态不正常时，不能添加到购物车
            boolean spuIsNotExist = isSpuIsNotExist(spu, sku);
            if (spuIsNotExist) {
                // 当返回商品不存在时，前端应该将商品从购物车界面移除
                return ServerResponseEntity.fail(ResponseEnum.SPU_NOT_EXIST);
            }
            SpuAndSkuVO spuAndSku = new SpuAndSkuVO();
            spuAndSku.setSkuId(sku.getSkuId());
            spuAndSku.setSpuId(sku.getSpuId());
            spuAndSku.setSku(sku);
            spuAndSku.setSpu(spu);
            spuAndSkuList.add(spuAndSku);
        }
        return ServerResponseEntity.success(spuAndSkuList);
    }

    @Override
    public ServerResponseEntity<List<SpuAdminVO>> isCommissionByShopIdAndSpuId(List<SpuAdminVO> spuAdminVOList, Long shopId) {
        //获取代销商品状态
        List<Long> supplierIds = spuAdminVOList.stream().map(SpuAdminVO::getSpuId).collect(Collectors.toList());
        //获取代销商品信息
        if (CollUtil.isEmpty(supplierIds) || supplierIds.size() == 0) {
            return ServerResponseEntity.success(spuAdminVOList);
        }
        List<SpuVO> spuVOList = spuService.listSpuByShopIdAndSupplierSpuId(shopId, supplierIds);
        Map<Long, List<SpuVO>> listMap = spuVOList.stream().collect(Collectors.groupingBy(SpuVO::getSupplierSpuId));
        Map<Long, Long> commissionSpuIds = spuVOList.stream().collect(Collectors.toMap(SpuVO::getSupplierSpuId, SpuVO::getSpuId));
        for (SpuAdminVO spuAdminVO : spuAdminVOList) {
            //根据供应商品id和商家id获取代销商品信息
            SpuVO commissionSpu = spuService.getSpuByShopIdAndSupplierSpuId(shopId, spuAdminVO.getSpuId());
            if (Objects.nonNull(commissionSpu)) {
                //判断代销商品是否选择了店铺分类
                if (Objects.nonNull(commissionSpu.getShopCategoryId()) && commissionSpu.getShopCategoryId() == -1) {
                    spuAdminVO.setIsSynUpdate(IsSynUpdate.NO_CHANGE.value());
                }
            }
            List<SpuVO> spuList = listMap.get(spuAdminVO.getSpuId());
            Long commissionSpuId = commissionSpuIds.get(spuAdminVO.getSpuId());
            if (!Objects.isNull(spuList)) {
                if (spuList.get(0).getStatus().equals(SpuStatus.PUT_SHELF.value())) {
                    spuAdminVO.setIsPut(SpuStatus.PUT_SHELF.value());
                }
                if (spuList.get(0).getStatus().equals(SpuStatus.OFF_SHELF.value())) {
                    spuAdminVO.setIsPut(SpuStatus.OFF_SHELF.value());
                }
            }
            if (!Objects.isNull(commissionSpuId)) {
                spuAdminVO.setCommissionSpuId(commissionSpuId);
            }
            spuAdminVO.setIsCommission(IsCommission.NO.value());
            Integer result = spuService.isCommissionByShopIdAndSpuId(shopId, spuAdminVO.getSpuId());
            if (!result.equals(0)) {
                //代销了
                spuAdminVO.setIsCommission(IsCommission.YES.value());
            }
        }
        return ServerResponseEntity.success(spuAdminVOList);
    }

    @Override
    public void soldOutSpuBySpu(Long spuId, Long price, Long marketPriceFee) {
        spuService.soldOutSpuBySpu(spuId, price, marketPriceFee);
    }


    @Override
    public void supplierUpdateSpuStatus(Long supplierSpuId, Integer status) {
        //根据供应商品id获取代销商品
        List<SpuVO> commissionSpuList = spuService.listSpuBySupplierSpuId(supplierSpuId);
        List<Long> commissionSpuIdList = commissionSpuList.stream().map(SpuVO::getSpuId).collect(Collectors.toList());
        // 只有非组合的商品需要改变发货方式为商家发货
        List<Long> noComboSpuIds = commissionSpuList.stream().filter(spuVO -> !Objects.equals(spuVO.getSpuMold(), SpuMold.COMBO.value())).map(SpuVO::getSpuId).toList();
        logger.info("筛选出非组合的商家供应商发货的代销商品:{}", noComboSpuIds);
        //供应商下架
        if (Objects.equals(status, SpuStatus.OFF_SHELF.value()) || Objects.equals(status, SpuStatus.PUBLISH_WAIT_AUDIT.value())) {
            //下架 商家供应商发货的代销商品
            logger.info("下架 商家供应商发货的代销商品:{}", commissionSpuIdList);
            supplierSpuUpdateLaterOperateService.batchChangeSpuStatus(commissionSpuIdList, SpuStatus.OFF_SHELF.value(), SysTypeEnum.MULTISHOP.value());
            boolean checkSpu = Objects.equals(status, SpuStatus.OFF_SHELF.value()) || Objects.equals(status, SpuStatus.PUBLISH_WAIT_AUDIT.value());
            //供应商品为下架状态修改代销商品发货方式
            if (CollUtil.isNotEmpty(noComboSpuIds) && checkSpu) {
                //修改发货方式
                spuService.batchUpdateCommissionSpuDeliverType(noComboSpuIds, SupplierDeliveryType.SHOP_DELIVERY.value());
            }
        }
        //供应商品平台下架
        if (Objects.equals(status, SpuStatus.PLATFORM_OFF_SHELF.value())) {
            logger.info("供应商品平台下架:{}", commissionSpuIdList);
            //下架
            spuService.batchChangeCommissionSpuStatus(commissionSpuIdList, SpuStatus.OFF_SHELF.value(), SysTypeEnum.MULTISHOP.value());
            if(CollectionUtil.isNotEmpty(noComboSpuIds)) {
                //修改发货方式
                spuService.batchUpdateCommissionSpuDeliverType(noComboSpuIds, SupplierDeliveryType.SHOP_DELIVERY.value());
            }
        }

        //供应商品为删除状态
        if (Objects.equals(status, SpuStatus.DELETE.value())) {
            logger.info("供应商品为删除状态:{}", commissionSpuIdList);
            //下架
            spuService.batchChangeCommissionSpuStatus(commissionSpuIdList, SpuStatus.OFF_SHELF.value(), SysTypeEnum.MULTISHOP.value());
            if(CollectionUtil.isNotEmpty(noComboSpuIds)) {
                //修改发货方式
                spuService.batchUpdateCommissionSpuDeliverType(noComboSpuIds, SupplierDeliveryType.SHOP_DELIVERY.value());
            }
        }
    }

    @Override
    public ServerResponseEntity<List<SpuVO>> getSpuIsSynUpdate(List<Long> spuIds) {
        return ServerResponseEntity.success(spuService.getSpuIsSynUpdate(spuIds));
    }

    @Override
    public ServerResponseEntity<Void> supplierSpuUpdateLaterOffSpu(Long supplierSpuId, Integer commissionSpuOffType) {

        spuService.supplierSpuUpdateLaterOffSpu(supplierSpuId, commissionSpuOffType);
        return ServerResponseEntity.success();
    }

    @Override
    public void skuUpdateLaterOperate(Long supplierSpuId) {
        supplierSpuUpdateLaterOperateService.skuUpdateLaterOperate(supplierSpuId);
    }

    @Override
    public ServerResponseEntity<Void> feePriceUnderSupplyPrice(EsProductBO esProductBO) {
        supplierSpuUpdateLaterOperateService.feePriceUnderSupplyPrice(esProductBO);
        return ServerResponseEntity.success();
    }

    @Override
    public void soldOutManualPriceCommissionSpu(Long supplierSpuId, Integer type, Integer isFiltration) {
        supplierSpuUpdateLaterOperateService.soldOutManualPriceCommissionSpu(supplierSpuId, type, isFiltration);
    }

    @Override
    public void supplierSkuAddOrDeleteLaterOperate(Long supplierSpuId, Long skuId) {
        supplierSpuUpdateLaterOperateService.supplierSkuAddOrDeleteLaterOperate(supplierSpuId, skuId);
    }

    @Override
    public void changeSynUpdate(Long supplierSpuId) {
        supplierSpuUpdateLaterOperateService.changeSynUpdate(supplierSpuId);
    }

    @Override
    public void updateManualPriceCommissionSpuPrice(Long shopId) {
        spuService.updateManualPriceCommissionSpuPrice(shopId);
    }

    @Override
    public ServerResponseEntity<List<SpuAdminVO>> checkCommissionSpuIsUpdateMsg(List<SpuAdminVO> spuAdminVOList) {
        List<Long> supplierSpuIdList = spuAdminVOList.stream().map(SpuAdminVO::getSupplierSpuId).collect(Collectors.toList());
        if (CollUtil.isEmpty(supplierSpuIdList)) {
            return ServerResponseEntity.success(spuAdminVOList);
        }
        List<SpuVO> supplierSpuVOList = spuService.listSpuBySpuIds(supplierSpuIdList);
        if (Objects.isNull(supplierSpuVOList)) {
            return ServerResponseEntity.success(spuAdminVOList);
        }
        Map<Long, List<SpuVO>> supplierSpuMap = supplierSpuVOList.stream().collect(Collectors.groupingBy(SpuVO::getSpuId));
        List<SpuAdminVO> spuAdminList = supplierSpuUpdateLaterOperateService.disposeSpuMsg(spuAdminVOList, supplierSpuMap);
        return ServerResponseEntity.success(spuAdminList);
    }

    @Override
    public ServerResponseEntity<List<ProductSearchVO>> usCheckCommissionSpuIsUpdateMsg(List<ProductSearchVO> productSearchVOList) {
        List<Long> spuIdList = new ArrayList<>(Constant.INITIAL_CAPACITY);
        List<Long> supplierSpuIdList = new ArrayList<>(Constant.INITIAL_CAPACITY);
        for (ProductSearchVO productSearchVO : productSearchVOList) {
            List<Long> spuIds = productSearchVO.getSpus().stream().map(SpuSearchVO::getSpuId).collect(Collectors.toList());
            List<Long> supplierSpuIds = productSearchVO.getSpus().stream().map(SpuSearchVO::getSupplierSpuId).collect(Collectors.toList());
            if (CollUtil.isNotEmpty(spuIds)) {
                spuIdList.addAll(spuIds);
            }
            if (CollUtil.isNotEmpty(supplierSpuIds)) {
                supplierSpuIdList.addAll(supplierSpuIds);
            }
        }

        if (CollUtil.isEmpty(spuIdList) && CollUtil.isEmpty(supplierSpuIdList)) {
            return ServerResponseEntity.success(productSearchVOList);
        }

        //批量获取代销商品基本信息是否改变
        List<SpuConsignmentChange> spuConsignmentChanges = spuConsignmentChangeService.batchGetBySpuIdList(spuIdList);
        Map<Long, List<SpuConsignmentChange>> spuConsignmentChangeMap = spuConsignmentChanges.stream().collect(Collectors.groupingBy(SpuConsignmentChange::getSpuId));
        //批量获取供应商商品信息
        List<SpuVO> supplierSpuVOList = spuService.listSpuBySpuIds(supplierSpuIdList);
        Map<Long, List<SpuVO>> supplierSpuMap = supplierSpuVOList.stream().collect(Collectors.groupingBy(SpuVO::getSpuId));
        for (ProductSearchVO productSearchVO : productSearchVOList) {
            List<SpuSearchVO> spuSearchVOList = supplierSpuUpdateLaterOperateService.usDisposeSpuMsg(productSearchVO.getSpus(), spuConsignmentChangeMap, supplierSpuMap);
            productSearchVO.setSpus(spuSearchVOList);
        }
        return ServerResponseEntity.success(productSearchVOList);
    }

    @Override
    public void updateSaleNumBySeckillOrder(Long spuId, Integer prodCount) {
        spuExtensionService.updateSaleNumBySeckillOrder(spuId, prodCount);
    }

    @Override
    public ServerResponseEntity<List<SpuVO>> listSpuBySpuIdsNo(List<Long> spuIds) {
        return ServerResponseEntity.success(spuService.listSpuBySpuIds(spuIds));
    }

    @Override
    public ServerResponseEntity<List<SpuVO>> getDetailBySpuIds(List<Long> spuIdS) {
        List<SpuVO> spuVOList = spuService.getBySpuIds(spuIdS);
        ProductLangUtil.spuList(spuVOList);
        return ServerResponseEntity.success(spuVOList);
    }

    @Override
    public ServerResponseEntity<Void> batchChangeSpuStatusToDisableBySupplierIds(List<Long> supplierIdsToStop) {
        spuService.batchChangeSpuStatusToDisableBySupplierIds(supplierIdsToStop);
        return ServerResponseEntity.success();
    }

    @Override
    public void syncUpdateCommissionSpuBrandOrSeq(Long brandId, Integer seq, Long supplierSpuId) {
        spuService.syncUpdateCommissionSpuBrandOrSeq(brandId, seq, supplierSpuId);
    }

    @Override
    public ServerResponseEntity<Void> voluntarilyImportSupplierSpu(List<SpuVO> spuVOList, Integer sysType, Long userId) {
        spuService.voluntarilyImportSupplierSpu(spuVOList, sysType, userId);
        return ServerResponseEntity.success();
    }

    @Override
    public ServerResponseEntity<Void> updateSpuByShopId(Long shopId, Integer sysType) {
        spuService.updateSpuByShopId(shopId, sysType);
        return ServerResponseEntity.success();
    }

    @Override
    public void processSupplierSkuInsert(Long spuId, Long skuId) {
        Object o = RedisUtil.get(ProductCacheNames.SKU_STATUS_DELETE + spuId);
        if (Objects.isNull(o)) {
            //sku种类增加，商家是手动设价，下架商家的商品
            this.skuUpdateLaterOperate(spuId);
        }
        //供应商品sku增加或者删除处理代销商品sku
        this.supplierSkuAddOrDeleteLaterOperate(spuId, skuId);
        this.changeSynUpdate(spuId);
    }

    @Override
    public List<SpuVO> listShopSpuBySupplierSpuIds(Long shopId, List<Long> supplierSpuIds) {
        return spuService.listShopSpuBySupplierSpuIds(shopId, supplierSpuIds);
    }
}
