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

import com.tmerclub.cloud.api.distribution.vo.DistributionSpuVO;
import com.tmerclub.cloud.api.platform.vo.OfflineHandleEventVO;
import com.tmerclub.cloud.common.product.vo.app.CategoryAppVO;
import com.moyuer.cloud.systemtest.api.amount.CategoryApi;
import com.moyuer.cloud.systemtest.api.amount.ProductApi;
import com.moyuer.cloud.systemtest.api.marketing.DistributionProdApi;
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.DistributionProdData;
import com.moyuer.cloud.systemtest.data.store.DeliveryData;
import com.moyuer.cloud.systemtest.data.supplier.SupplierProductData;
import org.springframework.beans.BeanUtils;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;

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

/**
 * @author lpj
 */
public class DistributionProdTest {
    @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 realDistributionProdFlow(Long productId) throws InterruptedException {
        distributionProdFlow(productId);
    }

    @Test(description = "虚拟分销商品基本流程", dataProvider = "virtualProduct")
    public void virtualDistributionProdFlow(Long productId) throws InterruptedException {
        distributionProdFlow(productId);
    }

    @Test(description = "全款预售分销商品基本流程", dataProvider = "fullPreSaleProduct")
    public void fullPreSaleDistributionProdFlow(Long productId) throws InterruptedException {
        distributionProdFlow(productId);
    }

    @Test(description = "定金预售分销商品基本流程", dataProvider = "depositPreSaleProduct")
    public void depositPreSaleDistributionProdFlow(Long productId) throws InterruptedException {
        distributionProdFlow(productId);
    }

    @Test(description = "供应商发货的供应商分销商品基本流程", dataProvider = "supplierProduct")
    public void supplierDistributionProdFlow(Long productId, Long supplierProductId) throws InterruptedException {
        distributionProdFlow(productId);
        SupplierProductApi.delete(supplierProductId, true);
    }

    /**
     * 分销商品基本流程
     */
    private void distributionProdFlow(Long productId) throws InterruptedException {
        //新增分销商品
        DistributionProdApi.save(DistributionProdData.getDistributionSpuDTO(productId, 0, 2000L, 0, 1000L, 1), true);
        //等待保存
        TimeUnit.SECONDS.sleep(2L);
        //获取分销商品id
        Long distributionProdId = DistributionProdApi.getDistributionProdPage(true).get(0).getDistributionInfo().getDistributionSpuId();
        //获取分销商品详情
        DistributionSpuVO distributionProdVO = new DistributionSpuVO();
        BeanUtils.copyProperties(DistributionProdApi.getDistributionProdPage(true).get(0).getDistributionInfo(), distributionProdVO);
        distributionProdVO.setSpuId(productId);
        //修改分销商品
        DistributionProdApi.update(DistributionProdData.getUpdateDistributionSpuDTO(distributionProdVO));
        //平台违规下架活动
        DistributionProdApi.offline(OfflineHandleEventDtoData.getOfflineHandleEventDTO(null, distributionProdId, "违规下架原因", null, null, null));
        //申请上架，审核失败
        //商家根据活动id获取下架信息
        OfflineHandleEventVO offlineHandleEventVO = DistributionProdApi.shopGetOfflineHandleEvent(distributionProdId);
        //商家申请上架
        DistributionProdApi.apply(OfflineHandleEventDtoData.getOfflineHandleEventDTO(offlineHandleEventVO.getEventId(), offlineHandleEventVO.getHandleId(), null, "申请上架原因", null, null));
        //平台根据活动id获取下架信息
        offlineHandleEventVO = DistributionProdApi.platformGetOfflineHandleEvent(distributionProdId);
        //平台审核失败
        DistributionProdApi.audit(OfflineHandleEventDtoData.getOfflineHandleEventDTO(offlineHandleEventVO.getEventId(), offlineHandleEventVO.getHandleId(), null, null, "审核失败原因", 4));
        //申请上架，审核成功
        //商家根据活动id获取下架信息
        offlineHandleEventVO = DistributionProdApi.shopGetOfflineHandleEvent(distributionProdId);
        //商家申请上架
        DistributionProdApi.apply(OfflineHandleEventDtoData.getOfflineHandleEventDTO(offlineHandleEventVO.getEventId(), offlineHandleEventVO.getHandleId(), null, "申请上架原因", null, null));
        //平台根据活动id获取下架信息
        offlineHandleEventVO = DistributionProdApi.platformGetOfflineHandleEvent(distributionProdId);
        //平台审核成功
        DistributionProdApi.audit(OfflineHandleEventDtoData.getOfflineHandleEventDTO(offlineHandleEventVO.getEventId(), offlineHandleEventVO.getHandleId(), null, null, null, 3));
        //删除分销商品
        DistributionProdApi.delete(distributionProdId, true);
        //删除商品
        ProductApi.delete(productId, true);
    }
}
