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

import com.tmerclub.cloud.common.order.dto.OrderDTO;
import com.tmerclub.cloud.common.product.vo.app.CategoryAppVO;
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.login.UserApi;
import com.moyuer.cloud.systemtest.api.order.MyOrderApi;
import com.moyuer.cloud.systemtest.api.order.ShopCartApi;
import com.moyuer.cloud.systemtest.api.platform.UserLevelApi;
import com.moyuer.cloud.systemtest.api.platform.UserRightsApi;
import com.moyuer.cloud.systemtest.api.store.ShopWalletApi;
import com.moyuer.cloud.systemtest.api.store.TransportApi;
import com.moyuer.cloud.systemtest.data.amount.SpuData;
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.platform.CategoryDtoData;
import com.moyuer.cloud.systemtest.data.platform.UserLevelDtoData;
import com.moyuer.cloud.systemtest.data.platform.UserRightsDtoData;
import com.moyuer.cloud.systemtest.data.store.DeliveryData;
import org.testng.Assert;
import org.testng.annotations.Test;

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

/**
 * @author lpj
 */
public class TransFeeFreesOrderTest {
    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 fullAmountFreeFreight() {
        List<Long> categoryIds = getCategoryId(true);
        //运费模板-8元/件，满100元包邮
        TransportApi.addTransport(DeliveryData.getTransportDTO(0, 1D, 800L, 1D, 800L, 1, 1, null, 10000L), true);
        Long transportId = TransportApi.infoTransport(true).get(0).getTransportId();
        Long productId = ProductApi.createProduct(SpuData.getRealSpu(categoryIds.get(0), categoryIds.get(1), transportId, 10000L), true);
        MyOrderApi.confirm(OrderDtoData.getOrderDtoData(productId, 1));
        Long orderNum = MyOrderApi.submit(SubmitOrderDtoData.getSubmitOrderDtoData(productId)).get(0);
        PayApi.orderPay(orderNum);
        ProductApi.delete(productId, true);
        TransportApi.deleteTransport(transportId, true);

        Assert.assertEquals(ShopWalletApi.getOrderDetailVO(orderNum, true).getFreightAmount(), 0);
        Assert.assertEquals(ShopWalletApi.getOrderDetailVO(orderNum, true).getFreeFreightAmount(), 800);
    }

    @Test(description = "运费模板满件包邮订单，运费是否正确")
    public void fullPriceFreeFreight() {
        List<Long> categoryIds = getCategoryId(true);
        //运费模板-8元/件，满1件包邮
        TransportApi.addTransport(DeliveryData.getTransportDTO(0, 1D, 800L, 1D, 800L,1, 0, 1D, null), true);
        Long transportId = TransportApi.infoTransport(true).get(0).getTransportId();
        Long productId = ProductApi.createProduct(SpuData.getRealSpu(categoryIds.get(0), categoryIds.get(1), transportId, 10000L), true);
        MyOrderApi.confirm(OrderDtoData.getOrderDtoData(productId, 1));
        Long orderNum = MyOrderApi.submit(SubmitOrderDtoData.getSubmitOrderDtoData(productId)).get(0);
        PayApi.orderPay(orderNum);
        ProductApi.delete(productId, true);
        TransportApi.deleteTransport(transportId, true);

        Assert.assertEquals(ShopWalletApi.getOrderDetailVO(orderNum, true).getFreightAmount(), 0);
        Assert.assertEquals(ShopWalletApi.getOrderDetailVO(orderNum, true).getFreeFreightAmount(), 800);
    }

    @Test(description = "运费模板满件和满金额包邮订单，运费是否正确")
    public void fullPriceAndAmountFreeFreight() {
        List<Long> categoryIds = getCategoryId(true);
        //运费模板-8元/件，满1件和满100元时包邮
        TransportApi.addTransport(DeliveryData.getTransportDTO(0, 1D, 800L, 1D, 800L,1, 2, 1D, 10000L), true);
        Long transportId = TransportApi.infoTransport(true).get(0).getTransportId();
        Long productId = ProductApi.createProduct(SpuData.getRealSpu(categoryIds.get(0), categoryIds.get(1), transportId, 10000L), true);
        MyOrderApi.confirm(OrderDtoData.getOrderDtoData(productId, 1));
        Long orderNum = MyOrderApi.submit(SubmitOrderDtoData.getSubmitOrderDtoData(productId)).get(0);
        PayApi.orderPay(orderNum);
        ProductApi.delete(productId, true);
        TransportApi.deleteTransport(transportId, true);

        Assert.assertEquals(ShopWalletApi.getOrderDetailVO(orderNum, true).getFreightAmount(), 0);
        Assert.assertEquals(ShopWalletApi.getOrderDetailVO(orderNum, true).getFreeFreightAmount(), 800);
    }

    @Test(description = "平台会员权益-全平台包邮与运费模板满条件包邮订单，运费是否正确")
    public void fullConditionFreeFreightAndFreeFeeUserRight() {
        List<Long> categoryIds = getCategoryId(true);
        //用户会员等级添加包邮权益
        Long freeFeeUserRightsId = UserRightsApi.save(UserRightsDtoData.getFreeFeeUserRightsDTO());
        UserLevelApi.update(UserLevelDtoData.getUserLevelDTO(UserApi.getUserInfo().getUserLevelId(), Collections.singletonList(freeFeeUserRightsId)));

        //运费模板-8元/件，满1件和满100元时包邮
        TransportApi.addTransport(DeliveryData.getTransportDTO(0, 1D, 800L, 1D, 800L,1, 2, 1D, 10000L), true);
        Long transportId = TransportApi.infoTransport(true).get(0).getTransportId();
        Long productId = ProductApi.createProduct(SpuData.getRealSpu(categoryIds.get(0), categoryIds.get(1), transportId, 10000L), true);
        MyOrderApi.confirm(OrderDtoData.getOrderDtoData(productId, 1));
        Long orderNum = MyOrderApi.submit(SubmitOrderDtoData.getSubmitOrderDtoData(productId)).get(0);
        PayApi.orderPay(orderNum);
        ProductApi.delete(productId, true);
        TransportApi.deleteTransport(transportId, true);
        UserRightsApi.delete(freeFeeUserRightsId);

        Assert.assertEquals(ShopWalletApi.getOrderDetailVO(orderNum, true).getFreightAmount(), 0);
        Assert.assertEquals(ShopWalletApi.getOrderDetailVO(orderNum, true).getFreeFreightAmount(), 800);
    }

    @Test(description = "运费模板满条件包邮商品与运费模板不包邮商品的订单，运费是否正确")
    public void fullConditionFreeFreightAndNotFreeFreight() throws InterruptedException {
        List<Long> categoryIds = getCategoryId(true);
        //运费模板-运费8元/件，满1件包邮
        TransportApi.addTransport(DeliveryData.getTransportDTO(0, 1D, 800L, 1D, 800L,1, 0, 1D, null), true);
        Long transportIdA = TransportApi.infoTransport(true).get(0).getTransportId();
        Long productIdA = ProductApi.createProduct(SpuData.getRealSpu(categoryIds.get(0), categoryIds.get(1), transportIdA, 10000L), true);

        //运费模板-运费8元/件
        TimeUnit.SECONDS.sleep(2L);
        TransportApi.addTransport(DeliveryData.getTransportDTO(), true);
        Long transportIdB = TransportApi.infoTransport(true).get(0).getTransportId();
        Long productIdB = ProductApi.createProduct(SpuData.getRealSpu(categoryIds.get(0), categoryIds.get(1), transportIdB, 10000L), true);

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

        ProductApi.delete(productIdB, true);
        TransportApi.deleteTransport(transportIdB, true);
        ProductApi.delete(productIdA, true);
        TransportApi.deleteTransport(transportIdA, true);

        Assert.assertEquals(ShopWalletApi.getOrderDetailVO(orderNum, true).getFreightAmount(), 800);
        Assert.assertEquals(ShopWalletApi.getOrderDetailVO(orderNum, true).getFreeFreightAmount(), 800);
    }

    @Test(description = "使用同一运费模板的不同商品满足包邮条件，运费是否正确")
    public void differentProductFullConditionFreeFreight() {
        List<Long> categoryIds = getCategoryId(true);
        //运费模板，运费8元/件，满2件包邮
        TransportApi.addTransport(DeliveryData.getTransportDTO(0, 1D, 800L, 1D, 800L,1, 0, 1D, null), true);
        Long transportIdA = TransportApi.infoTransport(true).get(0).getTransportId();
        //商品A
        Long productIdA = ProductApi.createProduct(SpuData.getRealSpu(categoryIds.get(0), categoryIds.get(1), transportIdA, 10000L), true);
        //商品B
        Long productIdB = ProductApi.createProduct(SpuData.getRealSpu(categoryIds.get(0), categoryIds.get(1), transportIdA, 10000L), true);

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

        ProductApi.delete(productIdB, true);
        ProductApi.delete(productIdA, true);
        TransportApi.deleteTransport(transportIdA, true);

        Assert.assertEquals(ShopWalletApi.getOrderDetailVO(orderNum, true).getFreightAmount(), 0);
        Assert.assertEquals(ShopWalletApi.getOrderDetailVO(orderNum, true).getFreeFreightAmount(), 1600);
    }

    @Test(description = "平台会员权益-全平台包邮、运费模板满条件包邮商品与运费模板不包邮商品的订单，运费是否正确")
    public void fullConditionFreeFreightAndNotFreeFreightAndFreeFeeUserRight() throws InterruptedException {
        List<Long> categoryIds = getCategoryId(true);
        //用户会员等级添加包邮权益
        Long freeFeeUserRightsId = UserRightsApi.save(UserRightsDtoData.getFreeFeeUserRightsDTO());
        UserLevelApi.update(UserLevelDtoData.getUserLevelDTO(UserApi.getUserInfo().getUserLevelId(), Collections.singletonList(freeFeeUserRightsId)));

        //运费模板-运费8元/件，满1件包邮
        TransportApi.addTransport(DeliveryData.getTransportDTO(0, 1D, 800L, 1D, 800L,1, 0, 1D, null), true);
        Long transportIdA = TransportApi.infoTransport(true).get(0).getTransportId();
        Long productIdA = ProductApi.createProduct(SpuData.getRealSpu(categoryIds.get(0), categoryIds.get(1), transportIdA, 10000L), true);

        //运费模板-运费8元/件
        TimeUnit.SECONDS.sleep(2L);
        TransportApi.addTransport(DeliveryData.getTransportDTO(), true);
        Long transportIdB = TransportApi.infoTransport(true).get(0).getTransportId();
        Long productIdB = ProductApi.createProduct(SpuData.getRealSpu(categoryIds.get(0), categoryIds.get(1), transportIdB, 10000L), true);

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

        ProductApi.delete(productIdB, true);
        TransportApi.deleteTransport(transportIdB, true);
        ProductApi.delete(productIdA, true);
        TransportApi.deleteTransport(transportIdA, true);
        UserRightsApi.delete(freeFeeUserRightsId);

        Assert.assertEquals(ShopWalletApi.getOrderDetailVO(orderNum, true).getFreightAmount(), 800);
        Assert.assertEquals(ShopWalletApi.getOrderDetailVO(orderNum, true).getFreeFreightAmount(), 800);
        Assert.assertEquals(ShopWalletApi.getOrderDetailVO(orderNum, true).getPlatformFreeFreightAmount(), 800);
    }

    @Test(description = "平台会员包邮订单，运费是否正确")
    public void freeFeeUserRight() {
        List<Long> categoryIds = getCategoryId(true);
        //用户会员等级添加包邮权益
        Long freeFeeUserRightsId = UserRightsApi.save(UserRightsDtoData.getFreeFeeUserRightsDTO());
        UserLevelApi.update(UserLevelDtoData.getUserLevelDTO(UserApi.getUserInfo().getUserLevelId(), Collections.singletonList(freeFeeUserRightsId)));

        //运费模板-8元/件
        TransportApi.addTransport(DeliveryData.getTransportDTO(), true);
        Long transportId = TransportApi.infoTransport(true).get(0).getTransportId();
        Long productId = ProductApi.createProduct(SpuData.getRealSpu(categoryIds.get(0), categoryIds.get(1), transportId, 10000L), true);
        MyOrderApi.confirm(OrderDtoData.getOrderDtoData(productId, 1));
        Long orderNum = MyOrderApi.submit(SubmitOrderDtoData.getSubmitOrderDtoData(productId)).get(0);
        PayApi.orderPay(orderNum);
        ProductApi.delete(productId, true);
        TransportApi.deleteTransport(transportId, true);
        UserRightsApi.delete(freeFeeUserRightsId);

        Assert.assertEquals(ShopWalletApi.getOrderDetailVO(orderNum, true).getFreightAmount(), 800);
        Assert.assertEquals(ShopWalletApi.getOrderDetailVO(orderNum, true).getPlatformFreeFreightAmount(), 800);
    }

    @Test(description = "多商家商品订单平台会员包邮，运费是否正确")
    public static void multipleShopProductWithFreeFeeUserRight() {
        //用户会员等级添加包邮权益
        Long freeFeeUserRightsId = UserRightsApi.save(UserRightsDtoData.getFreeFeeUserRightsDTO());
        UserLevelApi.update(UserLevelDtoData.getUserLevelDTO(UserApi.getUserInfo().getUserLevelId(), Collections.singletonList(freeFeeUserRightsId)));

        //A店铺运费模板-8元/件
        TransportApi.addTransport(DeliveryData.getTransportDTO(), true);
        Long transportIdA = TransportApi.infoTransport(true).get(0).getTransportId();
        List<Long> categoryIdsA = getCategoryId(true);
        Long productIdA = ProductApi.createProduct(SpuData.getRealSpu(categoryIdsA.get(0), categoryIdsA.get(1), transportIdA, 10000L), true);

        //B店铺运费模板-8元/件
        TransportApi.addTransport(DeliveryData.getTransportDTO(), false);
        Long transportIdB = TransportApi.infoTransport(false).get(0).getTransportId();
        List<Long> categoryIdsB = getCategoryId(false);
        Long productIdB = ProductApi.createProduct(SpuData.getRealSpu(categoryIdsB.get(0), categoryIdsB.get(1), transportIdB, 10000L), false);

        ShopCartApi.addItem(ShopCartData.getShopCartItem(productIdA, 1));
        ShopCartApi.addItem(ShopCartData.getShopCartItem(productIdB, 1));
        //确认订单数据
        Long shopIdA = ProductApi.getProductInfo(productIdA).getShopId();
        Long shopIdB = ProductApi.getProductInfo(productIdB).getShopId();
        OrderDTO orderDTO = OrderDtoData.getOrderDtoData(Arrays.asList(shopIdA, shopIdB));
        //确认订单
        MyOrderApi.confirm(orderDTO);
        //提交订单
        List<Long> orderNumberList = MyOrderApi.submit(SubmitOrderDtoData.getSubmitOrderDtoData(Arrays.asList(shopIdA, shopIdB)));
        //支付订单
        PayApi.orderPay(orderNumberList);

        ProductApi.delete(productIdB, false);
        TransportApi.deleteTransport(transportIdB, false);
        ProductApi.delete(productIdA, true);
        TransportApi.deleteTransport(transportIdA, true);
        UserRightsApi.delete(freeFeeUserRightsId);

        for(Long orderNum : orderNumberList) {
            if (MyOrderApi.orderDetail(orderNum).getShopId().equals(shopIdA)) {
                Assert.assertEquals(ShopWalletApi.getOrderDetailVO(orderNum, true).getFreightAmount(), 800);
                Assert.assertEquals(ShopWalletApi.getOrderDetailVO(orderNum, true).getPlatformFreeFreightAmount(), 800);
            }
            if (MyOrderApi.orderDetail(orderNum).getShopId().equals(shopIdB)) {
                Assert.assertEquals(ShopWalletApi.getOrderDetailVO(orderNum, true).getFreightAmount(), 800);
                Assert.assertEquals(ShopWalletApi.getOrderDetailVO(orderNum, true).getPlatformFreeFreightAmount(), 800);
            }
        }
    }
}
