package com.jeequan.jeepay.pay.ctrl.channel.alipay.util;

import cn.hutool.core.date.DateUtil;
import com.jeequan.jeepay.core.constants.CS;
import com.jeequan.jeepay.core.utils.AmountUtil;
import com.jeequan.jeepay.db.entity.PayOrder;
import com.jeequan.jeepay.db.entity.RefundOrder;
import com.jeequan.jeepay.pay.ctrl.channel.alipay.model.response.AlipaySpiPayOrderQueryResponse;
import com.jeequan.jeepay.pay.ctrl.channel.alipay.model.response.AlipaySpiPayOrderResponse;
import com.jeequan.jeepay.pay.ctrl.channel.alipay.model.response.AlipaySpiRefundOrderResponse;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

import java.util.HashMap;
import java.util.Map;

/*
* 支付宝spi工具
*
* @author zx
*
* @date 2021/6/8 17:40
*/
@Slf4j
public class AlipaySpiRuyiUtil {

    public static final String SPI_RUYI_UNKNOWN = "unknown";
    public static final String SPI_RUYI_PAY = "spi.alipay.merchant.order.commonisv.pay";
    public static final String SPI_RUYI_PAY_QUERY = "spi.alipay.merchant.order.commonisv.query";
    public static final String SPI_RUYI_BILL_QUERY = "spi.alipay.merchant.order.commonisv.bill.query";
    public static final String SPI_RUYI_REFUND_AUTHCODE_APPLY = "spi.alipay.commerce.refundauthcode.apply";
    public static final String SPI_RUYI_REFUND = "spi.alipay.merchant.order.commonisv.refund";
    public static final String SPI_RUYI_REFUND_QUERY = "spi.alipay.merchant.order.commonisv.refund.query";
    public static final String SPI_RUYI_BILL_DETAIL_QUERY = "spi.alipay.commerce.billdetail.query";
    public static final String SPI_RUYI_BILL_STATISTICS_QUERY = "spi.alipay.commerce.billstatistics.query";
    public static final String SPI_RUYI_CASHIER_SIGN = "spi.alipay.commerce.fmcgsaascashier.sign";
    public static final String SPI_RUYI_CASHIER_UNSIGN = "spi.alipay.commerce.fmcgsaascashier.unsign";
    public static final String SPI_RUYI_CASHIER_BATCH_QUERY = "spi.alipay.commerce.fmcgsaascashier.batchquery";
    public static final String SPI_RUYI_CASHIER_QUERY = "spi.alipay.commerce.fmcgsaascashier.query";

    public static Map<String, String> spiMap = new HashMap<>();
    static {
        spiMap.put(SPI_RUYI_UNKNOWN, "未知接口");
        spiMap.put(SPI_RUYI_PAY, "支付下单接口");
        spiMap.put(SPI_RUYI_PAY_QUERY, "支付查单接口");
        spiMap.put(SPI_RUYI_BILL_QUERY, "扫支付凭证商户单号查单接口");
        spiMap.put(SPI_RUYI_REFUND_AUTHCODE_APPLY, "申请退款授权码接口");
        spiMap.put(SPI_RUYI_REFUND, "发起退款接口");
        spiMap.put(SPI_RUYI_REFUND_QUERY, "退款查单接口");
        spiMap.put(SPI_RUYI_BILL_DETAIL_QUERY, "账单流水查询接口");
        spiMap.put(SPI_RUYI_BILL_STATISTICS_QUERY, "账单统计查询接口");
        spiMap.put(SPI_RUYI_CASHIER_SIGN, "收银员签到接口");
        spiMap.put(SPI_RUYI_CASHIER_UNSIGN, "收银员签退接口");
        spiMap.put(SPI_RUYI_CASHIER_BATCH_QUERY, "查询收银员列表接口");
        spiMap.put(SPI_RUYI_CASHIER_QUERY, "查询收银员状态接口");
    }

    /** 支付订单下单接口  转  支付宝订单响应 **/
    public static AlipaySpiPayOrderResponse covertToAlipaySpiPayOrderResponse(PayOrder payOrder) {
        AlipaySpiPayOrderResponse response = new AlipaySpiPayOrderResponse();
        response.setMerchantOrderNo(payOrder.getMchOrderNo());
        response.setIsvOrderNo(payOrder.getPayOrderId());
        response.setChannelOrderNo(StringUtils.defaultString(payOrder.getChannelOrderNo(), ""));
        response.setChannelType(covertPaywayTypeToChannelType(payOrder.getWayCodeType()));
        response.setOrderState(covertToAlipayOrderState(payOrder.getState(), payOrder.getRefundState()));
        response.setPayTime(DateUtil.formatDateTime(payOrder.getSuccessTime()));
        response.setTotalAmount(AmountUtil.convertCent2Dollar(payOrder.getAmount()));
        response.setReceiptAmount(AmountUtil.convertCent2Dollar(payOrder.getAmount()));
        response.setBuyerPayAmount(AmountUtil.convertCent2Dollar(payOrder.getAmount()));
        response.setBuyerUserId(payOrder.getChannelUser());
        response.setBuyerAccountName("");
        response.setAttachParams(payOrder.getExtParam());
        return response;
    }

    /** 支付订单查单接口  转  支付宝订单响应 **/
    public static AlipaySpiPayOrderQueryResponse covertToAlipaySpiPayOrderQueryResponse(PayOrder payOrder) {
        AlipaySpiPayOrderQueryResponse response = new AlipaySpiPayOrderQueryResponse();
        response.setMerchantOrderNo(payOrder.getMchOrderNo());
        response.setIsvOrderNo(payOrder.getPayOrderId());
        response.setChannelOrderNo(payOrder.getChannelOrderNo());
        response.setChannelType(covertPaywayTypeToChannelType(payOrder.getWayCodeType()));
        response.setTerminalId(payOrder.getDeviceNo());
        response.setOrderState(covertToAlipayOrderState(payOrder.getState(), payOrder.getRefundState()));
        response.setPayTime(DateUtil.formatDateTime(payOrder.getSuccessTime()));
        response.setTotalAmount(AmountUtil.convertCent2Dollar(payOrder.getAmount()));
        response.setReceiptAmount(AmountUtil.convertCent2Dollar(payOrder.getAmount()));
        response.setBuyerPayAmount(AmountUtil.convertCent2Dollar(payOrder.getAmount()));
        response.setBuyerUserId(payOrder.getChannelUser());
        response.setBuyerAccountName("");
        response.setAttachParams(payOrder.getExtParam());
        return response;
    }

    /** 退款订单  转  支付宝退款订单响应 **/
    public static AlipaySpiRefundOrderResponse covertToAlipaySpiRefundOrderResponse(RefundOrder refundOrder, String mchOrderNo) {
        AlipaySpiRefundOrderResponse response = new AlipaySpiRefundOrderResponse();
        response.setMerchantOrderNo(mchOrderNo);
        response.setIsvOrderNo(refundOrder.getPayOrderId());
        response.setMerchantRefundNo(refundOrder.getMchRefundNo());
        response.setIsvRefundNo(refundOrder.getRefundOrderId());
        response.setChannelRefundNo(refundOrder.getChannelOrderNo());
        response.setRefundAmount(AmountUtil.convertCent2Dollar(refundOrder.getRefundAmount()));

        if (refundOrder.getState() == RefundOrder.STATE_SUCCESS) {
            response.setFinishTime(DateUtil.formatDateTime(refundOrder.getSuccessTime()));
            response.setOrderState("SUCCESS");
        }else if (refundOrder.getState() == RefundOrder.STATE_ING) {
            response.setOrderState("ACCEPT");
        }else if (refundOrder.getState() == RefundOrder.STATE_INIT) {
            response.setOrderState("PROCESSING");
        }
        return response;
    }

    /** 转换支付订单状态 **/
    private static String covertToAlipayOrderState(Byte state, Byte refundState) {
        switch (state) {
            case PayOrder.STATE_INIT:
            case PayOrder.STATE_ING: return "WAIT_PAY";
            case PayOrder.STATE_SUCCESS: {
                if (refundState == PayOrder.REFUND_STATE_NONE) {
                    return "ORDER_SUCCESS";
                }else if (refundState == PayOrder.REFUND_STATE_SUB) {
                    return "REFUND_PART";
                }
            }
            case PayOrder.STATE_REFUND: return "REFUND_SUCCESS";
            default: return "ORDER_CLOSED";
        }
    }

    /** 转换支付方式类型 **/
    public static String covertPaywayTypeToChannelType(String wayCodeType) {
        switch (wayCodeType) {
            case CS.PAY_WAY_CODE_TYPE.ALIPAY: return "001";
            case CS.PAY_WAY_CODE_TYPE.WECHAT: return "002";
            case CS.PAY_WAY_CODE_TYPE.YSFPAY:
            case CS.PAY_WAY_CODE_TYPE.UNIONPAY:
                return "007";
            default: return "100";
        }
    }

}
