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

import com.jeequan.jeepay.components.mq.model.MchAuditMQ;
import com.jeequan.jeepay.components.mq.vender.IMQSender;
import com.jeequan.jeepay.converter.MchInfoConverter;
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.yspay.YspayApplymentApiService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component("yspayAuditReceiver")
public class YspayAuditReceiver implements IsvAuditReceiver {

    @Autowired
    private MchApplymentService mchApplymentService;

    @Autowired
    private YspayApplymentApiService yspayApplymentApiService;

    @Autowired
    private IMQSender mqSender;

    @Autowired
    private MchInfoConverter infoConverter;

    @Override
    public void consume(MchApplyment mchApplyment) {
        MchApplyment result = new MchApplyment();
        result.setApplyId(mchApplyment.getApplyId());

        // 这里需要检查一下
        if (mchApplyment.getRemainStep() == 2) {
            // 获取详细参数
            try {
                result = infoConverter.toDbEntity(yspayApplymentApiService.uploadImg(infoConverter.toModel(mchApplyment)));
                result.setState(MchApplyment.STATE_AUDITING_WAIT);
                result.setRemainStep((byte) 1);
                mchApplymentService.updateById(result);

                // 等待银盛OCR完成再进行后续操作
                mqSender.send(MchAuditMQ.build(mchApplyment.getApplyId()), 10);
            } catch (Exception e) {
                result.setState(MchApplyment.STATE_REJECT_WAIT_MODIFY);
                result.setApplyErrorInfo("上传图片失败[" + e.getMessage() + "]");
                mchApplymentService.updateById(result);
            }
        } else {
            // 最后一步提交的操作
            try {
                result = infoConverter.toDbEntity(yspayApplymentApiService.attachConfirm(infoConverter.toModel(mchApplyment)));

                MchApplyment tbMchApplymentCurrentData = mchApplymentService.getById(mchApplyment.getApplyId());
                if (tbMchApplymentCurrentData.getState() == com.jeequan.jeepay.core.entity.MchApplyment.STATE_WAIT_SIGN) {
                    // 如果签约回调已经过来，那么状态改为已签约
                    result.setState(com.jeequan.jeepay.core.entity.MchApplyment.STATE_WAIT_SIGN);
                } else {
                    result.setState(com.jeequan.jeepay.core.entity.MchApplyment.STATE_AUDITING);
                }

                result.setRemainStep((byte ) 0);
                // 至此，商户资料提交已处理完毕
                mchApplymentService.updateById(result);
            } catch (Exception e) {
                // 商户资料确认出现异常
                result.setState(MchApplyment.STATE_REJECT_WAIT_MODIFY);
                result.setApplyErrorInfo("商户资料确认失败[" + e.getMessage() + "]");
                mchApplymentService.updateById(result);
            }
        }
    }
}
