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

import com.tmerclub.cloud.common.util.Arith;
import com.tmerclub.cloud.order.dto.app.OrderRefundDTO;
import com.moyuer.cloud.systemtest.api.order.MyOrderApi;
import com.moyuer.cloud.systemtest.api.order.OrderApi;
import com.moyuer.cloud.systemtest.api.order.OrderRefundApi;
import com.moyuer.cloud.systemtest.api.order.ShopOrderRefundApi;
import com.moyuer.cloud.systemtest.api.supplier.SupplierOrderApi;
import com.moyuer.cloud.systemtest.api.supplier.SupplierRefundApi;
import com.moyuer.cloud.systemtest.data.order.DeliveryOrderDtoData;
import com.moyuer.cloud.systemtest.data.order.OrderRefundDeliveryDtoData;
import com.moyuer.cloud.systemtest.data.order.OrderRefundDtoData;
import com.moyuer.cloud.systemtest.data.order.ShopOrderRefundDtoData;
import com.moyuer.cloud.systemtest.data.supplier.SupplierOrderRefundDtoData;

import java.util.concurrent.TimeUnit;

/**
 * @author lpj
 */
public class OrderRefund {
    /**
     * 商家发货的实物订单退款
     */
    public static Long realOrderRefund(Long orderNum) throws InterruptedException {
        //发货
        OrderApi.delivery(DeliveryOrderDtoData.getNoDeliveryOrderDtoData(orderNum));
        //确认收货后，对订单进行整单退货退款且同意
        MyOrderApi.receipt(orderNum);
        TimeUnit.SECONDS.sleep(2L);
        OrderRefundDTO orderRefundDTO = OrderRefundDtoData.getAllOrderRefundDeliveryDTO(orderNum);
        orderRefundDTO.setRefundAmount((long) Arith.sub(MyOrderApi.orderDetail(orderNum).getCanRefundAmount(), MyOrderApi.orderDetail(orderNum).getTransfee()));
        Long refundId= OrderRefundApi.apply(orderRefundDTO);
        ShopOrderRefundApi.infoByRefundSn(refundId);
        ShopOrderRefundApi.processRefundOrder(ShopOrderRefundDtoData.getReceiveRefundDTO(refundId));
        OrderRefundApi.submitExpress(OrderRefundDeliveryDtoData.getOrderRefundDeliveryDTO(refundId));
        ShopOrderRefundApi.returnMoney(ShopOrderRefundDtoData.getReceiveDeliveryRefundDTO(refundId));

        return refundId;
    }

    /**
     * 虚拟订单退款
     */
    public static Long virtualOrderRefund(Long orderNum) throws InterruptedException {
        //部分退款，同意退款
        TimeUnit.SECONDS.sleep(2L);
        Long refundId= OrderRefundApi.apply(OrderRefundDtoData.getOrderRefundDTO(orderNum,0L));
        ShopOrderRefundApi.infoByRefundSn(refundId);
        ShopOrderRefundApi.processRefundOrder(ShopOrderRefundDtoData.getReceiveDeliveryRefundDTO(refundId));

        return refundId;
    }

    /**
     * 供应商订单退款
     */
    public static Long supplierOrderRefund(Long orderNum) throws InterruptedException {
        //供应商发货
        SupplierOrderApi.delivery(DeliveryOrderDtoData.getNoDeliveryOrderDtoData(orderNum));
        //确认收货后，对订单进行整单退货退款且同意
        MyOrderApi.receipt(orderNum);
        TimeUnit.SECONDS.sleep(2L);
        Long refundId= OrderRefundApi.apply(OrderRefundDtoData.getAllOrderRefundDeliveryDTO(orderNum));
        ShopOrderRefundApi.processRefundOrder(ShopOrderRefundDtoData.getSupplierRefundDTO(refundId));
        SupplierRefundApi.processRefundOrder(SupplierOrderRefundDtoData.getSupplierReceiveRefundDTO(refundId));
        OrderRefundApi.submitExpress(OrderRefundDeliveryDtoData.getOrderRefundDeliveryDTO(refundId));
        SupplierRefundApi.returnMoneyRefundOrder(SupplierOrderRefundDtoData.getReceiveDeliveryRefundDTO(refundId));
        return refundId;
    }
}
