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

import cn.hutool.core.bean.BeanUtil;
import com.tmerclub.cloud.common.product.dto.SkuDTO;
import com.tmerclub.cloud.common.product.dto.SpuDTO;
import com.tmerclub.cloud.common.product.vo.SkuVO;
import com.moyuer.cloud.systemtest.api.amount.ProductApi;
import com.moyuer.cloud.systemtest.api.store.ShopSubstituteSalesApi;
import com.moyuer.cloud.systemtest.api.supplier.SupplierProductApi;
import com.moyuer.cloud.systemtest.data.store.DeliveryData;
import com.moyuer.cloud.systemtest.data.store.ShopSubstituteSalesDtoData;
import com.moyuer.cloud.systemtest.data.supplier.SupplierProductData;
import org.testng.Assert;
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 ConsignmentManagementTest {
    @Test(description = "代销设置基本流程")
    public void consignmentManagementFlow() throws InterruptedException {
        //发布商品和导入商品
        Long supplierProductId = SupplierProductData.getSupplierProductId(1000L, DeliveryData.getSupplierFreeFeeDelivery(true), true);
        Long productId = SupplierProductData.getProductId(supplierProductId, 8800L, 1, null, true, true);
        //商家设置代销设置为按比例加价10%
        ShopSubstituteSalesApi.updateShopSubstituteSales(ShopSubstituteSalesDtoData.getShopSubstituteSalesDTO(0, 0, 1000L, 0), true);
        //根据商品ID获取商品销售价，判断销售价是否为11
        TimeUnit.SECONDS.sleep(2L);
        Assert.assertEquals(ProductApi.info(productId, true).getSkus().get(0).getPriceFee(), 1100);
        //商家修改代销设置为按比例加价为11%
        ShopSubstituteSalesApi.updateShopSubstituteSales(ShopSubstituteSalesDtoData.getShopSubstituteSalesDTO(0, 0, 1100L, 0), true);
        //根据商品ID获取商品销售价，判断销售价是否为11.1
        TimeUnit.SECONDS.sleep(2L);
        Assert.assertEquals(ProductApi.info(productId, true).getSkus().get(0).getPriceFee(), 1110);
        //商家修改代销设置为按固定数值加价0
        ShopSubstituteSalesApi.updateShopSubstituteSales(ShopSubstituteSalesDtoData.getShopSubstituteSalesDTO(0, 1, 0L, 0), true);
        //根据商品ID获取商品销售价，判断销售价是否为10
        TimeUnit.SECONDS.sleep(2L);
        Assert.assertEquals(ProductApi.info(productId, true).getSkus().get(0).getPriceFee(), 1000);
        //商家修改代销设置为按固定数值加价10
        ShopSubstituteSalesApi.updateShopSubstituteSales(ShopSubstituteSalesDtoData.getShopSubstituteSalesDTO(0, 1, 1000L, 0), true);
        //根据商品ID获取商品销售价，判断销售价是否为20
        TimeUnit.SECONDS.sleep(2L);
        Assert.assertEquals(ProductApi.info(productId, true).getSkus().get(0).getPriceFee(), 2000);
        //商家修改代销设置为手动设价
        ShopSubstituteSalesApi.updateShopSubstituteSales(ShopSubstituteSalesDtoData.getShopSubstituteSalesDTO(1, 0, null, 0), true);
        //根据商品ID获取商品销售价，判断销售价是否为20
        TimeUnit.SECONDS.sleep(2L);
        Assert.assertEquals(ProductApi.info(productId, true).getSkus().get(0).getPriceFee(), 2000);
        //商家根据商品ID删除商品
        ProductApi.delete(productId, true);
        //商家根据供应商商品ID导入商品获取商品ID
        Object obj = ProductApi.addSupplierProduct(Collections.singletonList(supplierProductId), true).get(0);
        if (obj instanceof Integer) {
            Integer ob = (Integer) obj;
            productId = ob.longValue();
        } else {
            productId = (Long) obj;
        }
        //根据商品ID获取商品销售价，判断销售价是否为10
        TimeUnit.SECONDS.sleep(2L);
        Assert.assertEquals(ProductApi.info(productId, true).getSkus().get(0).getPriceFee(), 1000);
        //商家修改代销设置为按比例加价100%
        ShopSubstituteSalesApi.updateShopSubstituteSales(ShopSubstituteSalesDtoData.getShopSubstituteSalesDTO(0, 0, 10000L, 0), true);
        //根据商品ID获取商品销售价，判断销售价是否为20
        TimeUnit.SECONDS.sleep(2L);
        Assert.assertEquals(ProductApi.info(productId, true).getSkus().get(0).getPriceFee(), 2000);
        //供应商根据供应商商品ID修改供货价为20
        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);
        //根据商品ID获取商品销售价，判断销售价是否为40
        TimeUnit.SECONDS.sleep(2L);
        Assert.assertEquals(ProductApi.info(productId, true).getSkus().get(0).getPriceFee(), 4000);

        //删除商品
        ProductApi.delete(productId, true);
        SupplierProductApi.delete(supplierProductId, true);
    }
}
