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

import com.tmerclub.cloud.common.product.vo.GiveawayVO;
import com.tmerclub.cloud.common.product.vo.app.CategoryAppVO;
import com.tmerclub.cloud.order.vo.OrderItemDetailVO;
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.GiveawayApi;
import com.moyuer.cloud.systemtest.api.order.MyOrderApi;
import com.moyuer.cloud.systemtest.api.order.OrderApi;
import com.moyuer.cloud.systemtest.api.order.OrderRefundApi;
import com.moyuer.cloud.systemtest.api.order.ShopOrderRefundApi;
import com.moyuer.cloud.systemtest.api.store.ShopWalletApi;
import com.moyuer.cloud.systemtest.data.amount.PreSaleSpuData;
import com.moyuer.cloud.systemtest.data.amount.SpuData;
import com.moyuer.cloud.systemtest.data.marketing.GiveawayData;
import com.moyuer.cloud.systemtest.data.order.*;
import com.moyuer.cloud.systemtest.data.store.DeliveryData;
import org.testng.Assert;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;

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

/**
 * @author lpj
 */
public class GiveawayTest {
    @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 mainProductId = ProductApi.createProduct(SpuData.getRealSpu(categoryId,shopCategoryId, DeliveryData.getFreeFeeDelivery(true),8800L), true);
        Long matchingProductId = ProductApi.createProduct(SpuData.getRealSpu(categoryId,shopCategoryId, DeliveryData.getFreeFeeDelivery(true),8800L), true);
        return new Object[][]{{mainProductId, Collections.singletonList(matchingProductId)}};
    }

    @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 mainProductId = ProductApi.createProduct(PreSaleSpuData.getFullPreSaleSpu(categoryId,shopCategoryId, DeliveryData.getFreeFeeDelivery(true),8800L), true);
        Long matchingProductId = ProductApi.createProduct(SpuData.getRealSpu(categoryId,shopCategoryId, DeliveryData.getFreeFeeDelivery(true),8800L), true);
        return new Object[][]{{mainProductId, Collections.singletonList(matchingProductId)}};
    }

    @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 mainProductId = ProductApi.createProduct(PreSaleSpuData.getDepositPreSaleSpu(categoryId,shopCategoryId, DeliveryData.getFreeFeeDelivery(true),8800L), true);
        Long matchingProductId = ProductApi.createProduct(SpuData.getRealSpu(categoryId,shopCategoryId, DeliveryData.getFreeFeeDelivery(true),8800L), true);
        return new Object[][]{{mainProductId, Collections.singletonList(matchingProductId)}};
    }

    @Test(description = "实物商品作为主商品的赠品活动基本流程", dataProvider = "realProduct")
    public void giveawayFlowWithRealProduct(Long mainProductId, List<Long> matchingProductIds) throws InterruptedException {
        //新增赠品活动
        GiveawayApi.save(GiveawayData.getGiveawayDTO(mainProductId, matchingProductIds));
        //获取赠品活动id
        Long giveawayId = GiveawayApi.getGiveawayPage().get(0).getGiveawayId();
        //获取赠品活动信息
        GiveawayVO giveawayVO = GiveawayApi.getGiveawayInfo(giveawayId);
        //修改赠品活动信息
        GiveawayApi.update(GiveawayData.getUpdateGiveawayDTO(giveawayVO));
        //购买五件主商品，赠送赠品
        MyOrderApi.confirm(OrderDtoData.getOrderDtoData(mainProductId, 5));
        //提交订单，获取订单id
        List<Long> orderNumberList =  MyOrderApi.submit(SubmitOrderDtoData.getSubmitOrderDtoData(mainProductId));
        //根据订单id支付订单
        PayApi.orderPay(orderNumberList);
        //对订单进行退款
        Long refundId = realOrderRefund(orderNumberList.get(0));
        //失效赠品活动
        GiveawayApi.updateGiveawayStatus(0, giveawayId);
        //删除赠品活动
        GiveawayApi.updateGiveawayStatus(-1, giveawayId);
        //删除商品
        ProductApi.delete(mainProductId, true);
        for (Long matchingProductId : matchingProductIds) {
            ProductApi.delete(matchingProductId, true);
        }
        //验证运行后数据是否正确
        Assert.assertEquals(ShopWalletApi.getMongoShopWalletLogBO(orderNumberList.get(0), true).size(), 3);
        for (OrderItemDetailVO orderItemDetailVO : ShopWalletApi.getOrderDetailVO(orderNumberList.get(0), refundId, true).getOrderItemDetailList()) {
            if (orderItemDetailVO.getCount().equals(5)) {
                Assert.assertEquals(orderItemDetailVO.getActualTotal(), 44000);
                Assert.assertEquals(orderItemDetailVO.getRefundAmount(), 42900);
            }
        }
    }

    @Test(description = "全款预售商品作为主商品的赠品活动基本流程", dataProvider = "fullPreSaleProduct")
    public void giveawayFlowWithFullPreSaleProduct(Long mainProductId, List<Long> matchingProductIds) throws InterruptedException {
        //新增赠品活动
        GiveawayApi.save(GiveawayData.getGiveawayDTO(mainProductId, matchingProductIds));
        //获取赠品活动id
        Long giveawayId = GiveawayApi.getGiveawayPage().get(0).getGiveawayId();
        //获取赠品活动信息
        GiveawayVO giveawayVO = GiveawayApi.getGiveawayInfo(giveawayId);
        //修改赠品活动信息
        GiveawayApi.update(GiveawayData.getUpdateGiveawayDTO(giveawayVO));
        //购买五件主商品，赠送赠品
        MyOrderApi.confirm(OrderDtoData.getOrderDtoData(mainProductId, 5));
        //提交订单，获取订单id
        List<Long> orderNumberList =  MyOrderApi.submit(SubmitOrderDtoData.getSubmitOrderDtoData(mainProductId));
        //根据订单id支付订单
        PayApi.orderPay(orderNumberList);
        //对订单进行退款
        Long refundId = realOrderRefund(orderNumberList.get(0));
        //失效赠品活动
        GiveawayApi.updateGiveawayStatus(0, giveawayId);
        //删除赠品活动
        GiveawayApi.updateGiveawayStatus(-1, giveawayId);
        //删除商品
        ProductApi.delete(mainProductId, true);
        for (Long matchingProductId : matchingProductIds) {
            ProductApi.delete(matchingProductId, true);
        }
        //验证运行后数据是否正确
        Assert.assertEquals(ShopWalletApi.getMongoShopWalletLogBO(orderNumberList.get(0), true).size(), 3);
        for (OrderItemDetailVO orderItemDetailVO : ShopWalletApi.getOrderDetailVO(orderNumberList.get(0), refundId, true).getOrderItemDetailList()) {
            if (orderItemDetailVO.getCount().equals(5)) {
                Assert.assertEquals(orderItemDetailVO.getActualTotal(), 44000);
                Assert.assertEquals(orderItemDetailVO.getRefundAmount(), 42900);
            }
        }
    }

    @Test(description = "定金预售商品作为主商品的赠品活动基本流程", dataProvider = "depositPreSaleProduct")
    public void giveawayFlowWithDepositProduct(Long mainProductId, List<Long> matchingProductIds) throws InterruptedException {
        //新增赠品活动
        GiveawayApi.save(GiveawayData.getGiveawayDTO(mainProductId, matchingProductIds));
        //获取赠品活动id
        Long giveawayId = GiveawayApi.getGiveawayPage().get(0).getGiveawayId();
        //获取赠品活动信息
        GiveawayVO giveawayVO = GiveawayApi.getGiveawayInfo(giveawayId);
        //修改赠品活动信息
        GiveawayApi.update(GiveawayData.getUpdateGiveawayDTO(giveawayVO));
        //购买五件主商品，赠送赠品
        MyOrderApi.confirm(OrderDtoData.getOrderDtoData(mainProductId, 5));
        //提交订单，获取订单id
        List<Long> orderNumberList =  MyOrderApi.submit(SubmitOrderDtoData.getSubmitOrderDtoData(mainProductId));
        //根据订单id支付订单
        PayApi.orderPay(orderNumberList);
        //根据订单id支付尾款
        TimeUnit.SECONDS.sleep(1L);
        PayApi.orderPayBalance(orderNumberList.get(0));
        //对订单进行退款
        Long refundId = realOrderRefund(orderNumberList.get(0));
        //失效赠品活动
        GiveawayApi.updateGiveawayStatus(0, giveawayId);
        //删除赠品活动
        GiveawayApi.updateGiveawayStatus(-1, giveawayId);
        //删除商品
        ProductApi.delete(mainProductId, true);
        for (Long matchingProductId : matchingProductIds) {
            ProductApi.delete(matchingProductId, true);
        }
        //验证运行后数据是否正确
        Assert.assertEquals(ShopWalletApi.getMongoShopWalletLogBO(orderNumberList.get(0), true).size(), 3);
        for (OrderItemDetailVO orderItemDetailVO : ShopWalletApi.getOrderDetailVO(orderNumberList.get(0), refundId, true).getOrderItemDetailList()) {
            if (orderItemDetailVO.getCount().equals(5)) {
                Assert.assertEquals(orderItemDetailVO.getActualTotal(), 39000);
                Assert.assertEquals(orderItemDetailVO.getRefundAmount(), 37900);
            }
        }
    }

    private static Long realOrderRefund(Long orderNum) throws InterruptedException {
        //申请第一次退款，部分退款 等待保存
        TimeUnit.SECONDS.sleep(2L);
        Long refundIdFirst= OrderRefundApi.apply(OrderRefundDtoData.getOrderRefundDTO(orderNum,0L));
        ShopOrderRefundApi.infoByRefundSn(refundIdFirst);
        ShopOrderRefundApi.processRefundOrder(ShopOrderRefundDtoData.getRufuseOrderRefundDTO(refundIdFirst));

        //申请第二次退款，整单退款
        TimeUnit.SECONDS.sleep(2L);
        Long refundIdSec= OrderRefundApi.apply(OrderRefundDtoData.getAllOrderRefundDTO(orderNum));
        ShopOrderRefundApi.infoByRefundSn(refundIdSec);
        ShopOrderRefundApi.processRefundOrder(ShopOrderRefundDtoData.getRufuseOrderRefundDTO(refundIdSec));
        //发货后对订单进行部分退款仅退款 & 整单退货退款：先同意退款申请，发货后拒绝退款
        OrderApi.delivery(DeliveryOrderDtoData.getNoDeliveryOrderDtoData(orderNum));
        TimeUnit.SECONDS.sleep(2L);
        Long refundIdTir= OrderRefundApi.apply(OrderRefundDtoData.getOrderRefundDTO(orderNum,0L));
        ShopOrderRefundApi.infoByRefundSn(refundIdTir);
        ShopOrderRefundApi.processRefundOrder(ShopOrderRefundDtoData.getRufuseOrderRefundDTO(refundIdTir));

        TimeUnit.SECONDS.sleep(2L);
        Long refundIdFourth= OrderRefundApi.apply(OrderRefundDtoData.getAllOrderRefundDeliveryDTO(orderNum));
        ShopOrderRefundApi.infoByRefundSn(refundIdFourth);
        ShopOrderRefundApi.processRefundOrder(ShopOrderRefundDtoData.getReceiveRefundDTO(refundIdFourth));
        OrderRefundApi.submitExpress(OrderRefundDeliveryDtoData.getOrderRefundDeliveryDTO(refundIdFourth));
        TimeUnit.SECONDS.sleep(1L);
        ShopOrderRefundApi.returnMoney(ShopOrderRefundDtoData.getRefuseDeliveryRefundDTO(refundIdFourth));
        //确认收货后，对订单进行部分退款仅退款 & 整单退货退款且同意
        MyOrderApi.receipt(orderNum);
        TimeUnit.SECONDS.sleep(2L);
        Long refundIdFirth= OrderRefundApi.apply(OrderRefundDtoData.getOrderRefundDtoRemoveGiveaway(orderNum));
        ShopOrderRefundApi.infoByRefundSn(refundIdFirth);
        ShopOrderRefundApi.processRefundOrder(ShopOrderRefundDtoData.getReceiveRefundDTO(refundIdFirth));
        OrderRefundApi.submitExpress(OrderRefundDeliveryDtoData.getOrderRefundDeliveryDTO(refundIdFirth));
        ShopOrderRefundApi.returnMoney(ShopOrderRefundDtoData.getReceiveDeliveryRefundDTO(refundIdFirth));

        return refundIdFirth;
    }
}
