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

import com.alibaba.fastjson2.JSON;
import com.tmerclub.cloud.api.delivery.dto.DeliveryOrderDTO;
import com.tmerclub.cloud.api.delivery.vo.DeliveryOrderFeignVO;
import com.moyuer.cloud.systemtest.api.amount.PayApi;
import com.moyuer.cloud.systemtest.api.order.MyOrderApi;
import com.moyuer.cloud.systemtest.api.platform.ScoreOrderApi;
import com.moyuer.cloud.systemtest.api.platform.ScoreProductApi;
import com.moyuer.cloud.systemtest.data.platform.*;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;

import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * @author lpj
 */
public class ScoreOrderTest {
    @DataProvider(name = "scoreProductDeliveryFree")
    private Object[][] scoreProductDeliveryFree() {
        Long transportId = PlatformDeliveryData.getPlatformFreeFeeDelivery();
        Long productId = ScoreProductApi.createScoreProduct(ScoreSpuData.getScoreSpu(transportId, 100L, 100L));
        return new Object[][]{{productId}};
    }

    @Test(description = "JK_52,积分商品订单取消删除", dataProvider = "scoreProductDeliveryFree")
    public void scoreOrderCancel(Long productId) {
        MyOrderApi.confirmScoreOrder(ScoreOrderDtoData.getScoreOrderDtoData(productId, 1));
        Long orderNum = MyOrderApi.submit(SubmitScoreOrderDtoData.getSubmitScoreOrderDtoData()).get(0);
        MyOrderApi.cancel(orderNum);
        ScoreProductApi.deleteScoreProduct(productId);
        MyOrderApi.delete(orderNum);
    }

    @Test(description = "JK_53,积分商品订单发货完整流程", dataProvider = "scoreProductDeliveryFree")
    public void scoreOrderDeliveryOrderTestCase(Long productId) throws InterruptedException {
        MyOrderApi.confirmScoreOrder(ScoreOrderDtoData.getScoreOrderDtoData(productId, 1));
        Long orderNum = MyOrderApi.submit(SubmitScoreOrderDtoData.getSubmitScoreOrderDtoData()).get(0);
        PayApi.orderPay(orderNum);
        TimeUnit.SECONDS.sleep(2L);
        ScoreOrderApi.scoreOrderDelivery(DeliveryScoreOrderDtoData.getDeliveryScoreOrderDtoData(orderNum));

        TimeUnit.SECONDS.sleep(2L);
        List<DeliveryOrderFeignVO> delivery = ScoreOrderApi.infoOrderDelivery(orderNum);
        List<DeliveryOrderDTO> deliveryOrderDtos = JSON.parseArray(JSON.toJSONString(delivery), DeliveryOrderDTO.class);
        deliveryOrderDtos.forEach(s -> s.setDeliveryNo("xgh123123123123"));
        ScoreOrderApi.updateOrderDelivery(deliveryOrderDtos);

        TimeUnit.SECONDS.sleep(2L);
        MyOrderApi.receipt(orderNum);
        ScoreProductApi.deleteScoreProduct(productId);
    }

    @Test(description = "JK_54,积分商品无需快递发货完成流程", dataProvider = "scoreProductDeliveryFree")
    public void scoreOrderNoDeliveryOrderTestCase(Long productId) throws InterruptedException {
        MyOrderApi.confirmScoreOrder(ScoreOrderDtoData.getScoreOrderDtoData(productId, 1));
        Long orderNum = MyOrderApi.submit(SubmitScoreOrderDtoData.getSubmitScoreOrderDtoData()).get(0);
        PayApi.orderPay(orderNum);
        TimeUnit.SECONDS.sleep(2L);
        ScoreOrderApi.scoreOrderDelivery(DeliveryScoreOrderDtoData.getNoDeliveryScoreOrderDtoData(orderNum));
        TimeUnit.SECONDS.sleep(2L);
        MyOrderApi.receipt(orderNum);
        ScoreProductApi.deleteScoreProduct(productId);
    }
}
