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

import cn.hutool.core.bean.BeanUtil;
import com.tmerclub.cloud.api.platform.dto.OfflineHandleEventDTO;
import com.tmerclub.cloud.api.platform.vo.OfflineHandleEventVO;
import com.tmerclub.cloud.common.product.dto.SkuDTO;
import com.tmerclub.cloud.common.product.dto.SpuDTO;
import com.tmerclub.cloud.common.product.dto.SpuLangDTO;
import com.tmerclub.cloud.common.product.vo.SkuVO;
import com.tmerclub.cloud.common.product.vo.SpuLangVO;
import com.tmerclub.cloud.common.product.vo.SpuVO;
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.platform.PlatformProductApi;
import com.moyuer.cloud.systemtest.data.amount.OfflineHandleEventDtoData;
import com.moyuer.cloud.systemtest.data.amount.SpuData;
import com.moyuer.cloud.systemtest.data.store.DeliveryData;
import com.moyuer.cloud.systemtest.data.store.SpuPageSearchDtoData;
import org.springframework.beans.BeanUtils;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;

import java.util.Collections;
import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * @author lpj
 */
public class ProductTest {
    private List<Long> getCategoryId() {
        List<CategoryAppVO> categoryAppVOList = CategoryApi.listShopCategory(true);
        Long shopCategoryId = categoryAppVOList.get(categoryAppVOList.size() - 1).getCategoryId();
        Long categoryId = CategoryApi.listSigningCategory(true).get(0).getCategoryId();
        return Stream.of(categoryId, shopCategoryId).collect(Collectors.toList());
    }

    @DataProvider(name = "realProduct")
    private Object[][] realProduct() {
        Long transportId = DeliveryData.getFreeFeeDelivery(true);
        Long productId = ProductApi.createProduct(SpuData.getRealSpu(getCategoryId().get(0), getCategoryId().get(1), transportId, 1000L), true);
        return new Object[][]{{productId}};
    }

    @Test(description = "商品管理基本流程", dataProvider = "realProduct")
    public void productFlow(Long productId) throws InterruptedException {
        //下架
        ProductApi.changeProductStatus(SpuPageSearchDtoData.getSpuPageSearchDTO(productId, null, 0));
        //上架
        ProductApi.changeProductStatus(SpuPageSearchDtoData.getSpuPageSearchDTO(productId, null, 1));
        //批量下架
        ProductApi.changeProductStatus(SpuPageSearchDtoData.getSpuPageSearchDTO(null, Collections.singletonList(productId), 0));
        //批量上架
        ProductApi.changeProductStatus(SpuPageSearchDtoData.getSpuPageSearchDTO(null, Collections.singletonList(productId), 1));

        //平台置顶商品
        PlatformProductApi.toTop(productId);
        //平台取消置顶商品
        PlatformProductApi.toTop(productId);

        //平台违规下架商品
        OfflineHandleEventDTO offlineHandleEventDTO = OfflineHandleEventDtoData.getOfflineHandleEventDTO(null, productId, "违规下架原因", null, null, null);
        offlineHandleEventDTO.setHandleIds(Collections.singletonList(productId));
        PlatformProductApi.offline(offlineHandleEventDTO);
        //申请上架，审核失败
        //商家根据活动id获取下架信息
        OfflineHandleEventVO offlineHandleEventVO = ProductApi.shopGetOfflineHandleEvent(productId);
        //商家申请上架
        ProductApi.apply(OfflineHandleEventDtoData.getOfflineHandleEventDTO(offlineHandleEventVO.getEventId(), offlineHandleEventVO.getHandleId(), null, "申请上架原因", null, null));
        //平台根据活动id获取下架信息
        offlineHandleEventVO = PlatformProductApi.platformGetOfflineHandleEvent(productId);
        //平台审核失败
        TimeUnit.SECONDS.sleep(2L);
        PlatformProductApi.audit(OfflineHandleEventDtoData.getOfflineHandleEventDTO(offlineHandleEventVO.getEventId(), offlineHandleEventVO.getHandleId(), null, null, "审核失败原因", 4));
        //申请上架，审核成功
        //商家根据活动id获取下架信息
        offlineHandleEventVO = ProductApi.shopGetOfflineHandleEvent(productId);
        //商家申请上架
        ProductApi.apply(OfflineHandleEventDtoData.getOfflineHandleEventDTO(offlineHandleEventVO.getEventId(), offlineHandleEventVO.getHandleId(), null, "申请上架原因", null, null));
        //平台根据活动id获取下架信息
        offlineHandleEventVO = PlatformProductApi.platformGetOfflineHandleEvent(productId);
        //平台审核成功
        TimeUnit.SECONDS.sleep(2L);
        PlatformProductApi.audit(OfflineHandleEventDtoData.getOfflineHandleEventDTO(offlineHandleEventVO.getEventId(), offlineHandleEventVO.getHandleId(), null, null, null, 3));

        //平台删除商品
        PlatformProductApi.platformDeleteProduct(productId);
    }

    @Test(description = "修改商品信息", dataProvider = "realProduct")
    public void updateProduct(Long productId) {
        SpuVO spuVO = ProductApi.info(productId, true);
        SpuDTO spuDTO = new SpuDTO();

        BeanUtils.copyProperties(spuVO, spuDTO);

        List<SkuVO> skuVOList = spuVO.getSkus();
        List<SkuDTO> skuDTOList = skuVOList.stream().map(i -> BeanUtil.copyProperties(i, SkuDTO.class)).collect(Collectors.toList());
        spuDTO.setSkuList(skuDTOList);

        List<SpuLangVO> spuLangVOList = ProductApi.info(productId, true).getSpuLangList();
        List<SpuLangDTO> spuLangDTOList = spuLangVOList.stream().map(i -> BeanUtil.copyProperties(i, SpuLangDTO.class)).collect(Collectors.toList());
        spuLangDTOList.forEach(i->i.setSpuName("修改后的商品名称"));
        spuDTO.setSpuLangList(spuLangDTOList);

        ProductApi.update(spuDTO, true);
        ProductApi.delete(productId, true);
    }

    @Test(description = "商品列表修改商品名称", dataProvider = "realProduct")
    public void updateProductName(Long productId) {
        SpuDTO spuDTO = new SpuDTO();
        spuDTO.setSpuId(productId);
        List<SpuLangVO> spuLangVOList = ProductApi.info(productId, true).getSpuLangList();
        List<SpuLangDTO> spuLangDTOList = spuLangVOList.stream().map(i -> BeanUtil.copyProperties(i, SpuLangDTO.class)).collect(Collectors.toList());
        spuLangDTOList.forEach(i->i.setSpuName("修改后的商品名称"));
        spuDTO.setSpuLangList(spuLangDTOList);
        ProductApi.updateProductData(spuDTO);
        ProductApi.delete(productId, true);
    }

    @Test(description = "商品列表修改商品价格", dataProvider = "realProduct")
    public void updateProductPrice(Long productId) {
        SpuDTO spuDTO = new SpuDTO();
        spuDTO.setSpuId(productId);
        List<SkuVO> skuVOList = ProductApi.info(productId, true).getSkus();
        List<SkuDTO> skuDTOList = skuVOList.stream().map(i -> BeanUtil.copyProperties(i, SkuDTO.class)).collect(Collectors.toList());
        skuDTOList.forEach(i->i.setPriceFee(2000L));
        spuDTO.setSkuList(skuDTOList);
        ProductApi.updateProductData(spuDTO);
        ProductApi.delete(productId, true);
    }

    @Test(description = "商品列表修改商品库存", dataProvider = "realProduct")
    public void updateProductStock(Long productId) {
        SpuDTO spuDTO = new SpuDTO();
        spuDTO.setSpuId(productId);
        List<SkuVO> skuVOList = ProductApi.info(productId, true).getSkus();
        List<SkuDTO> skuDTOList = skuVOList.stream().map(i -> BeanUtil.copyProperties(i, SkuDTO.class)).collect(Collectors.toList());
        skuDTOList.forEach(i->i.setChangeStock(1));
        spuDTO.setSkuList(skuDTOList);
        ProductApi.updateProductData(spuDTO);
        ProductApi.delete(productId, true);
    }

    @Test(description = "商品列表修改商品序号", dataProvider = "realProduct")
    public void updateProductSeq(Long productId) {
        SpuDTO spuDTO = new SpuDTO();
        spuDTO.setSpuId(productId);
        spuDTO.setSeq(1);
        ProductApi.updateProductData(spuDTO);
        ProductApi.delete(productId, true);
    }
}
