package com.ruoyi.cai.mq.consumer;

import com.alibaba.fastjson2.JSON;
import com.ruoyi.cai.manager.UserForbidManager;
import com.ruoyi.cai.mq.CommonDelayTypeEnum;
import com.ruoyi.cai.mq.config.CommonDelayMqConfig;
import com.ruoyi.cai.mq.dto.CommonDelayDto;
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 CommonDelayMqConsumer {
    @Autowired
    private UserForbidManager userForbidManager;

    @RabbitListener(queues = CommonDelayMqConfig.QUEUE_NAME
            ,containerFactory = "customContainerFactory")
    public void checkTimeOutMq(String message) {
        log.info("CommonDelayMqConsumer: " + message);
        CommonDelayDto dto = JSON.parseObject(message, CommonDelayDto.class);
        CommonDelayTypeEnum typeEnum = dto.getTypeEnum();
        if(typeEnum == null){
            log.warn("延时任务执行失败，未检测到正确的类型 dto={}",JSON.toJSONString(dto));
            return;
        }
        switch (typeEnum){
            case USER_FORBID:
                try {
                    userForbidManager.checkExpire(dto.getForbidId());
                }catch (Exception e){
                    log.error("检查用户封禁状态失败!需要开发确认！",e);
                }
                break;
            default:
                break;
        }
    }
}
