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

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.StrUtil;
import com.tmerclub.cloud.api.auth.constant.SysTypeEnum;
import com.tmerclub.cloud.api.coupon.feign.CouponFeignClient;
import com.tmerclub.cloud.api.discount.feign.DiscountFeignClient;
import com.tmerclub.cloud.api.distribution.feign.DistributionFeignClient;
import com.tmerclub.cloud.api.feign.SearchSpuFeignClient;
import com.tmerclub.cloud.api.multishop.feign.ShopSubstituteSalesFeignClient;
import com.tmerclub.cloud.api.product.vo.SkuStockVO;
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.exception.LuckException;
import com.tmerclub.cloud.common.i18n.I18nMessage;
import com.tmerclub.cloud.common.product.bo.EsProductBO;
import com.tmerclub.cloud.common.product.constant.*;
import com.tmerclub.cloud.common.product.dto.SkuDTO;
import com.tmerclub.cloud.common.product.vo.SkuLangVO;
import com.tmerclub.cloud.common.product.vo.SkuVO;
import com.tmerclub.cloud.common.product.vo.SpuSkuAttrValueVO;
import com.tmerclub.cloud.common.product.vo.SpuVO;
import com.tmerclub.cloud.common.product.vo.search.SpuAdminVO;
import com.tmerclub.cloud.common.product.vo.search.SpuSearchVO;
import com.tmerclub.cloud.common.response.ServerResponseEntity;
import com.tmerclub.cloud.common.security.AuthUserContext;
import com.tmerclub.cloud.common.util.BeanUtil;
import com.tmerclub.cloud.common.util.PriceUtil;
import com.tmerclub.cloud.common.vo.ShopSubstituteSalesVO;
import com.tmerclub.cloud.product.manager.StockManager;
import com.tmerclub.cloud.product.mapper.BrandMapper;
import com.tmerclub.cloud.product.mapper.SkuMapper;
import com.tmerclub.cloud.product.mapper.SpuMapper;
import com.tmerclub.cloud.product.model.Sku;
import com.tmerclub.cloud.product.model.SpuConsignmentChange;
import com.tmerclub.cloud.product.service.*;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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

/**
 * @author xxw
 * @date 2022/8/5 17:50
 */
@Service
public class SupplierSpuUpdateLaterOperateServiceImpl implements SupplierSpuUpdateLaterOperateService {
    @Autowired
    private SpuMapper spuMapper;
    @DubboReference
    private ShopSubstituteSalesFeignClient shopSubstituteSalesFeignClient;
    @Autowired
    private BrandMapper brandMapper;
    @Autowired
    private SpuOfflineService spuOfflineService;
    @DubboReference
    private DistributionFeignClient distributionFeignClient;
    @DubboReference
    private CouponFeignClient couponFeignClient;
    @DubboReference
    private DiscountFeignClient discountFeignClient;
    @Autowired
    private SpuTagReferenceService spuTagReferenceService;
    @Autowired
    private SkuMapper skuMapper;
    @Autowired
    private SkuService skuService;
    @DubboReference
    private SearchSpuFeignClient searchSpuFeignClient;
    @Autowired
    private SpuConsignmentChangeService spuConsignmentChangeService;
    @Autowired
    private StockManager stockManager;
    @Autowired
    private StockPointSkuService stockPointSkuService;


    @Override
    public void skuUpdateLaterOperate(Long supplierSpuId) {
        //获取所有代销商品信息
        List<SpuVO> commissionSpuLists = spuMapper.getCommissionSpuList(supplierSpuId);
        List<Long> commissionSpuIds = commissionSpuLists.stream().map(SpuVO::getSpuId).collect(Collectors.toList());
        //过滤掉自动设价的商品
        for (SpuVO commissionSpuList : commissionSpuLists) {
            //获取商家代销设置
            ShopSubstituteSalesVO substituteSalesVO = shopSubstituteSalesFeignClient.getByShopSubstituteSalesId(commissionSpuList.getShopId()).getData();
            if (Objects.equals(substituteSalesVO.getSalesType(), SalesType.SMART_PRICE.value())) {
                List<Long> list = Collections.singletonList(commissionSpuList.getSpuId());
                commissionSpuIds.removeAll(list);
            }
        }
        //下架手动设价的商品
        if (commissionSpuIds.size() != 0) {
            batchChangeSpuStatus(commissionSpuIds, SpuStatus.OFF_SHELF.value(), SysTypeEnum.MULTISHOP.value());
        }
    }

    @Override
    public void skuWeightOrVolumeUpdateOperate(Long supplierSpuId) {
        supplierSpuUpdateLaterOffSpu(supplierSpuId, CommissionSpuOffType.SHOP_DELIVERY.value());
    }

    @Override
    public void skuAddOperate(Long supplierSpuId) {

    }

    @Override
    public void feePriceUnderSupplyPrice(EsProductBO esProductBO) {
        //获取商家代销设置
        ShopSubstituteSalesVO substituteSalesVO = shopSubstituteSalesFeignClient.getByShopSubstituteSalesId(esProductBO.getShopId()).getData();
        if (Objects.equals(substituteSalesVO.getSalesType(), SalesType.MANUAL_OPERATION_PRICE.value())) {
            if (Objects.equals(substituteSalesVO.getAutomaticOff(), 1)) {
                //下架商品
                List<Long> spuIds = Collections.singletonList(esProductBO.getSpuId());
                batchChangeSpuStatus(spuIds, SpuStatus.OFF_SHELF.value(), SysTypeEnum.MULTISHOP.value());
            }
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void batchChangeSpuStatus(List<Long> spuIds, Integer status, Integer sysType) {
        if (CollUtil.isEmpty(spuIds)) {
            return;
        }
        spuMapper.batchChangeSpuStatus(spuIds, status);
        // 更新品牌信息
        List<Long> brandIds = brandMapper.listBrandIdBySpuIds(spuIds);
        brandMapper.batchUpdateSpuCount(brandIds);
        if (Objects.equals(status, StatusEnum.DISABLE.value()) || Objects.equals(status, StatusEnum.OFFLINE.value())) {
            // 更新商品分组信息
            handleStatusChange(spuIds);
            // T删掉满减、优惠券的商品关联信息
            if (!Objects.isNull(AuthUserContext.get())) {
                handleSpuStatusChange(spuIds);
            } else {
                handleCommissionSpuStatusChange(spuIds);
            }
            // 下线掉关联商品的活动 (在线下活动之前已经修改了商品的状态，下面的方法就查询不到已有的商品了，所以就需要带着修改后的商品去状态去查询)
            spuOfflineService.offlineCommissionSpuStatusAndActivity(1, spuIds, null, null, status, SysTypeEnum.MULTISHOP.value());
        } else if (Objects.equals(status, StatusEnum.ENABLE.value())) {
            handleDistributionSpuStatusChange(spuIds, status);
        }
    }

    @Override
    public void synMarketPrice(Long skuId, Long price) {
        List<Long> commissionSkuIds = skuMapper.getSkuIdsBySupplierSkuId(skuId);
        if (commissionSkuIds.size() != 0) {
            List<Sku> skus = new ArrayList<>(Constant.INITIAL_CAPACITY);
            for (Long commissionSkuId : commissionSkuIds) {
                Sku sku = new Sku();
                sku.setSkuId(commissionSkuId);
                sku.setMarketPriceFee(price);
                skus.add(sku);
            }
            if (CollUtil.isNotEmpty(skus)) {
                skuMapper.updateBatch(skus);
            }
        }
    }

    @Override
    public void soldOutManualPriceCommissionSpu(Long supplierSpuId, Integer type, Integer isFiltration) {
        //
        List<SpuVO> commissionSpuLists = spuMapper.getCommissionSpuList(supplierSpuId);
        // 手动设价商品id
        List<Long> manualSpuIds = new ArrayList<>(Constant.INITIAL_CAPACITY);
        // 智能设价商品id
        List<Long> smartSpuIds = new ArrayList<>(Constant.INITIAL_CAPACITY);
        for (SpuVO commissionSpuList : commissionSpuLists) {
            // 是否过滤商家自行发货的商品
            if (isFiltration != -1) {
                if (Objects.equals(commissionSpuList.getSupplierDeliveryType(), SupplierDeliveryType.SHOP_DELIVERY.value())) {
                    continue;
                }
            }
            ShopSubstituteSalesVO data = shopSubstituteSalesFeignClient.getByShopSubstituteSalesId(commissionSpuList.getShopId()).getData();
            if (Objects.equals(data.getSalesType(), SalesType.MANUAL_OPERATION_PRICE.value())) {
                manualSpuIds.add(commissionSpuList.getSpuId());
            } else {
                smartSpuIds.add(commissionSpuList.getSpuId());
            }
        }

        // 根据下架类型下架不同的代销商品
        if (Objects.equals(type, SalesType.SMART_PRICE.value())) {
            // 下架智能设价
            if (smartSpuIds.size() != 0) {
                // 下架商品
                batchChangeSpuStatus(smartSpuIds, SpuStatus.OFF_SHELF.value(), SysTypeEnum.MULTISHOP.value());
            }
        }

        if (Objects.equals(type, SalesType.MANUAL_OPERATION_PRICE.value())) {
            // 下架手动设价
            if (manualSpuIds.size() != 0) {
                // 下架商品
                batchChangeSpuStatus(manualSpuIds, SpuStatus.OFF_SHELF.value(), SysTypeEnum.MULTISHOP.value());
            }
        }


    }

    @Override
    public void supplierSkuAddOrDeleteLaterOperate(Long supplierSpuId, Long skuId) {
        SpuVO supplierSpu = spuMapper.getBySpuId(supplierSpuId);
        if (Objects.equals(supplierSpu.getSupplierSpuType(), SupplierSpuType.YES.value())) {
            SkuVO supplierSkuVO = skuMapper.getSkuBySkuId(skuId);
            //供应商sku信息
            List<SkuVO> skuVOList = skuMapper.listSkuWithAttrBySpuId(supplierSpuId);
            Map<Long, List<SpuSkuAttrValueVO>> supplierSkuAttrValueVO = skuVOList.stream().collect(Collectors.toMap(SkuVO::getSkuId, SkuVO::getSpuSkuAttrValues));
            // 获取sku总库存
            SkuStockVO skuStock = stockManager.getSkuTotalStock(skuId);
            supplierSkuVO.setSpuSkuAttrValues(supplierSkuAttrValueVO.get(skuId));
            if (supplierSkuVO.getSkuLangList().size() != 0) {
                List<SkuLangVO> skuLangList = supplierSkuVO.getSkuLangList();
                Map<Integer, String> stringMap = skuLangList.stream().collect(Collectors.toMap(SkuLangVO::getLang, SkuLangVO::getSkuName));
                supplierSkuVO.setSkuName(stringMap.get(I18nMessage.getLang()));
            }
            supplierSkuVO.setStock(skuStock.getStock());
            //供应商skuID对应的售价
            Map<Long, Long> supplierSkuIdPriceFee = skuVOList.stream().collect(Collectors.toMap(SkuVO::getSkuId, SkuVO::getPriceFee));
            //供应商skuID对应的市场价
            Map<Long, Long> supplierSkuIdMarketPriceFee = skuVOList.stream().collect(Collectors.toMap(SkuVO::getSkuId, SkuVO::getMarketPriceFee));
            //代销商品信息
            List<SpuVO> commissionSpuIdList = spuMapper.getCommissionSpuList(supplierSpuId);
            // sku变化时，需要更新是商品id
            Set<Long> spuIds = new HashSet<>();
            for (SpuVO spuVO : commissionSpuIdList) {
                spuIds.add(spuVO.getSpuId());
                //获取商家代销设置
                ShopSubstituteSalesVO salesVO = shopSubstituteSalesFeignClient.getByShopSubstituteSalesId(spuVO.getShopId()).getData();
                //获取代销商品sku信息
                List<SkuVO> commissionSkuVOList = this.listSkuWithAttrBySpuId(spuVO.getSpuId());
                List<String> commissionSkuNames = commissionSkuVOList.stream().map(SkuVO::getSkuName).toList();
                Map<String, Long> commissionSkuNameMap = commissionSkuVOList.stream().collect(Collectors.toMap(SkuVO::getSkuName, SkuVO::getSkuId));
                boolean contains = commissionSkuNames.contains(supplierSkuVO.getSkuName());
                //智能设价计算售价
                long price = 0L;
                if (!Objects.isNull(salesVO)) {
                    if (Objects.equals(salesVO.getSalesPriceType(), SalesPriceType.PROPORTION_PRICE.value())) {
                        //按比例加价
                        price = PriceUtil.divideByBankerRounding(supplierSkuVO.getPriceFee() * salesVO.getSalesPrice(), 10000) + supplierSkuVO.getPriceFee();
                    }
                    if (Objects.equals(salesVO.getSalesPriceType(), SalesPriceType.FIXED_NUMBER_PRICE.value())) {
                        //按固定数值加价
                        price = supplierSkuVO.getPriceFee() + salesVO.getSalesPrice();
                    }
                }
                if (contains) {
                    //存在,说明供应商新增的sku是之前删除的sku,同步改变代销商品中的供应商SkuId值和售价
                    Sku sku = new Sku();
                    sku.setSkuId(commissionSkuNameMap.get(supplierSkuVO.getSkuName()));
                    sku.setSupplierSkuId(supplierSkuVO.getSkuId());
                    //售价
                    if (!Objects.isNull(salesVO)) {
                        if (salesVO.getSalesType() == 0) {
                            sku.setPriceFee(price);
                        }
                        if (salesVO.getSalesType() == 1) {
                            sku.setPriceFee(supplierSkuIdPriceFee.get(supplierSkuVO.getSkuId()));
                        }
                    }
                    //市场价
                    sku.setMarketPriceFee(supplierSkuIdMarketPriceFee.get(supplierSkuVO.getSkuId()));
                    skuMapper.update(sku);
                    continue;
                }
                //不存在说明供应商sku属于新增的,同步新增
                if (salesVO.getSalesType() == 0) {
                    supplierSkuVO.setPriceFee(price);
                }
                SkuDTO skuDTO = BeanUtil.map(supplierSkuVO, SkuDTO.class);
                skuDTO.setSupplierSkuId(skuDTO.getSkuId());
                skuDTO.setSpuId(null);
                skuDTO.setStock(supplierSkuVO.getStock());
                List<SkuDTO> skuDTOList = Collections.singletonList(skuDTO);
                skuService.commissionSave(spuVO, skuDTOList, SysTypeEnum.MULTISHOP.value());
            }
            if(CollUtil.isNotEmpty(spuIds)) {
                // sku变化时通知商品更新
                spuMapper.updateSpuUpdateTime(spuIds.stream().toList(), null, null, null);
            }
        }
    }

    @Override
    public void changeSynUpdate(Long supplierSpuId) {
        List<Long> commissionSpuIdList = spuMapper.getCommissionSpuIdList(supplierSpuId, null);
        Object delete = RedisUtil.get(ProductCacheNames.SKU_STATUS_DELETE + supplierSpuId);
        Object insert = RedisUtil.get(ProductCacheNames.SKU_STATUS_INSERT + supplierSpuId);
        //改变 供应商商品更新后代销商品是否已同步更新
        if (commissionSpuIdList.size() != 0) {
            if (Objects.nonNull(delete)) {
                spuMapper.batchChangeSynUpdate(commissionSpuIdList, IsSynUpdate.YEW_CHANGE.value());
            }
            for (Long aLong : commissionSpuIdList) {
                SpuVO spuVO = spuMapper.getBySpuId(aLong);
                //获取商家代销设置
                ShopSubstituteSalesVO salesVO = shopSubstituteSalesFeignClient.getByShopSubstituteSalesId(spuVO.getShopId()).getData();
                if (!Objects.isNull(salesVO)) {
                    if (Objects.equals(salesVO.getSalesType(), 0) && !Objects.isNull(insert)) {
                        //智能设价时，供应商新增SKU时，编辑商品没有提示信息
                        spuMapper.batchChangeSynUpdate(commissionSpuIdList, IsSynUpdate.YEW_CHANGE.value());
                    }

                }
            }
            RedisUtil.del(ProductCacheNames.SKU_STATUS_INSERT + supplierSpuId);
        }
    }

    @Override
    public List<SpuAdminVO> disposeSpuMsg(List<SpuAdminVO> spuAdminList, Map<Long, List<SpuVO>> supplierSpuMap) {
        if (spuAdminList.size() == 0) {
            return spuAdminList;
        }
        ShopSubstituteSalesVO salesVO = null;
        if (Objects.nonNull(spuAdminList.get(0).getShopId())) {
            salesVO = shopSubstituteSalesFeignClient.getByShopSubstituteSalesId(spuAdminList.get(0).getShopId()).getData();
        }
        List<Long> spuIdList = spuAdminList.stream().map(SpuAdminVO::getSpuId).collect(Collectors.toList());
        //批量获取代销商品基本信息是否改变
        List<SpuConsignmentChange> spuConsignmentChanges = spuConsignmentChangeService.batchGetBySpuIdList(spuIdList);
        Map<Long, List<SpuConsignmentChange>> spuConsignmentChangeMap = new HashMap<>(16);
        if (spuConsignmentChanges.size() != 0) {
            spuConsignmentChangeMap = spuConsignmentChanges.stream().collect(Collectors.groupingBy(SpuConsignmentChange::getSpuId));
        }
        for (SpuAdminVO spuAdminVO : spuAdminList) {
            if (Objects.isNull(spuAdminVO.getSupplierSpuId())) {
                continue;
            }
            //返回代销设置类型
            if (Objects.nonNull(salesVO)) {
                spuAdminVO.setSalesType(salesVO.getSalesType());
            }
            //判断代销商品是否选择了店铺分类
            if (Objects.nonNull(spuAdminVO.getShopCategoryId()) && spuAdminVO.getShopCategoryId() == -1) {
                spuAdminVO.setIsSynUpdate(IsSynUpdate.NO_CHANGE.value());
            }
            //获取代销商品基本信息是否改变
            List<SpuConsignmentChange> consignmentChanges = spuConsignmentChangeMap.get(spuAdminVO.getSpuId());
            if (Objects.isNull(consignmentChanges)) {
                continue;
            }
            SpuConsignmentChange spuConsignmentChange = consignmentChanges.get(0);
            if(!supplierSpuMap.containsKey(spuAdminVO.getSupplierSpuId())){
                continue;
            }
            //获取供应商信息
            SpuVO supplierSpu = supplierSpuMap.get(spuAdminVO.getSupplierSpuId()).get(0);
            List<Long> supplierSpuIds = Collections.singletonList(spuAdminVO.getSupplierSpuId());
            List<SpuSearchVO> data = searchSpuFeignClient.listNotDeleteSpu(supplierSpuIds).getData();
            //没有数据说明供应商品已删除，
            if (data.size() == 0) {
                continue;
            }

            if (Objects.equals(spuAdminVO.getSupplierDeliveryType(), SupplierDeliveryType.SUPPLIER_DELIVERY.value())) {
                //名称
                if (Objects.equals(spuConsignmentChange.getNameChange(), 0)) {
                    spuAdminVO.setSpuName(supplierSpu.getName());
                }
                //图片
                if (Objects.equals(spuConsignmentChange.getImageChange(), 0)) {
                    spuAdminVO.setMainImgUrl(data.get(0).getMainImgUrl());
                }
                //spuCod
                if (Objects.equals(spuConsignmentChange.getSpuCodeChange(), 0)) {
                    spuAdminVO.setSpuCode(supplierSpu.getSpuCode());
                }
                //品牌
                if (Objects.equals(spuConsignmentChange.getBrandChange(), 0)) {
                    if (!Objects.isNull(supplierSpu.getBrand())) {
                        spuAdminVO.setBrandName(supplierSpu.getBrand().getName());
                    }
                }
            }

        }
        return spuAdminList;
    }

    @Override
    public List<SpuSearchVO> usDisposeSpuMsg(List<SpuSearchVO> spuSearchVOList, Map<Long, List<SpuConsignmentChange>> spuConsignmentChangeMap, Map<Long, List<SpuVO>> supplierSpuMap) {
        for (SpuSearchVO spuSearchVO : spuSearchVOList) {
            if (Objects.isNull(spuSearchVO.getSupplierSpuId())) {
                continue;
            }
            //获取代销商品基本信息是否改变
            if (Objects.isNull(spuConsignmentChangeMap.get(spuSearchVO.getSpuId()))) {
                continue;
            }
            SpuConsignmentChange spuConsignmentChange = spuConsignmentChangeMap.get(spuSearchVO.getSpuId()).get(0);
            if (Objects.isNull(spuConsignmentChange)) {
                continue;
            }
            //获取供应商信息
            SpuVO supplierSpu = supplierSpuMap.get(spuSearchVO.getSupplierSpuId()).get(0);
            List<Long> supplierSpuIds = Collections.singletonList(spuSearchVO.getSupplierSpuId());
            List<SpuSearchVO> data = searchSpuFeignClient.listNotDeleteSpu(supplierSpuIds).getData();

            if (Objects.equals(spuSearchVO.getSupplierDeliveryType(), SupplierDeliveryType.SUPPLIER_DELIVERY.value())) {
                //名称
                if (Objects.equals(spuConsignmentChange.getNameChange(), 0)) {
                    if (Objects.isNull(supplierSpu.getName())) {
                        supplierSpu.setName("");
                    }
                    spuSearchVO.setSpuName(supplierSpu.getName());
                }
                //图片
                if (Objects.equals(spuConsignmentChange.getImageChange(), 0)) {
                    if (Objects.nonNull(data) && data.size() != 0 && Objects.nonNull(data.get(0).getMainImgUrl())) {
                        spuSearchVO.setMainImgUrl(data.get(0).getMainImgUrl());
                    }
                }
                //spuCod
                if (Objects.equals(spuConsignmentChange.getSpuCodeChange(), 0)) {
                    spuSearchVO.setSpuCode(supplierSpu.getSpuCode());
                }
                //品牌
                if (Objects.equals(spuConsignmentChange.getBrandChange(), 0)) {
                    if (!Objects.isNull(supplierSpu.getBrand())) {
                        spuSearchVO.setBrandName(supplierSpu.getBrand().getName());
                    }
                }
            }
        }
        return spuSearchVOList;
    }

    @Override
    public void supplierSpuUpdateLaterOffSpu(Long supplierSpuId, Integer commissionSpuOffType) {
        if (Objects.equals(commissionSpuOffType, CommissionSpuOffType.ALL.value())) {
            List<Long> commissionSpuIds = spuMapper.getSupIdListBySupplierId(supplierSpuId);
            //下架
            batchChangeSpuStatus(commissionSpuIds, SpuStatus.OFF_SHELF.value(), SysTypeEnum.MULTISHOP.value());
        }

        if (Objects.equals(commissionSpuOffType, CommissionSpuOffType.SUPPLIER_DELIVERY_SPU.value())) {
            List<Long> commissionSpuIds = spuMapper.getCommissionSpuIdList(supplierSpuId, SupplierDeliveryType.SUPPLIER_DELIVERY.value());
            //下架
            batchChangeSpuStatus(commissionSpuIds, SpuStatus.OFF_SHELF.value(), SysTypeEnum.MULTISHOP.value());
        }

        if (Objects.equals(commissionSpuOffType, CommissionSpuOffType.SHOP_DELIVERY.value())) {
            List<Long> commissionSpuIds = spuMapper.getCommissionSpuIdList(supplierSpuId, SupplierDeliveryType.SHOP_DELIVERY.value());
            //下架
            batchChangeSpuStatus(commissionSpuIds, SpuStatus.OFF_SHELF.value(), SysTypeEnum.MULTISHOP.value());
        }
    }

    private void handleStatusChange(List<Long> spuIds) {
        // 删除商品的分组信息
        spuTagReferenceService.deleteSpuData(spuIds);
    }

    private void handleSpuStatusChange(List<Long> spuIds) {
        List<Long> shopIds;
        if (Objects.equals(AuthUserContext.get().getTenantId(), Constant.PLATFORM_SHOP_ID)) {
            shopIds = new ArrayList<>(Constant.INITIAL_CAPACITY);
        } else {
            shopIds = spuMapper.spuShopIdsBySpuIds(spuIds);
        }
        handleSpuOffline(spuIds, shopIds);
    }

    private void handleSpuOffline(List<Long> spuIds, List<Long> shopIds) {
        if (CollUtil.isNotEmpty(spuIds)) {
            ServerResponseEntity<Void> discountResponseEntity = discountFeignClient.handleSpuOffline(spuIds, shopIds);
            if (!discountResponseEntity.isSuccess()) {
                throw new LuckException(discountResponseEntity.getMsg());
            }
            ServerResponseEntity<Void> couponResponseEntity = couponFeignClient.handleSpuOffline(spuIds, shopIds);
            if (!couponResponseEntity.isSuccess()) {
                throw new LuckException(couponResponseEntity.getMsg());
            }
        }
    }

    private void handleCommissionSpuStatusChange(List<Long> spuIds) {
        List<Long> shopIds = spuMapper.spuShopIdsBySpuIds(spuIds);
        handleSpuOffline(spuIds, shopIds);
    }

    private void handleDistributionSpuStatusChange(List<Long> spuIds, Integer status) {
        List<Long> shopIds;
        if (Objects.equals(AuthUserContext.get().getTenantId(), Constant.PLATFORM_SHOP_ID)) {
            shopIds = new ArrayList<>(Constant.INITIAL_CAPACITY);
        } else {
            shopIds = spuMapper.spuShopIdsBySpuIds(spuIds);
        }
        ServerResponseEntity<Void> responseEntity = distributionFeignClient.handleSpuState(spuIds, shopIds, status);
        if (!responseEntity.isSuccess()) {
            throw new LuckException(responseEntity.getMsg());
        }
    }

    public List<SkuVO> listSkuWithAttrBySpuId(Long spuId) {
        List<SkuVO> skuVOList = skuMapper.listSkuWithAttrBySpuId(spuId);
        for (SkuVO skuVO : skuVOList) {
            if (skuVO.getSkuLangList().size() != 0) {
                skuVO.setSkuName(skuVO.getSkuLangList().get(0).getSkuName());
            }
            if (StrUtil.isNotBlank(skuVO.getImgUrl())) {
                skuVO.setMainImgUrl(skuVO.getImgUrl());
            }
        }
        return skuVOList;
    }
}
