package com.hzs.sale.wares.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hzs.common.core.constant.CountryConstants;
import com.hzs.common.core.constant.MagicNumberConstants;
import com.hzs.common.core.domain.R;
import com.hzs.common.core.enums.*;
import com.hzs.common.core.utils.ComputeUtil;
import com.hzs.common.core.utils.DistinctByKeyUtil;
import com.hzs.common.core.utils.StringUtils;
import com.hzs.common.domain.member.base.CuMember;
import com.hzs.common.domain.sale.ext.BdWaresSpecsRelationExt;
import com.hzs.common.domain.sale.ext.BdWaresSpecsSkuExt;
import com.hzs.common.domain.sale.wares.BdWaresLabel;
import com.hzs.common.domain.sale.wares.BdWaresRange;
import com.hzs.common.domain.sale.wares.BdWaresSpecsSku;
import com.hzs.common.domain.system.config.BdAwards;
import com.hzs.common.domain.system.config.BdLabel;
import com.hzs.common.domain.system.ext.BdLabelExt;
import com.hzs.common.security.utils.SecurityUtils;
import com.hzs.member.base.IMemberServiceApi;
import com.hzs.sale.order.service.ISaOrderService;
import com.hzs.sale.shopping.vo.ProductGroup;
import com.hzs.sale.shopping.vo.WaresItemVo;
import com.hzs.sale.shopping.vo.WaresItemWaresInfo;
import com.hzs.sale.wares.mapper.BdWaresSpecsSkuMapper;
import com.hzs.sale.wares.param.*;
import com.hzs.sale.wares.service.*;
import com.hzs.system.base.ICurrencyServiceApi;
import com.hzs.system.base.dto.CurrencyDTO;
import com.hzs.system.config.IAwardsServiceApi;
import com.hzs.system.config.IGradeServiceApi;
import com.hzs.system.config.IRangeServiceApi;
import com.hzs.system.config.dto.GradeDTO;
import com.hzs.system.config.dto.RangeDTO;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

/**
 * 规格sku表 服务实现类
 */
@Service
public class BdWaresSpecsSkuServiceImpl extends ServiceImpl<BdWaresSpecsSkuMapper, BdWaresSpecsSku> implements IBdWaresSpecsSkuService {

    @Autowired
    private IBdWaresSpecsRelationService iBdWaresSpecsRelationService;
    @Autowired
    private IBdWaresLabelService iBdWaresLabelService;
    @Autowired
    private IBdWaresDetailService iBdWaresDetailService;
    @Autowired
    private IBdWaresRangeService iBdWaresRangeService;
    @Autowired
    private ISaOrderService iSaOrderService;

    @DubboReference
    ICurrencyServiceApi iCurrencyServiceApi;
    @DubboReference
    IRangeServiceApi iRangeServiceApi;
    @DubboReference
    IMemberServiceApi iMemberServiceApi;
    @DubboReference
    IAwardsServiceApi iAwardsServiceApi;
    @DubboReference
    IGradeServiceApi iGradeServiceApi;

    @Override
    public CuWaresParams queryWaresSpecsSku(Integer specialArea, String waresCode, Long loginMemberId, Integer pkCountry) {
        List<BdWaresSpecsSkuExt> bdWaresSpecsSkuExtList;

        R<CuMember> memberDate = iMemberServiceApi.getMember(loginMemberId);
        CuMember cuMember = memberDate.getData();
        if (EOrderType.REPURCHASE_ORDER.getValue() == specialArea
                || EOrderType.SPECIAL_REPURCHASE_ORDER.getValue() == specialArea) {
            int level = cuMember.getPkSettleGrade();
            int rangeType = ERangeType.GRADE.getValue();
            RangeDTO rangeDTO = iRangeServiceApi.queryRangeDtoByCondition(cuMember.getPkSettleCountry(), rangeType, level).getData();
            if (rangeDTO == null) {
                return new CuWaresParams();
            }
            // 查询复购极差价格
            bdWaresSpecsSkuExtList = baseMapper.queryWaresSpecsRangeSku(specialArea, waresCode, rangeDTO.getPkRange());
        } else {
            bdWaresSpecsSkuExtList = baseMapper.queryWaresSpecsSku(specialArea, waresCode);
        }

        if (bdWaresSpecsSkuExtList.size() > 0) {
            R<CurrencyDTO> currency = iCurrencyServiceApi.getCurrency(SecurityUtils.getPkCountry());

            Set<Integer> pkSkuIdList = new HashSet<>();
            bdWaresSpecsSkuExtList.forEach(bdWaresSpecsSkuExt ->
                    pkSkuIdList.add(bdWaresSpecsSkuExt.getPkId()));
            List<BdWaresSpecsSkuExt> bdWaresSpecsSkuList = baseMapper.queryWaresSpecsNameSku(pkSkuIdList);
            Map<Integer, String> specsNameMap = new HashMap<>(ComputeUtil.mapInitCapacity(bdWaresSpecsSkuExtList.size()));
            for (BdWaresSpecsSkuExt bdWaresSpecsSku : bdWaresSpecsSkuList) {
                String specsName = bdWaresSpecsSku.getSpecsName();
                if (specsNameMap.containsKey(bdWaresSpecsSku.getPkId())) {
                    String oldSpecsName = specsNameMap.get(bdWaresSpecsSku.getPkId());
                    specsName = oldSpecsName + "，" + specsName;
                }
                specsNameMap.put(bdWaresSpecsSku.getPkId(), specsName);
            }
            CuWaresParams cuWaresParams = BeanUtil.copyProperties(bdWaresSpecsSkuExtList.get(0), CuWaresParams.class);
            cuWaresParams.setWaresPrice(bdWaresSpecsSkuExtList.get(0).getPrice());
            cuWaresParams.setPkWares(bdWaresSpecsSkuExtList.get(0).getPkWares());
            //查询标签合集
            BdWaresLabel waresLabel = new BdWaresLabel();
            waresLabel.setPkWares(bdWaresSpecsSkuExtList.get(0).getPkWares());
            List<BdLabelExt> labelList = iBdWaresLabelService.selectByList(waresLabel);
            // 保证标签
            List<BdLabel> ensureLabelList = new ArrayList<>();
            // 卖点标签
            List<BdLabel> sellingLabelList = new ArrayList<>();
            for (BdLabel bdLabel : labelList) {
                if (bdLabel.getLabelType() == ELabelType.PREFIX.getValue()) {
                    // 前缀标签
                    cuWaresParams.setPrefixLabelTarget(bdLabel);
                }
                if (bdLabel.getLabelType() == ELabelType.COOL.getValue()) {
                    // 酷炫标签
                    cuWaresParams.setCoolLabelTarget(bdLabel);
                }
                if (bdLabel.getLabelType() == ELabelType.ENSURE.getValue()) {
                    // 保证标签
                    ensureLabelList.add(bdLabel);
                }
                if (bdLabel.getLabelType() == ELabelType.SELLING.getValue()) {
                    // 卖点标签
                    sellingLabelList.add(bdLabel);
                }
                cuWaresParams.setEnsureLabelList(ensureLabelList.stream().filter(DistinctByKeyUtil.distinctByKey(BdLabel::getPkId)).collect(Collectors.toList()));
                cuWaresParams.setSellingLabelList(sellingLabelList.stream().filter(DistinctByKeyUtil.distinctByKey(BdLabel::getPkId)).collect(Collectors.toList()));
            }
            cuWaresParams.setInExchangeRate(currency.getData().getInExchangeRate());

            List<CuWaresItemsParams> cuWaresItemsParamsList = setUpWaresItemsParams(bdWaresSpecsSkuExtList, specsNameMap, currency.getData().getInExchangeRate());
            // 前台传递结算国 跨国报单计算价格使用
            R<CurrencyDTO> kgCurrency = null;
            if (pkCountry != null) {
                kgCurrency = iCurrencyServiceApi.getCurrency(pkCountry);
            }
            List<CuProductParams> productParams = iBdWaresDetailService.selectByWaresCode(waresCode, specialArea, null);
            List<CuProductParams> productParamsResultList = new ArrayList<>();
            for (CuProductParams cuProductParams : productParams) {
                List<CuWaresItemsParams> list = new ArrayList<>();
                for (CuWaresItemsParams cuWaresItemsParams : cuWaresItemsParamsList) {
                    if (cuWaresItemsParams.getIsGift().equals(cuProductParams.getIsGift())) {
                        if (cuWaresItemsParams.getPkProduct().equals(cuProductParams.getProductId())) {
                            if (pkCountry != null) {
                                // 当登陆人的结算国和前端结算国不一致 即为跨国报单
                                if (!pkCountry.equals(SecurityUtils.getPkCountry())) {
                                    BigDecimal price = cuWaresItemsParams.getPrice();
                                    cuWaresItemsParams.setPrice(price.multiply(cuWaresParams.getInExchangeRate()).divide(kgCurrency.getData().getInExchangeRate(), 2, BigDecimal.ROUND_HALF_UP));
                                    // 单价 * 数量计算汇率
                                    cuWaresItemsParams.setPriceTotal(price.multiply(new BigDecimal(cuWaresItemsParams.getQuantity())).multiply(cuWaresParams.getInExchangeRate()).divide(kgCurrency.getData().getInExchangeRate(), 2, BigDecimal.ROUND_HALF_UP));
                                }
                            }
                            list.add(cuWaresItemsParams);
                        }
                    }

                }
                cuProductParams.setWaresItemsParamsList(list);
                if (list.size() > 0) {
                    //定义新列表 过滤掉 为空的详情
                    productParamsResultList.add(cuProductParams);
                }
            }
            productParams.clear();
            productParams.addAll(productParamsResultList);

            if (EOrderType.SPECIAL_REPURCHASE_ORDER.getValue() == specialArea) {
                // 乐学复购，价格根据等级固定
                // 等级
                R<List<GradeDTO>> gradeDTO = iGradeServiceApi.queryGradeList(pkCountry);
                List<GradeDTO> gradeList = gradeDTO.getData();
                Map<Integer, GradeDTO> gradeMap = gradeList.stream().collect(Collectors.toMap(GradeDTO::getGradeValue, Function.identity()));

                BigDecimal price;
                // 乐学复购，价格根据等级固定
                if (cuMember.getPkSettleGrade().equals(gradeMap.get(EGrade.S_VIP.getValue()).getPkId())) {
                    price = MagicNumberConstants.LXYK_V4;
                } else if (cuMember.getPkSettleGrade().equals(gradeMap.get(EGrade.VIP.getValue()).getPkId())) {
                    price = MagicNumberConstants.LXYK_V3;
                } else if (cuMember.getPkSettleGrade().equals(gradeMap.get(EGrade.MAKER.getValue()).getPkId())) {
                    price = MagicNumberConstants.LXYK_V2;
                } else {
                    price = MagicNumberConstants.LXYK_V1;
                }
                cuWaresParams.setWaresPrice(price);

                for (CuProductParams productParam : productParams) {
                    for (CuWaresItemsParams cuWaresItemsParams : productParam.getWaresItemsParamsList()) {
                        cuWaresItemsParams.setPriceTotal(new BigDecimal(price.intValue() / productParams.size()));
                    }
                }
            }

            cuWaresParams.setProductParams(productParams);
            cuWaresParams.setSpecialArea(specialArea);
            return cuWaresParams;
        }
        return null;
    }

    /**
     * @description: 购物车查询数量专用
     * @author: zhang jing
     * @date: 2024/3/28 10:41
     * @param: [specialArea, waresCode, loginMemberId, pkCountry]
     * @return: com.hzs.sale.wares.param.CuWaresParams
     **/
    @Override
    public CuWaresParams queryWaresSpecsSkuCount(Integer specialArea, String waresCode, Long loginMemberId, Integer pkCountry) {
        List<BdWaresSpecsSkuExt> bdWaresSpecsSkuExtList;

        if (EOrderType.REPURCHASE_ORDER.getValue() == specialArea
                || EOrderType.SPECIAL_REPURCHASE_ORDER.getValue() == specialArea) {
            R<CuMember> memberDate = iMemberServiceApi.getMember(loginMemberId);
            CuMember cuMember = memberDate.getData();

            int level = cuMember.getPkSettleGrade();
            int rangeType = ERangeType.GRADE.getValue();
            RangeDTO rangeDTO = iRangeServiceApi.queryRangeDtoByCondition(cuMember.getPkSettleCountry(), rangeType, level).getData();
            if (rangeDTO == null) {
                return new CuWaresParams();
            }
            // 查询复购极差价格
            bdWaresSpecsSkuExtList = baseMapper.queryWaresSpecsRangeSku(specialArea, waresCode, rangeDTO.getPkRange());
        } else {
            bdWaresSpecsSkuExtList = baseMapper.queryWaresSpecsSku(specialArea, waresCode);
        }
        if (bdWaresSpecsSkuExtList.size() > 0) {
            Map<Integer, String> specsNameMap = new HashMap<>(ComputeUtil.mapInitCapacity(bdWaresSpecsSkuExtList.size()));
            CuWaresParams cuWaresParams = BeanUtil.copyProperties(bdWaresSpecsSkuExtList.get(0), CuWaresParams.class);

            R<CurrencyDTO> currency = iCurrencyServiceApi.getCurrency(SecurityUtils.getPkCountry());
            List<CuWaresItemsParams> cuWaresItemsParamsList = setUpWaresItemsParams(bdWaresSpecsSkuExtList, specsNameMap, currency.getData().getInExchangeRate());
            // 前台传递结算国 跨国报单计算价格使用
            List<CuProductParams> productParams = iBdWaresDetailService.selectByWaresCode(waresCode, specialArea, null);
            List<CuProductParams> productParamsResultList = new ArrayList<>();
            for (CuProductParams cuProductParams : productParams) {
                List<CuWaresItemsParams> list = new ArrayList<>();
                for (CuWaresItemsParams cuWaresItemsParams : cuWaresItemsParamsList) {
                    if (cuWaresItemsParams.getIsGift().equals(cuProductParams.getIsGift())) {
                        if (cuWaresItemsParams.getPkProduct().equals(cuProductParams.getProductId())) {
                            list.add(cuWaresItemsParams);
                        }
                    }
                }
                cuProductParams.setWaresItemsParamsList(list);
                if (list.size() > 0) {
                    //定义新列表 过滤掉 为空的详情
                    productParamsResultList.add(cuProductParams);
                }

            }
            productParams.clear();
            productParams.addAll(productParamsResultList);
            cuWaresParams.setProductParams(productParams);
            cuWaresParams.setSpecialArea(specialArea);
            return cuWaresParams;
        }
        return null;
    }

    /**
     * @description: 查询商品sku信息购物车专用
     * @author: zhang jing
     * @date: 2024/3/28 14:34
     * @param: [specialArea, waresCode, loginMemberId, pkCountry]
     * @return: com.hzs.sale.wares.param.CuWaresParams
     **/
    @Override
    public CuWaresParams queryWaresSpecsSkuShopping(Integer specialArea, String waresCode, Long loginMemberId, Integer pkCountry) {
        CuMember cuMember = iMemberServiceApi.getMember(loginMemberId).getData();

        // 获取货币信息
        R<CurrencyDTO> currency = iCurrencyServiceApi.getCurrency(SecurityUtils.getPkCountry());

        List<BdWaresSpecsSkuExt> bdWaresSpecsSkuExtList;

        if (EOrderType.REPURCHASE_ORDER.getValue() == specialArea || EOrderType.SPECIAL_REPURCHASE_ORDER.getValue() == specialArea) {
            int level = cuMember.getPkSettleGrade();
            int rangeType = ERangeType.GRADE.getValue();
//            BdAwards bdAwards = iAwardsServiceApi.getAwardsOne(cuMember.getPkAwards()).getData();
//            if (bdAwards.getAwardsValue() > EAwards.MEMBER.getValue()) {
//                level = cuMember.getPkAwards();
//                rangeType = ERangeType.AWARDS.getValue();
//            }
            RangeDTO rangeDTO = iRangeServiceApi.queryRangeDtoByCondition(cuMember.getPkSettleCountry(), rangeType, level).getData();
            if (rangeDTO == null) {
                return new CuWaresParams();
            }
            // 查询复购极差价格
            bdWaresSpecsSkuExtList = baseMapper.queryWaresSpecsRangeSku(specialArea, waresCode, rangeDTO.getPkRange());
        } else {
            bdWaresSpecsSkuExtList = baseMapper.queryWaresSpecsSku(specialArea, waresCode);
        }

        if (bdWaresSpecsSkuExtList.isEmpty()) {
            return null;
        }

        // 获取规格名
        Set<Integer> pkSkuIdList = bdWaresSpecsSkuExtList.stream().map(BdWaresSpecsSkuExt::getPkId).collect(Collectors.toSet());
        Map<Integer, String> specsNameMap = baseMapper.queryWaresSpecsNameSku(pkSkuIdList).stream()
                .collect(Collectors.toMap(BdWaresSpecsSkuExt::getPkId, BdWaresSpecsSkuExt::getSpecsName,
                        (oldValue, newValue) -> oldValue + "，" + newValue));

        CuWaresParams cuWaresParams = BeanUtil.copyProperties(bdWaresSpecsSkuExtList.get(0), CuWaresParams.class);
        cuWaresParams.setWaresPrice(bdWaresSpecsSkuExtList.get(0).getPrice());
        cuWaresParams.setPkWares(bdWaresSpecsSkuExtList.get(0).getPkWares());
        cuWaresParams.setInExchangeRate(currency.getData().getInExchangeRate());

        List<CuWaresItemsParams> cuWaresItemsParamsList = setUpWaresItemsParams(bdWaresSpecsSkuExtList, specsNameMap,
                currency.getData().getInExchangeRate());

        // 处理跨国报单价格
        R<CurrencyDTO> kgCurrency;
        if (pkCountry != null && !pkCountry.equals(SecurityUtils.getPkCountry())) {
            kgCurrency = iCurrencyServiceApi.getCurrency(pkCountry);
            BigDecimal inExchangeRate = cuWaresParams.getInExchangeRate();
            BigDecimal kgInExchangeRate = kgCurrency.getData().getInExchangeRate();
            cuWaresItemsParamsList.forEach(item -> {
                item.setPrice(item.getPrice().multiply(inExchangeRate).divide(kgInExchangeRate, 2, BigDecimal.ROUND_HALF_UP));
                item.setPriceTotal(item.getPrice().multiply(new BigDecimal(item.getQuantity())));
            });
        }

        // 获取商品详情
        List<CuProductParams> productParams = iBdWaresDetailService.selectByWaresCode(waresCode, specialArea, null);
        List<CuProductParams> productParamsResultList = productParams.stream()
                .peek(param -> {
                    List<CuWaresItemsParams> list = cuWaresItemsParamsList.stream()
                            .filter(item -> item.getIsGift().equals(param.getIsGift()) && item.getPkProduct().equals(param.getProductId()))
                            .collect(Collectors.toList());
                    param.setWaresItemsParamsList(list);
                })
                .filter(param -> !param.getWaresItemsParamsList().isEmpty())
                .collect(Collectors.toList());

        // 处理特殊情况下的价格修改
        if (EOrderType.SPECIAL_REPURCHASE_ORDER.getValue() == specialArea) {
            // 获取等级信息
            Map<Integer, GradeDTO> gradeMap = iGradeServiceApi.queryGradeList(pkCountry).getData().stream()
                    .collect(Collectors.toMap(GradeDTO::getGradeValue, Function.identity()));
            BigDecimal price;
            if (cuMember.getPkSettleGrade().equals(gradeMap.get(EGrade.S_VIP.getValue()).getPkId())) {
                price = MagicNumberConstants.LXYK_V4;
            } else if (cuMember.getPkSettleGrade().equals(gradeMap.get(EGrade.VIP.getValue()).getPkId())) {
                price = MagicNumberConstants.LXYK_V3;
            } else if (cuMember.getPkSettleGrade().equals(gradeMap.get(EGrade.MAKER.getValue()).getPkId())) {
                price = MagicNumberConstants.LXYK_V2;
            } else {
                price = MagicNumberConstants.LXYK_V1;
            }
            BigDecimal finalPrice = price;
            productParamsResultList.forEach(param -> param.getWaresItemsParamsList().forEach(item -> {
                if (EYesNo.NO.getIntValue() == item.getIsGift()) {
                    // 非赠品
                    item.setPrice(finalPrice);
                    item.setPriceTotal(finalPrice.multiply(new BigDecimal(item.getQuantity())));
                }
            }));
        }

        cuWaresParams.setProductParams(productParamsResultList);
        cuWaresParams.setSpecialArea(specialArea);
        return cuWaresParams;
    }


    /**
     * 设置返回到前台的商品明细表
     */
    private List<CuWaresItemsParams> setUpWaresItemsParams(List<BdWaresSpecsSkuExt> bdWaresSpecsSkuExtList, Map<Integer, String> specsNameMap, BigDecimal inExchangeRate) {
        List<CuWaresItemsParams> cuWaresItemsParamsList = new ArrayList<>();

        bdWaresSpecsSkuExtList.forEach(bdWaresSpecsSkuExt -> {
            BigDecimal achieveTotal;
            if (SecurityUtils.getPkCountry().equals(CountryConstants.CHINA_COUNTRY)) {
                achieveTotal = bdWaresSpecsSkuExt.getAchieve().multiply(new BigDecimal(bdWaresSpecsSkuExt.getQuantity())).multiply(inExchangeRate);
            } else {
                achieveTotal = bdWaresSpecsSkuExt.getAchieve().multiply(new BigDecimal(bdWaresSpecsSkuExt.getQuantity()));
            }
            // bv
            BigDecimal assAchieveTotal;
            assAchieveTotal = isitEmpty(bdWaresSpecsSkuExt.getAssAchieve()).multiply(new BigDecimal(bdWaresSpecsSkuExt.getQuantity()));

            // 汇率比例
            CuWaresItemsParams cuWaresItemsParams = CuWaresItemsParams.builder()
                    .quantity(bdWaresSpecsSkuExt.getQuantity())
                    .isGift(bdWaresSpecsSkuExt.getIsGift())
                    .pkProduct(bdWaresSpecsSkuExt.getPkProduct())
                    .pkWaresDetail(bdWaresSpecsSkuExt.getPkWaresDetail())
                    .price(bdWaresSpecsSkuExt.getPrice())
                    .achieve(bdWaresSpecsSkuExt.getAchieve())
                    .specCover(bdWaresSpecsSkuExt.getSpecCover())
                    .pkSkuId(bdWaresSpecsSkuExt.getPkId())
                    .assAchieve(bdWaresSpecsSkuExt.getAssAchieve())
                    .assAchieveTotal(assAchieveTotal)
                    .specsName(specsNameMap.get(bdWaresSpecsSkuExt.getPkId()))
                    .achieveTotal(achieveTotal)
                    .priceTotal(bdWaresSpecsSkuExt.getPrice().multiply(new BigDecimal(bdWaresSpecsSkuExt.getQuantity())))
                    .vipPrice(bdWaresSpecsSkuExt.getVipPrice())
                    .vipPriceTotal(null != bdWaresSpecsSkuExt.getVipPrice() ? bdWaresSpecsSkuExt.getVipPrice().multiply(new BigDecimal(bdWaresSpecsSkuExt.getQuantity())) : bdWaresSpecsSkuExt.getVipPrice())
                    .retailPrice(bdWaresSpecsSkuExt.getRetailPrice())
                    .retailPriceTotal(null != bdWaresSpecsSkuExt.getRetailPrice() ? bdWaresSpecsSkuExt.getRetailPrice().multiply(new BigDecimal(bdWaresSpecsSkuExt.getQuantity())) : bdWaresSpecsSkuExt.getRetailPrice())
                    .build();
            cuWaresItemsParamsList.add(cuWaresItemsParams);
        });
        return cuWaresItemsParamsList;
    }

    private BigDecimal isitEmpty(BigDecimal assAchieve) {
        if (StringUtils.isNotNull(assAchieve)) {
            return assAchieve;
        } else {
            return new BigDecimal(0);
        }
    }

    /**
     * 通过规格列表查询商品最小库存单位，
     *
     * @param cuWaresItemsParams 明细参数
     * @return: SpecsSkuParam
     * @Author: sui q
     * @Date: 2022/9/19 16:26
     */
    @Override
    public SpecsSkuParam queryWaresDetailSkuBySpecs(CuWaresItemsParams cuWaresItemsParams) {
        BdWaresSpecsSkuExt bdWaresSpecsSkuExt = baseMapper.queryWaresDetailSkuBySpecs(cuWaresItemsParams);
        return SpecsSkuParam.builder()
                .pkWaresSpecsSku(bdWaresSpecsSkuExt.getPkId())
                .price(bdWaresSpecsSkuExt.getWaresPrice())
                .achieve(bdWaresSpecsSkuExt.getWaresAchieve())
                .build();
    }

    /**
     * 根据id修改值
     *
     * @param sku
     */
    @Override
    public void updateBySkuId(BdWaresSpecsSku sku) {
        baseMapper.updateBySkuId(sku);
    }

    /**
     * 根据id查询sku （可查询删除数据）
     *
     * @param pkWaresSpecsSku
     * @return
     */
    @Override
    public BdWaresSpecsSku selectByWaresSpecsSkuIdAndDel(Integer pkWaresSpecsSku) {
        return baseMapper.selectByWaresSpecsSkuIdAndDel(pkWaresSpecsSku);
    }

    /**
     * 根据商品id 查询
     *
     * @param waresIds
     * @return
     */
    @Override
    public List<BdWaresSpecsSkuExt> selectByWaresSpecsSku(List<Integer> waresIds) {
        return baseMapper.selectByWaresSpecsSku(waresIds);
    }

    /**
     * 根据id 批量查询sku 信息
     *
     * @param pkWaresSpecsSku
     * @return
     */
    @Override
    public List<WaresItemWaresInfo> selectByPkIds(List<Integer> pkWaresSpecsSku) {
        return baseMapper.selectByPkIds(pkWaresSpecsSku);
    }

    @Override
    public WaresItemVo selectWaresItemsVO(Long pkMember, Integer pkCountry, Integer specialArea, String waresCode, List<ProductGroup> productGroup, Integer quantity) {
        WaresItemVo waresItemVo = WaresItemVo.builder().build();

        if (CollectionUtil.isNotEmpty(productGroup)) {
            waresItemVo = baseMapper.selectWaresItemsVO(specialArea, waresCode, productGroup);
            waresItemVo.setQuantity(quantity);
            waresItemVo.setSkuQuantity(waresItemVo.getQuantity());
            waresItemVo.setPrice(BigDecimal.ZERO);
            waresItemVo.setPriceAmount(BigDecimal.ZERO);
            waresItemVo.setAchieve(BigDecimal.ZERO);
            waresItemVo.setAchieveAmount(BigDecimal.ZERO);
            waresItemVo.setAssAchieve(BigDecimal.ZERO);
            waresItemVo.setAssAchieveAmount(BigDecimal.ZERO);
            Map<String, WaresItemWaresInfo> waresDetailExtMap = new HashMap<>();

            waresItemVo.getWaresItemsParamList().forEach(waresItemWaresInfo -> {
                waresDetailExtMap.put(waresItemWaresInfo.getPkWaresDetail().toString() + waresItemWaresInfo.getPkWaresSpecsSku(), waresItemWaresInfo);
                // 设置规格值 名称拼接
                List<BdWaresSpecsRelationExt> waresSpecsRelationExts = iBdWaresSpecsRelationService.selectByWaresSpecsSku(waresItemWaresInfo.getPkWaresSpecsSku());
                Set<String> specsNames = waresSpecsRelationExts.stream().map(BdWaresSpecsRelationExt::getSpecsName).collect(Collectors.toSet());
                waresItemWaresInfo.setSkuInfo(StringUtils.join(specsNames, ","));
            });

            if (EOrderType.REPURCHASE_ORDER.getValue() == specialArea || EOrderType.SPECIAL_REPURCHASE_ORDER.getValue() == specialArea) {
                CuMember cuMember = iSaOrderService.getCuMemberByKey(pkMember);
                int level = cuMember.getPkSettleGrade();
                int rangeType = ERangeType.GRADE.getValue();
//                BdAwards bdAwards = iAwardsServiceApi.getAwardsOne(cuMember.getPkAwards()).getData();
//                if (bdAwards.getAwardsValue() > EAwards.MEMBER.getValue()) {
//                    level = cuMember.getPkAwards();
//                    rangeType = ERangeType.AWARDS.getValue();
//                }
                List<Integer> pkWaresSpecsSku = productGroup.stream().map(ProductGroup::getPkSkuId).collect(Collectors.toList());
                List<BdWaresRange> bdWaresRangeList = iBdWaresRangeService.querySkuWaresRangeByCondition(cuMember.getPkSettleCountry(), rangeType, level, pkWaresSpecsSku);
                bdWaresRangeList.forEach(bdWaresRange -> {
                    String key = bdWaresRange.getPkWaresDetail().toString() + bdWaresRange.getPkWaresSpecsSku();
                    if (waresDetailExtMap.containsKey(key)) {
                        waresDetailExtMap.get(key).setPrice(bdWaresRange.getPrice());
                        waresDetailExtMap.get(key).setAchieve(bdWaresRange.getAchieve());
                        waresDetailExtMap.get(key).setAssAchieve(bdWaresRange.getAssAchieve());
                    }
                });
            }

            for (WaresItemWaresInfo waresItemWaresInfo : waresItemVo.getWaresItemsParamList()) {
                waresItemVo.setPrice(ComputeUtil.computeAdd(waresItemVo.getPrice(), ComputeUtil.computeMultiply(waresItemWaresInfo.getPrice(), waresItemWaresInfo.getQuantity())));
                waresItemVo.setAchieve(ComputeUtil.computeAdd(waresItemVo.getAchieve(), ComputeUtil.computeMultiply(waresItemWaresInfo.getAchieve(), waresItemWaresInfo.getQuantity())));
                waresItemVo.setAssAchieve(ComputeUtil.computeAdd(waresItemVo.getAssAchieve(), ComputeUtil.computeMultiply(waresItemWaresInfo.getAssAchieve(), waresItemWaresInfo.getQuantity())));
            }

            // 复购乐学专区 无奖衔 或者 是 vip 或者是 svip 价格改为2680
            if (EOrderType.SPECIAL_REPURCHASE_ORDER.getValue() == specialArea) {
                // 等级
                R<List<GradeDTO>> gradeDTO = iGradeServiceApi.queryGradeList(pkCountry);
                List<GradeDTO> gradeList = gradeDTO.getData();
                Map<Integer, GradeDTO> gradeMap = gradeList.stream().collect(Collectors.toMap(GradeDTO::getGradeValue, Function.identity()));

                // 查询会员信息
                R<CuMember> memberDate = iMemberServiceApi.getMember(pkMember);
                CuMember cuMemberDate = memberDate.getData();

                // 价格
                BigDecimal waresPrice;
                if (cuMemberDate.getPkSettleGrade().equals(gradeMap.get(EGrade.S_VIP.getValue()).getPkId())) {
                    waresPrice = MagicNumberConstants.LXYK_V4;
                } else if (cuMemberDate.getPkSettleGrade().equals(gradeMap.get(EGrade.VIP.getValue()).getPkId())) {
                    waresPrice = MagicNumberConstants.LXYK_V3;
                } else if (cuMemberDate.getPkSettleGrade().equals(gradeMap.get(EGrade.MAKER.getValue()).getPkId())) {
                    waresPrice = MagicNumberConstants.LXYK_V2;
                } else {
                    waresPrice = MagicNumberConstants.LXYK_V1;
                }
                waresItemVo.setPrice(waresPrice);
            }

            // 前台传递结算国 跨国报单计算价格使用
            R<CurrencyDTO> currency = iCurrencyServiceApi.getCurrency(SecurityUtils.getPkCountry());
            if (pkCountry != null) {
                // 当登陆人的结算国和前端结算国不一致 即为跨国报单
                if (!pkCountry.equals(SecurityUtils.getPkCountry())) {
                    R<CurrencyDTO> kgCurrency = iCurrencyServiceApi.getCurrency(pkCountry);

                    BigDecimal price = waresItemVo.getPrice();
                    waresItemVo.setPrice(price.multiply(currency.getData().getInExchangeRate()).divide(kgCurrency.getData().getInExchangeRate(), 2, BigDecimal.ROUND_HALF_UP));
                }
            }
            if (CountryConstants.CHINA_COUNTRY.equals(pkCountry)) {
                // 汇率比例
                BigDecimal inExchangeRate = currency.getData().getInExchangeRate();
                waresItemVo.setAchieve(ComputeUtil.computeMultiply(waresItemVo.getAchieve(), inExchangeRate));
            }
            waresItemVo.setPriceAmount(ComputeUtil.computeMultiply(waresItemVo.getPrice(), waresItemVo.getQuantity()));
            waresItemVo.setAchieveAmount(ComputeUtil.computeMultiply(waresItemVo.getAchieve(), waresItemVo.getQuantity()));
            waresItemVo.setAssAchieveAmount(ComputeUtil.computeMultiply(waresItemVo.getAssAchieve(), waresItemVo.getQuantity()));
        }
        return waresItemVo;
    }

    /**
     * 根据商品id查询规格价格最小值
     *
     * @param pkWares
     * @return
     */

    @Override
    public List<BdWaresSpecsSkuExt> selectByMinWaresSpecsSku(Integer pkWares) {
        return baseMapper.selectByMinWaresSpecsSku(pkWares);
    }

    /**
     * 根据商品id查询规格价格最小值
     *
     * @param pkWaresIds
     * @return
     */
    @Override
    public List<BdWaresSpecsSkuExt> selectByMinWaresSpecsSkuByPkWaresList(List<Integer> pkWaresIds) {
        return baseMapper.selectByMinWaresSpecsSkuByPkWaresList(pkWaresIds);
    }

    /**
     * 根据id 查询
     *
     * @param pkWaresSpecsSku
     * @return
     */
    @Override
    public BdWaresSpecsSkuExt selectByPkWaresSpecsSku(Integer pkWaresSpecsSku) {
        return baseMapper.selectByPkWaresSpecsSku(pkWaresSpecsSku);
    }

}
