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

import cn.hutool.core.bean.BeanUtil;
import com.alibaba.fastjson2.JSON;
import com.tmerclub.cloud.common.product.dto.*;
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.ProductApi;
import com.moyuer.cloud.systemtest.api.supplier.SupplierProductApi;
import com.moyuer.cloud.systemtest.data.store.DeliveryData;
import com.moyuer.cloud.systemtest.data.store.SpuPageSearchDtoData;
import com.moyuer.cloud.systemtest.data.supplier.SupplierProductData;
import org.springframework.beans.BeanUtils;
import org.testng.Assert;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

/**
 * @author lpj
 */
public class SupplierChangeSpuTest {
    @DataProvider(name = "supplierDeliveryProduct")
    private Object[][] supplierDeliveryProduct() {
        Long supplierProductId = SupplierProductData.getSupplierProductId(6600L, DeliveryData.getSupplierFreeFeeDelivery(true), true);
        Long productId = SupplierProductData.getProductId(supplierProductId, 8800L, 1, null, true, true);
        return new Object[][]{{productId, supplierProductId}};
    }

    @DataProvider(name = "shopDeliveryProduct")
    private Object[][] shopDeliveryProduct() {
        Long supplierProductId = SupplierProductData.getSupplierProductId(6600L, DeliveryData.getSupplierFreeFeeDelivery(true), true);
        Long productId = SupplierProductData.getProductId(supplierProductId, 8800L, 2, DeliveryData.getFreeFeeDelivery(true), true, true);
        return new Object[][]{{productId, supplierProductId}};
    }

    @Test(description = "供应商删除商品，供应商发货的代销商品状态是否正确", dataProvider = "supplierDeliveryProduct")
    public void supplierDeleteSupplierDeliveryProduct(Long productId, Long supplierProductId) {
        SupplierProductApi.delete(supplierProductId, true);
        Assert.assertEquals(ProductApi.info(productId, true).getStatus(), 0);
        ProductApi.delete(productId, true);
    }

    @Test(description = "供应商删除商品，商家发货的代销商品状态是否正确", dataProvider = "shopDeliveryProduct")
    public void supplierDeleteShopDeliveryProduct(Long productId, Long supplierProductId) {
        SupplierProductApi.delete(supplierProductId, true);
        Assert.assertEquals(ProductApi.info(productId, true).getStatus(), 1);
        ProductApi.delete(productId, true);
    }

    @Test(description = "供应商修改供货价，供应商发货的代销商品状态是否正确", dataProvider = "supplierDeliveryProduct")
    public void supplierChangeSupplierDeliveryProductPrice(Long productId, Long supplierProductId) throws InterruptedException {
        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);

        TimeUnit.SECONDS.sleep(2L);
        Assert.assertEquals(ProductApi.info(productId, true).getStatus(), 0);

        ProductApi.delete(productId, true);
        SupplierProductApi.delete(supplierProductId, true);
    }

    @Test(description = "供应商修改供货价，商家发货的代销商品状态是否正确", dataProvider = "shopDeliveryProduct")
    public void supplierChangeShopDeliveryProductPrice(Long productId, Long supplierProductId) throws InterruptedException {
        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);

        TimeUnit.SECONDS.sleep(2L);
        Assert.assertEquals(ProductApi.info(productId, true).getStatus(), 1);

        ProductApi.delete(productId, true);
        SupplierProductApi.delete(supplierProductId, true);
    }

    @Test(description = "供应商修改重量，供应商发货的代销商品状态是否正确", dataProvider = "supplierDeliveryProduct")
    public void supplierChangeSupplierDeliveryProductWeight(Long productId, Long supplierProductId) throws InterruptedException {
        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);
        skuDTOList.forEach(i->i.setWeight(1.1));
        List<SpuLangVO> spuLangVOList = SupplierProductApi.info(supplierProductId, true).getSpuLangList();
        List<SpuLangDTO> spuLangDTOList = spuLangVOList.stream().map(i -> BeanUtil.copyProperties(i, SpuLangDTO.class)).collect(Collectors.toList());
        spuDTO.setSpuLangList(spuLangDTOList);
        SupplierProductApi.update(spuDTO, true);

        TimeUnit.SECONDS.sleep(2L);
        Assert.assertEquals(ProductApi.info(productId, true).getStatus(), 1);

        ProductApi.delete(productId, true);
        SupplierProductApi.delete(supplierProductId, true);
    }

    @Test(description = "供应商修改重量，商家发货的代销商品状态是否正确", dataProvider = "shopDeliveryProduct")
    public void supplierChangeShopDeliveryProductWeight(Long productId, Long supplierProductId) throws InterruptedException {
        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);
        skuDTOList.forEach(i->i.setWeight(1.1));
        List<SpuLangVO> spuLangVOList = SupplierProductApi.info(supplierProductId, true).getSpuLangList();
        List<SpuLangDTO> spuLangDTOList = spuLangVOList.stream().map(i -> BeanUtil.copyProperties(i, SpuLangDTO.class)).collect(Collectors.toList());
        spuDTO.setSpuLangList(spuLangDTOList);
        SupplierProductApi.update(spuDTO, true);

        TimeUnit.SECONDS.sleep(2L);
        Assert.assertEquals(ProductApi.info(productId, true).getStatus(), 0);

        ProductApi.delete(productId, true);
        SupplierProductApi.delete(supplierProductId, true);
    }

    @Test(description = "供应商修改体积，供应商发货的代销商品状态是否正确", dataProvider = "supplierDeliveryProduct")
    public void supplierChangeSupplierDeliveryProductVolume(Long productId, Long supplierProductId) throws InterruptedException {
        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);
        skuDTOList.forEach(i->i.setVolume(1.1));
        List<SpuLangVO> spuLangVOList = SupplierProductApi.info(supplierProductId, true).getSpuLangList();
        List<SpuLangDTO> spuLangDTOList = spuLangVOList.stream().map(i -> BeanUtil.copyProperties(i, SpuLangDTO.class)).collect(Collectors.toList());
        spuDTO.setSpuLangList(spuLangDTOList);
        SupplierProductApi.update(spuDTO, true);

        TimeUnit.SECONDS.sleep(2L);
        Assert.assertEquals(ProductApi.info(productId, true).getStatus(), 1);

        ProductApi.delete(productId, true);
        SupplierProductApi.delete(supplierProductId, true);
    }

    @Test(description = "供应商修改体积，商家发货的代销商品状态是否正确", dataProvider = "shopDeliveryProduct")
    public void supplierChangeShopDeliveryProductVolume(Long productId, Long supplierProductId) throws InterruptedException {
        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);
        skuDTOList.forEach(i->i.setVolume(1.1));
        List<SpuLangVO> spuLangVOList = SupplierProductApi.info(supplierProductId, true).getSpuLangList();
        List<SpuLangDTO> spuLangDTOList = spuLangVOList.stream().map(i -> BeanUtil.copyProperties(i, SpuLangDTO.class)).collect(Collectors.toList());
        spuDTO.setSpuLangList(spuLangDTOList);
        SupplierProductApi.update(spuDTO, true);

        TimeUnit.SECONDS.sleep(2L);
        Assert.assertEquals(ProductApi.info(productId, true).getStatus(), 0);

        ProductApi.delete(productId, true);
        SupplierProductApi.delete(supplierProductId, true);
    }

    @Test(description = "供应商修改sku信息，供应商发货的代销商品状态是否正确", dataProvider = "supplierDeliveryProduct")
    public void supplierChangeSupplierDeliveryProductSku(Long productId, Long supplierProductId) throws InterruptedException {
        //供应商增加sku，供应商发货的代销商品状态是否正确
        SpuVO spuVO = SupplierProductApi.info(supplierProductId, true);
        SpuDTO spuDTO = new SpuDTO();
        BeanUtils.copyProperties(spuVO, spuDTO);
        spuDTO.setSkuList(getSkuList(6600L));
        List<SpuLangVO> spuLangVOList = SupplierProductApi.info(supplierProductId, true).getSpuLangList();
        List<SpuLangDTO> spuLangDTOList = spuLangVOList.stream().map(i -> BeanUtil.copyProperties(i, SpuLangDTO.class)).collect(Collectors.toList());
        spuDTO.setSpuLangList(spuLangDTOList);
        SupplierProductApi.update(spuDTO, true);
        TimeUnit.SECONDS.sleep(2L);
        Assert.assertEquals(ProductApi.info(productId, true).getStatus(), 0);

        //供应商禁用sku，供应商发货的代销商品状态是否正确
        updateProduct(productId);
        ProductApi.changeProductStatus(SpuPageSearchDtoData.getSpuPageSearchDTO(productId, null, 1));
        disableSku(supplierProductId);
        TimeUnit.SECONDS.sleep(2L);
        Assert.assertEquals(ProductApi.info(productId, true).getStatus(), 1);

        //供应商启用sku，供应商发货的代销商品状态是否正确
        updateProduct(productId);
        ProductApi.changeProductStatus(SpuPageSearchDtoData.getSpuPageSearchDTO(productId, null, 1));
        enableSku(supplierProductId);
        TimeUnit.SECONDS.sleep(2L);
        Assert.assertEquals(ProductApi.info(productId, true).getStatus(), 1);

        ProductApi.delete(productId, true);
        SupplierProductApi.delete(supplierProductId, true);
    }

    @Test(description = "供应商修改sku信息，商家发货的代销商品状态是否正确", dataProvider = "shopDeliveryProduct")
    public void supplierChangeShopDeliveryProductSku(Long productId, Long supplierProductId) throws InterruptedException {
        //供应商增加sku，商家发货的代销商品状态是否正确
        SpuVO spuVO = SupplierProductApi.info(supplierProductId, true);
        SpuDTO spuDTO = new SpuDTO();
        BeanUtils.copyProperties(spuVO, spuDTO);
        spuDTO.setSkuList(getSkuList(6600L));
        List<SpuLangVO> spuLangVOList = SupplierProductApi.info(supplierProductId, true).getSpuLangList();
        List<SpuLangDTO> spuLangDTOList = spuLangVOList.stream().map(i -> BeanUtil.copyProperties(i, SpuLangDTO.class)).collect(Collectors.toList());
        spuDTO.setSpuLangList(spuLangDTOList);
        SupplierProductApi.update(spuDTO, true);
        TimeUnit.SECONDS.sleep(2L);
        Assert.assertEquals(ProductApi.info(productId, true).getStatus(), 0);

        //供应商禁用sku，商家发货的代销商品状态是否正确
        updateProduct(productId);
        ProductApi.changeProductStatus(SpuPageSearchDtoData.getSpuPageSearchDTO(productId, null, 1));
        disableSku(supplierProductId);
        TimeUnit.SECONDS.sleep(2L);
        Assert.assertEquals(ProductApi.info(productId, true).getStatus(), 1);

        //供应商启用sku，商家发货的代销商品状态是否正确
        updateProduct(productId);
        ProductApi.changeProductStatus(SpuPageSearchDtoData.getSpuPageSearchDTO(productId, null, 1));
        enableSku(supplierProductId);
        TimeUnit.SECONDS.sleep(2L);
        Assert.assertEquals(ProductApi.info(productId, true).getStatus(), 1);

        ProductApi.delete(productId, true);
        SupplierProductApi.delete(supplierProductId, true);
    }

    /**
     * 商家更新代销商品
     */
    public static void updateProduct(Long productId) {
        SpuVO spuVO = ProductApi.info(productId, true);
        SpuDTO spuDTO = JSON.parseObject(JSON.toJSONString(spuVO), SpuDTO.class);
        List<SkuDTO> skuDTOList = JSON.parseArray(JSON.toJSONString(spuVO.getSkus()), SkuDTO.class);
        spuDTO.setSkuList(skuDTOList);
        ProductApi.update(spuDTO, true);
    }

    /**
     * 供应商禁用sku
     */
    public static void disableSku(Long supplierProductId) {
        SpuVO spuVO = SupplierProductApi.info(supplierProductId, true);
        SpuDTO spuDTO = JSON.parseObject(JSON.toJSONString(spuVO), SpuDTO.class);
        List<SkuDTO> skuDTOList = JSON.parseArray(JSON.toJSONString(spuVO.getSkus()), SkuDTO.class);
        skuDTOList.get(0).setStatus(0);
        spuDTO.setSkuList(skuDTOList);
        SupplierProductApi.update(spuDTO, true);
    }

    /**
     * 供应商启用sku
     */
    public static void enableSku(Long supplierProductId) {
        SpuVO spuVO = SupplierProductApi.info(supplierProductId, true);
        SpuDTO spuDTO = JSON.parseObject(JSON.toJSONString(spuVO), SpuDTO.class);
        List<SkuDTO> skuDTOList = JSON.parseArray(JSON.toJSONString(spuVO.getSkus()), SkuDTO.class);
        skuDTOList.get(0).setStatus(1);
        spuDTO.setSkuList(skuDTOList);
        SupplierProductApi.update(spuDTO, true);
    }

    public static List<SkuDTO> getSkuList(Long priceFee){
        List<SkuDTO> skuList = new ArrayList<>();
        SkuDTO skuDtoA = new SkuDTO();
        skuDtoA.setChangeStock(100);
        skuDtoA.setPriceFee(priceFee);
        skuDtoA.setMarketPriceFee(88L);
        skuDtoA.setStock(100);
        skuDtoA.setStatus(1);
        skuDtoA.setWeight(0.0);
        skuDtoA.setVolume(0.0);

        List<SkuLangDTO> skuLangDtoListA = new ArrayList<>();
        SkuLangDTO skuLangDtoA = new SkuLangDTO();
        skuLangDtoA.setAttrs("test");
        skuLangDtoA.setLang(1);
        skuLangDtoA.setSkuName("test1");
        skuLangDtoListA.add(skuLangDtoA);

        SkuLangDTO skuLangDtoB = new SkuLangDTO();
        skuLangDtoB.setAttrs("test");
        skuLangDtoB.setLang(2);
        skuLangDtoB.setSkuName("test1");
        skuLangDtoListA.add(skuLangDtoB);
        skuDtoA.setSkuLangList(skuLangDtoListA);

        List<SpuSkuAttrValueDTO> spuSkuAttrValueDtoListA = new ArrayList<>();
        SpuSkuAttrValueDTO spuSkuAttrValueDtoA = new SpuSkuAttrValueDTO();
        spuSkuAttrValueDtoA.setAttrName("test");
        spuSkuAttrValueDtoA.setAttrValueName("test1");
        spuSkuAttrValueDtoListA.add(spuSkuAttrValueDtoA);
        skuDtoA.setSpuSkuAttrValues(spuSkuAttrValueDtoListA);

        skuList.add(skuDtoA);

        SkuDTO skuDtoB = new SkuDTO();
        skuDtoB.setChangeStock(100);
        skuDtoB.setPriceFee(priceFee);
        skuDtoB.setMarketPriceFee(88L);
        skuDtoB.setStock(100);
        skuDtoB.setStatus(1);
        skuDtoB.setWeight(0.0);
        skuDtoB.setVolume(0.0);

        List<SkuLangDTO> skuLangDtoListB = new ArrayList<>();
        SkuLangDTO skuLangDtoC = new SkuLangDTO();
        skuLangDtoC.setAttrs("test");
        skuLangDtoC.setLang(1);
        skuLangDtoC.setSkuName("test2");
        skuLangDtoListB.add(skuLangDtoC);

        SkuLangDTO skuLangDtoD = new SkuLangDTO();
        skuLangDtoD.setAttrs("test");
        skuLangDtoD.setLang(2);
        skuLangDtoD.setSkuName("test2");
        skuLangDtoListB.add(skuLangDtoD);
        skuDtoB.setSkuLangList(skuLangDtoListB);

        List<SpuSkuAttrValueDTO> spuSkuAttrValueDtoListB = new ArrayList<>();
        SpuSkuAttrValueDTO spuSkuAttrValueDtoB = new SpuSkuAttrValueDTO();
        spuSkuAttrValueDtoB.setAttrName("test");
        spuSkuAttrValueDtoB.setAttrValueName("test2");
        spuSkuAttrValueDtoListB.add(spuSkuAttrValueDtoB);
        skuDtoB.setSpuSkuAttrValues(spuSkuAttrValueDtoListB);

        skuList.add(skuDtoB);

        return skuList;
    }
}
