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

import com.alibaba.fastjson.JSONObject;
import com.jeequan.jeepay.core.constants.CS;
import com.jeequan.jeepay.core.entity.RefundOrder;
import com.jeequan.jeepay.core.exception.ResponseException;
import com.jeequan.jeepay.core.model.context.MchAppConfigContext;
import com.jeequan.jeepay.core.model.params.ruipay.RyxpayIsvParams;
import com.jeequan.jeepay.core.model.rqrs.msg.ChannelRetMsg;
import com.jeequan.jeepay.core.utils.SpringBeansUtil;
import com.jeequan.jeepay.thirdparty.channel.AbstractChannelRefundNoticeService;
import com.jeequan.jeepay.thirdparty.channel.ryxpay.model.PayRespEntity;
import com.jeequan.jeepay.thirdparty.service.ConfigContextQueryService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.tuple.MutablePair;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;

/**
 * 瑞银信退款回调接口实现类
 *
 * @author xiaoyu
 *
 * @date 2021/12/24 11:31
 */
@Service
@Slf4j
public class RyxpayChannelRefundNoticeService extends AbstractChannelRefundNoticeService {

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

    @Override
    public MutablePair<String, Object> parseParams(HttpServletRequest request, String urlOrderId, NoticeTypeEnum noticeTypeEnum) {
        try {
            JSONObject params = getReqParamJSON();
            // 接收返回参数
            String refundOrderId = params.getString("reqMsgId");
            return MutablePair.of(refundOrderId, params);
        } catch (Exception e) {
            log.error("error", e);
            throw ResponseException.buildText("ERROR");
        }
    }

    @Override
    public ChannelRetMsg doNotice(HttpServletRequest request, Object params, RefundOrder refundOrder, MchAppConfigContext mchAppConfigContext, NoticeTypeEnum noticeTypeEnum) {
        try {
            // 获取请求参数
            JSONObject respJson = (JSONObject) params;
            // 校验支付回调
            //验证签名 并且解析参数
            PayRespEntity notifyData = verifyParams(respJson, mchAppConfigContext);
            // 验签成功后判断上游订单状态
            ResponseEntity okResponse = textResp(RyxpayKit.SUCCESS_CODE);
            ChannelRetMsg result = new ChannelRetMsg();
            result.setResponseEntity(okResponse);
            result.setChannelErrCode(notifyData.getRespCode());
            result.setChannelErrMsg(notifyData.getRespMsg());
            result.setChannelUserId(notifyData.getData().getString("buyerId"));
            result.setChannelOrderId(notifyData.getSmzfMsgId());
            //如果是支付宝 则需要去除前两位
            result.setPlatformOrderNo(notifyData.getData().getString("channelTradeNo"));
            result.setPlatformMchOrderNo(notifyData.getChannelMsgId());
            result.setChannelState(ChannelRetMsg.ChannelState.CONFIRM_SUCCESS);
            String respType = notifyData.getRespType();
            if(RyxpayKit.RESP_BIZ_FAIL.equals(respType)){
                result.setChannelState(ChannelRetMsg.ChannelState.CONFIRM_FAIL);
            }else if(RyxpayKit.RESP_BIZ_ING.equals(respType)){
                result.setChannelState(ChannelRetMsg.ChannelState.WAITING);
            }
            return result;
        } catch (Exception e) {
            log.error("error", e);
            throw ResponseException.buildText("ERROR");
        }
    }


    /**
     * 处理瑞银信退款通知参数
     * @return
     */
    public PayRespEntity verifyParams(JSONObject jsonParams, MchAppConfigContext mchAppConfigContext) {
        ConfigContextQueryService configContextQueryService = SpringBeansUtil.getBean(ConfigContextQueryService.class);
        RyxpayIsvParams isvParams = (RyxpayIsvParams) configContextQueryService.queryIsvParams(mchAppConfigContext.getMchApplyment());
        return RyxpayKit.resultParamsDecode(jsonParams, isvParams);
    }
}
