From ef81a5cbfbb98974ed1ba54d0e47b7e74a9b96b9 Mon Sep 17 00:00:00 2001 From: liusheng <337615773@qq.com> Date: 星期四, 06 六月 2024 15:33:55 +0800 Subject: [PATCH] 代码提交 --- ruoyi-admin/src/main/java/com/ruoyi/web/component/RabbitMqReceiver.java | 276 +++++++++++++++++++++++++++++++++++++++++------------- 1 files changed, 209 insertions(+), 67 deletions(-) diff --git a/ruoyi-admin/src/main/java/com/ruoyi/web/component/RabbitMqReceiver.java b/ruoyi-admin/src/main/java/com/ruoyi/web/component/RabbitMqReceiver.java index 4208bc2..58c5b8c 100644 --- a/ruoyi-admin/src/main/java/com/ruoyi/web/component/RabbitMqReceiver.java +++ b/ruoyi-admin/src/main/java/com/ruoyi/web/component/RabbitMqReceiver.java @@ -1,51 +1,100 @@ package com.ruoyi.web.component; +import com.alibaba.fastjson2.JSON; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; +import com.rabbitmq.client.Channel; import com.ruoyi.common.core.redis.RedisCache; -import com.ruoyi.common.utils.DateUtils; +import com.ruoyi.common.enums.ServiceFromEnum; +import com.ruoyi.common.utils.RSAPublicKeyExample; +import com.ruoyi.common.utils.StringUtils; import com.ruoyi.web.task.PhoneTask; -import com.smartor.config.RabbitMqConfig; -import com.smartor.domain.IvrLibaTemplateVO; -import com.smartor.domain.IvrTaskcall; -import com.smartor.domain.IvrTaskcallMQ; -import com.smartor.mapper.IvrTaskcallMapper; -import com.smartor.service.IIvrLibaTemplateService; +import com.smartor.common.SendService; +import com.smartor.config.PhoneUtils; +import com.smartor.config.RabbitMqCallPhoneConfig; +import com.smartor.config.RobotPhoneUtils; +import com.smartor.domain.*; +import com.smartor.mapper.IvrTaskMapper; +import com.smartor.mapper.IvrTaskSingleMapper; +import com.smartor.service.IIvrTaskTemplateService; 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.rabbit.connection.ConnectionFactory; +import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.context.annotation.Bean; import org.springframework.stereotype.Component; -import com.rabbitmq.client.Channel; import java.io.IOException; +import java.util.HashMap; import java.util.List; +import java.util.Map; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; +import java.util.concurrent.TimeUnit; @Slf4j @Component//鐩戝惉姝ら槦鍒� public class RabbitMqReceiver { - private static IIvrLibaTemplateService ivrLibaTemplateService; + @Value("${phonePath}") + private String phonePath; - private static IvrTaskcallMapper ivrTaskcallMapper; + @Value("${pub_key}") + private String pub_key; + + @Value("${req_path}") + private String req_path; + + private static IIvrTaskTemplateService ivrTaskTemplateService; + + private static IvrTaskSingleMapper ivrTaskcallMapper; + + private static IvrTaskMapper ivrTaskMapper; + + private static SendService sendService; private static RedisCache redisCache; + + private static RabbitMqCallPhoneConfig rabbitMqCallPhoneConfig; + + private static RobotPhoneUtils robotPhoneUtils; + // 鍒涘缓鍥哄畾澶у皬鐨勭嚎绋嬫睜 private static final ExecutorService executorService = Executors.newFixedThreadPool(10); - @Autowired - public void setIIvrLibaTemplateService(IIvrLibaTemplateService ivrLibaTemplateService) { - RabbitMqReceiver.ivrLibaTemplateService = ivrLibaTemplateService; + public void setIIvrTaskTemplateService(IIvrTaskTemplateService ivrTaskTemplateService) { + RabbitMqReceiver.ivrTaskTemplateService = ivrTaskTemplateService; } @Autowired - public void setIvrTaskcallMapper(IvrTaskcallMapper ivrTaskcallMapper) { + public void setRobotPhoneUtilsService(RobotPhoneUtils robotPhoneUtils) { + RabbitMqReceiver.robotPhoneUtils = robotPhoneUtils; + } + + @Autowired + public void setSendService(SendService sendService) { + RabbitMqReceiver.sendService = sendService; + } + + @Autowired + public void setRabbitMqCallPhoneConfig(RabbitMqCallPhoneConfig rabbitMqCallPhoneConfig) { + RabbitMqReceiver.rabbitMqCallPhoneConfig = rabbitMqCallPhoneConfig; + } + + @Autowired + public void setIvrTaskcallMapper(IvrTaskSingleMapper ivrTaskcallMapper) { RabbitMqReceiver.ivrTaskcallMapper = ivrTaskcallMapper; + } + + @Autowired + public void setIvrTaskMapper(IvrTaskMapper ivrTaskMapper) { + RabbitMqReceiver.ivrTaskMapper = ivrTaskMapper; } @Autowired @@ -53,66 +102,159 @@ RabbitMqReceiver.redisCache = redisCache; } -// @RabbitListener(queues = RabbitMqConfig.delay_queue) -// public void consultReceiveDealy(String content, Message message, Channel channel) throws IOException { -// log.info("----------------鎺ユ敹寤惰繜闃熷垪娑堟伅--------------------"); -// //閫氱煡 MQ 娑堟伅宸茶鎺ユ敹,鍙互ACK(浠庨槦鍒椾腑鍒犻櫎)浜� -// channel.basicAck(message.getMessageProperties().getDeliveryTag(), false); -// try { -// log.info("=============Do Something=============="); -// } catch (Exception e) { -// log.error("============娑堣垂澶辫触,灏濊瘯娑堟伅琛ュ彂鍐嶆娑堣垂!=============="); -// log.error(e.getMessage()); -// /** -// * basicRecover鏂规硶鏄繘琛岃ˉ鍙戞搷浣滐紝 -// * 鍏朵腑鐨勫弬鏁板鏋滀负true鏄妸娑堟伅閫�鍥炲埌queue浣嗘槸鏈夊彲鑳借鍏跺畠鐨刢onsumer(闆嗙兢)鎺ユ敹鍒帮紝 -// * 璁剧疆涓篺alse鏄彧琛ュ彂缁欏綋鍓嶇殑consumer -// */ -// channel.basicRecover(false); -// } -// } - /** - * 鐢佃瘽闅忚 + * 浠诲姟闅忚 * concurrency = "50" 骞跺彂鏁颁负50 */ - @RabbitListener(queues = RabbitMqConfig.phone_queue, concurrency = "50") + @RabbitListener(queues = "task_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鎷垮埌鎮h�呬俊鎭� -// 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(浠庨槦鍒椾腑鍒犻櫎)浜� 锛堣繖涓渶瑕佹牴鎹笟鍔″啀鍘诲鐞咥CK锛� - channel.basicAck(message.getMessageProperties().getDeliveryTag(), false); + System.out.println("娑堟伅杩涙潵浜�----------------"); + IvrTaskcallMQ ivrTaskcallMQ = null; try { - log.info("=============Do Something=============="); + ObjectMapper mapper = new ObjectMapper(); + try { + if (!content.startsWith("{")) { + ivrTaskcallMQ = mapper.readValue("{" + content + "}", IvrTaskcallMQ.class); + } else { + ivrTaskcallMQ = mapper.readValue(content, IvrTaskcallMQ.class); + } + } catch (JsonProcessingException e) { + e.printStackTrace(); + } + //鍒ゆ柇涓�涓媔vrTaskcallMQ涓殑stopstate鏄惁涓巌vr_task涓殑涓�鑷达紝涓嶄竴鑷达紝鍒欒鏄庢槸鏆傚仠浜� + IvrTask ivrTask1 = ivrTaskMapper.selectIvrTaskByTaskid(ivrTaskcallMQ.getTaskid()); + if (ivrTask1.getStopState() != ivrTaskcallMQ.getStopState()) { + //灏嗘秷鎭粠闃熷垪涓墧闄� + channel.basicAck(message.getMessageProperties().getDeliveryTag(), false); + return; + } + + if (StringUtils.isNotEmpty(ivrTaskcallMQ.getPreachform())) { + //濡備綍浠诲姟鍙戦�佹柟寮忎笉涓虹┖ + String[] split = ivrTaskcallMQ.getPreachform().split(","); + for (String serviceFrom : split) { + String descByCode = ServiceFromEnum.getDescByCode(Integer.valueOf(serviceFrom)); + //杩欓噷鍙互鑰冭檻鐢ㄧ瓥鐣ユā寮忎紭鍖栦竴涓嬶紝涓嶇劧澶毦鐪嬩簡 + + //閫氳繃妯℃澘ID鑾峰彇妯℃澘闂 + IvrTaskTemplateVO ivrTaskTemplateVO = new IvrTaskTemplateVO(); + ivrTaskTemplateVO.setID(Long.valueOf(ivrTaskcallMQ.getTemplateid())); + IvrTaskTemplateVO ivrTaskTemplateVO1 = ivrTaskTemplateService.selectInfoByCondition(ivrTaskTemplateVO); + + //閫氳繃浠诲姟ID鎷垮埌鎮h�呬俊鎭� + IvrTaskSingle ivrTaskcall = new IvrTaskSingle(); + ivrTaskcall.setTaskid(ivrTaskcallMQ.getTaskid()); + List<IvrTaskSingle> ivrTaskcalls = ivrTaskcallMapper.selectIvrTaskcallList(ivrTaskcall); + if (descByCode.equals("鐢佃瘽")) { + for (IvrTaskSingle ivrTaskcall1 : ivrTaskcalls) { + IvrTask ivrTask = ivrTaskMapper.selectIvrTaskByTaskid(ivrTaskcall1.getTaskid()); + if (ivrTask.getSendState() != null && ivrTask.getSendState() == 3 || ivrTask.getSendState() != null && ivrTask.getSendState() == 4) { + //濡備綍浠诲姟琚�滄殏鍋溾�濇垨鈥滅粓姝⑩�� + break; + } + //閫氳繃澶氱嚎绋嬬殑鏂瑰紡鍘绘墦鐢佃瘽 + executorService.submit(new PhoneTask(ivrTaskcall1, ivrTaskTemplateVO1, redisCache, rabbitMqCallPhoneConfig, message, sendService, phonePath, robotPhoneUtils)); + } + } else if (descByCode.equals("澶氬獟浣�")) { + //澶氬獟浣� + } else if (descByCode.equals("绾歌川")) { + //绾歌川 + } else if (descByCode.equals("鐭俊")) { + //鐭俊 +// http://localhost:8099/followvisit/particty?param1=3¶m2=348 + //瀵箄rl涓袱涓弬鏁板姞瀵� + RSAPublicKeyExample rsaPublicKeyExample = new RSAPublicKeyExample(); + String taskId = rsaPublicKeyExample.encryptedData(ivrTask1.getTaskid().toString(), pub_key); + + + for (IvrTaskSingle ivrTaskSingle : ivrTaskcalls) { + String patid = rsaPublicKeyExample.encryptedData(ivrTaskSingle.getPatid().toString(), pub_key); + SendMagParam sendMagParam = new SendMagParam(); + sendMagParam.setType("4"); + sendMagParam.setUrl(req_path + "/followvisit/particty?param1=" + taskId + "¶m2=" + patid); + sendService.sendMsg(sendMagParam); + } + } else if (descByCode.equals("鍏紬鍙�")) { + //鍏紬鍙� + RSAPublicKeyExample rsaPublicKeyExample = new RSAPublicKeyExample(); + String taskId = rsaPublicKeyExample.encryptedData(ivrTask1.getTaskid().toString(), pub_key); + for (IvrTaskSingle ivrTaskSingle : ivrTaskcalls) { + String patid = rsaPublicKeyExample.encryptedData(ivrTaskSingle.getPatid().toString(), pub_key); + SendMagParam sendMagParam = new SendMagParam(); + sendMagParam.setType("5"); + sendMagParam.setUrl(req_path + "/outsideChain?param1=" + taskId + "¶m2=" + patid + "¶m3=" + ivrTask1.getTaskName()); + log.info("閾炬帴瀹屾暣璺緞锛歿}", req_path + "/outsideChain?param1=" + taskId + "¶m2=" + patid + "¶m3=" + ivrTask1.getTaskName()); + //杩欎釜妯℃澘ID鍏堝啓姝伙紝鍚庨潰鍋氭垚鍙�� + sendMagParam.setTmpCode("oG3pJHPVWpE81DmZsua_2tKwgJ97r0qz37z56ns7NB4"); + Map map = new HashMap(); + map.put("first", ivrTask1.getTaskName()); + sendMagParam.setContent(JSON.toJSONString(map)); + sendService.sendMsg(sendMagParam); + } + } + //閫氱煡 MQ 娑堟伅宸茶鎺ユ敹,鍙互ACK(浠庨槦鍒椾腑鍒犻櫎)浜� 锛堣繖涓渶瑕佹牴鎹笟鍔″啀鍘诲鐞咥CK锛� + channel.basicAck(message.getMessageProperties().getDeliveryTag(), false); + } + } + } catch (Exception e) { + Integer integer = redisCache.getCacheObject(ivrTaskcallMQ.getTaskid().toString()); + if (integer != null && integer == 2) { + channel.basicAck(message.getMessageProperties().getDeliveryTag(), false); + } else if (integer == null) { + redisCache.setCacheObject(ivrTaskcallMQ.getTaskid().toString(), 1, 120, TimeUnit.MINUTES); + } else { + redisCache.setCacheObject(ivrTaskcallMQ.getTaskid().toString(), integer + 1, 120, TimeUnit.MINUTES); + } + + log.error("============娑堣垂澶辫触,灏濊瘯娑堟伅琛ュ彂鍐嶆娑堣垂!=============="); + log.error(e.getMessage()); + channel.basicReject(message.getMessageProperties().getDeliveryTag(), true); + } + + } + + /** + * 鏈哄櫒浜虹涓�鍙ヨ瘽璇煶 + * concurrency = "50" 骞跺彂鏁颁负50 + */ + @RabbitListener(queues = "ob_queue", concurrency = "50") + public void obVisit(String content, Message message, Channel channel) throws IOException { + try { + IvrTaskcallPhoneMQ ivrTaskcallMQ = null; + ObjectMapper mapper = new ObjectMapper(); + ivrTaskcallMQ = mapper.readValue(content, IvrTaskcallPhoneMQ.class); + //鍒ゆ柇鎮h�呮槸鍚﹀凡缁忔帴鐢佃瘽 + String str = redisCache.getCacheObject(ivrTaskcallMQ.getUuid() + "state_id"); + System.out.println("-----------------" + str); + if (StringUtils.isNotEmpty(str) && str.equals("0")) { + //鎮h�呭凡缁忔帴鍚簡鐢佃瘽 + new PhoneUtils().ttsPlayback(ivrTaskcallMQ.getScript(), ivrTaskcallMQ.getUuid()); + } else if (StringUtils.isNotEmpty(str) && str.equals("-10")) { + //杩欎釜璇存槑,宸茬粡鎵撲簡鎸囧畾閬嶆暟杩樻槸娌℃湁浜烘帴锛屽彲浠ュ皢娑堟伅浠庨槦鍒椾腑绉婚櫎浜� + channel.basicAck(message.getMessageProperties().getDeliveryTag(), false); + return; + } else { + //杩涜闃熷垪灏鹃儴锛岃繘琛屼笅涓�娆℃牎楠� +// channel.basicRecover(false); + channel.basicReject(message.getMessageProperties().getDeliveryTag(), true); + System.out.println("++++++++++++++++++++++" + str); + return; + } + //閫氱煡 MQ 娑堟伅宸茶鎺ユ敹,鍙互ACK(浠庨槦鍒椾腑鍒犻櫎)浜� 锛堣繖涓渶瑕佹牴鎹笟鍔″啀鍘诲鐞咥CK锛� + channel.basicAck(message.getMessageProperties().getDeliveryTag(), false); } catch (Exception e) { log.error("============娑堣垂澶辫触,灏濊瘯娑堟伅琛ュ彂鍐嶆娑堣垂!=============="); log.error(e.getMessage()); - /** - * basicRecover鏂规硶鏄繘琛岃ˉ鍙戞搷浣滐紝 - * 鍏朵腑鐨勫弬鏁板鏋滀负true鏄妸娑堟伅閫�鍥炲埌queue浣嗘槸鏈夊彲鑳借鍏跺畠鐨刢onsumer(闆嗙兢)鎺ユ敹鍒帮紝 - * 璁剧疆涓篺alse鏄彧琛ュ彂缁欏綋鍓嶇殑consumer - */ - channel.basicRecover(false); + channel.basicReject(message.getMessageProperties().getDeliveryTag(), true); } } + + + @Bean + public SimpleRabbitListenerContainerFactory jsonContainerFactory(ConnectionFactory connectionFactory) { + SimpleRabbitListenerContainerFactory factory = new SimpleRabbitListenerContainerFactory(); + factory.setConnectionFactory(connectionFactory); + factory.setMessageConverter(new Jackson2JsonMessageConverter()); + return factory; + } } -- Gitblit v1.9.3