package com.jeequan.jeepay.thirdparty.channel.sxfpay;

import com.alibaba.fastjson.JSONObject;
import com.jeequan.jeepay.core.constants.CS;
import com.jeequan.jeepay.core.entity.PayOrder;
import com.jeequan.jeepay.core.entity.RefundOrder;
import com.jeequan.jeepay.core.model.context.MchAppConfigContext;
import com.jeequan.jeepay.core.model.params.sxfpay.SxfpayIsvParams;
import com.jeequan.jeepay.core.model.params.sxfpay.SxfpayIsvsubMchParams;
import com.jeequan.jeepay.core.model.rqrs.msg.ChannelRefundLimit;
import com.jeequan.jeepay.core.model.rqrs.msg.ChannelRetMsg;
import com.jeequan.jeepay.core.model.rqrs.refund.RefundOrderRQ;
import com.jeequan.jeepay.core.utils.AmountUtil;
import com.jeequan.jeepay.thirdparty.channel.AbstractRefundService;
import com.jeequan.jeepay.thirdparty.channel.sxfpay.model.ReqMethod;
import org.springframework.stereotype.Service;

/**
 * 退款接口： 随行付支付
 *
 * @author xiaoyu
 *
 * @date 2022/4/15 9:34
 */
@Service
public class SxfpayRefundService extends AbstractRefundService {

    public final static String SUCCESS = "REFUNDSUC";

    public final static String FAIL = "REFUNDFAIL";

    public final static String ING = "REFUNDING";

    @Override
    public String getIfCode() {
        return CS.IF_CODE.SXFPAY;
    }

    @Override
    public String preCheck(RefundOrderRQ bizRQ, RefundOrder refundOrder, PayOrder payOrder) {
        return null;
    }

    @Override
    public ChannelRetMsg refund(RefundOrderRQ bizRQ, RefundOrder refundOrder, PayOrder payOrder, MchAppConfigContext mchAppConfigContext) throws Exception {
        ChannelRetMsg channelRetMsg = new ChannelRetMsg();
        JSONObject bizContent = new JSONObject();
        bizContent.put("mno",payOrder.getChannelMchNo());
        bizContent.put("ordNo",refundOrder.getRefundOrderId());
        bizContent.put("origOrderNo",payOrder.getPayOrderId());
        bizContent.put("amt", AmountUtil.convertCent2Dollar(bizRQ.getRefundAmount()));
        bizContent.put("notifyUrl",getNotifyUrl());
        bizContent.put("refundReason",bizRQ.getRefundReason());
        SxfpayIsvParams isvParams = (SxfpayIsvParams) configContextQueryService.queryIsvParams(mchAppConfigContext.getMchApplyment().getIsvNo(),getIfCode());
        try {
            JSONObject respBizData = SxfpayKit.reqApi(ReqMethod.Method.TRADE_ORDER_REFUND, isvParams, bizContent);
            String tranSts = respBizData.getString("tranSts");
            channelRetMsg.setChannelOrderId(respBizData.getString("uuid"));
            channelRetMsg.setPlatformOrderNo(respBizData.getString("transactionId"));
            channelRetMsg.setPlatformMchOrderNo(respBizData.getString("sxfUuid"));
            channelRetMsg.setChannelErrCode(respBizData.getString("bizCode"));
            channelRetMsg.setChannelErrMsg(respBizData.getString("bizMsg"));
            channelRetMsg.setChannelAttach(respBizData.toString());
            switch (tranSts){
                case SUCCESS:
                    channelRetMsg.setChannelState(ChannelRetMsg.ChannelState.CONFIRM_SUCCESS);
                    break;
                case FAIL:
                    channelRetMsg.setChannelState(ChannelRetMsg.ChannelState.CONFIRM_FAIL);
                    break;
                case ING:
                    channelRetMsg.setChannelState(ChannelRetMsg.ChannelState.WAITING);
                    break;
            }
        }catch (Exception e){
            channelRetMsg.setChannelState(ChannelRetMsg.ChannelState.CONFIRM_FAIL);
            channelRetMsg.setChannelErrMsg(e.getMessage());
        }
        return channelRetMsg;
    }

    @Override
    public ChannelRetMsg query(RefundOrder refundOrder, MchAppConfigContext mchAppConfigContext) throws Exception {
        SxfpayIsvParams isvParams = (SxfpayIsvParams) configContextQueryService.queryIsvParams(mchAppConfigContext.getMchApplyment().getIsvNo(), refundOrder.getIfCode());
        SxfpayIsvsubMchParams mchParams = (SxfpayIsvsubMchParams) configContextQueryService.queryIsvsubMchParams(mchAppConfigContext.getMchNo(), mchAppConfigContext.getMchApplyment().getAutoConfigMchAppId(), refundOrder.getIfCode());
        ChannelRetMsg channelRetMsg = new ChannelRetMsg();
        channelRetMsg.setChannelState(ChannelRetMsg.ChannelState.WAITING);
        JSONObject bizContent = new JSONObject();
        // 商户订单号
        bizContent.put("mno", mchParams.getMno());
        // 退款订单号
        bizContent.put("ordNo", refundOrder.getRefundOrderId());
        JSONObject respBizData = SxfpayKit.reqApi(ReqMethod.Method.TRADE_REFUND_QUERY, isvParams, bizContent);
        String tranSts = respBizData.getString("tranSts");
        channelRetMsg.setChannelOrderId(respBizData.getString("uuid"));
        channelRetMsg.setPlatformOrderNo(respBizData.getString("transactionId"));
        channelRetMsg.setPlatformMchOrderNo(respBizData.getString("sxfUuid"));
        channelRetMsg.setChannelErrCode(respBizData.getString("bizCode"));
        channelRetMsg.setChannelErrMsg(respBizData.getString("bizMsg"));
        channelRetMsg.setChannelAttach(respBizData.toString());
        switch (tranSts){
            case SUCCESS:
                channelRetMsg.setChannelState(ChannelRetMsg.ChannelState.CONFIRM_SUCCESS);
                break;
            case FAIL:
                channelRetMsg.setChannelState(ChannelRetMsg.ChannelState.CONFIRM_FAIL);
                break;
        }
        return channelRetMsg;
    }

    /**
     * 退款权限
     * @return
     */
    @Override
    public ChannelRefundLimit isRefundLimit(String settleType,String applyId) {
        return super.isRefundLimit(settleType,applyId);
    }

    @Override
    public void checkPlatAccount(RefundOrder refundOrder) {

    }
}
