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

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

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

public class OrderStatusUtils {
    //    创建退款单的前提条件：payOrderYZFList.size() >= 1，orderRefund.refundAmount <= payOrder.payAmount
//
//    此订单的支付订单列表
//    payOrderList 的查询条件，订单号，支付状态 !=（3:已取消）
    private List<PayOrder> payOrderList;
    //    此订单的支付中支付订单列表
    //    payOrderWZFList 的查询条件，订单号，支付状态为（1:支付中）
    private List<PayOrder> payOrderWZFList = new ArrayList<PayOrder>();
    //    此订单的已支付支付订单列表
    //    payOrderYZFList 的查询条件，订单号，支付状态为（2:已支付）
    private List<PayOrder> payOrderYZFList = new ArrayList<PayOrder>();
    //    此订单的退款订单列表
    //    orderRefundList 的查询条件，订单号，退款状态 !=（4:已取消）
    private List<OrderRefund> orderRefundList;
    //    此订单的待退款退款订单列表
    //    orderRefundDTKList 的查询条件，订单号，退款状态为（1:待退款）
    private List<OrderRefund> orderRefundDTKList1 = new ArrayList<OrderRefund>();
    //    此订单的退款中退款订单列表
    //    orderRefundWTKList 的查询条件，订单号，退款状态为（2:退款中）
    private List<OrderRefund> orderRefundWTKList = new ArrayList<OrderRefund>();
    //    此订单的已退款退款订单列表
    //    orderRefundYTKList 的查询条件，订单号，退款状态为（3:已退款）
    private List<OrderRefund> orderRefundYTKList = new ArrayList<OrderRefund>();
    //    totalYfk 已支付金额，payOrderYZFList中所有支付金额总
    private long totalYfk = 0l;
    //    totalYfk 已支付金额，payOrderYZFList中所有支付金额总 - orderRefundYTKList中所有支付金额总和
    private long totalYfkCount = 0l;
    //    totalYtk 已退款金额，orderRefundYTKList中所有支付金额总和
    private long totalYtk = 0l;
    private long totalDiscount = 0l;
    private long orderAmount = 0l;
    private long orderRealPayAmount = 0l;
    private String orderStatus;
    private boolean isInit = false;

    /**
     * 订单状态计算工具类构造方法
     *
     * @param intiPayOrderList
     * @param initOrderRefundList
     * @param order
     */
    public OrderStatusUtils(List<PayOrder> intiPayOrderList, List<OrderRefund> initOrderRefundList, OrderMaster order,long YfkTotal,long YtkTotal) {
        payOrderList = intiPayOrderList;
        orderRefundList = initOrderRefundList;
        orderAmount = order.getOrderAmount();
        orderStatus = order.getOrderStatus();
        orderRealPayAmount = order.getRealPayAmount();

        initPayOrderList();
        initOrderRefundList();
        totalYfk = YfkTotal;
        totalYtk = YtkTotal;
        totalYfkCount = totalYfk - totalYtk;
        isInit = true;
    }

    /**
     * 计算订单状态
     *
     * @return
     */
    public String computeOrderStatus() {
        String orderStraus = null;
        if(payOrderList==null)
            payOrderList = new ArrayList<>();
        if (isInit) {
            if (DictionaryConstant.ORDER_UPDATE_STATUS_DFH.getCode().equals(orderStatus) ||
                    DictionaryConstant.ORDER_UPDATE_STATUS_DQH.getCode().equals(orderStatus) ||
                    DictionaryConstant.ORDER_UPDATE_STATUS_DJD.getCode().equals(orderStatus) ||
                    DictionaryConstant.ORDER_UPDATE_STATUS_YFH.getCode().equals(orderStatus) ||
                    DictionaryConstant.ORDER_UPDATE_STATUS_YSD.getCode().equals(orderStatus) ||
                    DictionaryConstant.ORDER_UPDATE_STATUS_YSH.getCode().equals(orderStatus)) {
                //配送状态下不做订单状态不做计算
                orderStraus = this.orderStatus;
            } else if (DictionaryConstant.ORDER_UPDATE_STATUS_DFK.getCode().equals(orderStatus) && payOrderList.size() == 0 && totalYfkCount == 0) {
                //待支付
                orderStraus = DictionaryConstant.ORDER_UPDATE_STATUS_DFK.getCode();
            } else if (DictionaryConstant.ORDER_UPDATE_STATUS_DDQX.getCode().equals(orderStatus) && payOrderList.size() == 0 && totalYfkCount == 0) {
                //已取消
                orderStraus = DictionaryConstant.ORDER_UPDATE_STATUS_DDQX.getCode();
            } else if (payOrderList.size() > 0 && payOrderYZFList.size() == 0 && totalYfkCount < orderAmount) {
                //支付中
                orderStraus = DictionaryConstant.ORDER_UPDATE_STATUS_ZFZ.getCode();
            } else if (payOrderList.size() > 0 && payOrderYZFList.size() > 0 && totalYfkCount < orderAmount && orderRefundList.size() == 0) {
                //已支付
                orderStraus = DictionaryConstant.ORDER_UPDATE_STATUS_YZF.getCode();
            } else if (payOrderList.size() > 0 && payOrderYZFList.size() > 0 && orderRefundWTKList.size() == 0 && totalYfkCount == orderAmount) {
                // 支付完成
                orderStraus = DictionaryConstant.ORDER_UPDATE_STATUS_ZFWC.getCode();
            } else if (orderRefundList.size() > 0 && orderRefundWTKList.size() > 0 && orderRefundYTKList.size() == 0 && totalYfkCount == orderAmount) {
                //待退款
                orderStraus = DictionaryConstant.ORDER_UPDATE_STATUS_DTC.getCode();
            } else if (orderRefundList.size() > 0 && orderRefundWTKList.size() > 0 && orderRealPayAmount > 0) {
                //退款中
                orderStraus = DictionaryConstant.ORDER_UPDATE_STATUS_TKZ.getCode();
            } else if (orderRefundList.size() > 0 && orderRefundYTKList.size() > 0 && orderRefundYTKList.size() == orderRefundList.size() && orderRealPayAmount > 0) {
                //已退款
                orderStraus = DictionaryConstant.ORDER_UPDATE_STATUS_YTC.getCode();
            } else if (orderRefundList.size() > 0 && orderRefundYTKList.size() > 0 && orderRefundYTKList.size() == orderRefundList.size() && orderRealPayAmount == 0) {
                //退款完成
                orderStraus = DictionaryConstant.ORDER_UPDATE_STATUS_TKWC.getCode();
            } else{
                orderStraus = this.orderStatus;
            }
        }
        return orderStraus;
    }

    /**
     * 初始化支付订单列表
     */
    private void initPayOrderList() {
        if (payOrderList == null || payOrderList.size() == 0) {
            return;
        }
        for (val payOrder : payOrderList) {
            if (payOrder != null) {
                if (DictionaryConstant.PAY_STATUS_WFK.getCode().equals(payOrder.getPayStatus())) {
                    payOrderWZFList.add(payOrder);
                } else if (DictionaryConstant.PAY_STATUS_YFK.getCode().equals(payOrder.getPayStatus())) {
                    payOrderYZFList.add(payOrder);
                    if (DictionaryConstant.PAY_TYPE_DISCOUNT.getCode().equals(payOrder.getPayType())) {
                        totalDiscount = totalDiscount + payOrder.getPayAmount();//单位分
                    }
                    //totalYfk = totalYfk + payOrder.getPayAmount();//单位分
                } else {

                }
            }
        }
    }

    /**
     * 初始化退款单列表
     */
    private void initOrderRefundList() {
        if (orderRefundList == null || orderRefundList.size() == 0) {
            return;
        }
        for (val orderRefund : orderRefundList) {
            if (orderRefund != null) {
                if (DictionaryConstant.REFUND_STATUS_DTK.getCode().equals(orderRefund.getRefundStatus())) {
                    orderRefundWTKList.add(orderRefund);
                } else if (DictionaryConstant.REFUND_STATUS_TKZ.getCode().equals(orderRefund.getRefundStatus())) {
                    orderRefundWTKList.add(orderRefund);
                } else if (DictionaryConstant.REFUND_STATUS_TKWC.getCode().equals(orderRefund.getRefundStatus())) {
                    orderRefundYTKList.add(orderRefund);
                    //totalYtk = totalYtk + orderRefund.getRefundedAmount().multiply(BigDecimal.valueOf(100L)).longValue();
                } else {

                }
            }
        }
    }

    public Long getDiscountAmount() {
        return totalDiscount;
    }

    public Long getHasPay() {
        return totalYfkCount;
    }
}
