package com.ruoyi.cai.mq.consumer;

import com.ruoyi.cai.mq.AmqpProducer;
import com.ruoyi.cai.mq.config.RoomSettleDelayMqConfig;
import com.ruoyi.cai.ws.service.SettleService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Slf4j
@Component
public class RoomSettleDelayMqConsumer {
    @Autowired
    private SettleService settleService;
    @Autowired
    private AmqpProducer amqpProducer;

    @RabbitListener(queues = RoomSettleDelayMqConfig.QUEUE_NAME
            ,containerFactory = "customContainerFactory")
    public void checkTimeOutMq(String message) {
        log.info("开始执行预扣费: " + message);
        try {
            boolean next = settleService.withholdingFee(Long.valueOf(message));
            if(next){
                // 1分钟后继续执行
                amqpProducer.sendRoomSettleDelay(message,60);
            }
        }catch (Exception e){
            log.error("每分钟定时扣费失败!",e);
        }

    }
}
