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

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.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.thirdparty.channel.AbstractRefundService;
import com.jeequan.jeepay.thirdparty.channel.kqpay.model.KqpayOrderStatus;
import com.jeequan.jeepay.thirdparty.channel.kqpay.model.KqpayResp;
import com.jeequan.jeepay.thirdparty.channel.kqpay.util.KqpayConst;
import org.springframework.stereotype.Service;

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

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

    @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();
        KqpayKit.setPayParams(mchAppConfigContext,bizContent);
        bizContent.put("amount",refundOrder.getRefundAmount());
        bizContent.put("origOrderCtrl",refundOrder.getChannelPayOrderNo());
        bizContent.put("origRefNumber",payOrder.getPayOrderId());
        bizContent.put("tr3Url",getNotifyUrl());
        KqpayResp kqpayResp = KqpayKit.payRequest(KqpayConst.ReqMethod.A7003, mchAppConfigContext, bizContent, refundOrder.getRefundOrderId());
        JSONObject responseBody = kqpayResp.getResponseBody();
        channelRetMsg.setChannelOrderId(responseBody.getString("idOrderCtrl"));
        channelRetMsg.setPlatformMchOrderNo(responseBody.getString("idTxnCtrl"));
        channelRetMsg.setPlatformOrderNo(responseBody.getString("channelTradeNo"));
        String bizResponseMessage = responseBody.getString("bizResponseMessage");
        String bizResponseCode = responseBody.getString("bizResponseCode");
        channelRetMsg.setChannelErrCode(bizResponseCode);
        channelRetMsg.setChannelErrMsg(bizResponseMessage);
        if(KqpayConst.SUCCESS.equals(bizResponseCode)){
            KqpayOrderStatus txnStatus = KqpayOrderStatus.getVal(responseBody.getString("txnStatus"));
            switch (txnStatus){
                case FAIL:
                    channelRetMsg.setChannelState(ChannelRetMsg.ChannelState.CONFIRM_FAIL);
                    break;
                case PROCESS:
                    channelRetMsg.setChannelState(ChannelRetMsg.ChannelState.WAITING);
                    break;
                case CORRECT:
                    channelRetMsg.setChannelState(ChannelRetMsg.ChannelState.WAITING);
                    break;
                default:
                    channelRetMsg.setChannelState(ChannelRetMsg.ChannelState.CONFIRM_SUCCESS);
                    break;
            }
        }else{
            channelRetMsg.setChannelState(ChannelRetMsg.ChannelState.CONFIRM_FAIL);
        }
        return channelRetMsg;
    }

    @Override
    public ChannelRetMsg query(RefundOrder refundOrder, MchAppConfigContext mchAppConfigContext) throws Exception {
        ChannelRetMsg channelRetMsg = new ChannelRetMsg();
        channelRetMsg.setChannelState(ChannelRetMsg.ChannelState.WAITING); //认为是处理中
        return channelRetMsg;
    }

    /**
     * 是否支持平台户退款
     * @return
     */
    @Override
    public ChannelRefundLimit isRefundLimit(String settleType,String applyId) {
        return super.isRefundLimit(settleType,applyId);
    }

    @Override
    public void checkPlatAccount(RefundOrder refundOrder) {

    }
}
