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

import com.tmerclub.cloud.common.order.vo.OrderItemVO;
import com.tmerclub.cloud.common.order.vo.OrderVirtualInfoVO;
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 java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * @author Tu
 */
public class OrderRefundDtoData {
    public static OrderRefundDTO getOrderRefundDTO(Long orderNum,Long refundAmount) {
        OrderRefundDTO orderRefundDTO = new OrderRefundDTO();
        orderRefundDTO.setOrderId(orderNum);
        orderRefundDTO.setOrderItemId(MyOrderApi.orderDetail(orderNum).getOrderItems().get(0).getOrderItemId());
        orderRefundDTO.setApplyType(1);
        orderRefundDTO.setRefundType(2);
        orderRefundDTO.setBuyerDesc("部分退款自动化执行");
        orderRefundDTO.setBuyerReason(0);
        orderRefundDTO.setBuyerMobile("12345678910");
        orderRefundDTO.setImgUrls("");
        orderRefundDTO.setReceived(0);
        if(refundAmount == 0 ){
        orderRefundDTO.setRefundAmount(MyOrderApi.orderDetail(orderNum).getOrderItems().get(0).getActualTotal());
        }else {
            orderRefundDTO.setRefundAmount(refundAmount);
        }
        orderRefundDTO.setRefundCount(0);
        //如果有赠品，添加赠品id列表
        if (!Objects.isNull(MyOrderApi.orderDetail(orderNum).getOrderItems().get(0).getGiveawayList())) {
            List<Long> giveawayItemIds = new ArrayList<>();
            for (OrderItemVO orderItemVO : MyOrderApi.orderDetail(orderNum).getOrderItems().get(0).getGiveawayList()) {
                giveawayItemIds.add(orderItemVO.getOrderItemId());
            }
            orderRefundDTO.setGiveawayItemIds(giveawayItemIds);
        }
        //需核销的虚拟订单，设置可退款数量和退款金额
        if (MyOrderApi.orderDetail(orderNum).getOrderMold().equals(1) && !MyOrderApi.orderDetail(orderNum).getWriteOffNum().equals(0)) {
            Integer count = 0;
            for (OrderVirtualInfoVO orderVirtualInfoVO : MyOrderApi.orderDetail(orderNum).getVirtualInfoList()) {
                if (orderVirtualInfoVO.getIsWriteOff().equals(0)) {
                    count++;
                }
            }
            orderRefundDTO.setRefundCount(count);
            orderRefundDTO.setRefundAmount(getVirtualOrderRefundAmount(orderNum, count));
        }

        return orderRefundDTO;
    }
    public static OrderRefundDTO getAllOrderRefundDTO(Long orderNum) {//
        OrderRefundDTO orderRefundDTO = new OrderRefundDTO();
        orderRefundDTO.setOrderId(orderNum);
        orderRefundDTO.setOrderItemId(0L);
        orderRefundDTO.setApplyType(1);
        orderRefundDTO.setRefundType(1);
        orderRefundDTO.setBuyerReason(0);
        orderRefundDTO.setBuyerDesc("整单仅退款自动化执行");
        orderRefundDTO.setBuyerMobile("12345678910");
        orderRefundDTO.setImgUrls("");
        orderRefundDTO.setReceived(0);
        orderRefundDTO.setRefundAmount(getRefundAmount(orderNum));
        orderRefundDTO.setRefundCount(0);
        return orderRefundDTO;
    }
    public static OrderRefundDTO getAllOrderRefundDeliveryDTO(Long orderNum) {
        OrderRefundDTO orderRefundDTO = new OrderRefundDTO();
        orderRefundDTO.setOrderId(orderNum);
        orderRefundDTO.setOrderItemId(0L);
        orderRefundDTO.setApplyType(2);
        orderRefundDTO.setRefundType(1);
        orderRefundDTO.setBuyerReason(0);
        orderRefundDTO.setBuyerDesc("整单退货退款自动化执行");
        orderRefundDTO.setBuyerMobile("12345678910");
        orderRefundDTO.setImgUrls("");
        orderRefundDTO.setReceived(1);
        orderRefundDTO.setRefundAmount(getRefundAmount(orderNum));
        orderRefundDTO.setRefundCount(0);
        return orderRefundDTO;
    }

    public static Long getRefundAmount(Long orderNum) {
        Long refundAmount;
        Integer status = 2;
        if(MyOrderApi.orderDetail(orderNum).getStatus().equals(status)){
            refundAmount = MyOrderApi.orderDetail(orderNum).getCanRefundAmount();
        }else {
            refundAmount = MyOrderApi.orderDetail(orderNum).getCanRefundAmount() - MyOrderApi.orderDetail(orderNum).getTransfee();
        }
        return refundAmount;
    }
    /**
     * 部分退款-移除赠品，退货退款
     */
    public static OrderRefundDTO getOrderRefundDtoRemoveGiveaway(Long orderNum) {
        OrderRefundDTO orderRefundDTO = new OrderRefundDTO();
        orderRefundDTO.setOrderId(orderNum);
        orderRefundDTO.setOrderItemId(MyOrderApi.orderDetail(orderNum).getOrderItems().get(0).getOrderItemId());
        orderRefundDTO.setApplyType(2);
        orderRefundDTO.setRefundType(2);
        orderRefundDTO.setBuyerDesc("部分退款移除赠品自动化执行");
        orderRefundDTO.setBuyerReason(0);
        orderRefundDTO.setBuyerMobile("12345678910");
        orderRefundDTO.setImgUrls("");
        orderRefundDTO.setReceived(0);
        orderRefundDTO.setRefundAmount((long) Arith.sub(MyOrderApi.orderDetail(orderNum).getOrderItems().get(0).getActualTotal(), MyOrderApi.orderDetail(orderNum).getOrderItems().get(0).getGiveawayList().get(0).getGiveawayAmount()));
        orderRefundDTO.setRefundCount(0);
        orderRefundDTO.setGiveawayItemIds(new ArrayList<>());

        return orderRefundDTO;
    }
    public static OrderRefundDTO getOrderRefundDeliveryDTO(Long orderNum,Long refundAmount) {
        OrderRefundDTO orderRefundDTO = new OrderRefundDTO();
        orderRefundDTO.setOrderId(orderNum);
        orderRefundDTO.setOrderItemId(MyOrderApi.orderDetail(orderNum).getOrderItems().get(0).getOrderItemId());
        orderRefundDTO.setApplyType(2);
        orderRefundDTO.setRefundType(2);
        orderRefundDTO.setBuyerReason(0);
        orderRefundDTO.setBuyerDesc("部分退货退款自动化执行");
        orderRefundDTO.setBuyerMobile("12345678910");
        orderRefundDTO.setImgUrls("");
        orderRefundDTO.setReceived(1);
        if (refundAmount == 0){
        orderRefundDTO.setRefundAmount(getRefundAmount(orderNum));
        }else {
            orderRefundDTO.setRefundAmount(refundAmount);
        }
        orderRefundDTO.setRefundCount(1);
        return orderRefundDTO;
    }

    private static Long getVirtualOrderRefundAmount(Long orderNum, Integer count) {
        Integer prodNum = MyOrderApi.orderDetail(orderNum).getTotalNum();
        Long actualTotal = MyOrderApi.orderDetail(orderNum).getOrderItems().get(0).getActualTotal();
        return (long) Arith.mul(Arith.div(count, prodNum), actualTotal);
    }

    public static OrderRefundDTO getOrderRefundApartAmountDTO(Long orderNum,Long refundAmount) {
        OrderRefundDTO orderRefundDTO = new OrderRefundDTO();
        orderRefundDTO.setOrderId(orderNum);
        orderRefundDTO.setOrderItemId(OrderApi.getOrderItemAndAddress(orderNum).getOrderItems().get(0).getOrderItemId());
        orderRefundDTO.setApplyType(1);
        orderRefundDTO.setRefundType(2);
        orderRefundDTO.setBuyerDesc("部分退款自动化执行");
        orderRefundDTO.setBuyerReason(0);
        orderRefundDTO.setBuyerMobile("12345678910");
        orderRefundDTO.setImgUrls("");
        orderRefundDTO.setReceived(0);
        orderRefundDTO.setRefundAmount(refundAmount);
        orderRefundDTO.setRefundCount(0);
        return orderRefundDTO;
    }

    public static OrderRefundDTO getRefundDeliveryApartAmountDTO(Long orderNum,Long refundAmount) {
        OrderRefundDTO orderRefundDTO = new OrderRefundDTO();
        orderRefundDTO.setOrderId(orderNum);
        orderRefundDTO.setOrderItemId(OrderApi.getOrderItemAndAddress(orderNum).getOrderItems().get(0).getOrderItemId());
        orderRefundDTO.setApplyType(2);
        orderRefundDTO.setRefundType(2);
        orderRefundDTO.setBuyerDesc("退货退款退部分金额");
        orderRefundDTO.setBuyerReason(0);
        orderRefundDTO.setBuyerMobile("12345678910");
        orderRefundDTO.setImgUrls("");
        orderRefundDTO.setReceived(1);
        orderRefundDTO.setRefundAmount(refundAmount);
        orderRefundDTO.setRefundCount(1);
        return orderRefundDTO;
    }
}
