liusheng
2024-05-10 bb60b5747d5f4b85655a541d4990ec7464497b1b
ruoyi-admin/src/main/java/com/ruoyi/web/component/RabbitMqReceiver.java
@@ -3,21 +3,28 @@
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.web.task.PhoneTask;
import com.ruoyi.web.task.PhoneTask2;
import com.smartor.common.SendService;
import com.smartor.config.RabbitMqConfig;
import com.smartor.domain.IvrLibaTemplateVO;
import com.smartor.domain.IvrTaskcall;
import com.smartor.domain.IvrTask;
import com.smartor.domain.IvrTaskSingle;
import com.smartor.domain.IvrTaskcallMQ;
import com.smartor.mapper.IvrTaskcallMapper;
import com.smartor.mapper.IvrTaskMapper;
import com.smartor.mapper.IvrTaskSingleMapper;
import com.smartor.service.IIvrLibaTemplateService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.rabbit.config.SimpleRabbitListenerContainerFactory;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import java.io.IOException;
import java.util.List;
@@ -30,7 +37,11 @@
    private static IIvrLibaTemplateService ivrLibaTemplateService;
    private static IvrTaskcallMapper ivrTaskcallMapper;
    private static IvrTaskSingleMapper ivrTaskcallMapper;
    private static IvrTaskMapper ivrTaskMapper;
    private static SendService sendService;
    private static RedisCache redisCache;
@@ -44,8 +55,18 @@
    }
    @Autowired
    public void setIvrTaskcallMapper(IvrTaskcallMapper ivrTaskcallMapper) {
    public void setSendService(SendService sendService) {
        RabbitMqReceiver.sendService = sendService;
    }
    @Autowired
    public void setIvrTaskcallMapper(IvrTaskSingleMapper ivrTaskcallMapper) {
        RabbitMqReceiver.ivrTaskcallMapper = ivrTaskcallMapper;
    }
    @Autowired
    public void setIvrTaskMapper(IvrTaskMapper ivrTaskMapper) {
        RabbitMqReceiver.ivrTaskMapper = ivrTaskMapper;
    }
    @Autowired
@@ -78,31 +99,38 @@
     */
    @RabbitListener(queues = RabbitMqConfig.phone_queue, concurrency = "50")
    public void phoneVisit(String content, Message message, Channel channel) throws IOException {
//        System.out.println(content);
//        IvrTaskcallMQ ivrTaskcallMQ = null;
//        ObjectMapper mapper = new ObjectMapper();
//        try {
//            ivrTaskcallMQ = mapper.readValue(content, IvrTaskcallMQ.class);
//        } catch (JsonProcessingException e) {
//            e.printStackTrace();
//        }
//        //通过模板ID获取模板问题
//        IvrLibaTemplateVO ivrLibaTemplateVO = new IvrLibaTemplateVO();
//        ivrLibaTemplateVO.setID(Long.valueOf(ivrTaskcallMQ.getTemplateid()));
//        IvrLibaTemplateVO ivrLibaTemplateVO1 = ivrLibaTemplateService.selectInfoByCondition(ivrLibaTemplateVO);
//
//        //通过任务ID拿到患者信息
//        IvrTaskcall ivrTaskcall = new IvrTaskcall();
//        ivrTaskcall.setTaskid(ivrTaskcallMQ.getTaskid());
//        List<IvrTaskcall> ivrTaskcalls = ivrTaskcallMapper.selectIvrTaskcallList(ivrTaskcall);
//
//        for (IvrTaskcall ivrTaskcall1 : ivrTaskcalls) {
//            //通过多线程的方式去打电话
//            executorService.submit(new PhoneTask(ivrTaskcall1, ivrLibaTemplateVO1, redisCache));
//        }
        //通知 MQ 消息已被接收,可以ACK(从队列中删除)了   (这个需要根据业务再去处理ACK)
        channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
        try {
            IvrTaskcallMQ ivrTaskcallMQ = null;
            ObjectMapper mapper = new ObjectMapper();
            try {
                ivrTaskcallMQ = mapper.readValue("{" + content + "}", IvrTaskcallMQ.class);
            } catch (JsonProcessingException e) {
                e.printStackTrace();
            }
            //通过模板ID获取模板问题
            IvrLibaTemplateVO ivrLibaTemplateVO = new IvrLibaTemplateVO();
            ivrLibaTemplateVO.setID(Long.valueOf(ivrTaskcallMQ.getTemplateid()));
            IvrLibaTemplateVO ivrLibaTemplateVO1 = ivrLibaTemplateService.selectInfoByCondition(ivrLibaTemplateVO);
            //通过任务ID拿到患者信息
            IvrTaskSingle ivrTaskcall = new IvrTaskSingle();
            ivrTaskcall.setTaskid(ivrTaskcallMQ.getTaskid());
            List<IvrTaskSingle> ivrTaskcalls = ivrTaskcallMapper.selectIvrTaskcallList(ivrTaskcall);
            for (IvrTaskSingle ivrTaskcall1 : ivrTaskcalls) {
                IvrTask ivrTask = ivrTaskMapper.selectIvrTaskByTaskid(ivrTaskcall1.getTaskid());
                if (StringUtils.isNotEmpty(ivrTask.getSendState()) && ivrTask.getSendState().equals(3) || StringUtils.isNotEmpty(ivrTask.getSendState()) && ivrTask.getSendState().equals(4)) {
                    //如何任务被“暂停”或“终止”
                    break;
                }
//                //通过多线程的方式去打电话
//                executorService.submit(new PhoneTask(ivrTaskcall1, ivrLibaTemplateVO1, redisCache, channel, message, sendService));
                new PhoneTask2().runPhone(ivrTaskcall1, ivrLibaTemplateVO1, redisCache, channel, message, sendService);
            }
            //通知 MQ 消息已被接收,可以ACK(从队列中删除)了   (这个需要根据业务再去处理ACK)
            channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
            log.info("=============Do Something==============");
        } catch (Exception e) {
            log.error("============消费失败,尝试消息补发再次消费!==============");
@@ -115,4 +143,12 @@
            channel.basicRecover(false);
        }
    }
    @Bean
    public SimpleRabbitListenerContainerFactory jsonContainerFactory(ConnectionFactory connectionFactory) {
        SimpleRabbitListenerContainerFactory factory = new SimpleRabbitListenerContainerFactory();
        factory.setConnectionFactory(connectionFactory);
        factory.setMessageConverter(new Jackson2JsonMessageConverter());
        return factory;
    }
}