package com.jeequan.jeepay.pay.mq;

import cn.hutool.extra.spring.SpringUtil;
import com.jeequan.jeepay.components.mq.model.MchAuditMQ;
import com.jeequan.jeepay.components.mq.model.MchAuditThirdNotifyMQ;
import com.jeequan.jeepay.components.mq.vender.IMQSender;
import com.jeequan.jeepay.converter.MchInfoConverter;
import com.jeequan.jeepay.core.cache.RedisUtil;
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 com.jeequan.jeepay.thirdparty.channel.yspay.YspayMchApplymentService;
import com.jeequan.jeepay.thirdparty.service.ConfigContextQueryService;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;

@Data
@NoArgsConstructor
@AllArgsConstructor
@Slf4j
@Component
public class MchAuditMQReceiver implements MchAuditMQ.IMQReceiver {

    @Autowired
    private MchApplymentService mchApplymentService;

    @Autowired
    private YspayApplymentApiService yspayApplymentApiService;

    @Autowired
    private YspayMchApplymentService yspayMchApplymentService;

    @Autowired
    private ConfigContextQueryService configContextQueryService;

    @Autowired
    private IMQSender mqSender;

    @Autowired
    private MchInfoConverter infoConverter;

    @Override
    public void receive(MchAuditMQ.MsgPayload payload) {

        String applyId = payload.getApplyId();

        MchApplyment mchApplyment = mchApplymentService.getById(applyId);

        if (mchApplyment.getState() != MchApplyment.STATE_AUDITING_WAIT) {
            return;
        }

        if (mchApplyment.getRemainStep() <= 0) {
            // 没有剩余处理步骤了，不作处理
            return;
        }

        // 添加正在进件的一个键值，防止进件时间过长状态，队列重复处理
        String redisKey = "auditApplyId_" + mchApplyment.getApplyId();

        if (RedisUtil.hasKey(redisKey)) {
            // 正在处理，就将塞一条新的消息到rabbitmq中
            mqSender.send(MchAuditMQ.build(mchApplyment.getApplyId()), 20);
            return;
        } else {
            // 设置60秒超时，若上传图片速度实在太慢，还是需要升级服务器带宽
            RedisUtil.set(redisKey, "1", 120);
        }

        IsvAuditReceiver isvAuditReceiver = SpringUtil.getBean(mchApplyment.getIfCode() + "AuditReceiver", IsvAuditReceiver.class);
        mchApplyment.setLastApplyAt(new Date());
        try {
            isvAuditReceiver.consume(mchApplyment);

            if (mchApplyment.getState() == MchApplyment.STATE_AUDITING_WAIT) {
                return;
            }

            RedisUtil.del(redisKey);

            mqSender.send(MchAuditThirdNotifyMQ.build(mchApplyment.getApplyId(), MchAuditThirdNotifyMQ.TYPE_AUDIT));
        } catch (Exception e) {
            log.info("进件失败, 进件单号{}", applyId, e);
            MchApplyment result = new MchApplyment();
            result.setApplyId(applyId);
            result.setState(MchApplyment.STATE_REJECT_WAIT_MODIFY);
            result.setApplyErrorInfo("[进件失败, 请联系客服处理]");
            mchApplymentService.updateById(result);
        }
    }
}
