liusheng
2024-05-12 f243bb8af00a2464d16536d42150a4107f87b4ae
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
package com.ruoyi.web.task;
 
import com.alibaba.fastjson2.JSON;
import com.fasterxml.jackson.databind.ObjectMapper;
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.amqp.rabbit.core.RabbitTemplate;
 
import java.util.*;
import java.util.concurrent.TimeUnit;
 
public class PhoneTask implements Runnable {
    private IvrTaskSingle ivrTaskSingle;
    private List<IvrLibaTemplateScriptVO> ivrLibaTemplateScriptVO;
    private RedisCache redisCache;
    private IvrLibaTemplateVO ivrLibaTemplateVO;
    private RabbitMqCallPhoneConfig rabbitMqCallPhoneConfig;
    private Message message;
    private 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.rabbitMqCallPhoneConfig = rabbitMqCallPhoneConfig;
        this.message = message;
        this.sendService = sendService;
    }
 
    @Override
    public void run() {
        try {
            //获取电话组,查看哪些电话可以使用
            PhoneUtils phoneUtils = new PhoneUtils();
            final String uuid = IdUtils.randomUUID();
            //获取所有的“通配符”键值对
            ObjectMapper objectMapper = new ObjectMapper();
            Map<String, Map<String, String>> ivrTaskSingleMmap = objectMapper.readValue(ivrTaskSingle.getTextParam(), Map.class);
            List<Map<String, String>> mapList = new ArrayList<>();
            if (!Objects.isNull(ivrTaskSingleMmap)) {
                for (Map<String, String> map : ivrTaskSingleMmap.values()) {
                    mapList.add(map);
                }
                //将模板问题话术里的通配符替换
                for (IvrLibaTemplateScriptVO ivrLibaTemplateScriptVO : ivrLibaTemplateScriptVO) {
                    for (Map<String, String> map : mapList) {
                        for (String key : map.keySet()) {
                            ivrLibaTemplateScriptVO.setQuestionText(ivrLibaTemplateScriptVO.getQuestionText().replace(key, map.get(key)));
                        }
                    }
                }
            }
            Map<String, Object> objectMap = new HashMap<>();
            objectMap.put("ivrTaskSingle", ivrTaskSingle);
            objectMap.put("ivrLibaTemplateScriptVO", ivrLibaTemplateScriptVO);
            //将数据放到redis中,方便在回调方法里获取
            redisCache.setCacheObject(uuid, objectMap, 120, TimeUnit.MINUTES);
 
            //记录首次问题
            QuestionMessage returnQues = new QuestionMessage();
            returnQues.setKcb(ivrLibaTemplateVO.getRevisitBefore());
            for (IvrLibaTemplateScriptVO ivrLibaTemplateScriptVO : ivrLibaTemplateScriptVO) {
                if (ivrLibaTemplateScriptVO.getTargetid() == ivrLibaTemplateVO.getFirstQuestionNum().intValue()) {
                    returnQues.setNowQuestion(ivrLibaTemplateScriptVO);
                }
            }
            returnQues.setQuestionList(ivrLibaTemplateScriptVO);
            //将问题,和模板信息放到redis中
            redisCache.setCacheObject(uuid + "returnQues", returnQues, 120, TimeUnit.MINUTES);
            redisCache.setCacheObject(uuid + "ivrLibaTemplateVO", ivrLibaTemplateVO, 120, TimeUnit.MINUTES);
 
            System.out.println("----------------uuID为 :" + uuid + "       " + ivrTaskSingle.getSendname() + "       " + returnQues.getNowQuestion().getQuestionText() + "     " + ivrTaskSingle.getPhone());
 
            //先将话术放到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);
 
            //调用机器人电话,开始电话
            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();
 
        }
    }
}