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

import cn.hutool.core.io.resource.BytesResource;
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.ruipay.RyxpayIsvParams;
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.ryxpay.RyxpayKit;
import com.jeequan.jeepay.thirdparty.channel.ryxpay.RyxpayMchApplymentService;
import com.jeequan.jeepay.thirdparty.channel.ryxpay.model.ReqMethod;
import com.jeequan.jeepay.thirdparty.channel.ryxpay.model.RyxAccessResp;
import com.jeequan.jeepay.thirdparty.service.ConfigContextQueryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

@Slf4j
@Component("ryxpayAuditReceiver")
public class RyxpayAuditReceiver implements IsvAuditReceiver {

    private static final String logPrefix = "【瑞银信商户进件】";

    @Autowired
    private IMQSender mqSender;

    @Autowired
    private MchApplymentService mchApplymentService;

    @Autowired
    private MchInfoConverter infoConverter;

    @Autowired
    private RyxpayMchApplymentService ryxpayMchApplymentService;

    @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);

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

            // 进件参数
            JSONObject reqParams = ryxpayMchApplymentService.getApplymentParams(model);
            List<BytesResource> files = ((List<BytesResource>) reqParams.remove("files"));
            JSONObject otherParam = new JSONObject();
            otherParam.put("files", files);

            // 发送请求
            RyxAccessResp resData = RyxpayKit.auditReqApi(ReqMethod.Method.MERCHANT_INCOME, isvParams, reqParams, otherParam);
            String code = resData.getCode();
            String bizMsg = resData.getMsg();
            if (RyxpayKit.AUDIT_SUCCESS_CODE.equals(code)) {
                // 渠道号
                String channelOrderNo = resData.getResult().getString("orderId");
                result.setChannelApplyNo(channelOrderNo);
                result.setState(com.jeequan.jeepay.core.entity.MchApplyment.STATE_AUDITING);
                // 进件响应参数
                result.setChannelVar1(resData.getResult().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());
            result.setRemainStep((byte) 0);
            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);
        }
    }
}
