/*
 * Copyright (c) 2018-2999 广州亚米信息科技有限公司 All rights reserved.
 *
 * https://www.gz-yami.com/
 *
 * 未经允许，不可做商业用途！
 *
 * 版权所有，侵权必究！
 */

package com.yami.shop.coupon.api.listener;

import com.yami.shop.bean.app.dto.CouponOrderDto;
import com.yami.shop.bean.app.dto.ShopCartItemDto;
import com.yami.shop.bean.app.dto.ShopCartOrderDto;
import com.yami.shop.bean.app.dto.ShopCartOrderMergerDto;
import com.yami.shop.bean.event.ConfirmOrderEvent;
import com.yami.shop.bean.event.PlatformConfirmOrderEvent;
import com.yami.shop.bean.order.ConfirmOrderOrder;
import com.yami.shop.common.util.Arith;
import com.yami.shop.coupon.api.util.ChooseCouponHelper;
import com.yami.shop.coupon.comment.service.CouponService;
import com.yami.shop.security.api.util.SecurityUtils;
import lombok.AllArgsConstructor;
import org.springframework.context.event.EventListener;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 确认订单信息时的优惠券的相关操作，计算优惠券 优惠金额
 *
 * @author LGH
 */
@Component("couponConfirmOrderListener")
@AllArgsConstructor
public class ConfirmOrderListener {

    private final CouponService couponService;

    /**
     * 将店铺下的所有商品归属到该店铺的购物车当中
     *
     * @param event#getShopCart()       购物车
     * @param event#shopCartItemDtoList 该购物车的商品
     * @return 是否继续组装
     */
    @EventListener(ConfirmOrderEvent.class)
    @Order(ConfirmOrderOrder.COUPON)
    public void couponConfirmOrderEvent(ConfirmOrderEvent event) {
        String userId = SecurityUtils.getUser().getUserId();

        ShopCartOrderDto shopCartOrderDto = event.getShopCartOrderDto();

        // 该店铺中的购物车商品列表
        List<ShopCartItemDto> shopCartItems = event.getShopCartItems();

        // 获取用户有效的优惠券
        List<CouponOrderDto> shopCoupons = couponService.getCouponListByShopIds(userId, shopCartOrderDto.getShopId());

        // 用户选中的所有优惠券
        List<Long> couponIds = event.getOrderParam().getCouponIds();

        Integer userChangeCoupon = event.getOrderParam().getUserChangeCoupon();

        ChooseCouponHelper chooseCouponHelper = new ChooseCouponHelper(shopCartItems, shopCoupons, couponIds, userChangeCoupon).invoke();

        if (chooseCouponHelper.getChooseCoupon() != null) {
            // 最后组装订单信息
            assembleOrder(shopCartOrderDto, chooseCouponHelper.getCouponReduce());
        }

        shopCartOrderDto.setCoupons(shopCoupons);
    }


    /**
     * 进行最后订单的组装
     *
     * @param shopCartOrderDto 该店铺的购物车订单
     */
    private void assembleOrder(ShopCartOrderDto shopCartOrderDto,double couponReduce) {
        // 订单优惠券优惠金额要小于订单实际金额
        shopCartOrderDto.setCouponReduce(Math.min(couponReduce, shopCartOrderDto.getActualTotal()));
        shopCartOrderDto.setActualTotal(Arith.sub(shopCartOrderDto.getActualTotal(), shopCartOrderDto.getCouponReduce()));
        shopCartOrderDto.setShopReduce(Arith.add(shopCartOrderDto.getShopReduce(), shopCartOrderDto.getCouponReduce()));
    }



    /**
     * 计算平台优惠券
     */
    @EventListener(PlatformConfirmOrderEvent.class)
    @Order(ConfirmOrderOrder.PLATFORM_COUPON)
    public void calculatePlatformCoupon(PlatformConfirmOrderEvent event){
        String userId = SecurityUtils.getUser().getUserId();

        // 完整的订单信息
        ShopCartOrderMergerDto shopCartOrderMergerDto = event.getShopCartOrderMergerDto();
        // 所有商品 item
        List<ShopCartItemDto> shopAllShopCartItems = event.getAllCartItem();

        // 获取用户可用平台优惠券
        List<CouponOrderDto> couponList = couponService.getCouponListByShopIds(userId, 0L);

        List<ShopCartOrderDto> shopCartOrders = shopCartOrderMergerDto.getShopCartOrders();
        ChooseCouponHelper chooseCouponHelper = new ChooseCouponHelper(shopAllShopCartItems, couponList, event.getOrderParam().getCouponIds(), event.getOrderParam().getUserChangeCoupon()).invoke();


        if (chooseCouponHelper.getChooseCoupon() != null) {
            for (ShopCartOrderDto shopCartOrder : shopCartOrders) {
                // 组装店铺订单信息
                assembleOrder(shopCartOrder, chooseCouponHelper.getCouponReduce());
            }
            // 最后组装订单信息
            assembleOrder(shopCartOrderMergerDto,chooseCouponHelper.getCouponReduce());
        }
        shopCartOrderMergerDto.setCoupons(couponList);
    }


    /**
     * 进行最后订单的组装
     *
     * @param shopCartOrderMergerDto 整个平台的购物车订单
     */
    private void assembleOrder(ShopCartOrderMergerDto shopCartOrderMergerDto,double couponReduce) {
        // 订单优惠券优惠金额要小于订单实际金额
        double minCouponReduce = Math.min(couponReduce, shopCartOrderMergerDto.getActualTotal());
        shopCartOrderMergerDto.setOrderReduce(Arith.add(shopCartOrderMergerDto.getOrderReduce(), minCouponReduce));
        shopCartOrderMergerDto.setActualTotal(Arith.sub(shopCartOrderMergerDto.getActualTotal(), minCouponReduce));
    }
}
