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

import com.alibaba.fastjson.JSONObject;
import com.jeequan.jeepay.components.mq.vender.IMQSender;
import com.jeequan.jeepay.converter.MchInfoConverter;
import com.jeequan.jeepay.core.model.params.sxfpay.SxfpayIsvParams;
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.sxfpay.SxfpayKit;
import com.jeequan.jeepay.thirdparty.channel.sxfpay.SxfpayMchApplymentService;
import com.jeequan.jeepay.thirdparty.channel.sxfpay.model.ReqMethod;
import com.jeequan.jeepay.thirdparty.service.ConfigContextQueryService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Slf4j
@Component("sxfpayAuditReceiver")
public class SxfpayAuditReceiver implements IsvAuditReceiver {

    private static final String logPrefix = "【随行付商户进件】";

    @Autowired
    private IMQSender mqSender;

    @Autowired
    private MchApplymentService mchApplymentService;

    @Autowired
    private MchInfoConverter infoConverter;

    @Autowired
    private SxfpayMchApplymentService sxfpayMchApplymentService;

    @Autowired
    private ConfigContextQueryService configContextQueryService;

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

        MchApplyment result = new MchApplyment();

        // 获取详细参数
        try {
            com.jeequan.jeepay.core.entity.MchApplyment model = infoConverter.toModel(mchApplyment);

            SxfpayIsvParams isvParams = ((SxfpayIsvParams) configContextQueryService.queryIsvParams(model));

            // 进件参数
            JSONObject reqParams = sxfpayMchApplymentService.getApplymentParams(model);

            // 发送请求
            JSONObject resData = SxfpayKit.reqApi(ReqMethod.Method.MERCHANT_INCOME, isvParams, reqParams);
            String code = resData.getString("bizCode");
            String bizMsg = resData.getString("bizMsg");
            if (SxfpayKit.SUCCESS_CODE.equals(code)) {
                // 渠道号
                String channelOrderNo = resData.getString("applicationId");
                result.setChannelApplyNo(channelOrderNo);
                // 商户号
                String mno = resData.getString("mno");
                if (StringUtils.isNotEmpty(mno)) {
                    JSONObject jsonParams = new JSONObject();
                    jsonParams.put("mno", mno);
                    result.setSuccResParameter(jsonParams.toJSONString());
                    result.setChannelMchNo(mno);
                    result.setState(com.jeequan.jeepay.core.entity.MchApplyment.STATE_AUDITING);
                }
                // 进件响应参数
                result.setChannelVar1(resData.toJSONString());
                result.setRemainStep((byte) 0);
            } else {
                result.setApplyErrorInfo(code + "[" + bizMsg + "]");
                result.setRemainStep((byte) 0);
                result.setState(com.jeequan.jeepay.core.entity.MchApplyment.STATE_REJECT_WAIT_MODIFY);
                log.error("{} 请求失败：code={}, msg={}", logPrefix, code, bizMsg);
            }

            result.setApplyId(mchApplyment.getApplyId());
            mchApplymentService.updateById(result);
        } catch (Exception e) {
            log.info("进件异常", e);
            result.setApplyId(mchApplyment.getApplyId());
            result.setState(MchApplyment.STATE_REJECT_WAIT_MODIFY);
            result.setApplyErrorInfo("进件失败[" + e.getMessage() + "]");
            mchApplymentService.updateById(result);
        }
    }
}
