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

import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
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.ruipay.RyxpayIsvParams;
import com.jeequan.jeepay.core.model.params.ruipay.RyxpayIsvsubMchParams;
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.ryxpay.model.PayRespEntity;
import com.jeequan.jeepay.thirdparty.channel.ryxpay.model.ReqMethod;
import com.jeequan.jeepay.thirdparty.service.ConfigContextQueryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 退款接口： 瑞银信
 *
 * @author xiaoyu
 *
 * @date 2022/4/15 9:34
 */
@Service
public class RyxpayRefundService extends AbstractRefundService {

    @Autowired
    private ConfigContextQueryService configContextQueryService;

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

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

    /**
     * 退款
     * @param bizRQ
     * @param refundOrder
     * @param payOrder
     * @param mchAppConfigContext
     * @return
     * @throws Exception
     */
    @Override
    public ChannelRetMsg refund(RefundOrderRQ bizRQ, RefundOrder refundOrder, PayOrder payOrder, MchAppConfigContext mchAppConfigContext) throws Exception {
        RyxpayIsvParams isvParams = (RyxpayIsvParams) configContextQueryService.queryIsvParams(mchAppConfigContext.getMchApplyment().getIsvNo(),payOrder.getIfCode());
        ChannelRetMsg channelRetMsg = new ChannelRetMsg();
        JSONObject bizContent = new JSONObject();
        bizContent.put("merchantCode",payOrder.getChannelMchNo());
        bizContent.put("oriReqDate", DateUtil.format(payOrder.getCreatedAt(), DatePattern.PURE_DATE_PATTERN));
        bizContent.put("oriReqMsgId",payOrder.getPayOrderId());
        bizContent.put("refundAmount", AmountUtil.convertCent2Dollar(refundOrder.getRefundAmount()));
        bizContent.put("refundReason",refundOrder.getRefundReason());
        PayRespEntity response = RyxpayKit.payReqApi(ReqMethod.ServiceCode.SMZF004, isvParams, bizContent, refundOrder.getRefundOrderId(), getNotifyUrl());
        channelRetMsg.setChannelBizData(response.getData());
        channelRetMsg.setChannelOrderId(response.getSmzfMsgId());
        channelRetMsg.setChannelErrCode(response.getRespCode());
        channelRetMsg.setChannelErrMsg(response.getRespMsg());
        channelRetMsg.setPlatformMchOrderNo(response.getChannelMsgId());
        String respType = response.getRespType();
        if(RyxpayKit.RESP_BIZ_SUCCESS.equals(respType)){
            channelRetMsg.setChannelState(ChannelRetMsg.ChannelState.CONFIRM_SUCCESS);
        }else if(RyxpayKit.RESP_BIZ_FAIL.equals(respType)){
            channelRetMsg.setChannelState(ChannelRetMsg.ChannelState.CONFIRM_FAIL);
        }else{
            channelRetMsg.setChannelState(ChannelRetMsg.ChannelState.WAITING);
        }
        return channelRetMsg;
    }

    @Override
    public ChannelRetMsg query(RefundOrder refundOrder, MchAppConfigContext mchAppConfigContext) throws Exception {
        RyxpayIsvParams isvParams = (RyxpayIsvParams) configContextQueryService.queryIsvParams(mchAppConfigContext.getMchApplyment().getIsvNo(),refundOrder.getIfCode());
        RyxpayIsvsubMchParams mchParams = (RyxpayIsvsubMchParams) configContextQueryService.queryIsvsubMchParams(mchAppConfigContext.getMchNo(), mchAppConfigContext.getMchApplyment().getAutoConfigMchAppId(), refundOrder.getIfCode());
        JSONObject bizContent = new JSONObject();
        bizContent.put("merchantCode",mchParams.getMerchantCode());
        bizContent.put("oriReqDate",mchParams.getMerchantCode());
        bizContent.put("oriReqMsgId",refundOrder.getRefundOrderId());
        PayRespEntity response = RyxpayKit.payReqApi(ReqMethod.ServiceCode.SMZF006, isvParams, bizContent, null, null);
        JSONObject bizData = response.getData();
        String oriRespType = bizData.getString("oriRespType");
        if(RyxpayKit.RESP_BIZ_ING.equals(oriRespType)){
            return ChannelRetMsg.waiting();
        }else if(RyxpayKit.RESP_BIZ_FAIL.equals(oriRespType)){
            return ChannelRetMsg.confirmFail(response.getData().getString("oriRespCode"),response.getData().getString("oriRespMsg"));
        }else if(RyxpayKit.RESP_BIZ_SUCCESS.equals(oriRespType)){
            return ChannelRetMsg.confirmSuccess(response.getSmzfMsgId(),response.getData().getString("channelTradeNo") ,response.getChannelMsgId(),null);
        }
        return ChannelRetMsg.waiting();
    }

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

    @Override
    public void checkPlatAccount(RefundOrder refundOrder) {

    }
}
