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

import com.tmerclub.cloud.api.platform.vo.OfflineHandleEventVO;
import com.tmerclub.cloud.common.product.vo.app.CategoryAppVO;
import com.tmerclub.cloud.discount.dto.DiscountItemDTO;
import com.tmerclub.cloud.discount.vo.DiscountVO;
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.store.ShopWalletApi;
import com.moyuer.cloud.systemtest.api.supplier.SupplierProductApi;
import com.moyuer.cloud.systemtest.data.amount.OfflineHandleEventDtoData;
import com.moyuer.cloud.systemtest.data.amount.PreSaleSpuData;
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.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.Collections;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * @author lpj
 */
public class DiscountTest {
    @DataProvider(name = "realProduct")
    private Object[][] realProduct() {
        List<CategoryAppVO> categoryAppVOList = CategoryApi.listShopCategory(true);
        Long shopCategoryId = categoryAppVOList.get(categoryAppVOList.size() - 1).getCategoryId();
        Long categoryId = CategoryApi.listSigningCategory(true).get(0).getCategoryId();
        Long productId = ProductApi.createProduct(SpuData.getRealSpu(categoryId,shopCategoryId, DeliveryData.getFreeFeeDelivery(true),8800L), true);
        return new Object[][]{{productId}};
    }

    @DataProvider(name = "virtualProduct")
    private Object[][] virtualProduct() {
        List<CategoryAppVO> categoryAppVOList = CategoryApi.listShopCategory(true);
        Long shopCategoryId = categoryAppVOList.get(categoryAppVOList.size() - 1).getCategoryId();
        Long categoryId = CategoryApi.listSigningCategory(true).get(0).getCategoryId();
        Long productId = ProductApi.createProduct(SpuData.getUnRealSpu(categoryId, shopCategoryId, 8800L, 1), true);
        return new Object[][]{{productId}};
    }

    @DataProvider(name = "fullPreSaleProduct")
    private Object[][] fullPreSaleProduct() {
        List<CategoryAppVO> categoryAppVOList = CategoryApi.listShopCategory(true);
        Long shopCategoryId = categoryAppVOList.get(categoryAppVOList.size() - 1).getCategoryId();
        Long categoryId = CategoryApi.listSigningCategory(true).get(0).getCategoryId();
        Long productId = ProductApi.createProduct(PreSaleSpuData.getFullPreSaleSpu(categoryId,shopCategoryId, DeliveryData.getFreeFeeDelivery(true),8800L), true);
        return new Object[][]{{productId}};
    }

    @DataProvider(name = "depositPreSaleProduct")
    private Object[][] depositPreSaleProduct() {
        List<CategoryAppVO> categoryAppVOList = CategoryApi.listShopCategory(true);
        Long shopCategoryId = categoryAppVOList.get(categoryAppVOList.size() - 1).getCategoryId();
        Long categoryId = CategoryApi.listSigningCategory(true).get(0).getCategoryId();
        Long productId = ProductApi.createProduct(PreSaleSpuData.getDepositPreSaleSpu(categoryId,shopCategoryId, DeliveryData.getFreeFeeDelivery(true),8800L), true);
        return new Object[][]{{productId}};
    }

    @DataProvider(name = "supplierProduct")
    private Object[][] supplierProduct() {
        Long supplierProductId = SupplierProductData.getSupplierProductId(6600L, DeliveryData.getSupplierFreeFeeDelivery(true), true);
        Long productId = SupplierProductData.getProductId(supplierProductId, 8800L, 1, null, true, true);
        return new Object[][]{{productId, supplierProductId}};
    }

    @Test(description = "满减基本流程", dataProvider = "realProduct")
    public void discountFlow(Long productId) throws InterruptedException {
        //新建满减活动
        List<DiscountItemDTO> discountItemDTOList = Arrays.asList(DiscountData.getDiscountItemDTO(10000L,1000L), DiscountData.getDiscountItemDTO(10100L, 5000L));
        List<Long> productIds = Collections.singletonList(productId);
        DiscountApi.save(DiscountData.getDiscount(0, 0, discountItemDTOList, 50000L, 1, productIds, 1), true);
        //获取满减活动id
        Long discountId = DiscountApi.getDiscountPage(true).get(0).getDiscountId();
        //根据id获取满减活动信息
        DiscountVO discountVO = DiscountApi.getDiscountInfo(discountId);
        //修改信息后，更新满减活动
        DiscountApi.update(DiscountData.getUpdateDiscountDTO(discountVO));
        //用户购买商品
        MyOrderApi.confirm(OrderDtoData.getOrderDtoData(productId, 2));
        //提交订单，获取订单id
        List<Long> orderNumberList =  MyOrderApi.submit(SubmitOrderDtoData.getSubmitOrderDtoData(productId));
        //根据订单id支付订单
        PayApi.orderPay(orderNumberList);
        //对订单进行退款
        TimeUnit.SECONDS.sleep(2L);
        Long refundId = OrderRefund.realOrderRefund(orderNumberList.get(0));
        //平台违规下架活动
        DiscountApi.offline(OfflineHandleEventDtoData.getOfflineHandleEventDTO(null, discountId, "违规下架原因", null, null, null));
        //申请上架，审核失败
        //商家根据活动id获取下架信息
        OfflineHandleEventVO offlineHandleEventVO = DiscountApi.shopGetOfflineHandleEvent(discountId);
        //商家申请上架
        DiscountApi.applyDiscount(OfflineHandleEventDtoData.getOfflineHandleEventDTO(offlineHandleEventVO.getEventId(), offlineHandleEventVO.getHandleId(), null, "申请上架原因", null, null));
        //平台根据活动id获取下架信息
        offlineHandleEventVO = DiscountApi.platformGetOfflineHandleEvent(discountId);
        //平台审核失败
        DiscountApi.auditDiscount(OfflineHandleEventDtoData.getOfflineHandleEventDTO(offlineHandleEventVO.getEventId(), offlineHandleEventVO.getHandleId(), null, null, "审核失败原因", 4));
        //申请上架，审核成功
        //商家根据活动id获取下架信息
        offlineHandleEventVO = DiscountApi.shopGetOfflineHandleEvent(discountId);
        //商家申请上架
        DiscountApi.applyDiscount(OfflineHandleEventDtoData.getOfflineHandleEventDTO(offlineHandleEventVO.getEventId(), offlineHandleEventVO.getHandleId(), null, "申请上架原因", null, null));
        //平台根据活动id获取下架信息
        offlineHandleEventVO = DiscountApi.platformGetOfflineHandleEvent(discountId);
        //平台审核成功
        DiscountApi.auditDiscount(OfflineHandleEventDtoData.getOfflineHandleEventDTO(offlineHandleEventVO.getEventId(), offlineHandleEventVO.getHandleId(), null, null, null, 3));
        //商家删除满减
        DiscountApi.delete(discountId, true);
        //删除商品
        ProductApi.delete(productId, true);
        //验证运行后数据是否正确
        Assert.assertEquals(ShopWalletApi.getMongoShopWalletLogBO(orderNumberList.get(0), true).size(), 2);
        Assert.assertEquals(ShopWalletApi.getOrderDetailVO(orderNumberList.get(0), true).getOrderItemDetailList().get(0).getDiscountAmount(), 5000);
        Assert.assertEquals(ShopWalletApi.getOrderDetailVO(orderNumberList.get(0), refundId, true).getOrderItemDetailList().get(0).getRefundAmount(), 12600);
    }

    @Test(description = "虚拟商品参与满减基本流程", dataProvider = "virtualProduct")
    public void discountFlowWithVirtualProduct(Long productId) throws InterruptedException {
        //新建满减活动
        List<DiscountItemDTO> discountItemDTOList = Arrays.asList(DiscountData.getDiscountItemDTO(10000L,1000L), DiscountData.getDiscountItemDTO(10100L, 5000L));
        List<Long> productIds = Collections.singletonList(productId);
        DiscountApi.save(DiscountData.getDiscount(0, 0, discountItemDTOList, 50000L, 1, productIds, 1), true);
        //获取满减活动id
        Long discountId = DiscountApi.getDiscountPage(true).get(0).getDiscountId();
        //根据id获取满减活动信息
        DiscountVO discountVO = DiscountApi.getDiscountInfo(discountId);
        //修改信息后，更新满减活动
        DiscountApi.update(DiscountData.getUpdateDiscountDTO(discountVO));
        //用户购买商品
        MyOrderApi.confirm(OrderDtoData.getOrderDtoData(productId, 2));
        //提交订单，获取订单id
        List<Long> orderNumberList =  MyOrderApi.submit(SubmitOrderDtoData.getSubmitOrderDtoData(productId));
        //根据订单id支付订单
        PayApi.orderPay(orderNumberList);
        //对订单进行退款
        Long refundId = OrderRefund.virtualOrderRefund(orderNumberList.get(0));
        //商家删除满减
        DiscountApi.delete(discountId, true);
        //删除商品
        ProductApi.delete(productId, true);
        //验证运行后数据是否正确
        Assert.assertEquals(ShopWalletApi.getMongoShopWalletLogBO(orderNumberList.get(0), true).size(), 2);
        Assert.assertEquals(ShopWalletApi.getOrderDetailVO(orderNumberList.get(0), true).getOrderItemDetailList().get(0).getDiscountAmount(), 5000);
        Assert.assertEquals(ShopWalletApi.getOrderDetailVO(orderNumberList.get(0), refundId, true).getOrderItemDetailList().get(0).getRefundAmount(), 12600);
    }

    @Test(description = "全款预售商品参与满减基本流程", dataProvider = "fullPreSaleProduct")
    public void discountFlowWithFullPreSaleProduct(Long productId) throws InterruptedException {
        //新建满减活动
        List<DiscountItemDTO> discountItemDTOList = Arrays.asList(DiscountData.getDiscountItemDTO(10000L,1000L), DiscountData.getDiscountItemDTO(10100L, 5000L));
        List<Long> productIds = Collections.singletonList(productId);
        DiscountApi.save(DiscountData.getDiscount(0, 0, discountItemDTOList, 50000L, 1, productIds, 1), true);
        //获取满减活动id
        Long discountId = DiscountApi.getDiscountPage(true).get(0).getDiscountId();
        //根据id获取满减活动信息
        DiscountVO discountVO = DiscountApi.getDiscountInfo(discountId);
        //修改信息后，更新满减活动
        DiscountApi.update(DiscountData.getUpdateDiscountDTO(discountVO));
        //用户购买商品
        MyOrderApi.confirm(OrderDtoData.getOrderDtoData(productId, 2));
        //提交订单，获取订单id
        List<Long> orderNumberList =  MyOrderApi.submit(SubmitOrderDtoData.getSubmitOrderDtoData(productId));
        //根据订单id支付订单
        PayApi.orderPay(orderNumberList);
        //对订单进行退款
        Long refundId = OrderRefund.realOrderRefund(orderNumberList.get(0));
        //商家删除满减
        DiscountApi.delete(discountId, true);
        //删除商品
        ProductApi.delete(productId, true);
        //验证运行后数据是否正确
        Assert.assertEquals(ShopWalletApi.getMongoShopWalletLogBO(orderNumberList.get(0), true).size(), 2);
        Assert.assertEquals(ShopWalletApi.getOrderDetailVO(orderNumberList.get(0), true).getOrderItemDetailList().get(0).getDiscountAmount(), 5000);
        Assert.assertEquals(ShopWalletApi.getOrderDetailVO(orderNumberList.get(0), refundId, true).getOrderItemDetailList().get(0).getRefundAmount(), 12600);
    }

    @Test(description = "定金预售商品参与满减基本流程", dataProvider = "depositPreSaleProduct")
    public void discountFlowWithDepositPreSaleProduct(Long productId) throws InterruptedException {
        //新建满减活动
        List<DiscountItemDTO> discountItemDTOList = Arrays.asList(DiscountData.getDiscountItemDTO(10000L,1000L), DiscountData.getDiscountItemDTO(10100L, 5000L));
        List<Long> productIds = Collections.singletonList(productId);
        DiscountApi.save(DiscountData.getDiscount(0, 0, discountItemDTOList, 50000L, 1, productIds, 1), true);
        //获取满减活动id
        Long discountId = DiscountApi.getDiscountPage(true).get(0).getDiscountId();
        //根据id获取满减活动信息
        DiscountVO discountVO = DiscountApi.getDiscountInfo(discountId);
        //修改信息后，更新满减活动
        DiscountApi.update(DiscountData.getUpdateDiscountDTO(discountVO));
        //用户购买商品
        MyOrderApi.confirm(OrderDtoData.getOrderDtoData(productId, 2));
        //提交订单，获取订单id
        List<Long> orderNumberList =  MyOrderApi.submit(SubmitOrderDtoData.getSubmitOrderDtoData(productId));
        //根据订单id支付订单
        PayApi.orderPay(orderNumberList);
        //根据订单id支付尾款
        TimeUnit.SECONDS.sleep(2L);
        PayApi.orderPayBalance(orderNumberList.get(0));
        //对订单进行退款
        Long refundId = OrderRefund.realOrderRefund(orderNumberList.get(0));
        //商家删除满减
        DiscountApi.delete(discountId, true);
        //删除商品
        ProductApi.delete(productId, true);
        //验证运行后数据是否正确
        Assert.assertEquals(ShopWalletApi.getMongoShopWalletLogBO(orderNumberList.get(0), true).size(), 2);
        Assert.assertEquals(ShopWalletApi.getOrderDetailVO(orderNumberList.get(0), true).getOrderItemDetailList().get(0).getDiscountAmount(), 5000);
        Assert.assertEquals(ShopWalletApi.getOrderDetailVO(orderNumberList.get(0), refundId, true).getOrderItemDetailList().get(0).getRefundAmount(), 10600);
    }

    @Test(description = "供应商发货的供应商商品参与满减基本流程", dataProvider = "supplierProduct")
    public void discountFlowWithSupplierProduct(Long productId, Long supplierProductId) throws InterruptedException {
        //新建满减活动
        List<DiscountItemDTO> discountItemDTOList = Arrays.asList(DiscountData.getDiscountItemDTO(10000L,1000L), DiscountData.getDiscountItemDTO(10100L, 4000L));
        List<Long> productIds = Collections.singletonList(productId);
        DiscountApi.save(DiscountData.getDiscount(0, 0, discountItemDTOList, 50000L, 1, productIds, 1), true);
        //获取满减活动id
        Long discountId = DiscountApi.getDiscountPage(true).get(0).getDiscountId();
        //根据id获取满减活动信息
        DiscountVO discountVO = DiscountApi.getDiscountInfo(discountId);
        //修改信息后，更新满减活动
        DiscountApi.update(DiscountData.getUpdateDiscountDTO(discountVO));
        //用户购买商品
        MyOrderApi.confirm(OrderDtoData.getOrderDtoData(productId, 2));
        //提交订单，获取订单id
        TimeUnit.SECONDS.sleep(2L);
        List<Long> orderNumberList =  MyOrderApi.submit(SubmitOrderDtoData.getSubmitOrderDtoData(productId));
        //根据订单id支付订单
        PayApi.orderPay(orderNumberList);
        //对订单进行退款
        Long refundId = OrderRefund.supplierOrderRefund(orderNumberList.get(0));
        //商家删除满减
        DiscountApi.delete(discountId, true);
        //删除商品
        ProductApi.delete(productId, true);
        SupplierProductApi.delete(supplierProductId, true);
        //验证运行后数据是否正确
        Assert.assertEquals(ShopWalletApi.getMongoShopWalletLogBO(orderNumberList.get(0), true).size(), 2);
        Assert.assertEquals(ShopWalletApi.getOrderDetailVO(orderNumberList.get(0), true).getOrderItemDetailList().get(0).getDiscountAmount(), 4000);
        Assert.assertEquals(ShopWalletApi.getOrderDetailVO(orderNumberList.get(0), refundId, true).getOrderItemDetailList().get(0).getRefundAmount(), 13600);
    }
}
