liusheng
2024-05-12 f243bb8af00a2464d16536d42150a4107f87b4ae
ruoyi-admin/src/main/java/com/ruoyi/web/task/PhoneTask.java
@@ -1,15 +1,16 @@
package com.ruoyi.web.task;
import com.alibaba.fastjson2.JSON;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.rabbitmq.client.Channel;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.utils.DtoConversionUtils;
import com.ruoyi.common.utils.uuid.IdUtils;
import com.smartor.common.SendService;
import com.smartor.config.PhoneUtils;
import com.smartor.config.RabbitMqCallPhoneConfig;
import com.smartor.domain.*;
import org.springframework.amqp.core.Message;
import org.springframework.data.convert.Jsr310Converters;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import java.util.*;
import java.util.concurrent.TimeUnit;
@@ -19,24 +20,23 @@
    private List<IvrLibaTemplateScriptVO> ivrLibaTemplateScriptVO;
    private RedisCache redisCache;
    private IvrLibaTemplateVO ivrLibaTemplateVO;
    private Channel channel;
    private RabbitMqCallPhoneConfig rabbitMqCallPhoneConfig;
    private Message message;
    private SendService sendService;
    public PhoneTask(IvrTaskSingle ivrTaskSingle, IvrLibaTemplateVO ivrLibaTemplateVO, RedisCache redisCache, Channel channel, Message message, SendService sendService) {
    public PhoneTask(IvrTaskSingle ivrTaskSingle, IvrLibaTemplateVO ivrLibaTemplateVO, RedisCache redisCache, RabbitMqCallPhoneConfig rabbitMqCallPhoneConfig, Message message, SendService sendService) {
        this.ivrTaskSingle = DtoConversionUtils.sourceToTarget(ivrTaskSingle, IvrTaskSingle.class);
        this.ivrLibaTemplateScriptVO = DtoConversionUtils.sourceToTarget(ivrLibaTemplateVO.getIvrLibaTemplateScriptVOList(), IvrLibaTemplateScriptVO.class);
        this.ivrLibaTemplateVO = DtoConversionUtils.sourceToTarget(ivrLibaTemplateVO, IvrLibaTemplateVO.class);
        this.redisCache = redisCache;
        this.channel = channel;
        this.rabbitMqCallPhoneConfig = rabbitMqCallPhoneConfig;
        this.message = message;
        this.sendService = sendService;
    }
    @Override
    public void run() {
        synchronized (PhoneTask.class) {
            try {
                //获取电话组,查看哪些电话可以使用
                PhoneUtils phoneUtils = new PhoneUtils();
@@ -79,19 +79,36 @@
                System.out.println("----------------uuID为 :" + uuid + "       " + ivrTaskSingle.getSendname() + "       " + returnQues.getNowQuestion().getQuestionText() + "     " + ivrTaskSingle.getPhone());
                //调用机器人电话,开始电话(需要将回调方法传入,这个等”电话方“告诉怎么传,现在先空着,还有第一句问题话术)
                String ob = phoneUtils.ob("", "", "", "", "", "", "", ivrTaskSingle.getPhone(), uuid, true);
                System.out.println("OB的值为:" + ob);
                //通了之后,立马去调开场白
                System.out.println("问题的值为:" + returnQues.getNowQuestion().getQuestionText() + "       " + uuid);
                phoneUtils.ttsPlayback(ivrLibaTemplateVO.getRevisitBefore() + returnQues.getNowQuestion().getQuestionText(), uuid);
            //先将话术放到ob_queue队列中
            IvrTaskcallPhoneMQ ivrTaskcallPhoneMQ = new IvrTaskcallPhoneMQ();
            ivrTaskcallPhoneMQ.setScript(ivrLibaTemplateVO.getRevisitBefore() + returnQues.getNowQuestion().getQuestionText());
            ivrTaskcallPhoneMQ.setType("1");
            ivrTaskcallPhoneMQ.setUuid(uuid);
            String data = JSON.toJSONString(ivrTaskcallPhoneMQ);
            rabbitMqCallPhoneConfig.sendMessage("phone_exchange", "phone.ob1", data, 0L);
//                //通知 MQ 消息已被接收,可以ACK(从队列中删除)了   (这个需要根据业务再去处理ACK)
//                channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
            //调用机器人电话,开始电话
            String ob = phoneUtils.ob("", "", "", "", "", "", "", ivrTaskSingle.getPhone(), uuid, true);
            System.out.println("----------------uuID为 :" + uuid + "       " + ivrTaskSingle.getSendname() + "       " + returnQues.getNowQuestion().getQuestionText() + "     " + ivrTaskSingle.getPhone() + "----------已经拨打");
            Map<String, String> obMap = objectMapper.readValue(ob, Map.class);
            Thread.sleep(1000);
            redisCache.setCacheObject(uuid + "state_id", "0", 10, TimeUnit.MINUTES);
//这里先注释,state_id,电话那边,只能返回6,不能返回0,还需要电话那边进行修改,那边修改之后,上面两行可删除,将下面的解开注释
//            if (obMap.get("state_id").equals("0")) {
//                // 说明打通了
//                redisCache.setCacheObject(uuid + "state_id", "0", 10, TimeUnit.MINUTES);
//            } else {
//                while (true) {
//                    Thread.sleep(2000);
//                    redisCache.setCacheObject(uuid + "state_id", "0", 10, TimeUnit.MINUTES);
//                }
//            }
            System.out.println("OB的值为:" + ob);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}
}