
package com.tmerclub.cloud.coupon.feign;

import cn.hutool.core.collection.CollectionUtil;
import com.tmerclub.cloud.api.coupon.dto.LockCouponDTO;
import com.tmerclub.cloud.api.coupon.dto.PlatformChooseCouponDTO;
import com.tmerclub.cloud.api.coupon.feign.CouponOrderFeignClient;
import com.tmerclub.cloud.common.constant.Constant;
import com.tmerclub.cloud.common.order.vo.*;
import com.tmerclub.cloud.common.response.ServerResponseEntity;
import com.tmerclub.cloud.common.security.AuthUserContext;
import com.tmerclub.cloud.coupon.service.CouponLockService;
import com.tmerclub.cloud.coupon.service.CouponService;
import com.tmerclub.cloud.coupon.util.ChooseCouponHelper;
import org.apache.dubbo.config.annotation.DubboService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

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

/**
 * @author tmerclub
 * @date 2020/12/17
 */
@DubboService
public class CouponOrderFeignController implements CouponOrderFeignClient {


    private final Logger LOGGER = LoggerFactory.getLogger(CouponOrderFeignController.class);
    @Autowired
    private CouponService couponService;
    @Autowired
    private CouponLockService couponLockService;

    @Override
    public ServerResponseEntity<ShopCartOrderMergerVO> choosePlatformCoupon(PlatformChooseCouponDTO param) {
        Long userId = AuthUserContext.get().getUserId();
        ShopCartOrderMergerVO shopCartOrderMergerVO = param.getShopCartOrderMergerVO();

        // 获取用户可用平台优惠券
        List<CouponOrderVO> couponList = couponService.getCouponListByUserIdAndShopId(userId, 0L);
        if (CollectionUtil.isEmpty(couponList)) {
            return ServerResponseEntity.success(shopCartOrderMergerVO);
        }

        // 完整的订单信息
        // 订单项目
        List<ShopCartItemVO> shopAllShopCartItems = new ArrayList<>(Constant.INITIAL_CAPACITY);
        List<ShopCartOrderVO> shopCartOrders = shopCartOrderMergerVO.getShopCartOrders();
        for (ShopCartOrderVO shopCartOrder : shopCartOrders) {
            List<ShopCartItemDiscountVO> shopCartItemDiscounts = shopCartOrder.getShopCartItemDiscounts();
            for (ShopCartItemDiscountVO shopCartItemDiscount : shopCartItemDiscounts) {
                shopAllShopCartItems.addAll(shopCartItemDiscount.getShopCartItems());
            }
        }
        // 因为经过满减，实际金额的顺序已经变了
        shopAllShopCartItems = shopAllShopCartItems.stream().sorted(Comparator.comparingDouble(ShopCartItemVO::getActualTotal)).collect(Collectors.toList());

        ChooseCouponHelper chooseCouponHelper = new ChooseCouponHelper(shopAllShopCartItems, couponList, param.getCouponIds(), param.getUserChangeCoupon()).invoke();


        if (chooseCouponHelper.getChooseCoupon() != null) {
            Long couponReduce = chooseCouponHelper.getCouponReduce();
            Map<Long, Long> shopReduceMap = chooseCouponHelper.getShopReduceMap();
            for (ShopCartOrderVO shopCartOrder : shopCartOrders) {
                Long couponAmount = shopReduceMap.get(shopCartOrder.getShopId());
                shopCartOrder.setPlatformCouponReduce(couponAmount);
                couponAmount = Objects.isNull(shopCartOrder.getPlatformAmount()) ? couponAmount : shopCartOrder.getPlatformAmount() + couponAmount;
                shopCartOrder.setPlatformAmount(couponAmount);
                shopCartOrder.setActualTotal(shopCartOrder.getActualTotal() - shopCartOrder.getPlatformCouponReduce());
                shopCartOrder.setShopReduce(shopCartOrder.getShopReduce() + shopCartOrder.getPlatformCouponReduce());
            }
            shopCartOrderMergerVO.setOrderReduce(shopCartOrderMergerVO.getOrderReduce() + couponReduce);
            shopCartOrderMergerVO.setActualTotal(shopCartOrderMergerVO.getActualTotal() - couponReduce);
        }
        shopCartOrderMergerVO.setCoupons(couponList);
        return ServerResponseEntity.success(shopCartOrderMergerVO);
    }

    @Override
    public ServerResponseEntity<Void> lockCoupon(List<LockCouponDTO> lockCouponParams) {
        if (CollectionUtil.isEmpty(lockCouponParams)) {
            return ServerResponseEntity.success();
        }
        return couponLockService.lockCoupon(lockCouponParams);
    }

}
