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

import com.tmerclub.cloud.common.product.vo.app.CategoryAppVO;
import com.tmerclub.cloud.discount.dto.DiscountItemDTO;
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.DiscountApi;
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.DiscountData;
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 DiscountAmountTest {
    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(isMainShop).get(0).getCategoryId();
        CategoryApi.update(CategoryDtoData.getCategoryDTO(categoryId));
        return Stream.of(categoryId, shopCategoryId).collect(Collectors.toList());
    }

    @Test(description = "使用多个活动下单各个活动优惠金额是否正确")
    public void differentDiscountAmountTest() {
        Long transportId = DeliveryData.getFreeFeeDelivery(true);

        List<Long> categoryIds = getCategoryId(true);
        //新建满减活动A-满20减8
        Long productIdA = ProductApi.createProduct(SpuData.getRealSpu(categoryIds.get(0), categoryIds.get(1), transportId, 7310L), true);
        List<DiscountItemDTO> discountItemDtoListA = List.of(DiscountData.getDiscountItemDTO(2000L, 800L));
        List<Long> productIdsA = Collections.singletonList(productIdA);
        DiscountApi.save(DiscountData.getDiscount(0, 0, discountItemDtoListA, 800L, 1, productIdsA, 1), true);
        Long discountIdA = DiscountApi.getDiscountPage(true).get(0).getDiscountId();

        //新建满减活动B-满1件打3.9折
        Long productIdB = ProductApi.createProduct(SpuData.getRealSpu(categoryIds.get(0), categoryIds.get(1), transportId, 633L), true);
        List<DiscountItemDTO> discountItemDtoListB = List.of(DiscountData.getDiscountItemDTO(100L, 390L));
        List<Long> productIdsB = Collections.singletonList(productIdB);
        DiscountApi.save(DiscountData.getDiscount(3, 0, discountItemDtoListB, 50000L, 1, productIdsB, 1), true);
        Long discountIdB = DiscountApi.getDiscountPage(true).get(0).getDiscountId();

        //新建满减活动A-满2件打3折
        Long productIdC = ProductApi.createProduct(SpuData.getRealSpu(categoryIds.get(0), categoryIds.get(1), transportId, 270L), true);
        List<DiscountItemDTO> discountItemDtoListC = List.of(DiscountData.getDiscountItemDTO(200L, 300L));
        List<Long> productIdsC = Collections.singletonList(productIdC);
        DiscountApi.save(DiscountData.getDiscount(3, 0, discountItemDtoListC, 50000L, 1, productIdsC, 1), true);
        Long discountIdC = DiscountApi.getDiscountPage(true).get(0).getDiscountId();

        //新建满减活动A-满1件打3折，上限20
        Long productIdD = ProductApi.createProduct(SpuData.getRealSpu(categoryIds.get(0), categoryIds.get(1), transportId, 7347L), true);
        List<DiscountItemDTO> discountItemDtoListD = List.of(DiscountData.getDiscountItemDTO(100L, 300L));
        List<Long> productIdsD = Collections.singletonList(productIdD);
        DiscountApi.save(DiscountData.getDiscount(3, 0, discountItemDtoListD, 2000L, 1, productIdsD, 1), true);
        Long discountIdD = DiscountApi.getDiscountPage(true).get(0).getDiscountId();

        ShopCartApi.addItem(ShopCartData.getShopCartItem(productIdA, 1));
        ShopCartApi.addItem(ShopCartData.getShopCartItem(productIdB, 1));
        ShopCartApi.addItem(ShopCartData.getShopCartItem(productIdC, 3));
        ShopCartApi.addItem(ShopCartData.getShopCartItem(productIdD, 1));
        MyOrderApi.confirm(OrderDtoData.getOrderDtoData(productIdA));
        Long orderNum = MyOrderApi.submit(SubmitOrderDtoData.getSubmitOrderDtoData(productIdA)).get(0);
        PayApi.orderPay(orderNum);

        DiscountApi.delete(discountIdD, true);
        DiscountApi.delete(discountIdC, true);
        DiscountApi.delete(discountIdB, true);
        DiscountApi.delete(discountIdA, true);
        ProductApi.delete(productIdD, true);
        ProductApi.delete(productIdC, true);
        ProductApi.delete(productIdB, true);
        ProductApi.delete(productIdA, true);

        //验证数据
        WalletAssert.shopWalletLogAssert(ShopWalletApi.getMongoShopWalletLogBO(orderNum, true).get(0), 0, 1, 12101L, 12347L, 0L, 3753L, 0L, 246L, 0);
        OrderDetailVO orderDetailVO = ShopWalletApi.getOrderDetailVO(orderNum, true);
        for (int i=0; i<orderDetailVO.getOrderItemDetailList().size(); i++) {
            if (orderDetailVO.getOrderItemDetailList().get(i).getSpuTotalAmount().equals(7347L)) {
                WalletAssert.shopOrderDetailAssert(orderDetailVO.getOrderItemDetailList().get(i), 7347L, 5347L, 0L, 2000L, 0L, 0L, 0L, 1.99, 106L,
                        0L, 0L, 0L, 0L, 2000L, null, null, 0L, null, 1, null);
            }
            if (orderDetailVO.getOrderItemDetailList().get(i).getSpuTotalAmount().equals(7310L)) {
                WalletAssert.shopOrderDetailAssert(orderDetailVO.getOrderItemDetailList().get(i), 7310L, 6510L, 0L, 800L, 0L, 0L, 0L, 1.99, 130L,
                        0L, 0L, 0L, 0L, 800L, null, null, 0L, null, 1, null);
            }
            if (orderDetailVO.getOrderItemDetailList().get(i).getSpuTotalAmount().equals(810L)) {
                WalletAssert.shopOrderDetailAssert(orderDetailVO.getOrderItemDetailList().get(i), 810L, 243L, 0L, 567L, 0L, 0L, 0L, 1.99, 5L,
                        0L, 0L, 0L, 0L, 567L, null, null, 0L, null, 3, null);
            }
            if (orderDetailVO.getOrderItemDetailList().get(i).getSpuTotalAmount().equals(633L)) {
                WalletAssert.shopOrderDetailAssert(orderDetailVO.getOrderItemDetailList().get(i), 633L, 247L, 0L, 386L, 0L, 0L, 0L, 1.99, 5L,
                        0L, 0L, 0L, 0L, 386L, null, null, 0L, null, 1, null);
            }
        }
    }

    @Test(description = "优惠上限为*优惠金额数据是否正确")
    public void discountMaxReduceAmountTest() {
        Long transportId = DeliveryData.getFreeFeeDelivery(true);

        List<Long> categoryIds = getCategoryId(true);
        //新建满减活动A-每满10减1
        Long productIdA = ProductApi.createProduct(SpuData.getRealSpu(categoryIds.get(0), categoryIds.get(1), transportId, 7000L), true);
        List<DiscountItemDTO> discountItemDtoListA = List.of(DiscountData.getDiscountItemDTO(1000L, 100L));
        List<Long> productIdsA = Collections.singletonList(productIdA);
        DiscountApi.save(DiscountData.getDiscount(0, 1, discountItemDtoListA, 500L, 1, productIdsA, 1), true);
        Long discountIdA = DiscountApi.getDiscountPage(true).get(0).getDiscountId();

        //新建满减活动B-满1件打7折
        Long productIdB = ProductApi.createProduct(SpuData.getRealSpu(categoryIds.get(0), categoryIds.get(1), transportId, 7000L), true);
        List<DiscountItemDTO> discountItemDtoListB = List.of(DiscountData.getDiscountItemDTO(100L, 700L));
        List<Long> productIdsB = Collections.singletonList(productIdB);
        DiscountApi.save(DiscountData.getDiscount(3, 0, discountItemDtoListB, 500L, 1, productIdsB, 1), true);
        Long discountIdB = DiscountApi.getDiscountPage(true).get(0).getDiscountId();

        ShopCartApi.addItem(ShopCartData.getShopCartItem(productIdA, 1));
        ShopCartApi.addItem(ShopCartData.getShopCartItem(productIdB, 1));
        MyOrderApi.confirm(OrderDtoData.getOrderDtoData(productIdA));
        Long orderNum = MyOrderApi.submit(SubmitOrderDtoData.getSubmitOrderDtoData(productIdA)).get(0);
        PayApi.orderPay(orderNum);

        DiscountApi.delete(discountIdB, true);
        DiscountApi.delete(discountIdA, true);
        ProductApi.delete(productIdB, true);
        ProductApi.delete(productIdA, true);

        //验证数据
        WalletAssert.shopWalletLogAssert(ShopWalletApi.getMongoShopWalletLogBO(orderNum, true).get(0), 0, 1, 12742L, 13000L, 0L, 1000L, 0L, 258L, 0);
        OrderDetailVO orderDetailVO = ShopWalletApi.getOrderDetailVO(orderNum, true);
        WalletAssert.shopOrderDetailAssert(orderDetailVO.getOrderItemDetailList().get(0), 7000L, 6500L, 0L, 500L, 0L, 0L, 0L, 1.99, 129L,
                0L, 0L, 0L, 0L, 500L, null, null, 0L, null, 1, null);
        WalletAssert.shopOrderDetailAssert(orderDetailVO.getOrderItemDetailList().get(1), 7000L, 6500L, 0L, 500L, 0L, 0L, 0L, 1.99, 129L,
                0L, 0L, 0L, 0L, 500L, null, null, 0L, null, 1, null);
    }

    @Test(description = "满足满多层级减折活动，优惠金额是否正确")
    public void discountItemsAmountTest() {
        Long transportId = DeliveryData.getFreeFeeDelivery(true);

        List<Long> categoryIds = getCategoryId(true);
        //新建满减活动B-满1件打7折，满2件打3.7折
        Long productIdA = ProductApi.createProduct(SpuData.getRealSpu(categoryIds.get(0), categoryIds.get(1), transportId, 10000L), true);
        Long productIdB = ProductApi.createProduct(SpuData.getRealSpu(categoryIds.get(0), categoryIds.get(1), transportId, 7347L), true);
        List<DiscountItemDTO> discountItemDtoList = List.of(DiscountData.getDiscountItemDTO(100L, 700L), DiscountData.getDiscountItemDTO(200L, 370L));
        List<Long> productIds = Arrays.asList(productIdA, productIdB);
        DiscountApi.save(DiscountData.getDiscount(3, 0, discountItemDtoList, 50000L, 1, productIds, 1), true);
        Long discountId = DiscountApi.getDiscountPage(true).get(0).getDiscountId();

        ShopCartApi.addItem(ShopCartData.getShopCartItem(productIdA, 1));
        ShopCartApi.addItem(ShopCartData.getShopCartItem(productIdB, 2));
        MyOrderApi.confirm(OrderDtoData.getOrderDtoData(productIdA));
        Long orderNum = MyOrderApi.submit(SubmitOrderDtoData.getSubmitOrderDtoData(productIdA)).get(0);
        PayApi.orderPay(orderNum);

        DiscountApi.delete(discountId, true);
        ProductApi.delete(productIdB, true);
        ProductApi.delete(productIdA, true);

        //验证数据
        WalletAssert.shopWalletLogAssert(ShopWalletApi.getMongoShopWalletLogBO(orderNum, true).get(0), 0, 1, 8955L, 9137L, 0L, 15557L, 0L, 182L, 0);
        OrderDetailVO orderDetailVO = ShopWalletApi.getOrderDetailVO(orderNum, true);
        for (int i=0; i<orderDetailVO.getOrderItemDetailList().size(); i++) {
            if (orderDetailVO.getOrderItemDetailList().get(i).getSpuTotalAmount().equals(10000L)) {
                WalletAssert.shopOrderDetailAssert(orderDetailVO.getOrderItemDetailList().get(i), 10000L, 3700L, 0L, 6300L, 0L, 0L, 0L, 1.99, 74L,
                        0L, 0L, 0L, 0L, 6300L, null, null, 0L, null, 1, null);
            }
            if (orderDetailVO.getOrderItemDetailList().get(i).getSpuTotalAmount().equals(14694L)) {
                WalletAssert.shopOrderDetailAssert(orderDetailVO.getOrderItemDetailList().get(i), 14694L, 5437L, 0L, 9257L, 0L, 0L, 0L, 1.99, 108L,
                        0L, 0L, 0L, 0L, 9257L, null, null, 0L, null, 2, null);
            }
        }
    }
}
