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.moyuer.cloud.systemtest.api.amount.CategoryApi;
import com.moyuer.cloud.systemtest.api.platform.PlatformProductApi;
import com.moyuer.cloud.systemtest.api.supplier.SupplierProductApi;
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;

/**
 * @author lpj
 */
public class SupplierProductTest {
    @DataProvider(name = "supplierProduct")
    private Object[][] supplierProduct() {
        Long transportId = DeliveryData.getSupplierFreeFeeDelivery(true);
        Long categoryId = CategoryApi.listSupplierCategory(true).get(0).getCategoryId();
        Long supplierProductId = SupplierProductApi.createProduct(SpuData.getSupplierSpu(categoryId, transportId, 8800L), true);
        return new Object[][]{{supplierProductId}};
    }

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

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

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

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

    @Test(description = "修改商品信息", dataProvider = "supplierProduct")
    public void updateProduct(Long supplierProductId) {
        SpuVO spuVO = SupplierProductApi.info(supplierProductId, 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 = SupplierProductApi.info(supplierProductId, 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);

        SupplierProductApi.update(spuDTO, true);
        SupplierProductApi.delete(supplierProductId, true);
    }

    @Test(description = "商品列表修改商品名称", dataProvider = "supplierProduct")
    public void updateProductName(Long supplierProductId) {
        SpuDTO spuDTO = new SpuDTO();
        spuDTO.setSpuId(supplierProductId);
        List<SpuLangVO> spuLangVOList = SupplierProductApi.info(supplierProductId, 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);
        SupplierProductApi.updateProductData(spuDTO);
        SupplierProductApi.delete(supplierProductId, true);
    }

    @Test(description = "商品列表修改商品价格", dataProvider = "supplierProduct")
    public void updateProductPrice(Long supplierProductId) {
        SpuDTO spuDTO = new SpuDTO();
        spuDTO.setSpuId(supplierProductId);
        List<SkuVO> skuVOList = SupplierProductApi.info(supplierProductId, 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);
        SupplierProductApi.updateProductData(spuDTO);
        SupplierProductApi.delete(supplierProductId, true);
    }

    @Test(description = "商品列表修改商品库存", dataProvider = "supplierProduct")
    public void updateProductStock(Long supplierProductId) {
        SpuDTO spuDTO = new SpuDTO();
        spuDTO.setSpuId(supplierProductId);
        List<SkuVO> skuVOList = SupplierProductApi.info(supplierProductId, 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);
        SupplierProductApi.updateProductData(spuDTO);
        SupplierProductApi.delete(supplierProductId, true);
    }

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