package com.tmerclub.cloud.systemtest.test.amount;

import com.tmerclub.cloud.common.order.dto.OrderDTO;
import com.tmerclub.cloud.common.product.vo.app.CategoryAppVO;
import com.tmerclub.cloud.order.vo.OrderDetailVO;
import com.moyuer.cloud.systemtest.api.amount.CategoryApi;
import com.moyuer.cloud.systemtest.api.amount.PayApi;
import com.moyuer.cloud.systemtest.api.amount.ProductApi;
import com.moyuer.cloud.systemtest.api.marketing.CouponApi;
import com.moyuer.cloud.systemtest.api.order.MyOrderApi;
import com.moyuer.cloud.systemtest.api.order.ShopCartApi;
import com.moyuer.cloud.systemtest.api.store.ShopWalletApi;
import com.moyuer.cloud.systemtest.data.amount.SpuData;
import com.moyuer.cloud.systemtest.data.marketing.CouponData;
import com.moyuer.cloud.systemtest.data.order.OrderDtoData;
import com.moyuer.cloud.systemtest.data.order.ShopCartData;
import com.moyuer.cloud.systemtest.data.order.SubmitOrderDtoData;
import com.moyuer.cloud.systemtest.data.order.WalletAssert;
import com.moyuer.cloud.systemtest.data.platform.CategoryDtoData;
import com.moyuer.cloud.systemtest.data.store.DeliveryData;
import org.testng.annotations.Test;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * @author lpj
 */
public class CouponAmountTest {
    private static List<Long> getCategoryId(Boolean isMainShop) {
        List<CategoryAppVO> categoryAppVOList = CategoryApi.listShopCategory(isMainShop);
        Long shopCategoryId = categoryAppVOList.get(categoryAppVOList.size() - 1).getCategoryId();
        Long categoryId = CategoryApi.listSigningCategory(true).get(0).getCategoryId();
        CategoryApi.update(CategoryDtoData.getCategoryDTO(categoryId));
        return Stream.of(categoryId, shopCategoryId).collect(Collectors.toList());
    }

    @Test(description = "商品金额为小数额，使用店铺优惠券优惠金额计算是否正确")
    public void minPriceShopCouponAmountTest() {
        Long transportId = DeliveryData.getFreeFeeDelivery(true);

        List<Long> categoryIds = getCategoryId(true);
        //新建店铺折扣券-满0.01打5折
        Long productId = ProductApi.createProduct(SpuData.getRealSpu(categoryIds.get(0), categoryIds.get(1), transportId, 1L), true);
        List<Long> productIds = Collections.singletonList(productId);
        CouponApi.saveShopCoupon(CouponData.getCouponDTO(1, 0, 2, 1L, null, 5.0, 2, 10, 100, 1, productIds));
        Long couponId = CouponApi.getShopCouponPage().get(0).getCouponId();
        //用户领取优惠券
        CouponApi.receive(couponId);
        //用户使用优惠券购买商品
        OrderDTO orderDTO = OrderDtoData.getOrderDtoData(productId, 1);
        orderDTO.setCouponIds(Collections.singletonList(CouponApi.getUserCouponPage().get(0).getCouponUserId()));
        MyOrderApi.confirm(orderDTO);
        Long orderNum = MyOrderApi.submit(SubmitOrderDtoData.getSubmitOrderDtoData(productId)).get(0);
        PayApi.orderPay(orderNum);

        CouponApi.deleteShopCoupon(couponId);
        ProductApi.delete(productId, true);

        //验证数据
        WalletAssert.shopWalletLogAssert(ShopWalletApi.getMongoShopWalletLogBO(orderNum, true).get(0), 0, 1, 1L, 1L, 0L, 0L, 0L, 0L, 0);
        OrderDetailVO orderDetailVO = ShopWalletApi.getOrderDetailVO(orderNum, true);
        WalletAssert.shopOrderDetailAssert(orderDetailVO.getOrderItemDetailList().get(0), 1L, 1L, 0L, 0L, 0L, 0L, 0L, 1.99, 0L,
                0L, 0L, 0L, 0L, 0L, null, null, 0L, null, 1, null);
    }

    @Test(description = "商品金额为小数额，使用平台优惠券优惠金额计算是否正确")
    public void minPricePlatformCouponAmountTest() {
        Long transportId = DeliveryData.getFreeFeeDelivery(true);

        List<Long> categoryIds = getCategoryId(true);
        //新建店铺折扣券-满0.01打5折
        Long productId = ProductApi.createProduct(SpuData.getRealSpu(categoryIds.get(0), categoryIds.get(1), transportId, 1L), true);
        List<Long> productIds = Collections.singletonList(productId);
        CouponApi.savePlatformCoupon(CouponData.getCouponDTO(1, 0, 2, 1L, null, 5.0, 2, 10, 100, 1, productIds));
        Long couponId = CouponApi.getPlatformCouponPage().get(0).getCouponId();
        //用户领取优惠券
        CouponApi.receive(couponId);
        //用户使用优惠券购买商品
        OrderDTO orderDTO = OrderDtoData.getOrderDtoData(productId, 1);
        orderDTO.setCouponIds(Collections.singletonList(CouponApi.getUserCouponPage().get(0).getCouponUserId()));
        MyOrderApi.confirm(orderDTO);
        Long orderNum = MyOrderApi.submit(SubmitOrderDtoData.getSubmitOrderDtoData(productId)).get(0);
        PayApi.orderPay(orderNum);

        CouponApi.deletePlatformCoupon(couponId);
        ProductApi.delete(productId, true);

        //验证数据
        WalletAssert.shopWalletLogAssert(ShopWalletApi.getMongoShopWalletLogBO(orderNum, true).get(0), 0, 1, 1L, 1L, 0L, 0L, 0L, 0L, 0);
        OrderDetailVO orderDetailVO = ShopWalletApi.getOrderDetailVO(orderNum, true);
        WalletAssert.shopOrderDetailAssert(orderDetailVO.getOrderItemDetailList().get(0), 1L, 1L, 0L, 0L, 0L, 0L, 0L, 1.99, 0L,
                0L, 0L, 0L, 0L, 0L, null, null, 0L, null, 1, null);
    }

    @Test(description = "多个商品，其中一个商品不参与店铺优惠券，使用店铺优惠券金额计算是否正确")
    public void differentProductShopCouponAmountTest() {
        Long transportId = DeliveryData.getFreeFeeDelivery(true);

        List<Long> categoryIds = getCategoryId(true);
        //新建店铺代金券-满20减5
        Long productIdA = ProductApi.createProduct(SpuData.getRealSpu(categoryIds.get(0), categoryIds.get(1), transportId, 2000L), true);
        Long productIdB = ProductApi.createProduct(SpuData.getRealSpu(categoryIds.get(0), categoryIds.get(1), transportId, 1700L), true);
        Long productIdC = ProductApi.createProduct(SpuData.getRealSpu(categoryIds.get(0), categoryIds.get(1), transportId, 300L), true);
        List<Long> productIds = Arrays.asList(productIdB, productIdC);
        CouponApi.saveShopCoupon(CouponData.getCouponDTO(1, 0, 1, 2000L, 500L, null, 2, 10, 100, 1, productIds));
        Long couponId = CouponApi.getShopCouponPage().get(0).getCouponId();

        //用户领取优惠券
        CouponApi.receive(couponId);
        //用户使用优惠券购买商品
        ShopCartApi.addItem(ShopCartData.getShopCartItem(productIdA, 1));
        ShopCartApi.addItem(ShopCartData.getShopCartItem(productIdB, 1));
        ShopCartApi.addItem(ShopCartData.getShopCartItem(productIdC, 1));
        OrderDTO orderDTO = OrderDtoData.getOrderDtoData(productIdA);
        orderDTO.setCouponIds(Collections.singletonList(CouponApi.getUserCouponPage().get(0).getCouponUserId()));
        MyOrderApi.confirm(orderDTO);
        Long orderNum = MyOrderApi.submit(SubmitOrderDtoData.getSubmitOrderDtoData(productIdA)).get(0);
        PayApi.orderPay(orderNum);

        CouponApi.deleteShopCoupon(couponId);
        ProductApi.delete(productIdC, true);
        ProductApi.delete(productIdB, true);
        ProductApi.delete(productIdA, true);

        //验证数据
        WalletAssert.shopWalletLogAssert(ShopWalletApi.getMongoShopWalletLogBO(orderNum, true).get(0), 0, 1, 3431L, 3500L, 0L, 500L, 0L, 69L, 0);
        OrderDetailVO orderDetailVO = ShopWalletApi.getOrderDetailVO(orderNum, true);
        for (int i=0; i<orderDetailVO.getOrderItemDetailList().size(); i++) {
            if (orderDetailVO.getOrderItemDetailList().get(i).getSpuTotalAmount().equals(2000L)) {
                WalletAssert.shopOrderDetailAssert(orderDetailVO.getOrderItemDetailList().get(i), 2000L, 2000L, 0L, 0L, 0L, 0L, 0L, 1.99, 40L,
                        0L, 0L, 0L, 0L, 0L, null, null, 0L, null, 1, null);
            }
            if (orderDetailVO.getOrderItemDetailList().get(i).getSpuTotalAmount().equals(1700L)) {
                WalletAssert.shopOrderDetailAssert(orderDetailVO.getOrderItemDetailList().get(i), 1700L, 1275L, 0L, 425L, 0L, 0L, 0L, 1.99, 25L,
                        0L, 0L, 0L, 425L, 0L, null, null, 0L, null, 1, null);
            }
            if (orderDetailVO.getOrderItemDetailList().get(i).getSpuTotalAmount().equals(300L)) {
                WalletAssert.shopOrderDetailAssert(orderDetailVO.getOrderItemDetailList().get(i), 300L, 225L, 0L, 75L, 0L, 0L, 0L, 1.99, 4L,
                        0L, 0L, 0L, 75L, 0L, null, null, 0L, null, 1, null);
            }
        }
    }

    @Test(description = "多个商品，其中一个商品不参与平台优惠券，使用平台优惠券金额计算是否正确")
    public void differentProductPlatformCouponAmountTest() {
        Long transportId = DeliveryData.getFreeFeeDelivery(true);

        List<Long> categoryIds = getCategoryId(true);
        //新建店铺代金券-满20减5
        Long productIdA = ProductApi.createProduct(SpuData.getRealSpu(categoryIds.get(0), categoryIds.get(1), transportId, 2000L), true);
        Long productIdB = ProductApi.createProduct(SpuData.getRealSpu(categoryIds.get(0), categoryIds.get(1), transportId, 1700L), true);
        Long productIdC = ProductApi.createProduct(SpuData.getRealSpu(categoryIds.get(0), categoryIds.get(1), transportId, 300L), true);
        List<Long> productIds = Arrays.asList(productIdB, productIdC);
        CouponApi.savePlatformCoupon(CouponData.getCouponDTO(1, 0, 1, 2000L, 500L, null, 2, 10, 100, 1, productIds));
        Long couponId = CouponApi.getPlatformCouponPage().get(0).getCouponId();

        //用户领取优惠券
        CouponApi.receive(couponId);
        //用户使用优惠券购买商品
        ShopCartApi.addItem(ShopCartData.getShopCartItem(productIdA, 1));
        ShopCartApi.addItem(ShopCartData.getShopCartItem(productIdB, 1));
        ShopCartApi.addItem(ShopCartData.getShopCartItem(productIdC, 1));
        OrderDTO orderDTO = OrderDtoData.getOrderDtoData(productIdA);
        orderDTO.setCouponIds(Collections.singletonList(CouponApi.getUserCouponPage().get(0).getCouponUserId()));
        MyOrderApi.confirm(orderDTO);
        Long orderNum = MyOrderApi.submit(SubmitOrderDtoData.getSubmitOrderDtoData(productIdA)).get(0);
        PayApi.orderPay(orderNum);

        CouponApi.deletePlatformCoupon(couponId);
        ProductApi.delete(productIdC, true);
        ProductApi.delete(productIdB, true);
        ProductApi.delete(productIdA, true);

        //验证数据
        WalletAssert.shopWalletLogAssert(ShopWalletApi.getMongoShopWalletLogBO(orderNum, true).get(0), 0, 1, 3920L, 3500L, 500L, 0L, 0L, 80L, 0);
        OrderDetailVO orderDetailVO = ShopWalletApi.getOrderDetailVO(orderNum, true);
        for (int i=0; i<orderDetailVO.getOrderItemDetailList().size(); i++) {
            if (orderDetailVO.getOrderItemDetailList().get(i).getSpuTotalAmount().equals(2000L)) {
                WalletAssert.shopOrderDetailAssert(orderDetailVO.getOrderItemDetailList().get(i), 2000L, 2000L, 0L, 0L, 0L, 0L, 0L, 1.99, 40L,
                        0L, 0L, 0L, 0L, 0L, null, null, 0L, null, 1, null);
            }
            if (orderDetailVO.getOrderItemDetailList().get(i).getSpuTotalAmount().equals(1700L)) {
                WalletAssert.shopOrderDetailAssert(orderDetailVO.getOrderItemDetailList().get(i), 1700L, 1275L, 0L, 0L, 425L, 0L, 0L, 1.99, 34L,
                        0L, 0L, 425L, 0L, 0L, null, null, 0L, null, 1, null);
            }
            if (orderDetailVO.getOrderItemDetailList().get(i).getSpuTotalAmount().equals(300L)) {
                WalletAssert.shopOrderDetailAssert(orderDetailVO.getOrderItemDetailList().get(i), 300L, 225L, 0L, 0L, 75L, 0L, 0L, 1.99, 6L,
                        0L, 0L, 75L, 0L, 0L, null, null, 0L, null, 1, null);
            }
        }
    }
}
