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.seckill.model.SeckillCategory;
import com.tmerclub.cloud.seckill.vo.SeckillSpuVO;
import com.moyuer.cloud.systemtest.api.amount.CategoryApi;
import com.moyuer.cloud.systemtest.api.amount.ProductApi;
import com.moyuer.cloud.systemtest.api.marketing.SeckillApi;
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.SeckillData;
import com.moyuer.cloud.systemtest.data.store.DeliveryData;
import com.moyuer.cloud.systemtest.data.supplier.SupplierProductData;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;

import java.util.List;

/**
 * @author lpj
 */
public class SeckillTest {
    @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 secKillFlowWithRealProduct(Long productId) {
        //新增秒杀分类
        SeckillApi.saveSeckillCategory(SeckillData.getSeckillCategoryDTO());
        //获取秒杀分类id
        List<SeckillCategory> seckillCategoryList = JSON.parseArray(SeckillApi.getSeckillCategoryList().toString(), SeckillCategory.class);
        Long seckillCategoryId = seckillCategoryList.get(seckillCategoryList.size()-1).getCategoryId();
        //参与秒杀的时间戳
        Long startTimestamps = SeckillApi.getSeckillList().get(1).getStartTime().getTime();
        //新建秒杀活动
        SeckillApi.saveSeckill(SeckillData.getSeckillDTO(productId,seckillCategoryId, startTimestamps));
        //获取秒杀活动id
        List<SeckillSpuVO> seckillSpuVOList = SeckillApi.getSeckillSpuList(startTimestamps);
        Long seckillId = seckillSpuVOList.get(seckillSpuVOList.size()-1).getSeckillId();
        //平台违规下架活动
        SeckillApi.offline(OfflineHandleEventDtoData.getOfflineHandleEventDTO(null, seckillId, "违规下架原因", null, null, null));
        //申请上架，审核失败
        //商家根据活动id获取下架信息
        OfflineHandleEventVO offlineHandleEventVO = SeckillApi.shopGetOfflineHandleEvent(seckillId);
        //商家申请上架
        SeckillApi.apply(OfflineHandleEventDtoData.getOfflineHandleEventDTO(offlineHandleEventVO.getEventId(), offlineHandleEventVO.getHandleId(), null, "申请上架原因", null, null));
        //平台根据活动id获取下架信息
        offlineHandleEventVO = SeckillApi.platformGetOfflineHandleEvent(seckillId);
        //平台审核失败
        SeckillApi.audit(OfflineHandleEventDtoData.getOfflineHandleEventDTO(offlineHandleEventVO.getEventId(), offlineHandleEventVO.getHandleId(), null, null, "审核失败原因", 4));
        //申请上架，审核成功
        //商家根据活动id获取下架信息
        offlineHandleEventVO = SeckillApi.shopGetOfflineHandleEvent(seckillId);
        //商家申请上架
        SeckillApi.apply(OfflineHandleEventDtoData.getOfflineHandleEventDTO(offlineHandleEventVO.getEventId(), offlineHandleEventVO.getHandleId(), null, "申请上架原因", null, null));
        //平台根据活动id获取下架信息
        offlineHandleEventVO = SeckillApi.platformGetOfflineHandleEvent(seckillId);
        //平台审核成功
        SeckillApi.audit(OfflineHandleEventDtoData.getOfflineHandleEventDTO(offlineHandleEventVO.getEventId(), offlineHandleEventVO.getHandleId(), null, null, null, 3));
        //失效秒杀活动
        SeckillApi.invalidSeckill(seckillId);
        //删除商品
        ProductApi.delete(productId, true);
        //删除秒杀分类
        SeckillApi.deleteSeckillCategory(seckillCategoryId);
    }

    @Test(description = "虚拟秒杀活动基本流程", dataProvider = "virtualProduct")
    public void secKillFlowWithVirtualProduct(Long productId) {
        //新增秒杀分类
        SeckillApi.saveSeckillCategory(SeckillData.getSeckillCategoryDTO());
        //获取秒杀分类id
        List<SeckillCategory> seckillCategoryList = JSON.parseArray(SeckillApi.getSeckillCategoryList().toString(), SeckillCategory.class);
        Long seckillCategoryId = seckillCategoryList.get(seckillCategoryList.size()-1).getCategoryId();
        //参与秒杀的时间戳
        Long startTimestamps = SeckillApi.getSeckillList().get(1).getStartTime().getTime();
        //新建秒杀活动
        SeckillApi.saveSeckill(SeckillData.getSeckillDTO(productId,seckillCategoryId, startTimestamps));
        //获取秒杀活动id
        List<SeckillSpuVO> seckillSpuVOList = SeckillApi.getSeckillSpuList(startTimestamps);
        Long seckillId = seckillSpuVOList.get(seckillSpuVOList.size()-1).getSeckillId();
       //失效秒杀活动
        SeckillApi.invalidSeckill(seckillId);
        //删除商品
        ProductApi.delete(productId, true);
        //删除秒杀分类
        SeckillApi.deleteSeckillCategory(seckillCategoryId);
    }

    @Test(description = "全款预售秒杀活动基本流程", dataProvider = "fullPreSaleProduct")
    public void secKillFlowWithFullPreSaleProduct(Long productId) {
        //新增秒杀分类
        SeckillApi.saveSeckillCategory(SeckillData.getSeckillCategoryDTO());
        //获取秒杀分类id
        List<SeckillCategory> seckillCategoryList = JSON.parseArray(SeckillApi.getSeckillCategoryList().toString(), SeckillCategory.class);
        Long seckillCategoryId = seckillCategoryList.get(seckillCategoryList.size()-1).getCategoryId();
        //参与秒杀的时间戳
        Long startTimestamps = SeckillApi.getSeckillList().get(1).getStartTime().getTime();
        //新建秒杀活动
        SeckillApi.saveSeckill(SeckillData.getSeckillDTO(productId,seckillCategoryId, startTimestamps));
        //获取秒杀活动id
        List<SeckillSpuVO> seckillSpuVOList = SeckillApi.getSeckillSpuList(startTimestamps);
        Long seckillId = seckillSpuVOList.get(seckillSpuVOList.size()-1).getSeckillId();
        //失效秒杀活动
        SeckillApi.invalidSeckill(seckillId);
        //删除商品
        ProductApi.delete(productId, true);
        //删除秒杀分类
        SeckillApi.deleteSeckillCategory(seckillCategoryId);
    }

    @Test(description = "供应商发货的供应商商品秒杀活动基本流程", dataProvider = "supplierProduct")
    private void secKillFlowWithSupplierProduct(Long productId, Long supplierProductId) {
        //新增秒杀分类
        SeckillApi.saveSeckillCategory(SeckillData.getSeckillCategoryDTO());
        //获取秒杀分类id
        List<SeckillCategory> seckillCategoryList = JSON.parseArray(SeckillApi.getSeckillCategoryList().toString(), SeckillCategory.class);
        Long seckillCategoryId = seckillCategoryList.get(seckillCategoryList.size()-1).getCategoryId();
        //参与秒杀的时间戳
        Long startTimestamps = SeckillApi.getSeckillList().get(1).getStartTime().getTime();
        //新建秒杀活动
        SeckillApi.saveSeckill(SeckillData.getSeckillDTO(productId,seckillCategoryId, startTimestamps));
        //获取秒杀活动id
        List<SeckillSpuVO> seckillSpuVOList = SeckillApi.getSeckillSpuList(startTimestamps);
        Long seckillId = seckillSpuVOList.get(seckillSpuVOList.size()-1).getSeckillId();
        //失效秒杀活动
        SeckillApi.invalidSeckill(seckillId);
        //删除商品
        ProductApi.delete(productId, true);
        SupplierProductApi.delete(supplierProductId, true);
        //删除秒杀分类
        SeckillApi.deleteSeckillCategory(seckillCategoryId);
    }
}
