package com.ruoyi.cai.mq.consumer;

import com.ruoyi.cai.mq.AmqpProducer;
import com.ruoyi.cai.mq.config.RoomCheckDelayMqConfig;
import com.ruoyi.cai.ws.job.JobResp;
import com.ruoyi.cai.ws.job.RoomCheckJobService;
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 RoomCheckDelayMqConsumer {
    @Autowired
    private AmqpProducer amqpProducer;
    @Autowired
    private RoomCheckJobService roomCheckJobService;

    @RabbitListener(queues = RoomCheckDelayMqConfig.QUEUE_NAME
            ,containerFactory = "customContainerFactory")
    public void sendRoomCheck(String roomIdStr) {
        log.info("开始执行房间检测: roomId={}",roomIdStr);
        try {
            Long roomId = Long.valueOf(roomIdStr);
            JobResp resp = roomCheckJobService.checkRoom(roomId);
            if(resp.isNextCreateJob()){
                // 1分钟后继续执行
                amqpProducer.sendRoomCheckDelay(roomIdStr,60);
            }
        }catch (Exception e){
            log.error("每分钟定时检测房间失败! roomId={}",roomIdStr);
        }

    }
}
