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

import com.alibaba.fastjson2.JSON;
import com.tmerclub.cloud.api.platform.vo.OfflineHandleEventVO;
import com.tmerclub.cloud.common.product.vo.app.CategoryAppVO;
import com.tmerclub.cloud.common.product.vo.app.SpuAppVO;
import com.moyuer.cloud.group.dto.GroupActivityDTO;
import com.moyuer.cloud.group.vo.GroupActivityVO;
import com.moyuer.cloud.group.vo.app.AppGroupTeamVO;
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.GroupApi;
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.GroupData;
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.List;
import java.util.concurrent.TimeUnit;

/**
 * @author lpj
 */
public class GroupTest {
    @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 = "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 groupFlowWithRealProduct(Long productId) throws InterruptedException {
        //新建拼团活动
        GroupApi.save(GroupData.getGroupActivityDTO(productId));
        //获取拼团活动id
        Long groupActivityId = GroupApi.getGroupActivityPage().get(0).getGroupActivityId();
        //根据id获取拼团活动信息
        GroupActivityVO groupActivityVO = GroupApi.getGroupActivity(groupActivityId);
        //修改信息后，更新拼团活动
        GroupApi.updateGroupActivity(GroupData.getUpdateGroupActivityDTO(groupActivityVO));
        //平台违规下架活动
        GroupApi.offline(OfflineHandleEventDtoData.getOfflineHandleEventDTO(null, groupActivityId, "违规下架原因", null, null, null));
        //申请上架，审核失败
        //商家根据活动id获取下架信息
        OfflineHandleEventVO offlineHandleEventVO = GroupApi.shopGetOfflineHandleEvent(groupActivityId);
        //商家申请上架
        GroupApi.applyGroupActivity(OfflineHandleEventDtoData.getOfflineHandleEventDTO(offlineHandleEventVO.getEventId(), offlineHandleEventVO.getHandleId(), null, "申请上架原因", null, null));
        //平台根据活动id获取下架信息
        offlineHandleEventVO = GroupApi.platformGetOfflineHandleEvent(groupActivityId);
        //平台审核失败
        GroupApi.auditGroupActivity(OfflineHandleEventDtoData.getOfflineHandleEventDTO(offlineHandleEventVO.getEventId(), offlineHandleEventVO.getHandleId(), null, null, "审核失败原因", 4));
        //申请上架，审核成功
        //商家根据活动id获取下架信息
        offlineHandleEventVO = GroupApi.shopGetOfflineHandleEvent(groupActivityId);
        //商家申请上架
        GroupApi.applyGroupActivity(OfflineHandleEventDtoData.getOfflineHandleEventDTO(offlineHandleEventVO.getEventId(), offlineHandleEventVO.getHandleId(), null, "申请上架原因", null, null));
        //平台根据活动id获取下架信息
        offlineHandleEventVO = GroupApi.platformGetOfflineHandleEvent(groupActivityId);
        //平台审核成功
        GroupApi.auditGroupActivity(OfflineHandleEventDtoData.getOfflineHandleEventDTO(offlineHandleEventVO.getEventId(), offlineHandleEventVO.getHandleId(), null, null, null, 3));
        //商家启用活动
        GroupActivityDTO groupActivityDTO = new GroupActivityDTO();
        groupActivityDTO.setGroupActivityId(groupActivityId);
        GroupApi.activeGroupActivity(groupActivityDTO);
        //获取商品详情
        SpuAppVO productInfo = ProductApi.getProductInfo(productId);
        //获取店铺id
        Long shopId = productInfo.getShopId();
        //用户A开团
        GroupApi.openGroup(GroupData.getGroupOrderDTO(productId, 1));
        //用户A提交订单，获取订单id
        List<Long> orderNumberListA = GroupApi.submitGroupOrder(GroupData.getSubmitGroupOrderDTO(0L, shopId));
        //根据订单id支付订单
        PayApi.orderPay(orderNumberListA);
        //等待保存
        TimeUnit.SECONDS.sleep(2L);
        //获取可参团列表
        List<AppGroupTeamVO> joinGroupList = JSON.parseArray(GroupApi.getJoinGroupList(groupActivityId).toString(), AppGroupTeamVO.class);
        //获取团队id
        Long groupTeamId = joinGroupList.get(0).getGroupTeamId();
        //用户B参团
        GroupApi.joinGroup(GroupData.getGroupOrderDTO(productId, 1));
        //用户A提交订单，获取订单id
        List<Long> orderNumberListB = GroupApi.submitJoinGroupOrder(GroupData.getSubmitGroupOrderDTO(groupTeamId, shopId));
        //根据订单id支付订单
        PayApi.orderPay(orderNumberListB);
        //用户A对订单进行退款
        TimeUnit.SECONDS.sleep(2L);
        Long refundId = OrderRefund.realOrderRefund(orderNumberListA.get(0));
        //商家失效活动
        GroupApi.invalidActivity(groupActivityDTO);
        //删除拼团活动
        GroupApi.deleteGroupActivity(groupActivityId);
        //删除商品
        ProductApi.delete(productId, true);
        //验证运行后数据是否正确
        Assert.assertEquals(ShopWalletApi.getMongoShopWalletLogBO(orderNumberListA.get(0), true).size(), 2);
        Assert.assertEquals(ShopWalletApi.getOrderDetailVO(orderNumberListA.get(0), true).getOrderItemDetailList().get(0).getGroupAmount(), 2200);
        Assert.assertEquals(ShopWalletApi.getOrderDetailVO(orderNumberListA.get(0), refundId, true).getOrderItemDetailList().get(0).getRefundAmount(), 6600);
    }

    @Test(description = "虚拟商品拼团活动基本流程", dataProvider = "virtualProduct")
    public void groupFlowWithVirtualProduct(Long productId) throws InterruptedException {
        //新建拼团活动
        GroupApi.save(GroupData.getGroupActivityDTO(productId));
        //获取拼团活动id
        Long groupActivityId = GroupApi.getGroupActivityPage().get(0).getGroupActivityId();
        //根据id获取拼团活动信息
        GroupActivityVO groupActivityVO = GroupApi.getGroupActivity(groupActivityId);
        //修改信息后，更新拼团活动
        GroupApi.updateGroupActivity(GroupData.getUpdateGroupActivityDTO(groupActivityVO));
       //商家启用活动
        GroupActivityDTO groupActivityDTO = new GroupActivityDTO();
        groupActivityDTO.setGroupActivityId(groupActivityId);
        GroupApi.activeGroupActivity(groupActivityDTO);
        //获取商品详情
        SpuAppVO productInfo = ProductApi.getProductInfo(productId);
        //获取店铺id
        Long shopId = productInfo.getShopId();
        //用户A开团
        GroupApi.openGroup(GroupData.getGroupOrderDTO(productId, 1));
        //用户A提交订单，获取订单id
        List<Long> orderNumberListA = GroupApi.submitGroupOrder(GroupData.getSubmitGroupOrderDTO(0L, shopId));
        //根据订单id支付订单
        PayApi.orderPay(orderNumberListA);
        //等待保存
        TimeUnit.SECONDS.sleep(2L);
        //获取可参团列表
        List<AppGroupTeamVO> joinGroupList = JSON.parseArray(GroupApi.getJoinGroupList(groupActivityId).toString(), AppGroupTeamVO.class);
        //获取团队id
        Long groupTeamId = joinGroupList.get(0).getGroupTeamId();
        //用户B参团
        GroupApi.joinGroup(GroupData.getGroupOrderDTO(productId, 1));
        //用户A提交订单，获取订单id
        List<Long> orderNumberListB = GroupApi.submitJoinGroupOrder(GroupData.getSubmitGroupOrderDTO(groupTeamId, shopId));
        //根据订单id支付订单
        PayApi.orderPay(orderNumberListB);
        //用户A对订单进行退款
        TimeUnit.SECONDS.sleep(2L);
        Long refundId = OrderRefund.virtualOrderRefund(orderNumberListA.get(0));
        //商家失效活动
        GroupApi.invalidActivity(groupActivityDTO);
        //删除拼团活动
        GroupApi.deleteGroupActivity(groupActivityId);
        //删除商品
        ProductApi.delete(productId, true);
        //验证运行后数据是否正确
        Assert.assertEquals(ShopWalletApi.getMongoShopWalletLogBO(orderNumberListA.get(0), true).size(), 2);
        Assert.assertEquals(ShopWalletApi.getOrderDetailVO(orderNumberListA.get(0), true).getOrderItemDetailList().get(0).getGroupAmount(), 2200);
        Assert.assertEquals(ShopWalletApi.getOrderDetailVO(orderNumberListA.get(0), refundId, true).getOrderItemDetailList().get(0).getRefundAmount(), 6600);
    }

    @Test(description = "全款预售商品拼团活动基本流程", dataProvider = "fullPreSaleProduct")
    public void groupFlowWithFullPreSaleProduct(Long productId) throws InterruptedException {
        //新建拼团活动
        GroupApi.save(GroupData.getGroupActivityDTO(productId));
        //获取拼团活动id
        Long groupActivityId = GroupApi.getGroupActivityPage().get(0).getGroupActivityId();
        //根据id获取拼团活动信息
        GroupActivityVO groupActivityVO = GroupApi.getGroupActivity(groupActivityId);
        //修改信息后，更新拼团活动
        GroupApi.updateGroupActivity(GroupData.getUpdateGroupActivityDTO(groupActivityVO));
        //商家启用活动
        GroupActivityDTO groupActivityDTO = new GroupActivityDTO();
        groupActivityDTO.setGroupActivityId(groupActivityId);
        GroupApi.activeGroupActivity(groupActivityDTO);
        //获取商品详情
        SpuAppVO productInfo = ProductApi.getProductInfo(productId);
        //获取店铺id
        Long shopId = productInfo.getShopId();
        //用户A开团
        GroupApi.openGroup(GroupData.getGroupOrderDTO(productId, 1));
        //用户A提交订单，获取订单id
        List<Long> orderNumberListA = GroupApi.submitGroupOrder(GroupData.getSubmitGroupOrderDTO(0L, shopId));
        //根据订单id支付订单
        PayApi.orderPay(orderNumberListA);
        //等待保存
        TimeUnit.SECONDS.sleep(2L);
        //获取可参团列表
        List<AppGroupTeamVO> joinGroupList = JSON.parseArray(GroupApi.getJoinGroupList(groupActivityId).toString(), AppGroupTeamVO.class);
        //获取团队id
        Long groupTeamId = joinGroupList.get(0).getGroupTeamId();
        //用户B参团
        GroupApi.joinGroup(GroupData.getGroupOrderDTO(productId, 1));
        //用户A提交订单，获取订单id
        List<Long> orderNumberListB = GroupApi.submitJoinGroupOrder(GroupData.getSubmitGroupOrderDTO(groupTeamId, shopId));
        //根据订单id支付订单
        PayApi.orderPay(orderNumberListB);
        //用户A对订单进行退款
        TimeUnit.SECONDS.sleep(2L);
        Long refundId = OrderRefund.virtualOrderRefund(orderNumberListA.get(0));
        //商家失效活动
        GroupApi.invalidActivity(groupActivityDTO);
        //删除拼团活动
        GroupApi.deleteGroupActivity(groupActivityId);
        //删除商品
        ProductApi.delete(productId, true);
        //验证运行后数据是否正确
        Assert.assertEquals(ShopWalletApi.getMongoShopWalletLogBO(orderNumberListA.get(0), true).size(), 2);
        Assert.assertEquals(ShopWalletApi.getOrderDetailVO(orderNumberListA.get(0), true).getOrderItemDetailList().get(0).getGroupAmount(), 2200);
        Assert.assertEquals(ShopWalletApi.getOrderDetailVO(orderNumberListA.get(0), refundId, true).getOrderItemDetailList().get(0).getRefundAmount(), 6600);
    }

    @Test(description = "供应商发货的供应商商品拼团活动基本流程", dataProvider = "supplierProduct")
    public void groupFlowWithSupplierProduct(Long productId, Long supplierProductId) throws InterruptedException {
        //新建拼团活动
        GroupApi.save(GroupData.getGroupActivityDTO(productId));
        //获取拼团活动id
        Long groupActivityId = GroupApi.getGroupActivityPage().get(0).getGroupActivityId();
        //根据id获取拼团活动信息
        GroupActivityVO groupActivityVO = GroupApi.getGroupActivity(groupActivityId);
        //修改信息后，更新拼团活动
        GroupApi.updateGroupActivity(GroupData.getUpdateGroupActivityDTO(groupActivityVO));
        //商家启用活动
        GroupActivityDTO groupActivityDTO = new GroupActivityDTO();
        groupActivityDTO.setGroupActivityId(groupActivityId);
        GroupApi.activeGroupActivity(groupActivityDTO);
        //获取商品详情
        SpuAppVO productInfo = ProductApi.getProductInfo(productId);
        //获取店铺id
        Long shopId = productInfo.getShopId();
        //用户A开团
        GroupApi.openGroup(GroupData.getGroupOrderDTO(productId, 1));
        //用户A提交订单，获取订单id
        TimeUnit.SECONDS.sleep(2L);
        List<Long> orderNumberListA = GroupApi.submitGroupOrder(GroupData.getSubmitGroupOrderDTO(0L, shopId));
        //根据订单id支付订单
        PayApi.orderPay(orderNumberListA);
        //等待保存
        TimeUnit.SECONDS.sleep(2L);
        //获取可参团列表
        List<AppGroupTeamVO> joinGroupList = JSON.parseArray(GroupApi.getJoinGroupList(groupActivityId).toString(), AppGroupTeamVO.class);
        //获取团队id
        Long groupTeamId = joinGroupList.get(0).getGroupTeamId();
        //用户B参团
        GroupApi.joinGroup(GroupData.getGroupOrderDTO(productId, 1));
        //用户A提交订单，获取订单id
        List<Long> orderNumberListB = GroupApi.submitJoinGroupOrder(GroupData.getSubmitGroupOrderDTO(groupTeamId, shopId));
        //根据订单id支付订单
        PayApi.orderPay(orderNumberListB);
        //用户A对订单进行退款
        TimeUnit.SECONDS.sleep(2L);
        Long refundId = OrderRefund.supplierOrderRefund(orderNumberListA.get(0));
        //商家失效活动
        GroupApi.invalidActivity(groupActivityDTO);
        //删除拼团活动
        GroupApi.deleteGroupActivity(groupActivityId);
        //删除商品
        ProductApi.delete(productId, true);
        SupplierProductApi.delete(supplierProductId, true);
        //验证运行后数据是否正确
        Assert.assertEquals(ShopWalletApi.getMongoShopWalletLogBO(orderNumberListA.get(0), true).size(), 2);
        Assert.assertEquals(ShopWalletApi.getOrderDetailVO(orderNumberListA.get(0), true).getOrderItemDetailList().get(0).getSupplyPriceFee(), 6600);
        Assert.assertEquals(ShopWalletApi.getOrderDetailVO(orderNumberListA.get(0), true).getOrderItemDetailList().get(0).getGroupAmount(), 2200);
        Assert.assertEquals(ShopWalletApi.getOrderDetailVO(orderNumberListA.get(0), refundId, true).getOrderItemDetailList().get(0).getRefundAmount(), 6600);
    }
}
