
package com.tmerclub.cloud.product.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.text.CharSequenceUtil;
import cn.hutool.core.util.BooleanUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson2.JSON;
import com.tmerclub.cloud.api.auth.constant.SysTypeEnum;
import com.tmerclub.cloud.api.product.manager.ShopCartItemAdapter;
import com.tmerclub.cloud.common.cache.constant.CacheNames;
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.exception.LuckException;
import com.tmerclub.cloud.common.order.bo.DeliveryModeBO;
import com.tmerclub.cloud.common.order.util.OrderLangUtil;
import com.tmerclub.cloud.common.order.vo.OrderSkuLangVO;
import com.tmerclub.cloud.common.order.vo.OrderSpuLangVO;
import com.tmerclub.cloud.common.order.vo.ShopCartItemVO;
import com.tmerclub.cloud.common.order.vo.VirtualRemarkVO;
import com.tmerclub.cloud.common.product.constant.PreSaleType;
import com.tmerclub.cloud.common.product.constant.SpuMold;
import com.tmerclub.cloud.common.product.constant.SpuType;
import com.tmerclub.cloud.common.product.vo.GiveawaySpuVO;
import com.tmerclub.cloud.common.product.vo.GiveawayVO;
import com.tmerclub.cloud.common.product.vo.SkuVO;
import com.tmerclub.cloud.common.product.vo.SpuVO;
import com.tmerclub.cloud.common.util.BeanUtil;
import com.tmerclub.cloud.common.util.Json;
import com.tmerclub.cloud.common.util.PriceUtil;
import com.tmerclub.cloud.product.bo.CategoryRateBO;
import com.tmerclub.cloud.product.service.CategoryShopService;
import com.tmerclub.cloud.product.service.ShopCartItemService;
import com.tmerclub.cloud.product.service.SkuService;
import com.tmerclub.cloud.product.service.SpuService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

/**
 * 购物车项数据组装
 *
 * @author tmerclub
 * @date 2020-11-20 15:47:32
 */
@Service
public class ShopCartItemServiceImpl implements ShopCartItemService {
    private final Logger LOGGER = LoggerFactory.getLogger(ShopCartItemAdapter.class);

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

    @Override
    public void assembleShopCartItem(List<ShopCartItemVO> shopCartItems, ShopCartItemVO mainShopCartItem, Boolean isShopCartOrder) {
        List<Long> spuIds = new ArrayList<>(shopCartItems.size());
        List<Long> skuIds = new ArrayList<>(shopCartItems.size());
        for (ShopCartItemVO shopCartItem : shopCartItems) {
            spuIds.add(shopCartItem.getSpuId());
            skuIds.add(shopCartItem.getSkuId());
        }
        Map<Long, SpuVO> spuMap = this.mapSpuByCache(spuIds);
        Map<Long, SkuVO> skuMap = this.mapSkuByCache(skuIds);
        spuIds.clear();
        skuIds.clear();
        Long totalAmount = Objects.nonNull(mainShopCartItem) ? mainShopCartItem.getTotalAmount() : 0L;
        Iterator<ShopCartItemVO> iterator = shopCartItems.iterator();
        while (iterator.hasNext()) {
            ShopCartItemVO shopCartItem = iterator.next();
            boolean isLastItem = !iterator.hasNext();
            SpuVO spu = spuMap.get(shopCartItem.getSpuId());
            SkuVO sku = skuMap.get(shopCartItem.getSkuId());

            // 从购物车提交的订单，需要去除失效商品
            if (BooleanUtil.isTrue(isShopCartOrder)) {
                boolean invalidProduct = !Objects.equals(spu.getStatus(), StatusEnum.ENABLE.value()) ||
                        !Objects.equals(sku.getStatus(), StatusEnum.ENABLE.value()) ||
                        Objects.equals(spu.getPreSaleType(), StatusEnum.ENABLE.value());
                if (invalidProduct) {
                    iterator.remove();
                    continue;
                }
            }

            // 填充购物车项基本数据
            handShopCartItemBaseInfo(shopCartItem, spu, sku,mainShopCartItem);

            // 区域库存列表
            shopCartItem.setStockPointList(sku.getStockPointList());

            // 赠品信息
            shopCartItem.setGiveaway(spu.getGiveawayVO());

            // 预售信息 -- 处理下发货时间，定金相关优惠金额和实付金额
            handlePreSaleInfo(shopCartItem, spu);

            // 虚拟商品信息
            handleVirtual(spu, shopCartItem);

            // 供应商代销商品数据
            totalAmount = handleSupplier(spuIds, skuIds, shopCartItem, spu, sku,mainShopCartItem,totalAmount,isLastItem);

        }
        // 订单项金额从小到大排序
        shopCartItems = shopCartItems.stream().sorted(Comparator.comparingDouble(ShopCartItemVO::getActualTotal)).collect(Collectors.toList());
        // 没有供应商发货的代销商品数据需要查询
        if (CollUtil.isEmpty(spuIds)) {
            return;
        }
        LOGGER.info("查询代销商品对应的供应商商品数据");
        // 获取供应商商品的数据
        spuMap.putAll(this.mapSpuByCache(spuIds));
        skuMap.putAll(this.mapSkuByCache(skuIds));

        Iterator<ShopCartItemVO> supplilerIterator = shopCartItems.iterator();
        while (supplilerIterator.hasNext()) {
            ShopCartItemVO shopCartItem = supplilerIterator.next();
            // 非供应商代销商品， 或者不是供应商发货、或者是秒杀商品（独立库存，不使用供应商库存数据）
            if (!Objects.equals(shopCartItem.getSupplierDeliveryType(), 1) || shopCartItem.getSpuType().equals(SpuType.SECKILL.value())) {
                continue;
            }
            SpuVO supplierSpu = spuMap.get(shopCartItem.getSupplierSpuId());
            SkuVO supplierSku = skuMap.get(shopCartItem.getSupplierSkuId());

            // 从购物车提交的订单，需要去除失效商品
            if (BooleanUtil.isTrue(isShopCartOrder)) {
                if (!Objects.equals(supplierSku.getStatus(), StatusEnum.ENABLE.value())) {
                    supplilerIterator.remove();
                    continue;
                }
            }
            // 处理下供应商发货的代销商品信息
            shopCartItem.setStockPointList(supplierSku.getStockPointList());
            shopCartItem.setSupplierSpuLangList(BeanUtil.mapAsList(supplierSpu.getSpuLangList(), OrderSpuLangVO.class));
            shopCartItem.setSupplierSkuLangList(BeanUtil.mapAsList(supplierSku.getSkuLangList(), OrderSkuLangVO.class));
            shopCartItem.setSupplierUrl((StrUtil.isNotBlank(supplierSku.getImgUrl())) ? supplierSku.getImgUrl() : supplierSpu.getMainImgUrl());
            shopCartItem.setDeliveryTemplateId(supplierSpu.getDeliveryTemplateId());
            shopCartItem.setDeliveryAmount(supplierSpu.getDeliveryAmount());
            shopCartItem.setSupplierPriceFee(supplierSpu.getPriceFee());
            shopCartItem.setTotalPurchaseAmount(shopCartItem.getCount() * supplierSku.getPriceFee());
            shopCartItem.setSupplierRate(supplierSpu.getRate());
        }
    }

    @Override
    public void buildItemGiveawayInfo(List<ShopCartItemVO> shopCartItems) {
        if (CollUtil.isEmpty(shopCartItems)) {
            return;
        }
        Long userId = shopCartItems.get(0).getUserId();
        List<Long> skuIds = new ArrayList<>(Constant.INITIAL_CAPACITY);
        List<ShopCartItemVO> shopCartItemList = new ArrayList<>(shopCartItems.size());
        for (ShopCartItemVO shopCartItem : shopCartItems) {
            // 没有赠品要处理
            if (Objects.isNull(shopCartItem.getGiveaway())) {
                continue;
            }
            GiveawayVO giveaway = shopCartItem.getGiveaway();
            // 主商品购买数量是否满足赠送的最低要求
            if (shopCartItem.getCount() < giveaway.getBuyNum()) {
                continue;
            }
            for (GiveawaySpuVO giveawaySpu : giveaway.getGiveawaySpus()) {
                if (skuIds.contains(giveawaySpu.getSkuId())) {
                    continue;
                }
                Long skuId = giveawaySpu.getSkuId();
                skuIds.add(skuId);
                shopCartItemList.add(new ShopCartItemVO(userId, giveawaySpu.getSpuId(), giveawaySpu.getSkuId(), giveawaySpu.getGiveawayNum()));
            }
        }

        // 没有赠品，直接返回
        if (CollUtil.isEmpty(shopCartItemList)) {
            return;
        }
        // 组装赠品的spu和sku信息
        this.assembleShopCartItem(shopCartItemList, null, null);

        Map<Long, ShopCartItemVO> shopCartItemMap = shopCartItemList.stream().collect(Collectors.toMap(ShopCartItemVO::getSkuId, Function.identity()));

        // 将组装好的赠品数据，放入购物车项
        for (ShopCartItemVO shopCartItem : shopCartItems) {
            // 没有赠品要处理,或者主商品购买数量不满足赠送的最低要求
            if (Objects.isNull(shopCartItem.getGiveaway()) || shopCartItem.getCount() < shopCartItem.getGiveaway().getBuyNum()) {
                continue;
            }

            GiveawayVO giveaway = shopCartItem.getGiveaway();
            // 组装赠品订单项的赠品活动数据
            List<ShopCartItemVO> giveawayShopCartItemList = new ArrayList<>(giveaway.getGiveawaySpus().size());
            // 赠品数量 = 主商品数 / 最少购买赠送数量
            int giveawayNum = shopCartItem.getCount() / giveaway.getBuyNum();
            for (GiveawaySpuVO giveawaySpu : giveaway.getGiveawaySpus()) {
                ShopCartItemVO giveawayShopCartItemVO = shopCartItemMap.get(giveawaySpu.getSkuId());
                giveawayShopCartItemVO = BeanUtil.map(giveawayShopCartItemVO, ShopCartItemVO.class);
                giveawayShopCartItemVO.setCount(giveawaySpu.getGiveawayNum() * giveawayNum);
                giveawaySpu.setGiveawayNum(giveawayShopCartItemVO.getCount());
                giveawayShopCartItemVO.setGiveawayRefundPrice(giveawaySpu.getRefundPrice());
                // 根据数量，重新计算赠品金额
                giveawayShopCartItemVO.setTotalAmount(giveawayShopCartItemVO.getCount() * giveawayShopCartItemVO.getSkuPriceFee());
                giveawayShopCartItemVO.setActualTotal(giveawayShopCartItemVO.getTotalAmount());
                giveawayShopCartItemList.add(giveawayShopCartItemVO);
            }
            shopCartItem.setGiveawayShopCartItemList(giveawayShopCartItemList);
        }
    }

    private static void handShopCartItemBaseInfo(ShopCartItemVO shopCartItem, SpuVO spu, SkuVO sku, ShopCartItemVO mainShopCartItem) {
        shopCartItem.setShopId(Objects.isNull(shopCartItem.getShopId()) ? spu.getShopId() : shopCartItem.getShopId());
        if (Objects.isNull(shopCartItem.getCartItemId())) {
            shopCartItem.setCartItemId(0L);
        }
        shopCartItem.setSpuCode(spu.getSpuCode());
        shopCartItem.setCategoryId(spu.getCategoryId());
        shopCartItem.setSpuType(spu.getSpuType());
        shopCartItem.setTotalPurchaseAmount(0L);
        // 没有指定满减活动时，初始化满减id
        if (Objects.isNull(shopCartItem.getDiscountId())) {
            shopCartItem.setDiscountId(0L);
        }
        // 初始化商品实际金额
        shopCartItem.setImgUrl((Objects.nonNull(sku.getImgUrl()) && StrUtil.isNotBlank(sku.getImgUrl())) ? sku.getImgUrl() : spu.getMainImgUrl());
        if(Objects.nonNull(shopCartItem.getMainComboSpuId())) {
//            // 组合商品信息 -- 设置一下组合主商品信息
//            shopCartItem.setComboAmount(shopCartItem.getSkuPriceFee() * shopCartItem.getCount() * shopCartItem.getComboCount());
//            shopCartItem.setTotalAmount(shopCartItem.getCount() * shopCartItem.getSkuPriceFee() * shopCartItem.getComboCount());
            shopCartItem.setComboCount(shopCartItem.getCount() * mainShopCartItem.getCount());
            shopCartItem.setCount(shopCartItem.getComboCount());
        }
        shopCartItem.setSkuPriceFee(sku.getPriceFee());
        shopCartItem.setTotalAmount(shopCartItem.getCount() * shopCartItem.getSkuPriceFee());
        // sku数据
        shopCartItem.setActualTotal(shopCartItem.getTotalAmount());
        shopCartItem.setScoreFee(sku.getScoreFee());
        shopCartItem.setPartyCode(sku.getPartyCode());
        shopCartItem.setWeight(sku.getWeight());
        shopCartItem.setVolume(sku.getVolume());
        shopCartItem.setShareReduce(0L);
        shopCartItem.setPlatformShareReduce(0L);
        // 商品分类扣率
        shopCartItem.setRate(spu.getRate());
        shopCartItem.setSupplierRate(0.0);
        // 如果主组合商品不为空使用组合商品的扣率
        if(Objects.nonNull(mainShopCartItem)){
            shopCartItem.setRate(mainShopCartItem.getRate());
        }
        // 商品物流配送信息
        shopCartItem.setDeliveryMode(spu.getDeliveryMode());
        shopCartItem.setDeliveryTemplateId(spu.getDeliveryTemplateId());
        shopCartItem.setDeliveryAmount(spu.getDeliveryAmount());
        shopCartItem.setDeliveryModeBO(Json.parseObject(shopCartItem.getDeliveryMode(), DeliveryModeBO.class));
        // 国际化
        shopCartItem.setSpuLangList(BeanUtil.mapAsList(spu.getSpuLangList(), OrderSpuLangVO.class));
        shopCartItem.setSkuLangList(BeanUtil.mapAsList(sku.getSkuLangList(), OrderSkuLangVO.class));
        OrderLangUtil.shopCartItemLang(shopCartItem);
        // 积分商品加入积分价格
        if (Objects.equals(spu.getSpuType(), SpuType.SCORE.value())) {
            shopCartItem.setScorePrice(sku.getScoreFee() * shopCartItem.getCount());
        } else {
            shopCartItem.setScorePrice(Constant.ZERO_LONG);
        }
    }

    private static void handleVirtual(SpuVO spu, ShopCartItemVO shopCartItem) {
        shopCartItem.setMold(spu.getSpuMold());
        if (!Objects.equals(shopCartItem.getMold(), SpuMold.VIRTUAL.value())) {
            return;
        }
        shopCartItem.setMold(spu.getSpuMold());
        shopCartItem.setIsRefund(spu.getIsRefund());
        shopCartItem.setVirtualRemark(spu.getVirtualRemark());
        shopCartItem.setWriteOffNum(spu.getWriteOffNum());
        shopCartItem.setWriteOffMultipleCount(spu.getWriteOffMultipleCount());
        shopCartItem.setWriteOffStart(spu.getWriteOffStart());
        shopCartItem.setWriteOffEnd(spu.getWriteOffEnd());
        shopCartItem.setWriteOffTime(spu.getWriteOffTime());
        // 确保字符串为数组格式类型后，再进行JSON转换，否则会报错
        String virtualRemark = spu.getVirtualRemark();
        boolean isHandleVirtualRemark = StrUtil.isNotBlank(virtualRemark)
                && virtualRemark.startsWith("[")
                && virtualRemark.endsWith("]");
        if (isHandleVirtualRemark) {
            List<VirtualRemarkVO> virtualRemarkList = JSON.parseArray(spu.getVirtualRemark()).toJavaList(VirtualRemarkVO.class);
            for (VirtualRemarkVO virtualRemarkVO : virtualRemarkList) {
                if (virtualRemarkVO.getIsRequired() == null) {
                    virtualRemarkVO.setIsRequired(false);
                }
                virtualRemarkVO.setSpuId(shopCartItem.getSpuId());
            }
            shopCartItem.setVirtualRemarkList(virtualRemarkList);
        }
    }

    private void handlePreSaleInfo(ShopCartItemVO shopCartItem, SpuVO spu) {
        LOGGER.info("预售商品，处理下发货时间，定金相关优惠金额和实付金额");
        shopCartItem.setPreSaleType(spu.getPreSaleType());
        shopCartItem.setPreSaleDeliveryType(spu.getPreSaleDeliveryType());
        shopCartItem.setPreSaleDeliveryTime(spu.getPreSaleDeliveryTime());
        if (!Objects.equals(shopCartItem.getPreSaleType(), PreSaleType.DEPOSIT.value())) {
            return;
        }
        long depositAmount = spu.getDepositAmount() * shopCartItem.getCount();
        long depositExpansionAmount = spu.getDepositExpansionAmount() * shopCartItem.getCount();
        LOGGER.info("预售定金金额：{}，预售尾款金额：{}", depositAmount, depositExpansionAmount);
        if (Objects.equals(spu.getDepositType(), 0)) {
            depositAmount = PriceUtil.divideByBankerRounding(shopCartItem.getTotalAmount() * spu.getDepositAmount(), 10000);
            depositExpansionAmount = PriceUtil.divideByBankerRounding(shopCartItem.getTotalAmount() * spu.getDepositExpansionAmount(), 10000);
            LOGGER.info("定金收取类型为按比例收取，预售定金金额：{}，预售尾款金额：{}", depositAmount, depositExpansionAmount);
        }
        // 定金最小是1分钱
        depositAmount = Math.max(depositAmount, 1);
        shopCartItem.setDepositAmount(depositAmount);
        shopCartItem.setActualDepositAmount(depositAmount);
        shopCartItem.setDepositExpansionAmount(depositExpansionAmount);
        shopCartItem.setDepositReduceAmount(depositExpansionAmount - depositAmount);
        shopCartItem.setBalanceAmount(shopCartItem.getTotalAmount() - depositAmount);
        shopCartItem.setActualBalanceAmount(shopCartItem.getTotalAmount() - depositExpansionAmount);
        shopCartItem.setBalanceStartTime(spu.getBalanceStartTime());
        shopCartItem.setBalanceEndTime(spu.getBalanceEndTime());
        shopCartItem.setShareReduce(shopCartItem.getDepositReduceAmount());
        shopCartItem.setActualTotal(shopCartItem.getActualTotal() - shopCartItem.getDepositReduceAmount());
    }

    private Long handleSupplier(List<Long> spuIds, List<Long> skuIds, ShopCartItemVO shopCartItem, SpuVO spu, SkuVO sku, ShopCartItemVO mainShopCartItem, Long totalAmount, boolean isLastItem) {
        shopCartItem.setSupplierDeliveryType(spu.getSupplierDeliveryType());
        // 供应商发货的代销商品
        if (Objects.equals(shopCartItem.getSupplierDeliveryType(), 1)) {
            LOGGER.info("商家代销供应商商品且为供应商发货,设置供应商信息");
            // 处理下供应商信息， 这里只要加商品中有的供应商信息即可，如果是供应商发货的代销商品，
            shopCartItem.setSupplierId(spu.getSupplierId());
            shopCartItem.setSupplierSpuId(spu.getSupplierSpuId());
            shopCartItem.setSupplierSkuId(sku.getSupplierSkuId());
            spuIds.add(spu.getSupplierSpuId());
            skuIds.add(sku.getSupplierSkuId());

        } else if(Objects.nonNull(mainShopCartItem) && !Objects.equals(mainShopCartItem.getSupplierId(), 0L) && Objects.equals(mainShopCartItem.getSupplierDeliveryType(), 1)){
            LOGGER.info("商家代销的供应商发布的组合商品");
            // 如果主商品不为空且主商品为供应商商品,表示为代销供应商的组合商品
            // 处理下供应商信息
            shopCartItem.setSupplierId(mainShopCartItem.getSupplierId());
            shopCartItem.setSupplierDeliveryType(mainShopCartItem.getSupplierDeliveryType());
            // 因为这里是商家代销组合商品,单个没有代销的情况下还是自身的相关信息
            shopCartItem.setSupplierSpuId(shopCartItem.getSpuId());
            shopCartItem.setSupplierSkuId(shopCartItem.getSkuId());
            shopCartItem.setDeliveryAmount(spu.getDeliveryAmount());
            // 组合单价就是采购价,总采购价就是组合总价
            shopCartItem.setSupplierPriceFee(shopCartItem.getSkuPriceFee());
            shopCartItem.setTotalPurchaseAmount(shopCartItem.getTotalAmount());
//            // 计算一下,当前供应商的组合商品关联子商品金额 = 当前子商品总组合金额 * 商家总代销金额 / 供应商总采购价
//            long itemTotalAmount = PriceUtil.divideByBankerRounding(shopCartItem.getTotalPurchaseAmount() *  mainShopCartItem.getTotalAmount(),mainShopCartItem.getTotalPurchaseAmount());
//            long itemComboAmount = PriceUtil.divideByBankerRounding(shopCartItem.getComboAmount() *  mainShopCartItem.getTotalAmount(),mainShopCartItem.getTotalPurchaseAmount());
//            long itemSkuPriceFee = PriceUtil.divideByBankerRounding(shopCartItem.getSkuPriceFee() *  mainShopCartItem.getTotalAmount(),mainShopCartItem.getTotalPurchaseAmount());
//            if(isLastItem){
//                itemTotalAmount = totalAmount;
//            }
//            shopCartItem.setTotalAmount(itemTotalAmount);
//            shopCartItem.setComboAmount(itemComboAmount);
//            shopCartItem.setSkuPriceFee(itemSkuPriceFee);
            shopCartItem.setActualTotal(shopCartItem.getTotalAmount());
            shopCartItem.setSupplierRate(mainShopCartItem.getSupplierRate());
            shopCartItem.setRate(mainShopCartItem.getRate());
            spuIds.add(shopCartItem.getSupplierSpuId());
            skuIds.add(shopCartItem.getSupplierSkuId());
//            totalAmount -= itemTotalAmount;
        }else{
            LOGGER.info("非商家代销供应商商品或者不为供应商发货,设置供应商信息为空");
            shopCartItem.setSupplierId(0L);
            shopCartItem.setTotalPurchaseAmount(0L);
            shopCartItem.setSupplierRate(0.0);
            shopCartItem.setSupplierPriceFee(0L);
        }
        return totalAmount;
    }

    private Map<Long, SpuVO> mapSpuByCache(List<Long> spuIds) {
        if (CollUtil.isEmpty(spuIds)) {
            return Collections.emptyMap();
        }
        Map<Long, SpuVO> spuMap = new HashMap<>(Constant.INITIAL_CAPACITY);
        if (spuIds.size() == 1) {
            SpuVO spuVO = spuService.getBySpuId(spuIds.get(0));
            spuMap.put(spuIds.get(0),spuVO);
        }else {
            spuMap.putAll(RedisUtil.mapByIds(CacheNames.SPU_KEY, spuIds, SpuVO.class));
            spuIds.removeAll(spuMap.keySet());
            if (CollUtil.isNotEmpty(spuIds)) {
                for (Long spuId : spuIds) {
                    SpuVO spuVO = spuService.getBySpuId(spuId);
                    if (Objects.isNull(spuVO)) {
                        throw new LuckException("商品不存在或者已删除");
                    }
                    spuMap.put(spuId, spuVO);
                }
            }
        }
        // 添加分类扣率
        this.loadSpuCategory(spuMap);
        return spuMap;
    }

    private Map<Long, SkuVO> mapSkuByCache(List<Long> skuIds) {
        if (CollUtil.isEmpty(skuIds)) {
            return Collections.emptyMap();
        }
        if (skuIds.size() == 1) {
            SkuVO skuVO = skuService.getSkuBySkuId(skuIds.get(0));
            Map<Long, SkuVO> skuMap = new HashMap<>(Constant.INITIAL_CAPACITY);
            skuMap.put(skuIds.get(0), skuVO);
            return skuMap;
        }

        Map<Long, SkuVO> skuMap = RedisUtil.mapByIds(CacheNames.SKU_KEY, skuIds , SkuVO.class);
        skuIds.removeAll(skuMap.keySet());
        if (CollUtil.isEmpty(skuIds)) {
            return skuMap;
        }
        for (Long skuId : skuIds) {
            SkuVO skuVO = skuService.getSkuBySkuId(skuIds.get(0));
            skuMap.put(skuId, skuVO);
        }
        return skuMap;
    }

    private void loadSpuCategory(Map<Long, SpuVO> spuMap) {
        if (spuMap.isEmpty()) {
            return;
        }
        // 处理一下,获取平台佣金比例
        for (Long spuId : spuMap.keySet()) {
            SpuVO spuVO = spuMap.get(spuId);
            boolean isSupplierSpu = !Objects.equals(spuVO.getSupplierId(), 0L) && Objects.equals(spuVO.getSupplierDeliveryType(), 1);
            List<CategoryRateBO> shopCategoryRates = categoryShopService.listRateByShopId(spuVO.getShopId(), SysTypeEnum.MULTISHOP.value());
            spuVO.setRate(0.0);
            for (CategoryRateBO shopCategoryRate : shopCategoryRates) {
                if (Objects.equals(shopCategoryRate.getCategoryId(), spuVO.getCategoryId())) {
                    spuVO.setRate(shopCategoryRate.getRate());
                }
            }
            // 判断下是否为供应商商品,如果是商家代销供应商商品且为供应商发货设置供应商信息
            if (isSupplierSpu) {
                List<CategoryRateBO> supplierCategoryRates = categoryShopService.listRateByShopId(spuVO.getSupplierId(), SysTypeEnum.SUPPLIER.value());
                spuVO.setSupplierRate(0.0);
                for (CategoryRateBO supplierCategoryRate : supplierCategoryRates) {
                    if (Objects.equals(supplierCategoryRate.getCategoryId(), spuVO.getCategoryId())) {
                        spuVO.setSupplierRate(supplierCategoryRate.getRate());
                    }
                }
            }
        }
    }
}
