package com.hzs.retail.sale.controller.service.impl;

import cn.hutool.core.codec.Base64Encoder;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.lang.Validator;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.hzs.common.core.config.BdConfig;
import com.hzs.common.core.constant.*;
import com.hzs.common.core.domain.R;
import com.hzs.common.core.enums.*;
import com.hzs.common.core.exception.ServiceException;
import com.hzs.common.core.service.RedisService;
import com.hzs.common.core.utils.*;
import com.hzs.common.core.web.domain.AjaxResult;
import com.hzs.common.domain.member.account.CuMemberAccount;
import com.hzs.common.domain.member.base.CuMember;
import com.hzs.common.domain.member.ext.CuMemberAccountExt;
import com.hzs.common.domain.sale.ext.BdWaresDetailExt;
import com.hzs.common.domain.sale.ext.SaOrderExt;
import com.hzs.common.domain.sale.ext.SaOrderWaresLimitExt;
import com.hzs.common.domain.sale.order.*;
import com.hzs.common.domain.sale.product.BdProduct;
import com.hzs.common.domain.sale.wares.BdWaresRange;
import com.hzs.common.domain.system.base.BdStorehouse;
import com.hzs.common.domain.system.config.BdAwards;
import com.hzs.common.domain.system.config.BdGrade;
import com.hzs.common.security.utils.SecurityUtils;
import com.hzs.member.account.IMemberAccountServiceApi;
import com.hzs.member.base.IMemberServiceApi;
import com.hzs.retail.sale.controller.service.IRetailOrderPostageService;
import com.hzs.retail.sale.controller.service.IRetailOrderService;
import com.hzs.retail.sale.param.RetailOrderOtherParam;
import com.hzs.retail.sale.param.RetailOrderParam;
import com.hzs.retail.sale.param.RetailOrderRegisterParam;
import com.hzs.retail.sale.param.RetailOrderUpgradeParam;
import com.hzs.sale.order.param.OrderItemsParam;
import com.hzs.sale.order.param.WaresNumberParam;
import com.hzs.sale.order.service.*;
import com.hzs.sale.order.service.impl.SaOrderHandle;
import com.hzs.sale.product.service.IBdProductService;
import com.hzs.sale.wares.service.IBdWaresDetailService;
import com.hzs.sale.wares.service.IBdWaresExtendService;
import com.hzs.sale.wares.service.IBdWaresRangeService;
import com.hzs.sale.wares.service.IBdWaresService;
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 lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.*;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

/**
 * 新零售订单服务
 */
@Slf4j
@Service
public class RetailOrderServiceImpl implements IRetailOrderService {

    @Autowired
    private SaOrderHandle saOrderHandle;
    @Autowired
    private ISaOrderService iSaOrderService;
    @Autowired
    private ISaOrderItemsService iSaOrderItemsService;
    @Autowired
    private ISaOrderWaresService iSaOrderWaresService;
    @Autowired
    private IBdWaresService iBdWaresService;
    @Autowired
    private IBdWaresExtendService iBdWaresExtendService;
    @Autowired
    private IBdWaresDetailService iBdWaresDetailService;
    @Autowired
    private IBdWaresRangeService iBdWaresRangeService;
    @Autowired
    private IRetailOrderPostageService iRetailOrderPostageService;
    @Autowired
    private ISaOrderTempService iSaOrderTempService;
    @Autowired
    private ISaOrderWaresLimitService iSaOrderWaresLimitService;
    @Autowired
    private IBdProductService iBdProductService;
    @Autowired
    private ISaOrderSyncService iSaOrderSyncService;

    @Autowired
    private RedisService redisService;
    @Autowired
    private RabbitTemplate rabbitTemplate;

    @DubboReference
    IGradeServiceApi iGradeServiceApi;
    @DubboReference
    IAwardsServiceApi iAwardsServiceApi;
    @DubboReference
    ICurrencyServiceApi iCurrencyServiceApi;
    @DubboReference
    IMemberServiceApi iMemberServiceApi;
    @DubboReference
    IMemberAccountServiceApi iMemberAccountServiceApi;


    @Override
    public String confirmRegOrder(RetailOrderRegisterParam registerParam, List<BdGrade> gradeList) {
        // 新会员
        CuMember newMember = new CuMember();
        newMember.setMemberName(registerParam.getMemberName());
        newMember.setPhone(registerParam.getPhone());
        // 封装订单信息
        SaOrderExt saOrderExt = this.createSaOrder(registerParam, gradeList, newMember);

        // 订单缓存（60分钟有效，防止在线支付回调慢）
        redisService.setCacheObject(CacheConstants.RETAIL_TEMP_ORDER + registerParam.getPkCreator() + registerParam.getOrderCode(), saOrderExt, CacheConstants.RETAIL_ORDER_EXPIRE_TIME * 2, TimeUnit.MINUTES);
        registerParam.setPkGrade(newMember.getPkSettleGrade());
        registerParam.setPkAwards(newMember.getPkAwards());
        // 缓存订单参数（30分钟有效）
        redisService.setCacheObject(CacheConstants.RETAIL_TEMP_REG_PARAM + registerParam.getPkCreator() + registerParam.getOrderCode(), registerParam, CacheConstants.RETAIL_ORDER_EXPIRE_TIME * 2, TimeUnit.MINUTES);

        // 保存订单时间信息
        this.saveOrderTemp(registerParam.getPkCreator(), registerParam.getOrderCode(), JSONUtil.toJsonStr(saOrderExt), JSONUtil.toJsonStr(registerParam));

        return null;
    }

    @Override
    public String confirmUpgOrder(RetailOrderUpgradeParam upgradeParam) {
        // 升级会员
        CuMember upgradeMember = upgradeParam.getUpgradeMember();
        // 封装订单信息
        SaOrderExt saOrderExt = this.createSaOrder(upgradeParam, null, upgradeMember);

        // 订单缓存（60分钟有效，防止在线支付回调慢）
        redisService.setCacheObject(CacheConstants.RETAIL_TEMP_ORDER + upgradeParam.getPkCreator() + upgradeParam.getOrderCode(), saOrderExt, CacheConstants.RETAIL_ORDER_EXPIRE_TIME * 2, TimeUnit.MINUTES);
        upgradeParam.setPkGrade(upgradeMember.getPkSettleGrade());
        // 缓存订单参数（30分钟有效）
        redisService.setCacheObject(CacheConstants.RETAIL_TEMP_UPG_PARAM + upgradeParam.getPkCreator() + upgradeParam.getOrderCode(), upgradeParam, CacheConstants.RETAIL_ORDER_EXPIRE_TIME * 2, TimeUnit.MINUTES);

        // 保存订单时间信息
        this.saveOrderTemp(upgradeParam.getPkCreator(), upgradeParam.getOrderCode(), JSONUtil.toJsonStr(saOrderExt), JSONUtil.toJsonStr(upgradeParam));

        return null;
    }

    @Override
    public String confirmOtherOrder(RetailOrderOtherParam orderParam) {
        // 购买会员
        CuMember buyMember = orderParam.getBuyMember();
        // 封装订单信息
        SaOrderExt saOrderExt = this.createSaOrder(orderParam, null, buyMember);

        // 订单缓存（60分钟有效，防止在线支付回调慢）
        redisService.setCacheObject(CacheConstants.RETAIL_TEMP_ORDER + orderParam.getPkCreator() + orderParam.getOrderCode(), saOrderExt, CacheConstants.RETAIL_ORDER_EXPIRE_TIME * 2, TimeUnit.MINUTES);
        // 缓存订单参数（30分钟有效）
        redisService.setCacheObject(CacheConstants.RETAIL_TEMP_OTHER_PARAM + orderParam.getPkCreator() + orderParam.getOrderCode(), orderParam, CacheConstants.RETAIL_ORDER_EXPIRE_TIME * 2, TimeUnit.MINUTES);

        // 保存订单时间信息
        this.saveOrderTemp(orderParam.getPkCreator(), orderParam.getOrderCode(), JSONUtil.toJsonStr(saOrderExt), JSONUtil.toJsonStr(orderParam));

        return null;
    }

    @Override
    public SaOrderExt createSaOrder(RetailOrderParam orderParam, List<BdGrade> gradeList, CuMember cuMember) {
        // 需要计算等级，以及商品价格
        if (CollectionUtil.isEmpty(gradeList)) {
            gradeList = iGradeServiceApi.getRetailGradeList().getData();
        }

        // 封装订单
        SaOrderExt saOrderExt = this.packageSaOrder(orderParam, cuMember);
        if (null != cuMember) {
            saOrderExt.setMemberCode(cuMember.getMemberCode());
            saOrderExt.setMemberName(cuMember.getMemberName());
            saOrderExt.setMemberPhone(cuMember.getPhone());
        } else {
            saOrderExt.setMemberName(orderParam.getMemberName());
            saOrderExt.setMemberPhone(orderParam.getPhone());
        }
        orderParam.setOrderCode(saOrderExt.getOrderCode());

        // 获取汇率
        CurrencyDTO currency = iCurrencyServiceApi.getCurrency(orderParam.getPkCountry()).getData();
        // 封装订单明细
        List<SaOrderItems> orderItemsList = this.createSaOrderItems(orderParam, gradeList, cuMember, saOrderExt, currency);

        // 订单金额
        BigDecimal orderAmount = BigDecimal.ZERO;
        // 订单业绩
        BigDecimal orderAchieve = BigDecimal.ZERO;
        // 订单考核业绩
        BigDecimal orderAssAchieve = BigDecimal.ZERO;

        for (SaOrderItems orderItems : orderItemsList) {
            if (EYesNo.NO.getIntValue() == orderItems.getIsGift()) {
                // 不是赠品
                orderAmount = ComputeUtil.computeAddMultiply(orderAmount, orderItems.getPrice(), orderItems.getQuantity());
                orderAchieve = ComputeUtil.computeAddMultiply(orderAchieve, orderItems.getAchievement(), orderItems.getQuantity());
                orderAssAchieve = ComputeUtil.computeAddMultiply(orderAssAchieve, orderItems.getAssAchievement(), orderItems.getQuantity());
            }
        }
        saOrderExt.setOrderItemsList(orderItemsList);
        saOrderExt.setOrderAmount(orderAmount.setScale(2, RoundingMode.HALF_UP));
        saOrderExt.setOrderAchieve(orderAchieve);
        saOrderExt.setUploadAchieve(orderAchieve);
        saOrderExt.setOrderAssAchieve(orderAssAchieve);
        saOrderExt.setPkRate(currency.getPkId());
        if (null != cuMember) {
            saOrderExt.setPkGradeOld(cuMember.getPkSettleGrade());
        }

        if (ESpecialArea.RETAIL_REGISTER.getValue() == orderParam.getSpecialArea()
                || ESpecialArea.RETAIL_UPGRADE.getValue() == orderParam.getSpecialArea()) {
            // 注册、升级订单，计算会员等级
            BdGrade newGrade = iSaOrderService.calculateGrade(orderParam.getSpecialArea(), orderParam.getOrderItemsParams(), gradeList,
                    cuMember, orderParam.getSystemType(), orderParam.getIsPay());
            if (null == newGrade) {
                throw new ServiceException("会员不满足升级条件");
            }
            saOrderExt.setPkGrade(newGrade.getPkId());
            // 会员信息
            if (null != cuMember) {
                cuMember.setPkSettleGrade(newGrade.getPkId());
                if (null == cuMember.getPkAwards()) {
                    // 新会员没有奖衔，则进行赋值
                    cuMember.setPkAwards(newGrade.getPkAwards());
                } else {
                    // 老会员奖衔如果为无，也进行赋值
                    BdAwards bdAwards = iAwardsServiceApi.getAwardsOne(cuMember.getPkAwards()).getData();
                    if (EAwards.MEMBER.getValue() == bdAwards.getAwardsValue()
                            && EAwards.MEMBER.getValue() != newGrade.getPkAwards()) {
                        cuMember.setPkAwards(newGrade.getPkAwards());
                    }
                }
            }
        }

        return saOrderExt;
    }

    @Override
    public List<SaOrderItems> createSaOrderItems(RetailOrderParam orderParam, List<BdGrade> gradeList,
                                                 CuMember cuMember, SaOrderExt saOrderExt, CurrencyDTO currency) {
        // 需要查询盒数商品
        List<String> boxWaresList = BoxProductUtil.getBoxWaresListBySystemType(null);

        // 订单明细信息
        List<SaOrderItems> orderItemsList = new ArrayList<>();
//        // 需要计算等级，以及商品价格
//        if (CollectionUtil.isEmpty(gradeList)) {
//            gradeList = iGradeServiceApi.getRetailGradeList().getData();
//        }
        if (null == currency) {
            currency = iCurrencyServiceApi.getCurrency(orderParam.getPkCountry()).getData();
        }

        // 商品+产品明细
        List<BdWaresDetailExt> waresDetailExtList = getWaresDetailExtList(ESpecialArea.getRetailSpecialAreaValue(orderParam.getSpecialArea()), orderParam.getOrderItemsParams(), cuMember, orderParam.getSystemType());
        // 包邮邮费
        BigDecimal freePostage = BigDecimal.ZERO;
        // 邮费模板map（key：邮费模板ID，value：邮费订单明细）
        Map<Integer, List<SaOrderItems>> postageMap = new HashMap<>(ComputeUtil.mapInitCapacity(waresDetailExtList.size()));

//        // 升级盒数基数
//        int baseBoxNum = 0;

//        // 当前会员等级
//        BdGrade memberGrade = null;
//        if (null != cuMember.getPkSettleGrade()) {
//            // 获取当前会员等级（注册专区，目前没有会员等级）
//            memberGrade = gradeList.stream().filter(tmpGrade -> tmpGrade.getPkId().equals(cuMember.getPkSettleGrade())).findFirst().get();
//        }
//        // 2025.02.24 升级专区取消，复购专区指定盒数商品走升级逻辑
//        if (ESpecialArea.RETAIL_REPURCHASE.getValue() == orderParam.getSpecialArea()) {
//            // 获取升级盒数基数
//            baseBoxNum = iSaOrderService.handleBaseBoxNum(cuMember, memberGrade, gradeList, boxProductList, orderParam.getSystemType());
//        }

        // 当前盒数
        BigDecimal boxNum = BigDecimal.ZERO;
        // 2025.08.02 雨汐团队，普通复购、重消品不累计盒数
        List<String> tmpBoxWaresList = new ArrayList<>();
        if (ESpecialArea.RETAIL_REPURCHASE.getValue() == orderParam.getSpecialArea()
                || ESpecialArea.RETAIL_RESCISSION.getValue() == orderParam.getSpecialArea()) {
            if (RetailConstants.VERTEX_PK_ID.equals(cuMember.getPkVertex())) {
                tmpBoxWaresList = iBdWaresService.listWaresCodeByVertex(RetailConstants.VERTEX_PK_ID);
            }
        }


//        // 2025.03.11 添加需求，复购专区、重消专区，指定盒数商品每用户每月限购50件
//        this.checkWaresLimit(cuMember.getPkId(), orderParam.getSpecialArea(), waresDetailExtList, boxProductList);

        // 设置已经累计盒数的商品列表
        List<String> tmpBoxWares = new ArrayList<>();

        // 遍历生成订单明细数据
        for (BdWaresDetailExt waresDetailExt : waresDetailExtList) {
//            // 价格KEY（商品编号 + 价格）
//            String waresGroupKey = null;
//            // 商品数量
//            int waresQuantity = 1;

            // 订单明细
            SaOrderItems orderItems = saOrderHandle.getSaOrderItems(cuMember, saOrderExt, currency, orderItemsList, waresDetailExt);
            orderItems.setWaresBoxNum(null != waresDetailExt.getBoxNum() ? waresDetailExt.getBoxNum() : BigDecimal.ZERO);

            // 不是赠品，需要累积包邮邮费
            if (EYesNo.NO.getIntValue() == waresDetailExt.getIsGift()) {
                // 包邮金额
                freePostage = freePostage.add(orderItems.getPrice().multiply(new BigDecimal(orderItems.getQuantity())));
                if (EYesNo.NO.getIntValue() == waresDetailExt.getIsFreeMail()) {
                    // 不包邮，处理邮费模板map
                    saOrderHandle.setPostageTmpMap(orderParam.getTranType(), postageMap, waresDetailExt, orderItems);
                }

                if (boxWaresList.contains(waresDetailExt.getWaresCode())) {
                    if (ESpecialArea.RETAIL_REPURCHASE.getValue() == orderParam.getSpecialArea()
                            || ESpecialArea.RETAIL_RESCISSION.getValue() == orderParam.getSpecialArea()) {
                        if (RetailConstants.VERTEX_PK_ID.equals(cuMember.getPkVertex())) {
                            // 雨汐团队，复购、重消盒数品
                            if (tmpBoxWaresList.contains(waresDetailExt.getWaresCode())) {
                                // 商品是盒数商品，需要累计数量
                                if (!tmpBoxWares.contains(waresDetailExt.getWaresCode())) {
                                    boxNum = boxNum.add(waresDetailExt.getBoxNum().multiply(new BigDecimal(orderItems.getWaresQuantity())));
                                    tmpBoxWares.add(waresDetailExt.getWaresCode());
                                }
                            }
                        } else {
                            // 商品是盒数商品，需要累计数量
                            if (!tmpBoxWares.contains(waresDetailExt.getWaresCode())) {
                                boxNum = boxNum.add(waresDetailExt.getBoxNum().multiply(new BigDecimal(orderItems.getWaresQuantity())));
                                tmpBoxWares.add(waresDetailExt.getWaresCode());
                            }
                        }
                    } else {
                        // 商品是盒数商品，需要累计数量
                        if (!tmpBoxWares.contains(waresDetailExt.getWaresCode())) {
                            boxNum = boxNum.add(waresDetailExt.getBoxNum().multiply(new BigDecimal(orderItems.getWaresQuantity())));
                            tmpBoxWares.add(waresDetailExt.getWaresCode());
                        }
                    }

                }
            }

//            if (ESpecialArea.RETAIL_REGISTER.getValue() == orderParam.getSpecialArea()
//                    && boxProductList.contains(waresDetailExt.getWaresCode())
//                    && EYesNo.NO.getIntValue() == waresDetailExt.getIsGift()) {
//                // 会员专区（注册） 并且 指定的商品不为赠品，需要根据盒数重新计算价格
//                // 指定盒数商品，价格需要按等级和购买数量重新计算
//                for (int i = 0; i < waresDetailExt.getWaresQuantity(); i++) {
//                    // 存在商品，盒数累加1
//                    boxNum += 1;
//                    // 按盒数计算当前商品实际支付金额
//                    BigDecimal price = this.getWaresPrice(baseBoxNum + boxNum, gradeList);
//
//                    if (null == waresGroupKey || !waresGroupKey.equals(waresDetailExt.getWaresCode() + "-" + price)) {
//                        // 第一盒 或者 商品 + 价格有变化，累计商品数量
//                        waresGroupKey = waresDetailExt.getWaresCode() + "-" + price;
//                        waresQuantity = 1;
//
//                        orderItems = saOrderHandle.getSaOrderItems(cuMember, saOrderExt, currency, orderItemsList, waresDetailExt);
//                        orderItems.setPrice(price);
//                    } else {
//                        waresQuantity += 1;
//                    }
//                    orderItems.setWaresQuantity(waresQuantity);
//                    orderItems.setQuantity(waresDetailExt.getQuantity() * waresQuantity);
//                }
//            } else {
//                // 非会员专区
//                if (ESpecialArea.RETAIL_REPURCHASE.getValue() == orderParam.getSpecialArea()
//                        && memberGrade.getGradeValue() < EGrade.MAKER.getValue()
//                        && boxProductList.contains(waresDetailExt.getWaresCode())
//                        && EYesNo.NO.getIntValue() == waresDetailExt.getIsGift()) {
//                    // 复购专区，如果会员等级小于V3，并且为盒数商品，需要计算等级和价格
//                    // 2025.02.24 升级专区取消，复购专区指定盒数商品可以升级，需要根据盒数商品重新计算价格
//                    for (int i = 0; i < waresDetailExt.getWaresQuantity(); i++) {
//                        // 存在商品，盒数累加1
//                        boxNum += 1;
//
//                        // 产品价格
//                        BigDecimal price = this.getWaresPrice(baseBoxNum + boxNum, gradeList);
//
//                        if (null == waresGroupKey || !waresGroupKey.equals(waresDetailExt.getWaresCode() + "-" + price)) {
//                            // 第一盒 或者 商品 + 价格有变化，累计商品数量
//                            waresGroupKey = waresDetailExt.getWaresCode() + "-" + price;
//                            waresQuantity = 1;
//
//                            orderItems = saOrderHandle.getSaOrderItems(cuMember, saOrderExt, currency, orderItemsList, waresDetailExt);
//                            orderItems.setPrice(price);
//                        } else {
//                            waresQuantity += 1;
//                        }
//                        orderItems.setWaresQuantity(waresQuantity);
//                        orderItems.setQuantity(waresDetailExt.getQuantity() * waresQuantity);
//                    }
//                } else {
//                    if ((ESpecialArea.RETAIL_REPURCHASE.getValue() == orderParam.getSpecialArea()
//                            || ESpecialArea.RETAIL_RESCISSION.getValue() == orderParam.getSpecialArea())
//                            && boxProductList.contains(waresDetailExt.getWaresCode())
//                            && EYesNo.NO.getIntValue() == waresDetailExt.getIsGift()) {
//                        // 重消也累计盒数
//                        boxNum += waresDetailExt.getWaresQuantity();
//                    }
//                    orderItems = saOrderHandle.getSaOrderItems(cuMember, saOrderExt, currency, orderItemsList, waresDetailExt);
//                }
//
//                // 不包邮 并且 不是赠品，需要累积包邮邮费
//                if (EYesNo.NO.getIntValue() == waresDetailExt.getIsGift()) {
//                    // 包邮金额
//                    freePostage = freePostage.add(orderItems.getPrice().multiply(new BigDecimal(orderItems.getQuantity())));
//                    if (EYesNo.NO.getIntValue() == waresDetailExt.getIsFreeMail()) {
//                        // 不包邮，处理邮费模板map
//                        saOrderHandle.setPostageTmpMap(orderParam.getTranType(), postageMap, waresDetailExt, orderItems);
//                    }
//                }
//            }
        }

        // 计算订单邮费
        String postageStr = iRetailOrderPostageService.calculatePostage(freePostage, postageMap, orderParam);
        if (null != postageStr) {
            throw new ServiceException(postageStr);
        }
        if (null != saOrderExt) {
            // 订单邮费
            saOrderExt.setPostage(orderParam.getPostage());
            // 订单指定商品盒数
            saOrderExt.setBoxNum(boxNum);
        }
        return orderItemsList;
    }

    /**
     * 获取商品及产品详情
     *
     * @param specialArea      所属专区
     * @param orderItemsParams 订单参数列表
     * @param cuMember         订单会员（复购使用）
     * @param systemTyype      系统类型
     * @return
     */
    private List<BdWaresDetailExt> getWaresDetailExtList(Integer specialArea, List<OrderItemsParam> orderItemsParams, CuMember cuMember, Integer systemTyype) {
        // 商品编号 + sku列表找到所有的商品信息
        List<BdWaresDetailExt> bdWaresDetailExtList = new ArrayList<>();
        if (CollectionUtil.isNotEmpty(orderItemsParams)) {
            List<BdWaresDetailExt> waresDetailExtList = iBdWaresDetailService.queryWaresDetailByCondition(specialArea, ETransportType.LAND.getValue(), orderItemsParams, systemTyype);
            // 复购处理极差
            setWaresRange(specialArea, waresDetailExtList, cuMember);
            if (waresDetailExtList.size() > 0) {
                bdWaresDetailExtList.addAll(waresDetailExtList);
            }
        }
        return bdWaresDetailExtList;
    }


    /**
     * 设置商品的极差
     *
     * @param specialArea        所属专区
     * @param waresDetailExtList 商品明细
     * @param cuMember           订单会员
     */
    private void setWaresRange(Integer specialArea, List<BdWaresDetailExt> waresDetailExtList, CuMember cuMember) {
        // 复购专区，处理极差
        if (EOrderType.RETAIL_REPURCHASE.getValue() == specialArea
                || EOrderType.RETAIL_CONSUME.getValue() == specialArea) {
            // 2024.12.30 添加需求，复消也需要处理级差
            Map<String, List<BdWaresDetailExt>> waresDetailExtMap = new HashMap<>();
            List<Integer> specsSkuList = new ArrayList<>();
            waresDetailExtList.forEach(waresDetailExt -> {
                String key = waresDetailExt.getPkId().toString() + waresDetailExt.getPkWaresSpecsSku();
                if (waresDetailExtMap.containsKey(key)) {
                    waresDetailExtMap.get(key).add(waresDetailExt);
                } else {
                    List<BdWaresDetailExt> tmpWaresDetailExtList = new ArrayList<>();
                    tmpWaresDetailExtList.add(waresDetailExt);
                    waresDetailExtMap.put(key, tmpWaresDetailExtList);
                }
                specsSkuList.add(waresDetailExt.getPkWaresSpecsSku());
            });
            int level = cuMember.getPkSettleGrade();
            int rangeType = ERangeType.GRADE.getValue();
            if (specsSkuList.size() == 0) {
                return;
            }
            List<BdWaresRange> bdWaresRangeList = iBdWaresRangeService.querySkuWaresRangeByCondition(cuMember.getPkSettleCountry(), rangeType, level, specsSkuList);
            bdWaresRangeList.forEach(bdWaresRange -> {
                String key = bdWaresRange.getPkWaresDetail().toString() + bdWaresRange.getPkWaresSpecsSku();
                if (waresDetailExtMap.containsKey(key)) {
                    for (BdWaresDetailExt bdWaresDetailExt : waresDetailExtMap.get(key)) {
                        bdWaresDetailExt.setPrice(bdWaresRange.getPrice());
                        bdWaresDetailExt.setAchieve(bdWaresRange.getAchieve());
                        bdWaresDetailExt.setAssAchieve(bdWaresRange.getAssAchieve());
                    }
                }
            });
        }
    }

    /**
     * 封装订单信息
     *
     * @param orderParam 订单入参
     * @return
     */
    private SaOrderExt packageSaOrder(RetailOrderParam orderParam, CuMember cuMember) {
        SaOrderExt saOrderExt = new SaOrderExt();
        if (StringUtils.isNotEmpty(orderParam.getOrderCode())) {
            saOrderExt.setOrderCode(orderParam.getOrderCode());
        } else {
            saOrderExt.setOrderCode(saOrderHandle.createOrderCode(EOrderPrefix.ORDER_CODE.getValue()));
        }
        saOrderExt.setOrderAmount(BigDecimal.ZERO);
        saOrderExt.setOrderAchieve(BigDecimal.ZERO);
        saOrderExt.setOrderAssAchieve(BigDecimal.ZERO);
        saOrderExt.setPkMember(cuMember.getPkId());
        saOrderExt.setOrderType(orderParam.getSpecialArea());
        saOrderExt.setPkReference(null != cuMember.getPkParent() ? cuMember.getPkParent() : orderParam.getPkParent());
        saOrderExt.setRecName(orderParam.getRecName());
        saOrderExt.setRecPhone(orderParam.getRecPhone());
        saOrderExt.setRecProvince(orderParam.getRecProvince());
        saOrderExt.setRecCity(orderParam.getRecCity());
        saOrderExt.setRecCounty(orderParam.getRecCounty());
        saOrderExt.setRecAddress(orderParam.getRecAddress());
        if (orderParam.getRecProvince() != null) {
            // 根据收货省获取仓库
            BdStorehouse bdStorehouse = iCurrencyServiceApi.queryBdStorehouseByProvince(orderParam.getRecProvince()).getData();
            if (bdStorehouse != null) {
                saOrderExt.setPkStorehouse(bdStorehouse.getPkId());
            }
        }
        saOrderExt.setRemark(orderParam.getRemark());
        saOrderExt.setTranType(orderParam.getTranType());
        saOrderExt.setDeliveryWay(EDelivery.EMPTY.getValue() != orderParam.getDeliveryWay() ? orderParam.getDeliveryWay() : EDelivery.FAST_MAIL.getValue());
        saOrderExt.setOrderStatus(EOrderStatus.WAIT_PAY.getValue());
        saOrderExt.setPkCreator(orderParam.getPkCreator());
        saOrderExt.setPkCountry(orderParam.getPkCountry());
        saOrderExt.setPkVertex(cuMember.getPkVertex());
        saOrderExt.setCreationTime(new Date());
        saOrderExt.setSystemType(orderParam.getSystemType());
        return saOrderExt;
    }

    @Override
    public CuMemberAccountExt getMemberAccount(Long pkMember) {
        CuMemberAccount memberAccount = new CuMemberAccount();
        memberAccount.setPkMember(pkMember);
        memberAccount.setPkCountry(CountryConstants.CHINA_COUNTRY);
        List<CuMemberAccountExt> list = iMemberAccountServiceApi.queryMemberAccountByCondition(memberAccount).getData();
        if (CollectionUtil.isNotEmpty(list)) {
            return list.get(0);
        }
        return null;
    }

    @Override
    public R<CuMemberAccountExt> validMemberAccount(Long pkMember, String payPwd) {
        CuMemberAccountExt cuMemberAccountExt = this.getMemberAccount(SecurityUtils.getUserId());
        if (null == cuMemberAccountExt) {
            return R.fail("会员账户异常，请联系客服处理");
        }
        // 验证密码
        if (!SecurityUtils.matchesPassword(payPwd, cuMemberAccountExt.getPayPwd())) {
            return R.fail("支付密码输入错误，请重新输入");
        }
        // 验证账户状态、钱包状态
        if (cuMemberAccountExt.getAccountStatus() != EAccountStatus.NORMAL.getValue()) {
            return R.fail("会员账户存在异常，请及时联系客服");
        }
        return R.ok(cuMemberAccountExt);
    }

    @Override
    public CuMember packageCuMember(RetailOrderParam orderParam) {
        String email = null;
        if (Validator.isEmail(orderParam.getPhone())) {
            // 邮箱
            email = orderParam.getPhone();
        }

        // 获取新的订单编号
        CuMember cuMember = CuMember.builder()
                .memberName(orderParam.getMemberName())
                .nickName(orderParam.getMemberName())
                .phone(orderParam.getPhone())
                .pkSettleCountry(CountryConstants.CHINA_COUNTRY)
                .loginPwd(SecurityUtils.encryptPassword(SysConstants.LOGIN_PASSWORD))
                .loginPassword(SysConstants.LOGIN_PASSWORD)
                .payPwd(SecurityUtils.encryptPassword(SysConstants.PAY_PASSWORD))
                .payPassword(SysConstants.PAY_PASSWORD)
                .source(orderParam.getSource() == null ? EDataSource.H5.getValue() : orderParam.getSource())
                .pkParent(orderParam.getPkParent())
                .accountStatus(EAccountStatus.NORMAL.getValue())
                .pkCenterCode(orderParam.getPkCreator())
                // 这个时候是待支付，等支付的时候变为支付，可以跟待支付公用
                .payStatus(EPayStatus.UNPAID.getValue())
                .category(ECategory.NORMAL.getValue())
                .orderSource(EOrderSource.SOLID.getValue())
                .consumeMoney(BigDecimal.ZERO)
                .consumeAchieve(BigDecimal.ZERO)
                .registerAuthority(ERegistrationAuthority.ORDINARY.getValue())
                .email(email)
                .build();
        cuMember.setCustomerType(ECustomerType.NORMAL.getValue());
        cuMember.setPkCountry(orderParam.getPkCountry());
        cuMember.setCreationTime(new Date());
        cuMember.setPkCreator(orderParam.getPkCreator());

        if (null == orderParam.getCreatorMember()) {
            orderParam.setCreatorMember(iMemberServiceApi.getRetailMember(orderParam.getPkCreator(), null).getData());
        }
        cuMember.setPkVertex(orderParam.getCreatorMember().getPkVertex());
        cuMember.setPkTeamCode(orderParam.getCreatorMember().getPkTeamCode());
        // 系统类型使用购买人的
        cuMember.setSystemType(orderParam.getCreatorMember().getSystemType());

        // 获取会员编号、会员主键
        cuMember = iMemberServiceApi.createRetailMemberIdAndCode(cuMember).getData();

        return cuMember;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public String payRegOrder(SaOrderExt saOrderExt, CuMember newMember, CuMemberAccountExt memberAccountExt) {
        // 保存订单以及明细
        this.saveOrder(saOrderExt);

        newMember.setPkRegisterGrade(newMember.getPkSettleGrade());
        newMember.setCreationTime(new Date());
        newMember.setPkCreator(saOrderExt.getPkCreator());
        newMember.setConsumeMoney(saOrderExt.getOrderAmount());
        newMember.setConsumeAchieve(saOrderExt.getOrderAchieve());
        // 有效期按20年来处理，休止期按21年
        newMember.setExpireDate(DateUtils.beforeMonthLastDate(DateUtils.addYears(newMember.getCreationTime(), 20)));
        newMember.setStopDate(DateUtils.addMonths(newMember.getExpireDate(), 12));

        // 处理会员激活
        this.handleMemberActivate(newMember, saOrderExt);
        // 保存会员信息，扣减账户等
        // 调用其他服务保存会员信息、会员注册信息
        R<?> saveResult = iMemberServiceApi.saveRetailMember(newMember, saOrderExt, memberAccountExt);
        if (!saveResult.isSuccess()) {
            log.error("新零售 payRegSaOrder 会员注册失败，{}", saveResult.getMsg());
            throw new RuntimeException("会员注册失败");
        }

        this.pushOrderMq(saOrderExt);
        return null;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public String payUpgOrder(SaOrderExt saOrderExt, CuMember updateGradeMember, CuMemberAccountExt memberAccountExt) {
        // 保存订单以及明细
        this.saveOrder(saOrderExt);

        updateGradeMember.setModifiedTime(new Date());
        updateGradeMember.setPkModified(saOrderExt.getPkCreator());

        // 处理会员激活
        this.handleMemberActivate(updateGradeMember, saOrderExt);
        // 更新会员信息，扣减账户等
        // 调用其他服务保存会员信息、会员升级信息
        R<?> saveResult = iMemberServiceApi.updateRetailMember(updateGradeMember, saOrderExt, memberAccountExt);
        if (!saveResult.isSuccess()) {
            log.error("新零售 payUpgOrder 会员升级失败，{}", saveResult.getMsg());
            throw new RuntimeException("会员升级失败");
        }

        this.pushOrderMq(saOrderExt);
        return null;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public String payOtherOrder(SaOrderExt saOrderExt, CuMember buyMember, CuMemberAccountExt memberAccountExt) {
        // 保存订单以及明细
        this.saveOrder(saOrderExt);

        // 处理会员激活
        Boolean activateFlag = this.handleMemberActivate(buyMember, saOrderExt);
        // 扣减账户等
        // 调用其他服务保存更新会员账户等
        R<?> saveResult = iMemberServiceApi.otherRetailMember(buyMember, saOrderExt, memberAccountExt, activateFlag);
        if (!saveResult.isSuccess()) {
            log.error("新零售 payOtherOrder 会员其它订单失败，{}", saveResult.getMsg());
            throw new RuntimeException("下单支付失败");
        }

        this.pushOrderMq(saOrderExt);
        return null;
    }

    /**
     * 订单推送MQ处理
     *
     * @param saOrderExt 会员订单
     */
    private void pushOrderMq(SaOrderExt saOrderExt) {
        try {
            // 同步全网产品库存
            this.allProductSync(saOrderExt);

            if (EOrderType.RETAIL_REGISTER.getValue() == saOrderExt.getOrderType()
                    || EOrderType.RETAIL_UPGRADE.getValue() == saOrderExt.getOrderType()
                    || EOrderType.RETAIL_REPURCHASE.getValue() == saOrderExt.getOrderType()
                    || EOrderType.RETAIL_CONSUME.getValue() == saOrderExt.getOrderType()
            ) {
                // 计算奖金，通过mq分发消息，异步处理
                saOrderExt.setCancelBool(Boolean.FALSE);
                log.info("新零售秒结消息，order.second.exchange：{}", saOrderExt.getOrderCode());
                rabbitTemplate.convertAndSend(RabbitMqConstants.ORDER_SECOND_EXCHANGE, RabbitMqConstants.ORDER_SECOND_KEY, saOrderExt);
            }
        } catch (Exception e) {
            log.error("新零售 pushOrderMq 推送MQ失败. order: {}", saOrderExt, e);
        }
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public String onlinePaymentCallBack(Long pkMember, String orderCode) {
        String resultStr;

        // 获取缓存订单
        SaOrderExt saOrderExt = redisService.getCacheObject(CacheConstants.RETAIL_TEMP_ORDER + pkMember + orderCode);
        if (null == saOrderExt) {
//            saOrderTemp = iSaOrderTempService.getOne(Wrappers.<SaOrderTemp>lambdaQuery()
//                    .eq(SaOrderTemp::getPkMember, pkMember)
//                    .eq(SaOrderTemp::getOrderCode, orderCode)
//            );
//            if (null != saOrderTemp) {
//                saOrderExt = JSONUtil.toBean(saOrderTemp.getTempOrder(), SaOrderExt.class);
//            } else {
            return "在线支付订单已经不存在";
//            }
        }

        // 在线支付的订单支付方式为钱包
        saOrderExt.setPayType(EOrderPayType.ONLINE.getValue());

        if (EOrderType.RETAIL_REGISTER.getValue() == saOrderExt.getOrderType()) {
            // 注册订单
            // 获取缓存参数
            RetailOrderRegisterParam registerParam = redisService.getCacheObject(CacheConstants.RETAIL_TEMP_REG_PARAM + pkMember + orderCode);
            if (null == registerParam) {
//                if (null != saOrderTemp) {
//                    registerParam = JSONUtil.toBean(saOrderTemp.getTempParam(), RetailOrderRegisterParam.class);
//                } else {
                return "在线支付注册会员不存在";
//                }
            }
            // 封装会员信息
            CuMember newMember = this.packageCuMember(registerParam);
            BdGrade tmpGrade = iSaOrderService.handleGradeEnd(null, registerParam.getPkGrade(), newMember.getPkVertex());
            newMember.setPkSettleGrade(tmpGrade.getPkId());
            newMember.setPkAwards(tmpGrade.getPkAwards());

            saOrderExt.setPkMember(newMember.getPkId());

            resultStr = this.payRegOrder(saOrderExt, newMember, null);
        } else {
            // 其它订单
            // 查询购买会员
            CuMember cuMember = iMemberServiceApi.getRetailMember(pkMember, null).getData();
            if (null == cuMember) {
                return "会员信息不存在";
            }
            if (EOrderType.RETAIL_UPGRADE.getValue() == saOrderExt.getOrderType()) {
                // 升级订单
                BdGrade tmpGrade = iSaOrderService.handleGradeEnd(null, saOrderExt.getPkGrade(), cuMember.getPkVertex());
                cuMember.setPkSettleGrade(tmpGrade.getPkId());
                resultStr = this.payUpgOrder(saOrderExt, cuMember, null);
            } else {
                // 其它订单
                resultStr = this.payOtherOrder(saOrderExt, cuMember, null);
            }
        }
        if (null == resultStr) {
            iSaOrderTempService.remove(Wrappers.<SaOrderTemp>lambdaQuery()
                    .eq(SaOrderTemp::getPkMember, pkMember)
                    .eq(SaOrderTemp::getOrderCode, orderCode)
            );
        }
        return resultStr;
    }

    /**
     * 保存订单以及明细
     *
     * @param saOrderExt 订单信息
     */
    private void saveOrder(SaOrderExt saOrderExt) {
        saOrderExt.setPayTime(new Date());
        saOrderExt.setOrderStatus(EOrderStatus.PAY.getValue());
        iSaOrderService.save(saOrderExt);

        // 订单商品拆分数据
        List<SaOrderWares> orderWaresList = new ArrayList<>();
        // 订单商品拆分map（key：商品主键，value：商品数量）
        Map<Integer, BigDecimal> orderWaresMap = new HashMap<>();
        // 订单商品价格拆分map（key：商品主键，value：商品价格）
        Map<Integer, BigDecimal> orderWaresPriceMap = new HashMap<>();
        // 商品销量数据
        List<WaresNumberParam> waresNumberList = new ArrayList<>();
        // 商品销量map（key：商品主键，value：商品数量）
        Map<Integer, Integer> waresNumberMap = new HashMap<>();
        // 商品限购列表
        List<SaOrderWaresLimit> waresLimitList = new ArrayList<>();
        // 商品限购map（key：商品主键，value：商品数量）
        Map<Integer, Integer> waresLimitMap = new HashMap<>();

        // 盒数商品
        List<String> boxProductList = BoxProductUtil.getBoxWaresListBySystemType(saOrderExt.getSystemType());

        for (SaOrderItems saOrderItems : saOrderExt.getOrderItemsList()) {
            saOrderItems.setPkOrder(saOrderExt.getPkId());
            saOrderItems.setOrderStatus(EOrderStatus.PAY.getValue());
            saOrderItems.setPkCreator(saOrderExt.getPkCreator());
            saOrderItems.setCreationTime(saOrderExt.getCreationTime());

            // 指定商品拆分 并且 非赠品
            if (boxProductList.contains(saOrderItems.getWareCode())
                    && EYesNo.NO.getIntValue() == saOrderItems.getIsGift()) {
                // 盒数 = 商品上传盒数 * 商品数量
                BigDecimal boxNum = saOrderItems.getWaresBoxNum().multiply(new BigDecimal(saOrderItems.getWaresQuantity()));
                orderWaresMap.put(saOrderItems.getPkWares(), boxNum);

                // 价格 = 产品上传价格 * 产品数量，不存在直接放入，存在则进行累加
                BigDecimal waresPrice = saOrderItems.getPrice().multiply(new BigDecimal(saOrderItems.getQuantity()));
                if (orderWaresPriceMap.containsKey(saOrderItems.getPkWares())) {
                    BigDecimal tmpWaresPrice = orderWaresPriceMap.get(saOrderItems.getPkWares());
                    orderWaresPriceMap.put(saOrderItems.getPkWares(), tmpWaresPrice.add(waresPrice));
                } else {
                    orderWaresPriceMap.put(saOrderItems.getPkWares(), waresPrice);
                }

                waresLimitMap.merge(saOrderItems.getPkWares(), saOrderItems.getWaresQuantity(), Integer::sum);
            }
            // 商品销量累计
            waresNumberMap.merge(saOrderItems.getPkWares(), saOrderItems.getWaresQuantity(), Integer::sum);
        }
        iSaOrderItemsService.saveBatch(saOrderExt.getOrderItemsList());

        if (orderWaresMap.size() > 0) {
            // 处理订单商品拆分数据
            for (Integer key : orderWaresMap.keySet()) {
                SaOrderWares saOrderWares = SaOrderWares.builder()
                        .pkOrder(saOrderExt.getPkId())
                        .pkWares(key)
                        .waresPrice(orderWaresPriceMap.get(key))
                        .waresQuantity(orderWaresMap.get(key))
                        .build();
                saOrderWares.setPkCreator(saOrderExt.getPkCreator());
                saOrderWares.setCreationTime(saOrderExt.getCreationTime());
                orderWaresList.add(saOrderWares);
            }
        }
        if (waresNumberMap.size() > 0) {
            // 处理订单商品销量、会员商品限购
            for (Integer key : waresNumberMap.keySet()) {
                waresNumberList.add(WaresNumberParam.builder()
                        .pkWares(key)
                        .number(waresNumberMap.get(key))
                        .build());
            }
        }
        if (ESpecialArea.RETAIL_REPURCHASE.getValue() == saOrderExt.getOrderType()
                || ESpecialArea.RETAIL_RESCISSION.getValue() == saOrderExt.getOrderType()) {
            if (waresLimitMap.size() > 0) {
                String limitDate = DateUtils.parseDateToFormat(DateUtils.YYYYMM, new Date());
                for (Integer key : waresLimitMap.keySet()) {
                    SaOrderWaresLimit saOrderWaresLimit = SaOrderWaresLimit.builder()
                            .pkMember(saOrderExt.getPkMember())
                            .orderType(saOrderExt.getOrderType())
                            .pkWares(key)
                            .waresQuantity(waresLimitMap.get(key))
                            .limitDate(limitDate)
                            .build();
                    saOrderWaresLimit.setPkCreator(saOrderExt.getPkCreator());
                    saOrderWaresLimit.setCreationTime(saOrderExt.getCreationTime());
                    saOrderWaresLimit.setPkCountry(saOrderExt.getPkCountry());
                    waresLimitList.add(saOrderWaresLimit);
                }
            }
        }

        if (orderWaresList.size() > 0) {
            // 订单商品拆分表
            iSaOrderWaresService.saveBatch(orderWaresList);
        }
        if (waresNumberList.size() > 0) {
            // 更新商品销量表
            iBdWaresExtendService.updateWaresSalesAccrual(waresNumberList);
        }
        if (waresLimitList.size() > 0) {
            // 更新会员商品限购
            iSaOrderWaresLimitService.saveBatchLimit(waresLimitList);
        }
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public Long savePickOrder(SaOrder saOrder, List<SaOrderItems> saOrderItemsList) {
        // 保存订单
        iSaOrderService.save(saOrder);

        for (SaOrderItems saOrderItems : saOrderItemsList) {
            saOrderItems.setPkOrder(saOrder.getPkId());
        }
        iSaOrderItemsService.saveBatch(saOrderItemsList);

        return saOrder.getPkId();
    }

    /**
     * 保存订单临时信息
     *
     * @param pkCreator 会员ID
     * @param orderCode 订单编号
     * @param tempOrder 订单信息
     * @param tempParam 参数信息
     */
    private void saveOrderTemp(Long pkCreator, String orderCode, String tempOrder, String tempParam) {
        try {
            iSaOrderTempService.save(SaOrderTemp.builder()
                    .pkMember(pkCreator)
                    .orderCode(orderCode)
                    .tempOrder(tempOrder)
                    .tempParam(tempParam)
                    .build());
        } catch (Exception e) {
            log.error("保存订单临时信息失败");
        }
    }

    /**
     * 处理会员激活
     *
     * @param cuMember
     * @param saOrder
     * @return
     */
    private Boolean handleMemberActivate(CuMember cuMember, SaOrderExt saOrder) {
        if ((null == cuMember.getIsActivate() || EYesNo.YES.getIntValue() != cuMember.getIsActivate())
                && (EOrderType.RETAIL_REGISTER.getValue() == saOrder.getOrderType() || EOrderType.RETAIL_UPGRADE.getValue() == saOrder.getOrderType())
        ) {
            // 会员激活状态为空 或 未激活，并且是 注册、升级 订单，判断会员是否激活
            if (saOrder.getBoxNum().compareTo(BigDecimal.ZERO) > 0) {
                // 存在指定盒数商品
                cuMember.setIsActivate(EYesNo.YES.getIntValue());
                if (EPayStatus.UNPAID.getValue() == cuMember.getPayStatus()) {
                    cuMember.setPayTime(saOrder.getPayTime());
                    cuMember.setPayStatus(EPayStatus.PAID.getValue());
                }
                return true;
            }
        }
        return false;

//        if (null == cuMember.getIsActivate()) {
////            // 会员激活状态为空，按未激活处理
////            cuMember.setIsActivate(EYesNo.NO.getIntValue());
//
//            // new 暂时不处理激活，只要是会员就按激活处理，有订单就有支付状态和时间
//            cuMember.setIsActivate(EYesNo.YES.getIntValue());
//            if (null != saOrder) {
//                cuMember.setPayTime(saOrder.getPayTime());
//                cuMember.setPayStatus(EPayStatus.PAID.getValue());
//            }
//        }
//        return true;

//        if (EYesNo.YES.getIntValue() != cuMember.getIsActivate()
//                && (EOrderType.RETAIL_REGISTER.getValue() == saOrder.getOrderType()
//                || EOrderType.RETAIL_UPGRADE.getValue() == saOrder.getOrderType()
//                || EOrderType.RETAIL_REPURCHASE.getValue() == saOrder.getOrderType())
//        ) {
//            // 会员未激活，并且是 注册、升级、复购 订单，判断会员是否激活
//            List<String> boxProductList = BoxProductUtil.getBoxProductListBySystemType(saOrder.getSystemType());
//
//            for (SaOrderItems saOrderItems : saOrder.getOrderItemsList()) {
//                if (boxProductList.contains(saOrderItems.getWareCode())) {
//                    // 存在指定盒数商品
//                    cuMember.setIsActivate(EYesNo.YES.getIntValue());
//                    if (EPayStatus.UNPAID.getValue() == cuMember.getPayStatus()) {
//                        cuMember.setPayTime(saOrder.getPayTime());
//                        cuMember.setPayStatus(EPayStatus.PAID.getValue());
//                    }
//                    return true;
//                }
//            }
//        }
//        return false;
    }

    /**
     * 2025.03.11 添加需求，复购专区、重消专区，指定盒数商品每用户每月限购50件
     *
     * @param pkMember           会员ID
     * @param specialArea        所属专区
     * @param waresDetailExtList 购买商品列表
     * @param boxProductList     盒数商品列表
     * @return
     */
    private void checkWaresLimit(Long pkMember, Integer specialArea, List<BdWaresDetailExt> waresDetailExtList, List<String> boxProductList) {
        String limitDate = DateUtils.parseDateToFormat(DateUtils.YYYYMM, new Date());
        // 商品数量map（key: 商品编号，value: 商品数量）
        Map<String, Integer> waresNumMap = new HashMap<>();
        // 商品信息map（key: 商品编号，value: 商品名称）
        Map<String, String> waresNameMap = new HashMap<>();
        for (BdWaresDetailExt waresDetailExt : waresDetailExtList) {
            if ((ESpecialArea.RETAIL_REPURCHASE.getValue() == specialArea
                    || ESpecialArea.RETAIL_RESCISSION.getValue() == specialArea)
                    && boxProductList.contains(waresDetailExt.getWaresCode())
                    && EYesNo.NO.getIntValue() == waresDetailExt.getIsGift()
            ) {
                // 2025.03.11 添加需求，复购专区、重消专区，指定盒数商品每月每人限购50件（赠品排除）
                waresNumMap.merge(waresDetailExt.getWaresCode(), waresDetailExt.getWaresQuantity(), Integer::sum);
                waresNameMap.put(waresDetailExt.getWaresCode(), waresDetailExt.getWaresName());
            }
        }

        if (!waresNumMap.isEmpty()) {
            for (String key : waresNumMap.keySet()) {
                if (waresNumMap.get(key) > 50) {
                    throw new ServiceException("商品【" + waresNameMap.get(key) + "】每月限购50件");
                }
            }

            List<SaOrderWaresLimitExt> limitList = iSaOrderWaresLimitService.listWaresList(pkMember, specialArea, limitDate, new ArrayList<>(waresNumMap.keySet()));
            if (CollectionUtil.isNotEmpty(limitList)) {
                for (SaOrderWaresLimitExt waresLimitExt : limitList) {
                    Integer waresNum = waresNumMap.get(waresLimitExt.getWaresCode());
                    if (null != waresNum
                            && waresNum + waresLimitExt.getWaresQuantity() > 50) {
                        throw new ServiceException("商品【" + waresLimitExt.getWaresName() + "】每月限购50件");
                    }
                }
            }
        }
    }

    /**
     * 同步全网产品库存
     *
     * @param saOrderExt
     */
    private void allProductSync(SaOrderExt saOrderExt) {
        if (EYesNo.NO.getIntValue() == BdConfig.getProductSync()) {
            return;
        }
        Map<Integer, BdProduct> productMap = iBdProductService.queryProductMap(saOrderExt.getOrderItemsList().stream().map(SaOrderItems::getPkProduct).collect(Collectors.toSet()), null);
        // 开启线程调用全网产品库存
        // 请求参数
        Map<String, Object> bodyMap = new HashMap<>();
        bodyMap.put("source", BdConfig.getSysName());
        bodyMap.put("orderCode", saOrderExt.getOrderCode());
        List<Map<String, Object>> bodyDetailList = new ArrayList<>();
        for (SaOrderItems saOrderItems : saOrderExt.getOrderItemsList()) {
            Map<String, Object> detailMap = new HashMap<>();
            detailMap.put("wmsCode", productMap.get(saOrderItems.getPkProduct()).getWmsCode());
            detailMap.put("changeNum", saOrderItems.getQuantity());
            bodyDetailList.add(detailMap);
        }
        bodyMap.put("detailList", bodyDetailList);
        // 请求头
        String header = Base64Encoder.encode(BdConfig.getSysName() + "!" + saOrderExt.getOrderCode());
        ThreadUtils.threadPoolExecutor.submit(() -> {
            log.info("同步产品,header: {}", header);
            log.info("同步产品,bodyMap: {}", bodyMap);
            HttpRequest httpRequest = HttpUtil.createPost(BdConfig.getProductSyncUrl());
            httpRequest.header("authorization", header);
            httpRequest.body(JSONUtil.toJsonStr(bodyMap));
            httpRequest.setReadTimeout(5000);
            try {
                // 同步产品接口返回
                String resultStr = httpRequest.execute().body();
                AjaxResult ajaxResult = JSONUtil.toBean(resultStr, AjaxResult.class);
                log.info("同步产品,resultStr: {}", resultStr);
                if (!ajaxResult.isSuccess()) {
                    // 同步产品失败，250毫秒后重试
                    Thread.sleep(250);
                    resultStr = httpRequest.execute().body();
                    log.info("同步产品失败重试,resultStr: {}", resultStr);
                }
            } catch (Exception e) {
                log.error("同步产品库存异常", e);
                // 保存订单产品同步记录
                iSaOrderSyncService.save(SaOrderSync.builder()
                        .orderCode(saOrderExt.getOrderCode())
                        .bodyDetail(JSONUtil.toJsonStr(bodyDetailList))
                        .build());
            }
        });
    }

}
