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

import com.alibaba.fastjson.JSONObject;
import com.jeequan.jeepay.components.mq.vender.IMQSender;
import com.jeequan.jeepay.converter.MchInfoConverter;
import com.jeequan.jeepay.core.entity.MchInfo;
import com.jeequan.jeepay.core.model.applyment.DgpayApplymentInfo;
import com.jeequan.jeepay.core.model.context.MchAppConfigContext;
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.dgpay.DgPayKit;
import com.jeequan.jeepay.thirdparty.channel.dgpay.DgpayMchApplymentService;
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("dgpayAuditReceiver")
public class DgpayAuditReceiver implements IsvAuditReceiver {

    private static final String logPrefix = "【斗拱商户进件】";

    @Autowired
    private IMQSender mqSender;

    @Autowired
    private MchApplymentService mchApplymentService;

    @Autowired
    private MchInfoConverter infoConverter;

    @Autowired
    private DgpayMchApplymentService dgpayMchApplymentService;

    @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);
            // 获取支付参数
            MchAppConfigContext mchAppConfigContext = new MchAppConfigContext()
                    .setMchType(MchInfo.TYPE_ISVSUB)
                    .setMchApplyment(model);
            // 进件参数
            JSONObject reqParams = dgpayMchApplymentService.getApplymentParams(model);

            // 发送请求
            JSONObject response = DgPayKit.request(mchAppConfigContext, reqParams, DgpayApplymentInfo.MCH_APPLY_REG_URL, mchApplyment.getIfCode());
            JSONObject resData = response.getJSONObject("data");
            String code = resData.getString("resp_code");
            if ("90000000".equals(code)) {
                // 渠道号
                String channelOrderNo = resData.getString("apply_no");
                result.setChannelApplyNo(channelOrderNo);
                // 商户号
                String huifuId = resData.getString("huifu_id");
                //根据汇付技术返回的情况 如同步返回的huifu_id的这个参数 则提示为入网成功 待签约 如果没有这个参数 就触发了审核操作
                if (StringUtils.isNotEmpty(huifuId)) {
                    JSONObject jsonParams = new JSONObject();
                    jsonParams.put("huifuId", huifuId);
                    result.setSuccResParameter(jsonParams.toJSONString());
                    result.setState(com.jeequan.jeepay.core.entity.MchApplyment.STATE_WAIT_SIGN);
                } else {
                    result.setState(com.jeequan.jeepay.core.entity.MchApplyment.STATE_AUDITING);
                }
                // 进件响应参数
                result.setChannelVar1(resData.toJSONString());
                result.setRemainStep((byte) 0);
            } else {
                result.setApplyErrorInfo(code + "[" + resData.getString("resp_desc") + "]");
                result.setRemainStep((byte) 0);
                result.setState(com.jeequan.jeepay.core.entity.MchApplyment.STATE_REJECT_WAIT_MODIFY);
                log.error("{} 请求失败：code={}, msg={}", logPrefix, code, resData.getString("resp_desc"));
            }

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