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

import com.tmerclub.cloud.combo.vo.ComboVO;
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.marketing.ComboApi;
import com.moyuer.cloud.systemtest.api.order.MyOrderApi;
import com.moyuer.cloud.systemtest.api.order.OrderApi;
import com.moyuer.cloud.systemtest.api.store.ShopWalletApi;
import com.moyuer.cloud.systemtest.api.supplier.SupplierProductApi;
import com.moyuer.cloud.systemtest.data.amount.PreSaleSpuData;
import com.moyuer.cloud.systemtest.data.amount.SpuData;
import com.moyuer.cloud.systemtest.data.marketing.ComboData;
import com.moyuer.cloud.systemtest.data.order.SubmitOrderDtoData;
import com.moyuer.cloud.systemtest.data.store.DeliveryData;
import com.moyuer.cloud.systemtest.data.supplier.SupplierProductData;
import org.testng.Assert;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;

import java.util.Arrays;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * @author lpj
 */
public class ComboTest {

    @DataProvider(name = "productProvider")
    private Object[][] productProvider() {
        List<CategoryAppVO> categoryAppVOList = CategoryApi.listShopCategory(true);
        Long shopCategoryId = categoryAppVOList.get(categoryAppVOList.size() - 1).getCategoryId();
        Long categoryId = CategoryApi.listSigningCategory(true).get(0).getCategoryId();
        Long mainProductId = ProductApi.createProduct(SpuData.getRealSpu(categoryId,shopCategoryId, DeliveryData.getFreeFeeDelivery(true),8800L), true);
        Long realProductId = ProductApi.createProduct(SpuData.getRealSpu(categoryId,shopCategoryId, DeliveryData.getFreeFeeDelivery(true),8800L), true);
        Long virtualProductId = ProductApi.createProduct(SpuData.getUnRealSpu(categoryId,shopCategoryId,8800L, 1), true);
        Long fullPreSaleProductId = ProductApi.createProduct(PreSaleSpuData.getFullPreSaleSpu(categoryId,shopCategoryId, DeliveryData.getFreeFeeDelivery(true),8800L), true);
        Long supplierProductId = SupplierProductData.getSupplierProductId(6600L, DeliveryData.getSupplierFreeFeeDelivery(true), true);
        Long productId = SupplierProductData.getProductId(supplierProductId, 8800L, 1, null, true, true);
        return new Object[][]{{mainProductId, Arrays.asList(realProductId, virtualProductId, fullPreSaleProductId, productId), supplierProductId}};
    }

    @Test(description = "套餐基本流程", dataProvider = "productProvider")
    public void comboFlow(Long mainProductId, List<Long> matchingProductIds, Long supplierProductId) throws InterruptedException {
        //新增套餐
        ComboApi.save(ComboData.getComboDTO(mainProductId, matchingProductIds));
        //获取套餐id
        Long comboId = ComboApi.getComboPage().get(0).getComboId();
        //购买套餐
        MyOrderApi.confirm(ComboData.getComboOrderDTO(comboId, 2));
        //提交订单获取订单id
        TimeUnit.SECONDS.sleep(2L);
        List<Long> orderNumberList = MyOrderApi.submit(SubmitOrderDtoData.getSubmitOrderDtoData(mainProductId));
        //根据订单id支付订单
        PayApi.orderPay(orderNumberList);
        //获取套餐信息
        ComboVO comboVO = ComboApi.getComboInfo(comboId);
        //修改套餐信息，去掉搭配商品
        ComboApi.update(ComboData.getUpdateComboDTO(comboVO));
        //失效套餐
        ComboApi.updateComboStatus(ComboData.getUpdateStatusData(comboId, 0));
        //删除套餐
        ComboApi.updateComboStatus(ComboData.getUpdateStatusData(comboId, -1));
        //删除商品
        ProductApi.delete(mainProductId, true);
        for (Long matchingProductId : matchingProductIds) {
            ProductApi.delete(matchingProductId, true);
        }
        SupplierProductApi.delete(supplierProductId, true);

        //对订单进行退款并验证数据是否正确
        for (Long orderNumber : orderNumberList) {
            if (OrderApi.infoOrder(orderNumber).getSupplierDeliveryType().equals(1)){
                Long refundId = OrderRefund.supplierOrderRefund(orderNumber);
                //验证运行后数据是否正确
                TimeUnit.SECONDS.sleep(1L);
                Assert.assertEquals(ShopWalletApi.getMongoShopWalletLogBO(orderNumber, true).size(), 2);
                Assert.assertEquals(ShopWalletApi.getOrderDetailVO(orderNumber, refundId, true).getOrderItemDetailList().get(0).getRefundAmount(), 26400);
            }else if (MyOrderApi.orderDetail(orderNumber).getOrderMold().equals(0)) {
                Long refundId = OrderRefund.realOrderRefund(orderNumber);
                //验证运行后数据是否正确
                TimeUnit.SECONDS.sleep(1L);
                Assert.assertEquals(ShopWalletApi.getMongoShopWalletLogBO(orderNumber, true).size(), 2);
                Assert.assertEquals(ShopWalletApi.getOrderDetailVO(orderNumber, refundId, true).getOrderItemDetailList().get(0).getRefundAmount(), 26400);
            } else {
                Long refundId = OrderRefund.virtualOrderRefund(orderNumber);
                //验证运行后数据是否正确
                TimeUnit.SECONDS.sleep(1L);
                Assert.assertEquals(ShopWalletApi.getMongoShopWalletLogBO(orderNumber, true).size(), 2);
                Assert.assertEquals(ShopWalletApi.getOrderDetailVO(orderNumber, refundId, true).getOrderItemDetailList().get(0).getRefundAmount(), 26400);
            }
        }
    }
}
