package cn.freemud.service.impl.mcoffee.calculation;

import cn.freemud.adapter.CouponAdapter;
import cn.freemud.base.entity.BaseResponse;
import cn.freemud.base.util.DateUtil;
import cn.freemud.entities.dto.*;
import cn.freemud.entities.dto.activity.ActivityDiscountsDto;
import cn.freemud.entities.dto.shoppingCart.ShoppingCartGoodsDto;
import cn.freemud.entities.vo.*;
import cn.freemud.enums.*;
import cn.freemud.interceptor.ServiceException;
import cn.freemud.service.CommonService;
import cn.freemud.service.impl.ItemServiceImpl;
import cn.freemud.service.impl.mcoffee.entity.*;
import cn.freemud.service.thirdparty.CouponClient;
import cn.freemud.service.thirdparty.CustomerExtendClient;
import cn.freemud.utils.BarcodeUtil;
import cn.freemud.utils.LogUtil;
import com.alibaba.fastjson.JSON;
import com.google.common.collect.Lists;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang.ObjectUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

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

/**
 * All rights Reserved, Designed By www.freemud.cn
 *
 * @version V1.0
 * @Title: CouponDiscountCalculation
 * @Package cn.freemud.service.impl.mcoffee
 * @Description:
 * @author: ping1.wu
 * @date: 2020/8/3 16:02
 * @Copyright: 2020 www.freemud.cn Inc. All rights reserved.
 * 注意：本内容仅限于上海非码科技内部传阅，禁止外泄以及用于其他的商业目
 */
@Slf4j
@Service
public class CouponDiscountCalculation {

    @Autowired
    private CustomerExtendClient customerExtendClient;
    @Autowired
    private ItemServiceImpl itemService;

    @Autowired
    private CommonService commonService;
    @Autowired
//    private CouponAvailableService couponAvailableService;
    private CouponClient mCoffeeCouponClient;
    @Autowired
    private CouponAdapter couponAdapter;
//    @Autowired
//    private RedisCache redisCache;
//    @Autowired
//    private CardBinClient cardBinClient;
    @Value("${coupon.app.id}")
    private String appid;
    @Value("${mccafe.universal.coupon.code}")
    private String mccafeUniversalCouponCode;

    private static final String mcafe = "mccafe";

    private static final String mcafePickup ="mccafe_pickup";

    /**
     * 优惠计算
     */
    public void updateShoppingCartGoodsDiscount(CouponPromotionVO couponPromotionVO, ActivityCalculationDiscountResponseDto.CalculationDiscountResult calculationDiscountResult, List<CartGoods> cartGoodsList, ShoppingCartGoodsResponseVo shoppingCartGoodsResponseVo) {

        if (calculationDiscountResult == null || CollectionUtils.isEmpty(calculationDiscountResult.getDiscounts())
                || !calculationDiscountResult.getDiscounts().stream().anyMatch(discount -> (ActivityTypeEnum.TYPE_3.getCode().equals(discount.getType())
                || (ActivityTypeEnum.TYPE_31.getCode().equals(discount.getType())) || (ActivityTypeEnum.TYPE_32.getCode().equals(discount.getType()))
                || (ActivityTypeEnum.TYPE_37.getCode().equals(discount.getType()))))){
            return;
        }
        List<ActivityCalculationDiscountResponseDto.CalculationDiscountResult.Goods> goodsList = calculationDiscountResult.getGoods();
        if (CollectionUtils.isEmpty(goodsList)) {
            return;
        }

        List<ActivityCalculationDiscountResponseDto.CalculationDiscountResult.Discount> discounts =  calculationDiscountResult.getDiscounts();
        if (CollectionUtils.isEmpty(discounts)) {
            return;
        }
        List<ActivityCalculationDiscountResponseDto.CalculationDiscountResult.Discount> couponDiscounts = discounts.stream().filter(discount -> (ActivityTypeEnum.TYPE_3.getCode().equals(discount.getType())
                || (ActivityTypeEnum.TYPE_31.getCode().equals(discount.getType())) || (ActivityTypeEnum.TYPE_32.getCode().equals(discount.getType()))
                || (ActivityTypeEnum.TYPE_37.getCode().equals(discount.getType())) )).collect(Collectors.toList());
        if (CollectionUtils.isEmpty(couponDiscounts)) {
            return;
        }
        Map<String, ActivityCalculationDiscountResponseDto.CalculationDiscountResult.Goods> goodsMap = goodsList.parallelStream()
                .collect(Collectors.toMap(ActivityCalculationDiscountResponseDto.CalculationDiscountResult.Goods::getCartGoodsUid, Function.identity(), (k1, k2) -> k1));

        // 匹配随单购悦享卡
        ActivityCalculationDiscountResponseDto.CalculationDiscountResult.Discount discountMon = couponDiscounts.stream().filter(t -> (
                (ActivityTypeEnum.TYPE_32.getCode().equals(t.getType()) || ActivityTypeEnum.TYPE_37.getCode().equals(t.getType()))
                                                && mccafeUniversalCouponCode.equals(t.getActivityCode()))).findFirst().orElse(null);

        for (CartGoods cartGoods : cartGoodsList) {
            //商品券商品
            if (StringUtils.isNotBlank(cartGoods.getCouponCode()) || discountMon != null) {
//                String couponCode = cartGoods.getCouponCode();
//                // 计算优惠信息时，已将券校验过，放在map中
//                Map<String, CheckSpqInfoResponseDto> validCouponMap = calculationDiscountResult.getValidCouponMap();
//                if (MapUtils.isNotEmpty(validCouponMap)) {
//                    CheckSpqInfoResponseDto checkSpqInfoResponseDto = validCouponMap.get(couponCode);
//                    if (checkSpqInfoResponseDto != null) {
//                        cartGoods.setOriginalPrice(checkSpqInfoResponseDto.getPrice());
//                        cartGoods.setSpuId(checkSpqInfoResponseDto.getSpuId());
//                        cartGoods.setSkuId(StringUtils.isEmpty(checkSpqInfoResponseDto.getSkuId()) ? checkSpqInfoResponseDto.getSpuId() : checkSpqInfoResponseDto.getSkuId());
//                        cartGoods.setQty(1);
//                        cartGoods.setCouponCode(checkSpqInfoResponseDto.getCouponCode());
//                        cartGoods.setSpuName(checkSpqInfoResponseDto.getSpuName());
//                    }
//                }
                if (goodsMap.get(cartGoods.getCartGoodsUid()) == null) {
                    continue;
                }
                cartGoods.setAmount(goodsMap.get(cartGoods.getCartGoodsUid()).getRealAmount());
                List<ActivityCalculationDiscountResponseDto.CalculationDiscountResult.Goods.GoodsDiscount> discountsNew = goodsMap.get(cartGoods.getCartGoodsUid()).getDiscounts();
                if (CollectionUtils.isNotEmpty(discountsNew)) {
                    log.info("couponDiscountCalculation:{}",discountsNew);
                    Optional<ActivityCalculationDiscountResponseDto.CalculationDiscountResult.Goods.GoodsDiscount> targetDiscount =  discountsNew.stream()
                            .filter(t -> ActivityTypeEnum.TYPE_32.getCode().equals(t.getType()) || ActivityTypeEnum.TYPE_37.getCode().equals(t.getType())).findFirst();
                    if (null != targetDiscount && targetDiscount.isPresent()) {
                        ActivityCalculationDiscountResponseDto.CalculationDiscountResult.Goods.GoodsDiscount goodsDiscount = targetDiscount.get();
                        cartGoods.setCouponName(goodsDiscount.getActivityName());
                    }
                }
            }
        }

        // 当couponCode不为空时，需计算优惠价格
        long couponDiscount = 0;
        if (CollectionUtils.isNotEmpty(couponDiscounts)) {
            for (ActivityCalculationDiscountResponseDto.CalculationDiscountResult.Discount couponResults : couponDiscounts) {
                couponDiscount = couponDiscount + (couponResults.getDiscount() == null ? 0 : couponResults.getDiscount());
            }
        }
        shoppingCartGoodsResponseVo.setCouponDiscount(couponDiscount);
    }

    /**
     * 商品均摊
     */
    public void updateShoppingCartGoodsApportion(ActivityCalculationDiscountResponseDto.CalculationDiscountResult calculationDiscountResult, ShoppingCartGoodsDto shoppingCartGoodsDto, ShoppingCartInfoRequestVo shoppingCartInfoRequestVo){

        if (calculationDiscountResult == null || CollectionUtils.isEmpty(calculationDiscountResult.getDiscounts())
                || !calculationDiscountResult.getDiscounts().stream().anyMatch(discount -> (ActivityTypeEnum.TYPE_3.getCode().equals(discount.getType())
                ||(ActivityTypeEnum.TYPE_31.getCode().equals(discount.getType())) || (ActivityTypeEnum.TYPE_32.getCode().equals(discount.getType()))
                ||(ActivityTypeEnum.TYPE_37.getCode().equals(discount.getType()))))) {
            return;
        }
        Map<String, Integer> extendTypeMap = calculationDiscountResult.getDiscounts().stream().filter(o -> o.getExtendType() != null)
                .collect(Collectors.toMap(ActivityCalculationDiscountResponseDto.CalculationDiscountResult.Discount::getActivityCode
                        , ActivityCalculationDiscountResponseDto.CalculationDiscountResult.Discount::getExtendType, (k1, k2) -> k1));
        //订单级别券优惠
        List<ActivityCalculationDiscountResponseDto.CalculationDiscountResult.Discount> couponDiscounts =  calculationDiscountResult.getDiscounts();
        if (CollectionUtils.isEmpty(couponDiscounts)) {
            return;
        }
        List<ActivityCalculationDiscountResponseDto.CalculationDiscountResult.Discount> discounts = couponDiscounts.stream().filter(discount -> (ActivityTypeEnum.TYPE_3.getCode().equals(discount.getType())
                || (ActivityTypeEnum.TYPE_31.getCode().equals(discount.getType())) || (ActivityTypeEnum.TYPE_32.getCode().equals(discount.getType())
                || (ActivityTypeEnum.TYPE_37.getCode().equals(discount.getType()))))).collect(Collectors.toList());
        if (CollectionUtils.isEmpty(discounts)) {
            return;
        }

        List<ActivityCalculationDiscountResponseDto.CalculationDiscountResult.Goods> goodsList = calculationDiscountResult.getGoods();
        if (CollectionUtils.isEmpty(goodsList)) {
            return;
        }

        Map<String, ActivityCalculationDiscountResponseDto.CalculationDiscountResult.Goods> goodsMap = goodsList.parallelStream()
                .collect(Collectors.toMap(ActivityCalculationDiscountResponseDto.CalculationDiscountResult.Goods::getCartGoodsUid, Function.identity(), (k1, k2) -> k1));

        // 匹配随单购悦享卡
        ActivityCalculationDiscountResponseDto.CalculationDiscountResult.Discount discount = discounts.stream().filter(t -> (ActivityTypeEnum.TYPE_32.getCode().equals(t.getType()) && mccafeUniversalCouponCode.equals(t.getActivityCode()))).findFirst().orElse(null);
        for (ShoppingCartGoodsDto.CartGoodsDetailDto cartGoods : shoppingCartGoodsDto.getProducts()) {
            if (goodsMap.get(cartGoods.getCartGoodsUid()) == null) {
                continue;
            }
            //商品券商品
            if (StringUtils.isNotEmpty(cartGoods.getCouponCode()) ) {
                List<ActivityDiscountsDto> productActivityDiscountsDtos = new ArrayList<>();
                if(CollectionUtils.isNotEmpty(calculationDiscountResult.getGoods())) {
                    for(ActivityCalculationDiscountResponseDto.CalculationDiscountResult.Goods goods : calculationDiscountResult.getGoods()) {
                        if(cartGoods.getCartGoodsUid().equals(goods.getCartGoodsUid())) {
                            List<ActivityCalculationDiscountResponseDto.CalculationDiscountResult.Goods.GoodsDiscount> goodsDiscounts = goods.getDiscounts();
                            if(CollectionUtils.isNotEmpty(goodsDiscounts)) {
                                for(ActivityCalculationDiscountResponseDto.CalculationDiscountResult.Goods.GoodsDiscount goodsDiscount : goodsDiscounts) {
                                    if(goodsDiscount.getActivityCode().equals(cartGoods.getCouponCode())) {
                                        ActivityDiscountsDto activityDiscountsDto = new ActivityDiscountsDto();
                                        activityDiscountsDto.setActivityCode(goodsDiscount.getActivityCode());
                                        activityDiscountsDto.setActivityName(goodsDiscount.getActivityName());
                                        activityDiscountsDto.setActivityType(goodsDiscount.getType());
                                        activityDiscountsDto.setDiscountAmount(0 - goodsDiscount.getDiscount().intValue());
                                        Integer extendType = extendTypeMap.get(goodsDiscount.getActivityCode());
                                        activityDiscountsDto.setExtendType(extendType);
                                        activityDiscountsDto.setTenderId(goodsMap.get(cartGoods.getCartGoodsUid()).getArtNo());
                                        productActivityDiscountsDtos.add(activityDiscountsDto);
                                        cartGoods.setTotalDiscountAmount(-activityDiscountsDto.getDiscountAmount());
                                        cartGoods.setSalePrice(0L);
                                        cartGoods.setActivityDiscountsDtos(productActivityDiscountsDtos);
                                    }
                                }
                            }
                        }
                    }
                }
            }
            cartGoods.setCustomerCode(goodsMap.get(cartGoods.getCartGoodsUid()).getArtNo());
            List<ActivityCalculationDiscountResponseDto.CalculationDiscountResult.Goods.GoodsDiscount> list = goodsMap.get(cartGoods.getCartGoodsUid()).getDiscounts();
            if (CollectionUtils.isNotEmpty(list) && discount != null) {
                ActivityCalculationDiscountResponseDto.CalculationDiscountResult.Goods.GoodsDiscount goodsDiscount = list.stream()
                        .filter(t -> ((ActivityTypeEnum.TYPE_32.getCode().equals(t.getType()) || ActivityTypeEnum.TYPE_37.getCode().equals(t.getType())) && mccafeUniversalCouponCode.equals(t.getActivityCode()))).findFirst().orElse(null);
                if (goodsDiscount != null){
                    List<ActivityDiscountsDto> productActivityDiscountsDtos = new ArrayList<>();
                    ActivityDiscountsDto activityDiscountsDto = new ActivityDiscountsDto();
                    activityDiscountsDto.setActivityCode(goodsDiscount.getActivityCode());
                    activityDiscountsDto.setActivityName(goodsDiscount.getActivityName());
                    activityDiscountsDto.setActivityType(goodsDiscount.getType());
                    activityDiscountsDto.setDiscountAmount(0 - goodsDiscount.getDiscount().intValue());
                    activityDiscountsDto.setExtendType(discount.getExtendType());
                    activityDiscountsDto.setTenderId(goodsMap.get(cartGoods.getCartGoodsUid()).getArtNo());
                    productActivityDiscountsDtos.add(activityDiscountsDto);
                    cartGoods.setTotalDiscountAmount(-activityDiscountsDto.getDiscountAmount());
                    cartGoods.setSalePrice(0L);
                    cartGoods.setActivityDiscountsDtos(productActivityDiscountsDtos);
                }
            }
        }

    }

//    public void  getCoupon(CouponPromotionVO couponPromotionVO, ActivityCalculationDiscountResponseDto.CalculationDiscountResult calculationDiscountResult, List<CartGoods> cartGoodsList, ShoppingCartGoodsResponseVo shoppingCartGoodsResponseVo){
//        // 用户选择了查询优惠券信息
//        if (couponPromotionVO != null && ObjectUtils.equals(CouponFlag.YES.getCode(), couponPromotionVO.getFlg())) {
//            // 构建可用不可用优惠券
//            ActivityClassifyCouponBean activityClassifyCouponBean = availableCoupon(couponPromotionVO.getPartnerId()
//                    , couponPromotionVO.getUserId(), couponPromotionVO.getStoreId(), couponPromotionVO.getCouponCode(), couponPromotionVO.getOrderType(),cartGoodsList);
//            if (Objects.equals(activityClassifyCouponBean, null)) {
//                // 构建一个空得订单券信息
//                activityClassifyCouponBean = new ActivityClassifyCouponBean();
//                activityClassifyCouponBean.setCouponNum(0);
//                activityClassifyCouponBean.setDisableCouponNum(0);
//                activityClassifyCouponBean.setUsableCouponNum(0);
//                activityClassifyCouponBean.setDisableCoupons(Lists.newArrayList());
//                activityClassifyCouponBean.setUsableCoupons(Lists.newArrayList());
//            }
//            shoppingCartGoodsResponseVo.setAvailableCoupon(activityClassifyCouponBean);
//        }
//    }




    /**
     * 可选优惠券
     */
    public ActivityClassifyCouponBean availableCoupon( String partnerId, String storeId,String userId,String thirdPartyMemberId,String unionId,String appId
            ,  String couponCode, Integer orderType,String menuType,List<CartGoods> cartGoods) {

//        List<CartGoods> tmpCartGoods = cartGoods.parallelStream().filter(k -> StringUtils.isBlank(k.getCouponCode())).collect(Collectors.toList());
        boolean hasGoodsCoupon = false;

        // 获取会员服务的优惠券列表
        GetMemberCouponListRequestDto getMemberCouponListRequestDto = new GetMemberCouponListRequestDto(partnerId, userId);
        getMemberCouponListRequestDto.setStatusFlags(Arrays.asList(CouponStatus.STATUS_0.getCode()));
        getMemberCouponListRequestDto.setAppId(appId);
        getMemberCouponListRequestDto.setThirdPartyMemberId(thirdPartyMemberId);
        getMemberCouponListRequestDto.setUnionId(unionId);
        String tag = mcafe;
        if("saas".equals(menuType)){
            tag = mcafePickup;
        }
        getMemberCouponListRequestDto.setTag(tag);
        getMemberCouponListRequestDto.setPageNum(1);
        getMemberCouponListRequestDto.setPageSize(Integer.MAX_VALUE);
        GetMemberCouponListResponseDto.Result result = getMemberCouponList(getMemberCouponListRequestDto);
        LogUtil.debug("CouponServiceImpl_getMemberCouponList", JSON.toJSONString(getMemberCouponListRequestDto), JSON.toJSONString(result));
        if (result == null || CollectionUtils.isEmpty(result.getMemberCoupons())) {
            return null;
        }
        ActivityClassifyCouponBean availableCouponResponseVo = new ActivityClassifyCouponBean();
        // 可用优惠券信息
        List<ActivityCouponBean> usableCoupons = Lists.newArrayList();
        // 不可用优惠券信息
        List<ActivityCouponBean> disableCoupons = Lists.newArrayList();

        List<GetMemberCouponListResponseDto.Result.MemberCoupon> memberCoupons = result.getMemberCoupons();
        // 对象转map
        Map<String, GetMemberCouponListResponseDto.Result.MemberCoupon> memberCouponMap = memberCoupons.stream().collect(
                Collectors.toMap(GetMemberCouponListResponseDto.Result.MemberCoupon::getCouponCode, a -> a, (k1, k2) -> k1));
        List<String> couponCodes = Lists.newArrayList();
        memberCoupons.forEach(memberCoupon -> {
            // 只筛选小于结束时间得券
//            if (Objects.equals(memberCoupon.getCouponType(), CouponTypeEnum.TYPE_1.getCode())
//                    || Objects.equals(memberCoupon.getCouponType(), CouponTypeEnum.TYPE_3.getCode())) {
            // 只筛选小于结束时间得券
//            if (System.currentTimeMillis() < DateUtil.convert2Date(memberCoupon.getEndTime(), DateUtil.FORMAT_YYYY_MM_DD_HHMMSS).getTime()) {
//
//            }
            couponCodes.add(memberCoupon.getCouponCode());
//            }
        });

        //券码可用券校验
        List<CouponState> couponStateList = buildAvailableCoupons(cartGoods, partnerId, storeId, couponCodes);
        if (CollectionUtils.isEmpty(couponStateList)) {
            return null;
        }
        return returnSuccessAvailableCoupons(couponCode, hasGoodsCoupon, availableCouponResponseVo, usableCoupons, disableCoupons, memberCouponMap, couponStateList);
    }

    /**
     * 获取会员服务的优惠券列表
     *
     * @param getMemberCouponListRequestDto
     * @return
     */
    public GetMemberCouponListResponseDto.Result getMemberCouponList(GetMemberCouponListRequestDto getMemberCouponListRequestDto) {
        GetMemberCouponListResponseDto getMemberCouponListResponseDto = null;
        try {
            getMemberCouponListResponseDto = customerExtendClient.getMemberCouponListRequestDto(getMemberCouponListRequestDto);
        } catch (Exception ex) {
            log.error("会员服务优惠券接口调用失败", ex);
        }
        if (getMemberCouponListResponseDto != null && Objects.equals(getMemberCouponListResponseDto.getCode(), ResponseResult.SUCCESS.getCode())) {
            return getMemberCouponListResponseDto.getData();
        }
        return null;
    }




    public List<CouponState> buildAvailableCoupons(List<CartGoods> cartGoods, String partnerId, String storeId, List<String> couponCodes) {
//        String appSecret = getAppSecret(partnerId);
        // 最大15一提交
        int maxNum = 15;
        int count = couponCodes.size() / maxNum;
        // 计算可用不可用券
        CouponAvailableReq couponAvailableReqVo = new CouponAvailableReq();
        couponAvailableReqVo.setProviderId(partnerId);
        couponAvailableReqVo.setMerchantId(partnerId);
        couponAvailableReqVo.setStoreId(storeId);
        List<CouponProductVo> productList = Lists.newArrayList();
        // 优惠券优先级最高，根据商品原价做计算
        // 订单金额
        Long totalAmount = createProductRequest(cartGoods, productList);
        couponAvailableReqVo.setTotalAmount(Integer.valueOf(totalAmount + ""));
        couponAvailableReqVo.setProductList(productList);
        couponAvailableReqVo.setChannelCodeList(commonService.getOrgCodes(partnerId, storeId));
        couponAvailableReqVo.setStationId("999");
        couponAvailableReqVo.setOperatorId("999");
        // 构建计算SDK
        List<CouponState> couponStateList = Lists.newArrayList();
        for (int j = 0; j <= count; j++) {
            List calCouponCodes = null;
            if (j == count) {
                calCouponCodes = couponCodes.subList(maxNum * j, couponCodes.size());
            } else {
                calCouponCodes = couponCodes.subList(maxNum * j, maxNum * j + maxNum);
            }
            // 书写逻辑
            couponAvailableReqVo.setCouponCodes(calCouponCodes);
            CouponAvailableResp couponsAvailable = null;
            try {
                couponsAvailable = mCoffeeCouponClient.getCouponsAvailable(couponAvailableReqVo);
            }catch (Exception e){
                LogUtil.error("getCouponsAvailable_error",JSON.toJSONString(couponAvailableReqVo),null,e);
                throw new ServiceException(ResponseResult.SYSTEM_BUSINESS_ERROR);
            }
            LogUtil.info("getCouponsAvailable", JSON.toJSONString(couponAvailableReqVo), JSON.toJSONString(couponsAvailable));
            if (couponsAvailable != null && Objects.equals(String.valueOf(couponsAvailable.getResult()), ResponseResult.SUCCESS.getCode())
                    && CollectionUtils.isNotEmpty(couponsAvailable.getCouponStateList())) {
                couponStateList.addAll(couponsAvailable.getCouponStateList());
            }
        }
        return couponStateList;
    }


    public List<CouponAvailableReqByCart> buildAvailableCoupons(QueryCartInfoRequestVo requestVo, List<CartGoods> cartGoods,
                                                                List<String> couponCodes){
        List<CouponAvailableReqByCart> resList = new ArrayList<>();
        CouponAvailableReqByCart res = new CouponAvailableReqByCart();
        res.setProviderId(requestVo.getPartnerId());
        res.setMerchantId(requestVo.getPartnerId());
        res.setStoreId(requestVo.getStoreId());
        res.setChannelIdList(commonService.getOrgCodes(requestVo.getPartnerId(), requestVo.getStoreId()));
        res.setCouponCodes(couponCodes);
        List<CouponProductVo> calculAmountProductList = new ArrayList<>();
        if (CollectionUtils.isNotEmpty(cartGoods)) {
            cartGoods.forEach(cartGood -> {
                if (StringUtils.isNotEmpty(cartGood.getCouponCode())) {
                    CouponProductVo couponProductVo = new CouponProductVo();
                    couponProductVo.setAmount(cartGood.getAmount().intValue());
                    couponProductVo.setKeyProductCode(cartGood.getCustomerCode());
                    couponProductVo.setQuantity(cartGood.getQty());
                    couponProductVo.setProductId(cartGood.getGoodsId());
                    couponProductVo.setCouponCode(cartGood.getCouponCode());
                    couponProductVo.setCategoryCode(null);
                    calculAmountProductList.add(couponProductVo);
                }

//                if (CollectionUtils.isNotEmpty(cartGood.getProductComboList())) {
//                    cartGood.getProductComboList().forEach(comboxGoods -> {
//                        CouponProductVo couponProductVo = new CouponProductVo();
//                        couponProductVo.setAmount(Integer.valueOf(comboxGoods.getAmount().toString()));
//                        couponProductVo.setKeyProductCode(comboxGoods.getCustomerCode());
//                        couponProductVo.setQuantity(comboxGoods.getQty());
//                        couponProductVo.setProductId(comboxGoods.getGoodsId());
//                        couponProductVo.setCategoryCode(null);
//                        productList.add(couponProductVo);
//                    });
//                }
//                if (CollectionUtils.isNotEmpty(cartGood.getProductGroupList())) {
//                    cartGood.getProductGroupList().forEach(comboxGoods -> {
//                        CouponProductVo couponProductVo = new CouponProductVo();
//                        couponProductVo.setAmount(Integer.valueOf(comboxGoods.getAmount().toString()));
//                        couponProductVo.setKeyProductCode(comboxGoods.getCustomerCode());
//                        couponProductVo.setQuantity(comboxGoods.getQty());
//                        couponProductVo.setProductId(comboxGoods.getGoodsId());
//                        couponProductVo.setCategoryCode(null);
//                        productList.add(couponProductVo);
//                    });
//                }
            });
            res.setProductList(cartGoods);
            Long totalAmount = createProductRequest(cartGoods, calculAmountProductList);
            res.setTotalAmount(Integer.valueOf(totalAmount.toString()));
            resList.add(res);
        }
        return resList;
    }


//    public String getAppSecret(String partnerId) {
//        // 获取券服务的密钥
//        String couponAppSecret = RedisUtil.getCouponAppSecret(partnerId);
//        String appSecret = redisCache.getValue(couponAppSecret);
//        if (StringUtils.isBlank(appSecret)) {
//            GetAppKeyResponseDto getAppKeyResponseDto = cardBinClient.getAppKey(GetAppKeyRequestDto.builder().partnerId(partnerId).appId(appid).build());
//            if (Objects.equals(String.valueOf(getAppKeyResponseDto.getStatusCode()), ResponseResult.SUCCESS.getCode())
//                    && getAppKeyResponseDto.getData() != null) {
//                redisCache.save(couponAppSecret, getAppKeyResponseDto.getData().getAppKey());
//                appSecret = getAppKeyResponseDto.getData().getAppKey();
//            }
//        }
//        return appSecret;
//    }

    private ActivityClassifyCouponBean returnEmptyAvailableCoupon(ActivityClassifyCouponBean availableCouponResponseVo, List<ActivityCouponBean> usableCoupons, List<ActivityCouponBean> disableCoupons) {
        availableCouponResponseVo.setCouponNum(0);
        availableCouponResponseVo.setDisableCouponNum(0);
        availableCouponResponseVo.setUsableCouponNum(0);
        availableCouponResponseVo.setDisableCoupons(disableCoupons);
        availableCouponResponseVo.setUsableCoupons(usableCoupons);
        return availableCouponResponseVo;
    }

    private Long createProductRequest(List<CartGoods> cartGoods, List<CouponProductVo> productList) {
        Long totalAmount = 0L;
        try {
            for (CartGoods cartGood : cartGoods) {
                // 剔除商品券得商品
                if (StringUtils.isEmpty(cartGood.getSkuId()) && StringUtils.isEmpty(cartGood.getSpuId())) {
                    continue;
                }
                // 剔除商品券得商品,麦咖啡逻辑
                if(StringUtils.isNotEmpty(cartGood.getCouponCode())){
                    continue;
                }
                CouponProductVo product = new CouponProductVo();
                product.setKeyProductCode(cartGood.getCustomerCode());
                product.setProductId(StringUtils.isEmpty(cartGood.getSkuId()) ? cartGood.getSpuId() : cartGood.getSkuId());
                // 普通商品价格
                product.setAmount(Integer.valueOf(cartGood.getOriginalPrice() + ""));
                // 套餐价格为： 套餐价+可选商品总价格
                if (ObjectUtils.equals(GoodsTypeEnum.SET_MEAL_GOODS.getGoodsType(), cartGood.getGoodsType())) {
                    Long finalPrice = cartGood.getFinalPrice();
                    if (CollectionUtils.isNotEmpty(cartGood.getProductGroupList())){
                        finalPrice+=cartGood.getProductGroupList().stream().mapToLong(group->group.getFinalPrice()*group.getQty()).sum();
                    }
                    product.setAmount(finalPrice.intValue());
                }
                // 数量
                product.setQuantity(cartGood.getQty());
                productList.add(product);
                totalAmount = totalAmount +product.getAmount() * cartGood.getQty();
            }
        } catch (NumberFormatException e) {
            throw new ServiceException(ResponseResult.SYSTEM_ERROR);
        }
        return totalAmount;
    }



    private ActivityClassifyCouponBean returnSuccessAvailableCoupons(String couponCode, boolean hasGoodsCoupon, ActivityClassifyCouponBean availableCouponResponseVo, List<ActivityCouponBean> usableCoupons, List<ActivityCouponBean> disableCoupons, Map<String, GetMemberCouponListResponseDto.Result.MemberCoupon> memberCouponMap, List<CouponState> couponStateList) {
        int disableCouponNum = 0;
        int usableCouponNum = 0;
        for (CouponState couponStateVo : couponStateList) {
            // 过滤0得代金券
            if (Objects.equals(couponStateVo.getType(), CouponTypeEnum.TYPE_0.getCode())) {
                continue;
            }
            // 过滤0得代金券
//            if (Objects.equals(couponStateVo.getType(), CouponTypeEnum.TYPE_1.getCode())
//                    && (couponStateVo.getOriginalPrice() == null
//                    || couponStateVo.getOriginalPrice() == 0)) {
//                continue;
//            }

            // 过滤折扣券为0折得
            if (Objects.equals(couponStateVo.getType(), CouponTypeEnum.TYPE_3.getCode())
                    && (couponStateVo.getDiscount() == null
                    || couponStateVo.getDiscount() == 0)) {
                continue;
            }
            ActivityCouponBean activityCouponBean = new ActivityCouponBean();
            activityCouponBean.setCouponCode(couponStateVo.getCouponCode());
            GetMemberCouponListResponseDto.Result.MemberCoupon memberCoupon = memberCouponMap.get(couponStateVo.getCouponCode());
            if (memberCoupon != null) {
                activityCouponBean.setCouponName(couponStateVo.getActName());
                activityCouponBean.setEndTime(memberCoupon.getEndTime());
                if (Objects.equals(couponStateVo.getType(), CouponTypeEnum.TYPE_1.getCode())) {
                    activityCouponBean.setDiscountAmount(couponStateVo.getOriginalPrice() + "");
                } else if (Objects.equals(couponStateVo.getType(), CouponTypeEnum.TYPE_3.getCode())) {
                    // TODO 折扣券 百分值10 乘以10   前端统一除以100作为操作依据
                    activityCouponBean.setDiscountAmount(couponStateVo.getDiscount() != null ? couponStateVo.getDiscount() * 10 + "" : "0");
                }
                activityCouponBean.setThresholdDesc("满" + formatAmount((couponStateVo.getMinAmount() == null ? 0 : couponStateVo.getMinAmount()) * 1.00 / 100) + "元可用");
                activityCouponBean.setActivityDesc("不与其他活动优惠同时享受。");
                activityCouponBean.setUnusedReason(couponStateVo.getMessage());
                // 详情
                GetMemberCouponListResponseVo.MemberCouponDetail detail = new GetMemberCouponListResponseVo.MemberCouponDetail();
                detail.setActivityName(memberCoupon.getCouponName());
                String startDate = "";
                String endDate = "";
                if(couponStateVo.getValidStartTime() != null && couponStateVo.getValidEndTime() != null){
                    startDate = DateUtil.convert2String(DateUtil.convert2Date(couponStateVo.getValidStartTime(), DateUtil.FORMAT_yyyyMMdd_date), DateUtil.FORMAT_yyyyMMdd_date).replace("-", ".");
                    endDate = DateUtil.convert2String(DateUtil.convert2Date(couponStateVo.getValidEndTime(), DateUtil.FORMAT_yyyyMMdd_date), DateUtil.FORMAT_yyyyMMdd_date).replace("-", ".");
                }
                detail.setEndTime(endDate);
                detail.setStartTime(startDate);
                String remark = couponStateVo.getActDesc();
                detail.setRemark(couponAdapter.getDescribeText(remark));
                if (Objects.equals(couponStateVo.getType(), CouponTypeEnum.TYPE_1.getCode())) {
                    detail.setAmountDesc("满" + formatAmount((couponStateVo.getMinAmount() == null ? 0 : couponStateVo.getMinAmount()) * 1.00 / 100) + "元减" + formatAmount((couponStateVo.getOriginalPrice() == null ? 0 : couponStateVo.getOriginalPrice()) * 1.00 / 100) + "元");
                } else if (Objects.equals(couponStateVo.getType(), CouponTypeEnum.TYPE_3.getCode())) {
                    detail.setAmountDesc("满" + formatAmount((couponStateVo.getMinAmount() == null ? 0 : couponStateVo.getMinAmount()) * 1.00 / 100) + "元" + formatAmount((couponStateVo.getDiscount() == null ? 0 : couponStateVo.getDiscount()) * 1.00 / 10) + "折");
                }
                // 设置条形码
                GetCouponBarCodeResponseVo responseVo = this.getCouponBarCode(couponStateVo.getCouponCode());
                detail.setCouponBarCode(responseVo.getBase64Image());

                activityCouponBean.setDetail(detail);
                activityCouponBean.setActivityCode(memberCoupon.getActivityCode());
                activityCouponBean.setCouponType(couponStateVo.getType());
                activityCouponBean.setDateDescribe(couponAdapter.getDateDescribe(memberCoupon.getStatus(), memberCoupon.getStartTime(), memberCoupon.getEndTime()));
                // 券状态 0 可用 1 不可用 2 限制可用
                if (Objects.equals(activityCouponBean.getCouponCode(), couponCode)) {
                    activityCouponBean.setSelected(YesOrNoEnum.YES.getCode());
                } else {
                    activityCouponBean.setSelected(YesOrNoEnum.NO.getCode());
                }
                if (hasGoodsCoupon) {
                    disableCoupons.add(activityCouponBean);
                    disableCouponNum = disableCouponNum + 1;
                } else {
                    if (Objects.equals(couponStateVo.getState(), CouponStateEnum.STATE_1.getCode())) {
                        disableCoupons.add(activityCouponBean);
                        disableCouponNum = disableCouponNum + 1;
                    } else {
                        usableCoupons.add(activityCouponBean);
                        usableCouponNum = usableCouponNum + 1;
                    }
                }
            }

        }
        availableCouponResponseVo.setCouponNum(disableCouponNum + usableCouponNum);
        availableCouponResponseVo.setDisableCouponNum(disableCouponNum);
        availableCouponResponseVo.setUsableCouponNum(usableCouponNum);
        availableCouponResponseVo.setDisableCoupons(disableCoupons);
        availableCouponResponseVo.setUsableCoupons(usableCoupons);
        return availableCouponResponseVo;
    }

    public GetCouponBarCodeResponseVo getCouponBarCode(String couponCode) {
        byte[] bytes = BarcodeUtil.generateBarCode128(couponCode, 6D, null, true, false);
        String base64Image = Base64.getEncoder().encodeToString(bytes);
        String base64Prefix = "data:image/png;base64,";
        GetCouponBarCodeResponseVo getCouponBarCodeResponseVo = new GetCouponBarCodeResponseVo();
        getCouponBarCodeResponseVo.setBase64Image(base64Prefix + base64Image);
        return getCouponBarCodeResponseVo;
    }

    private static Object formatAmount(Double amount) {
        if (amount % (amount.intValue()) == 0.0) {
            return amount.intValue();
        } else {
            return amount;
        }
    }

    public CouponPromotionVO getCouponPromotionVO(String partnerId, String storeId, String userId, String couponCode, Integer orderType){
        CouponPromotionVO couponPromotionVO = new CouponPromotionVO();
        couponPromotionVO.setPartnerId(partnerId);
        couponPromotionVO.setUserId(userId);
        couponPromotionVO.setStoreId(storeId);
        couponPromotionVO.setCouponCode(couponCode);
        couponPromotionVO.setOrderType(orderType);
        couponPromotionVO.setFlg(CouponFlag.YES.getCode());
        return couponPromotionVO;
    }

}
