package com.ywt.mg.services;

import com.ywt.mg.core.utils.Checker;
import com.ywt.mg.domain.entities.*;
import com.ywt.mg.domain.models.enums.RefundStatusEnum;
import com.ywt.mg.domain.models.nutrimeal.MealOrderInfo;
import com.ywt.mg.domain.ywtDrugEntities.PrescriptionInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class RefundLogService extends ServiceBase {

    private static final Logger logger = LoggerFactory.getLogger(RefundLogService.class);

    @Autowired
    private RefundLogRepository refundLogRepository;

    @Autowired
    private JdbcTemplate jdbcTemplate;


    /**
     * 根据订单IDs得到对应的退款记录
     *
     * @param ) 退款编号s
     * @return {@link List<RefundLog>}
     */
    public List<RefundLog> getRefundLogListByRefundNos(List<String> refundNos) {
        refundNos = refundNos.stream().filter(p -> !Checker.isNone(p)).collect(Collectors.toList());
        int size = refundNos.size();
        if (!Checker.isNone(refundNos)) {
            // payment_status:2 表示支付成功
            StringBuilder sqlBuilder = new StringBuilder("select * from  refund_log where refund_status = 1 and ( ");
            for (int i = 0, j = refundNos.size(); i < j; i++) {
                if (i == 0) {
                    sqlBuilder.append(" refund_no = ");
                } else {
                    sqlBuilder.append(" or refund_no =  ");
                }
                sqlBuilder.append("'" + refundNos.get(i) + "'");
            }
            sqlBuilder.append(" ) limit ").append(size);
            return jdbcTemplate.query(sqlBuilder.toString(), new BeanPropertyRowMapper<>(RefundLog.class));
        }
        return null;
    }

    public List<RefundLog> getRefundLogListByRegisteredOrderList(List<RegisteredOrder> registeredOrderList) {
        if (Checker.isNone(registeredOrderList)) {
            return new ArrayList<>();
        }
        registeredOrderList = registeredOrderList.stream().filter(p -> Checker.getIntegerValue(p.getRefundStatus()) == RefundStatusEnum.SUCCESS.getValue() && !Checker.isNone(p.getRefundNo())).collect(Collectors.toList());
        List<String> refundNos = registeredOrderList.stream().map(RegisteredOrder::getRefundNo).collect(Collectors.toList());
        return getRefundLogListByRefundNos(refundNos);
    }

    public List<RefundLog> getRefundLogListByMealOrderList(List<MealOrderInfo> list) {
        if (Checker.isNone(list)) {
            return new ArrayList<>();
        }
        list = list.stream().filter(p -> !Checker.isNone(p.getRefundTime())).collect(Collectors.toList());
        List<String> paymentNos = list.stream().map(MealOrderInfo::getPaymentNo).collect(Collectors.toList());
        return getRefundLogListByPaymentNos(paymentNos);
    }

    private List<RefundLog> getRefundLogListByPaymentNos(List<String> paymentNos) {
        paymentNos = paymentNos.stream().filter(p -> !Checker.isNone(p)).collect(Collectors.toList());
        int size = paymentNos.size();
        if (!Checker.isNone(paymentNos)) {
            // payment_status:2 表示支付成功
            StringBuilder sqlBuilder = new StringBuilder("select * from  refund_log where refund_status = 1 and ( ");
            for (int i = 0, j = paymentNos.size(); i < j; i++) {
                if (i == 0) {
                    sqlBuilder.append(" out_order_no = ");
                } else {
                    sqlBuilder.append(" or out_order_no =  ");
                }
                sqlBuilder.append("'" + paymentNos.get(i).trim() + "'");
            }
            sqlBuilder.append(" ) limit ").append(size);
            return jdbcTemplate.query(sqlBuilder.toString(), new BeanPropertyRowMapper<>(RefundLog.class));
        }
        return null;
    }

    public List<RefundLog> getRefundLogListByCheckResultOrderList(List<CheckResultOrder> list) {
        if (Checker.isNone(list)) {
            return new ArrayList<>();
        }
        list = list.stream().filter(p -> !Checker.isNone(p.getRefundNo())).collect(Collectors.toList());
        List<String> paymentNos = list.stream().map(CheckResultOrder::getPaymentNo).collect(Collectors.toList());
        return getRefundLogListByPaymentNos(paymentNos);
    }

    public List<RefundLog> getRefundLogListByOfflineConsultationList(List<OfflineConsultation> list) {
        if (Checker.isNone(list)) {
            return new ArrayList<>();
        }
        list = list.stream().filter(p -> !Checker.isNone(p.getRefundTime())).collect(Collectors.toList());
        List<String> paymentNos = list.stream().map(OfflineConsultation::getPaymentNo).collect(Collectors.toList());
        return getRefundLogListByPaymentNos(paymentNos);
    }

    public List<RefundLog> getRefundLogListByOutPatientOrderList(List<OutpatientOrder> list) {
        if (Checker.isNone(list)) {
            return new ArrayList<>();
        }
        list = list.stream().filter(p -> !Checker.isNone(p.getRefundTime())).collect(Collectors.toList());
        List<String> paymentNos = list.stream().map(OutpatientOrder::getPaymentNo).collect(Collectors.toList());
        return getRefundLogListByPaymentNos(paymentNos);
    }

    public List<RefundLog> getRefundLogListByCheckUpOrderList(List<CheckUpOrder> list) {
        if (Checker.isNone(list)) {
            return new ArrayList<>();
        }
        list = list.stream().filter(p -> !Checker.isNone(p.getRefundTime())).collect(Collectors.toList());
        List<String> paymentNos = list.stream().map(CheckUpOrder::getPaymentNo).collect(Collectors.toList());
        return getRefundLogListByPaymentNos(paymentNos);
    }

    public List<RefundLog> getRefundLogListByNatOrderList(List<NatOrder> list) {
        if (Checker.isNone(list)) {
            return new ArrayList<>();
        }
        list = list.stream().filter(p -> !Checker.isNone(p.getRefundTime())).collect(Collectors.toList());
        List<String> paymentNos = list.stream().map(NatOrder::getPaymentNo).collect(Collectors.toList());
        return getRefundLogListByPaymentNos(paymentNos);
    }

    public String getRefundIdByRefundNo(List<RefundLog> refundLogList, String refundNo) {
        if (!Checker.isNone(refundLogList) && !Checker.isNone(refundNo)) {
            RefundLog refundLog = refundLogList.stream().filter(o -> o.getRefundNo().equals(refundNo)).findFirst().orElse(null);
            if (refundLog != null && !Checker.isNone(refundLog.getRefundId())) {
                return refundLog.getRefundId();
            }
        }
        return "";
    }

    public List<RefundLog> getRefundLogListByPrescriptionInfoList(List<PrescriptionInfo> prescriptionInfoList) {
        if (Checker.isNone(prescriptionInfoList)) {
            return new ArrayList<>();
        }
        prescriptionInfoList = prescriptionInfoList.stream().filter(p -> Checker.getIntegerValue(p.getRefundStatus()) == RefundStatusEnum.SUCCESS.getValue() && !Checker.isNone(p.getRefundNo())).collect(Collectors.toList());
        List<String> refundNos = prescriptionInfoList.stream().map(PrescriptionInfo::getRefundNo).collect(Collectors.toList());
        return getRefundLogListByRefundNos(refundNos);
    }
}
