package com.neusoft.smart.pos.order.utils;

import com.neusoft.smart.pos.constants.DictionaryConstant;
import com.neusoft.smart.pos.order.persistent.domain.OrderRefund;
import com.neusoft.smart.pos.payorder.persistent.domain.PayRefundOrder;
import lombok.val;

import java.util.ArrayList;
import java.util.List;

public class RefundStatusUtils {
    private boolean isInit = false;
    private String refundStatus;
    private double refundAmount;
    private List<PayRefundOrder> payRefundOrderList;
    private List<PayRefundOrder> payOrderYTKList;
    private double totalYTK;

    /**
     * 退款订单状态计算工具类构造方法
     * @param initPayRefundList
     * @param orderRefund
     */
    public RefundStatusUtils(List<PayRefundOrder> initPayRefundList, OrderRefund orderRefund) {
        payRefundOrderList = initPayRefundList;
        refundAmount = orderRefund.getRefundedAmount().doubleValue();
        refundStatus = orderRefund.getRefundStatus();

        initPayRefundOrderList();
        isInit = true;
    }

    /**
     * 初始化支付订单列表
     */
    private void initPayRefundOrderList() {
        if (payRefundOrderList == null || payRefundOrderList.size() == 0) {
            return;
        }
        payOrderYTKList = new ArrayList<PayRefundOrder>();
        for (val payRefundOrder : payRefundOrderList) {
            if (payRefundOrder != null) {
                if (DictionaryConstant.REFUND_STATUS_TKWC.getCode().equals(payRefundOrder.getRefundStatus())) {
                    payOrderYTKList.add(payRefundOrder);
                    double refundAmount = payRefundOrder.getRefundedAmount().doubleValue() / 100;//单位从分到元
                    totalYTK = totalYTK + refundAmount;
                } else {

                }
            }
        }
    }

    /**
     * 计算订单状态
     * @return
     */
    public String computeRefundStatus() {
        String refundStatus = null;
        if (isInit) {
            if(DictionaryConstant.REFUND_STATUS_TKQX.getCode().equals(refundStatus)){
                refundStatus = DictionaryConstant.REFUND_STATUS_TKQX.getCode();
            } else if (payOrderYTKList == null || payOrderYTKList.size() == 0) {
                //已退款
                refundStatus = DictionaryConstant.REFUND_STATUS_DTK.getCode();
            } else if (payOrderYTKList.size() >= 1) {
                if (totalYTK > 0 && totalYTK < refundAmount) {
                    //退款完成
                    refundStatus = DictionaryConstant.REFUND_STATUS_TKZ.getCode();
                } else if (totalYTK == refundAmount) {
                    //退款完成
                    refundStatus = DictionaryConstant.REFUND_STATUS_TKWC.getCode();
                }
            }
        }
        return refundStatus;
    }
}
