package com.jeequan.jeepay.pay.mq.channel.kqpay;

import com.alibaba.fastjson.JSONObject;
import com.jeequan.jeepay.components.mq.vender.IMQSender;
import com.jeequan.jeepay.converter.MchInfoConverter;
import com.jeequan.jeepay.core.exception.BizException;
import com.jeequan.jeepay.core.model.params.kqpay.KqpayIsvParams;
import com.jeequan.jeepay.db.entity.MchApplyment;
import com.jeequan.jeepay.pay.mq.channel.IsvAuditReceiver;
import com.jeequan.jeepay.service.impl.MchApplymentService;
import com.jeequan.jeepay.thirdparty.channel.kqpay.KqpayKit;
import com.jeequan.jeepay.thirdparty.channel.kqpay.KqpayMchApplymentService;
import com.jeequan.jeepay.thirdparty.channel.kqpay.model.KqpayResp;
import com.jeequan.jeepay.thirdparty.channel.kqpay.util.KqpayConst;
import com.jeequan.jeepay.thirdparty.service.ConfigContextQueryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Slf4j
@Component("kqpayAuditReceiver")
public class KqpayAuditReceiver implements IsvAuditReceiver {

    @Autowired
    private KqpayMchApplymentService kqpayMchApplymentService;

    @Autowired
    private ConfigContextQueryService configContextQueryService;

    @Autowired
    private MchInfoConverter infoConverter;

    @Autowired
    private MchApplymentService mchApplymentService;

    @Autowired
    private IMQSender mqSender;

    @Override
    public void consume(MchApplyment mchApplyment) {
        log.info("快钱进件业务开始, 商户号为{}", mchApplyment.getApplyId());

        MchApplyment result = new MchApplyment();
        result.setApplyId(mchApplyment.getApplyId());

        com.jeequan.jeepay.core.entity.MchApplyment infoConverterModel = infoConverter.toModel(mchApplyment);
        KqpayIsvParams isvParams = ((KqpayIsvParams) configContextQueryService.queryIsvParams(infoConverterModel));

        // 获取详细参数
        try {
            JSONObject applymentParams = kqpayMchApplymentService.getApplymentParams(infoConverterModel, isvParams);
            applymentParams.put("externalRefNumber", mchApplyment.getApplyId());

            KqpayResp kqpayResp = KqpayKit.applymentRequest(KqpayConst.ReqMethod.BS001, isvParams, applymentParams);
            String bizResponseCode = kqpayResp.getResponseBody().getString("bizResponseCode");
            String bizResponseMessage = kqpayResp.getResponseBody().getString("bizResponseMessage");
            String orderId = kqpayResp.getResponseBody().getString("orderId");

            if (!KqpayConst.SUCCESS.equals(bizResponseCode)) {
                throw new BizException(bizResponseMessage);
            }

            JSONObject resData = kqpayResp.getResponseBody();

            // 进件响应参数
            result.setChannelVar1(resData.toString());
            result.setChannelApplyNo(orderId);
            result.setRemainStep(((byte) 0));
            result.setState(com.jeequan.jeepay.core.entity.MchApplyment.STATE_AUDITING);
        } catch (Exception e) {
            result.setState(com.jeequan.jeepay.core.entity.MchApplyment.STATE_REJECT_WAIT_MODIFY);
            result.setApplyErrorInfo("[" + e.getMessage() + "]");
            result.setRemainStep((byte) 0);
        } finally {
            mchApplymentService.updateById(result);
        }
    }
}
