liusheng
2024-07-06 4bd9498d45b4d9f4bc948c04ec8efc9bb02d937e
ruoyi-admin/src/main/java/com/ruoyi/web/component/RedisMqReceiver.java
@@ -4,6 +4,7 @@
import com.fasterxml.jackson.databind.ObjectMapper;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.enums.ServiceFromEnum;
import com.ruoyi.common.utils.IPUtils;
import com.ruoyi.common.utils.RSAPublicKeyExample;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.web.task.PhoneTask;
@@ -14,6 +15,7 @@
import com.smartor.mapper.ServiceTaskMapper;
import com.smartor.mapper.SvyTaskMapper;
import com.smartor.mapper.SvyTaskSingleMapper;
import com.smartor.service.IBaseSmsaccountService;
import com.smartor.service.IIvrTaskTemplateService;
import com.smartor.service.IServiceSubtaskRecordService;
import com.smartor.service.ISvyTaskTemplateService;
@@ -67,6 +69,9 @@
    @Autowired
    private IServiceSubtaskRecordService serviceSubtaskRecordService;
    @Autowired
    IBaseSmsaccountService baseSmsaccountService;
    // 创建固定大小的线程池
    private static final ExecutorService executorService = Executors.newFixedThreadPool(10);
@@ -108,6 +113,8 @@
    @Override
    public void onMessage(Message message, byte[] pattern) {
        log.info("监听Redis key过期,key:{},channel:{}", message.toString(), new String(pattern));
        String ip = "116.62.18.175";
        log.error("本机的网络IP为:{}", ip);
        String content = message.toString();
        //判断是不是任务信息,如果不是,直接返回,不需要执行
        if (!content.contains("taskid")) {
@@ -125,12 +132,13 @@
            if (commonTaskcallMQ.getTaskType() == 1) {
                //随访
                sfHandle((commonTaskcallMQ));
                sfHandle(commonTaskcallMQ, ip);
            } else if (commonTaskcallMQ.getTaskType() == 2) {
                //问卷
                wjHandle(commonTaskcallMQ);
                wjHandle(commonTaskcallMQ, ip);
            } else if (commonTaskcallMQ.getTaskType() == 3) {
                //宣教
                xjHandle(commonTaskcallMQ, ip);
            }
@@ -155,7 +163,120 @@
     *
     * @param commonTaskcallMQ
     */
    private void sfHandle(CommonTaskcallMQ commonTaskcallMQ) {
    private void sfHandle(CommonTaskcallMQ commonTaskcallMQ, String ip) {
        //判断一下commonTaskcallMQ中的stopstate是否与ivr_task中的一致,不一致,则说明是暂停了
        ServiceTask ivrTask1 = ivrTaskMapper.selectServiceTaskByTaskid(commonTaskcallMQ.getTaskid());
        if (ivrTask1.getStopState() != commonTaskcallMQ.getStopState()) {
            //将消息从队列中剔除
            return;
        }
        if (StringUtils.isNotEmpty(commonTaskcallMQ.getPreachform())) {
            //如何任务发送方式不为空
            String[] split = commonTaskcallMQ.getPreachform().split(",");
            System.out.println("split的值为:" + split);
            for (String serviceFrom : split) {
                String descByCode = ServiceFromEnum.getDescByCode(Integer.valueOf(serviceFrom));
                //这里可以考虑用策略模式优化一下,不然太难看了
                //通过模板ID获取模板问题
                IvrTaskTemplateVO ivrTaskTemplateVO = new IvrTaskTemplateVO();
                ivrTaskTemplateVO.setID(Long.valueOf(commonTaskcallMQ.getTemplateid()));
                IvrTaskTemplateVO ivrTaskTemplateVO1 = ivrTaskTemplateService.selectInfoByCondition(ivrTaskTemplateVO);
                //通过任务ID拿到患者信息
                ServiceSubtask ivrTaskcall = new ServiceSubtask();
                ivrTaskcall.setTaskid(commonTaskcallMQ.getTaskid());
                List<ServiceSubtask> ivrTaskcalls = ivrTaskcallMapper.selectServiceSubtaskList(ivrTaskcall);
                if (descByCode.equals("电话")) {
                    for (ServiceSubtask ivrTaskcall1 : ivrTaskcalls) {
                        ServiceTask ivrTask = ivrTaskMapper.selectServiceTaskByTaskid(ivrTaskcall1.getTaskid());
                        if (ivrTask.getSendState() != null && ivrTask.getSendState() == 3 || ivrTask.getSendState() != null && ivrTask.getSendState() == 4) {
                            //如何任务被“暂停”或“终止”
                            break;
                        }
                        //任务发送记录(这个代码应该写在phoneTask中,先写在这,后面再改)
                        ServiceSubtaskRecord serviceSubtaskRecord = new ServiceSubtaskRecord();
                        serviceSubtaskRecord.setTaskid(ivrTaskcall1.getTaskid().toString());
                        serviceSubtaskRecord.setUuid(UUID.randomUUID().toString());
                        serviceSubtaskRecord.setTasktype(ivrTaskcall1.getType());
                        serviceSubtaskRecord.setPreachform("3");
                        serviceSubtaskRecord.setStartTime(System.currentTimeMillis());
                        serviceSubtaskRecordService.insertServiceSubtaskRecord(serviceSubtaskRecord);
                        //通过多线程的方式去打电话
                        executorService.submit(new PhoneTask(ivrTaskcall1, ivrTaskTemplateVO1, redisCache, sendService, phonePath, robotPhoneUtils));
                    }
                } else if (descByCode.equals("多媒体")) {
                    //多媒体
                } else if (descByCode.equals("纸质")) {
                    //纸质
                } else if (descByCode.equals("短信")) {
                    //短信
//                        http://localhost:8099/followvisit/particty?param1=3&param2=348
                    //对url中两个参数加密
                    RSAPublicKeyExample rsaPublicKeyExample = new RSAPublicKeyExample();
                    String taskId = rsaPublicKeyExample.encryptedData(ivrTask1.getTaskid().toString(), pub_key);
                    for (ServiceSubtask ivrTaskSingle : ivrTaskcalls) {
                        String patid = rsaPublicKeyExample.encryptedData(ivrTaskSingle.getPatid().toString(), pub_key);
                        SendMagParam sendMagParam = new SendMagParam();
                        sendMagParam.setType("4");
                        sendMagParam.setUrl(ip + ":" + req_path + "/followvisit/particty?param1=" + taskId + "&param2=" + patid);
                        Boolean aBoolean = sendService.sendMsg(sendMagParam);
                        //任务发送记录
                        ServiceSubtaskRecord serviceSubtaskRecord = new ServiceSubtaskRecord();
                        serviceSubtaskRecord.setTaskid(ivrTaskSingle.getTaskid().toString());
                        serviceSubtaskRecord.setUuid(UUID.randomUUID().toString());
                        serviceSubtaskRecord.setTasktype(ivrTaskSingle.getType());
                        serviceSubtaskRecord.setPreachform("4");
                        serviceSubtaskRecord.setStartTime(System.currentTimeMillis());
                        serviceSubtaskRecord.setResult(aBoolean == true ? "成功" : "失败");
                        serviceSubtaskRecordService.insertServiceSubtaskRecord(serviceSubtaskRecord);
                    }
                } else if (descByCode.equals("公众号")) {
                    //公众号
                    RSAPublicKeyExample rsaPublicKeyExample = new RSAPublicKeyExample();
                    String taskId = rsaPublicKeyExample.encryptedData(ivrTask1.getTaskid().toString(), pub_key);
                    for (ServiceSubtask ivrTaskSingle : ivrTaskcalls) {
                        String patid = rsaPublicKeyExample.encryptedData(ivrTaskSingle.getPatid().toString(), pub_key);
                        SendMagParam sendMagParam = new SendMagParam();
                        sendMagParam.setType("5");
                        sendMagParam.setUrl(ip + ":" + req_path + "/outsideChain?param1=" + ivrTask1.getTaskid().toString() + "&param2=" + ivrTaskSingle.getPatid().toString() + "&param3=" + ivrTask1.getTaskName());
                        log.info("链接完整路径:{}", req_path + "/outsideChain?param1=" + taskId + "&param2=" + patid + "&param3=" + ivrTask1.getTaskName());
                        //这个模板ID先写死,后面做成可选
                        sendMagParam.setTmpCode("oG3pJHPVWpE81DmZsua_2tKwgJ97r0qz37z56ns7NB4");
                        Map map = new HashMap();
                        map.put("first", ivrTask1.getTaskName());
                        sendMagParam.setContent(JSON.toJSONString(map));
                        Boolean aBoolean = sendService.sendMsg(sendMagParam);
                        //任务发送记录
                        ServiceSubtaskRecord serviceSubtaskRecord = new ServiceSubtaskRecord();
                        serviceSubtaskRecord.setTaskid(ivrTaskSingle.getTaskid().toString());
                        serviceSubtaskRecord.setUuid(UUID.randomUUID().toString());
                        serviceSubtaskRecord.setTasktype(ivrTaskSingle.getType());
                        serviceSubtaskRecord.setPreachform("4");
                        serviceSubtaskRecord.setStartTime(System.currentTimeMillis());
                        serviceSubtaskRecord.setResult(aBoolean == true ? "成功" : "失败");
                        serviceSubtaskRecordService.insertServiceSubtaskRecord(serviceSubtaskRecord);
                    }
                }
                //通知 MQ 消息已被接收,可以ACK(从队列中删除)了   (这个需要根据业务再去处理ACK)
            }
        }
    }
    /**
     * 宣教任务处理
     *
     * @param commonTaskcallMQ
     */
    private void xjHandle(CommonTaskcallMQ commonTaskcallMQ, String ip) {
        //判断一下commonTaskcallMQ中的stopstate是否与ivr_task中的一致,不一致,则说明是暂停了
        ServiceTask ivrTask1 = ivrTaskMapper.selectServiceTaskByTaskid(commonTaskcallMQ.getTaskid());
        if (ivrTask1.getStopState() != commonTaskcallMQ.getStopState()) {
@@ -216,7 +337,7 @@
                        String patid = rsaPublicKeyExample.encryptedData(ivrTaskSingle.getPatid().toString(), pub_key);
                        SendMagParam sendMagParam = new SendMagParam();
                        sendMagParam.setType("4");
                        sendMagParam.setUrl(req_path + "/followvisit/particty?param1=" + taskId + "&param2=" + patid);
                        sendMagParam.setUrl(ip + ":" + req_path + "/followvisit/particty?param1=" + taskId + "&param2=" + patid);
                        Boolean aBoolean = sendService.sendMsg(sendMagParam);
                        //任务发送记录
@@ -237,7 +358,7 @@
                        String patid = rsaPublicKeyExample.encryptedData(ivrTaskSingle.getPatid().toString(), pub_key);
                        SendMagParam sendMagParam = new SendMagParam();
                        sendMagParam.setType("5");
                        sendMagParam.setUrl(req_path + "/outsideChain?param1=" + taskId + "&param2=" + patid + "&param3=" + ivrTask1.getTaskName());
                        sendMagParam.setUrl(ip + ":" + req_path + "/outsideChain?param1=" + taskId + "&param2=" + patid + "&param3=" + ivrTask1.getTaskName());
                        log.info("链接完整路径:{}", req_path + "/outsideChain?param1=" + taskId + "&param2=" + patid + "&param3=" + ivrTask1.getTaskName());
                        //这个模板ID先写死,后面做成可选
                        sendMagParam.setTmpCode("oG3pJHPVWpE81DmZsua_2tKwgJ97r0qz37z56ns7NB4");
@@ -267,7 +388,7 @@
     *
     * @param commonTaskcallMQ
     */
    private void wjHandle(CommonTaskcallMQ commonTaskcallMQ) {
    private void wjHandle(CommonTaskcallMQ commonTaskcallMQ, String ip) {
        //判断一下commonTaskcallMQ中的stopstate是否与ivr_task中的一致,不一致,则说明是暂停了
        SvyTask svyTask = svyTaskMapper.selectSvyTaskByTaskid(commonTaskcallMQ.getTaskid());
        if (svyTask.getStopState() != commonTaskcallMQ.getStopState()) {
@@ -325,7 +446,7 @@
                        String patid = rsaPublicKeyExample.encryptedData(svyTaskSingle1.getPatid().toString(), pub_key);
                        SendMagParam sendMagParam = new SendMagParam();
                        sendMagParam.setType("5");
                        sendMagParam.setUrl(req_path + "/outsideChain?param1=" + taskId + "&param2=" + patid + "&param3=" + svyTask.getTaskName());
                        sendMagParam.setUrl(ip + ":" + req_path + "/outsideChain?param1=" + taskId + "&param2=" + patid + "&param3=" + svyTask.getTaskName());
                        log.info("链接完整路径:{}", req_path + "/outsideChain?param1=" + taskId + "&param2=" + patid + "&param3=" + svyTask.getTaskName());
                        //这个模板ID先写死,后面做成可选
                        sendMagParam.setTmpCode("oG3pJHPVWpE81DmZsua_2tKwgJ97r0qz37z56ns7NB4");