liusheng
2024-06-27 afb277a81e060c6275f11e9f9f102b5dcfd80a72
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
package com.smartor.service.impl;
 
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.concurrent.TimeUnit;
 
import com.alibaba.fastjson2.JSON;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.exception.base.BaseException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.DtoConversionUtils;
import org.apache.commons.lang3.StringUtils;
import com.smartor.domain.*;
import com.smartor.mapper.*;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.smartor.service.IIvrTaskService;
import org.springframework.transaction.annotation.Transactional;
 
/**
 * 语音任务Service业务层处理
 *
 * @author smartor
 * @date 2023-03-24
 */
@Slf4j
@Service
public class IvrTaskServiceImpl implements IIvrTaskService {
    @Autowired
    private IvrTaskMapper ivrTaskMapper;
 
    @Autowired
    private IvrTaskSingleMapper ivrTaskcallMapper;
 
    @Autowired
    private IvrLibaTemplateScriptMapper ivrLibaTemplateScriptMapper;
 
    @Autowired
    private PatArchiveMapper patArchiveMapper;
 
    @Autowired
    private IvrLibaTemplateTargetoptionMapper ivrLibaTemplateTargetOptionMapper;
 
    @Autowired
    private RedisCache redisCache;
 
    /**
     * 查询语音任务
     *
     * @param taskid 语音任务主键
     * @return 语音任务
     */
    @Override
    public IvrTask selectIvrTaskByTaskid(Long taskid) {
        return ivrTaskMapper.selectIvrTaskByTaskid(taskid);
    }
 
    /**
     * 查询语音任务列表
     *
     * @param ivrTask 语音任务
     * @return 语音任务
     */
    @Override
    public List<IvrTask> selectIvrTaskList(IvrTask ivrTask) {
        return ivrTaskMapper.selectIvrTaskList(ivrTask);
    }
 
    /**
     * 新增语音任务
     *
     * @param ivrTask 语音任务
     * @return 结果
     */
    @Override
    public int insertIvrTask(IvrTask ivrTask) {
        ivrTask.setCreateTime(DateUtils.getNowDate());
        return ivrTaskMapper.insertIvrTask(ivrTask);
    }
 
    /**
     * 修改语音任务
     *
     * @param ivrTask 语音任务
     * @return 结果
     */
    @Override
    public int updateIvrTask(IvrTask ivrTask) {
        ivrTask.setUpdateTime(DateUtils.getNowDate());
        return ivrTaskMapper.updateIvrTask(ivrTask);
    }
 
    /**
     * 批量删除语音任务
     *
     * @param taskids 需要删除的语音任务主键
     * @return 结果
     */
    @Override
    public int deleteIvrTaskByTaskids(Long[] taskids) {
        return ivrTaskMapper.deleteIvrTaskByTaskids(taskids);
    }
 
    /**
     * 删除语音任务信息
     *
     * @param taskid 语音任务主键
     * @return 结果
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public Boolean deleteIvrTaskByTaskid(Long taskid) {
        IvrTaskSingle ivrTaskcall = new IvrTaskSingle();
        ivrTaskcall.setTaskid(taskid);
        ivrTaskcall.setDelFlag("1");
        Boolean aBoolean = ivrTaskcallMapper.updateIvrTaskcall(ivrTaskcall);
        Boolean isSuccess = false;
        if (!aBoolean) {
            new BaseException("任务删除失败");
        }
        isSuccess = ivrTaskMapper.deleteIvrTaskByTaskid(taskid);
        return isSuccess;
    }
 
    @Override
    public List<IvrLibaTemplateScriptVO> getScriptInfoByCondition(Long taskid, Long patid) {
        //通过任务ID获取模板ID
        IvrTask ivrTask = selectIvrTaskByTaskid(taskid);
 
        //通过模板ID获取问题信息
        IvrLibaTemplateScript ivrLibaTemplateScript = new IvrLibaTemplateScript();
        ivrLibaTemplateScript.setTemplateID(ivrTask.getTemplateid());
        ivrLibaTemplateScript.setDelFlag("0");
        List<IvrLibaTemplateScript> ivrLibaTemplateScripts = ivrLibaTemplateScriptMapper.selectIvrLibaTemplateScriptList(ivrLibaTemplateScript);
        if (CollectionUtils.isEmpty(ivrLibaTemplateScripts) || ivrLibaTemplateScripts.size() == 0) {
            log.info("ivrLibaTemplateScripts为空了,请尽快联系管理员处理");
            return new ArrayList<>();
        }
        List<IvrLibaTemplateScriptVO> ivrLibaTemplateScriptVOS = DtoConversionUtils.sourceToTarget(ivrLibaTemplateScripts, IvrLibaTemplateScriptVO.class);
 
        //获取患者信息
        PatArchive patArchive = patArchiveMapper.selectPatArchiveByPatid(patid);
 
        //通过患者信息和任务信息中的textParam对问题中的变量进行填充
        ObjectMapper objectMapper = new ObjectMapper();
        Map<String, Map<String, String>> ivrTaskMap = null;
        try {
            ivrTaskMap = objectMapper.readValue(ivrTask.getTextParam(), Map.class);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        List<Map<String, String>> mapList = new ArrayList<>();
 
        for (Map<String, String> map : ivrTaskMap.values()) {
            mapList.add(map);
        }
 
        //将模板问题话术里的通配符替换
        for (IvrLibaTemplateScriptVO ivrLibaTemplateScriptVO : ivrLibaTemplateScriptVOS) {
            if (StringUtils.isNotEmpty(ivrLibaTemplateScriptVO.getQuestionText())) {
                for (Map<String, String> map : mapList) {
                    for (String key : map.keySet()) {
 
                        ivrLibaTemplateScriptVO.setQuestionText(ivrLibaTemplateScriptVO.getQuestionText().replace(key, StringUtils.isNotEmpty(map.get(key)) ? map.get(key) : ""));
                    }
                }
                //替换患者个人信息数据
                ivrLibaTemplateScriptVO.setQuestionText(ivrLibaTemplateScriptVO.getQuestionText().replace("${name}", StringUtils.isNotEmpty(patArchive.getName()) ? patArchive.getName() : ""));
                ivrLibaTemplateScriptVO.setQuestionText(ivrLibaTemplateScriptVO.getQuestionText().replace("${dzz}", StringUtils.isNotEmpty(patArchive.getPlaceOfResidence()) ? patArchive.getPlaceOfResidence() : ""));
                ivrLibaTemplateScriptVO.setQuestionText(ivrLibaTemplateScriptVO.getQuestionText().replace("${dhh}", StringUtils.isNotEmpty(patArchive.getTelcode()) ? patArchive.getTelcode() : ""));
 
                //获取问题选项
                IvrLibaTemplateTargetoption ivrLibaTemplateTargetoption = new IvrLibaTemplateTargetoption();
                ivrLibaTemplateTargetoption.setScriptid(ivrLibaTemplateScriptVO.getID());
                List<IvrLibaTemplateTargetoption> ivrLibaTemplateTargetoptions = ivrLibaTemplateTargetOptionMapper.selectIvrLibaTemplateTargetoptionList(ivrLibaTemplateTargetoption);
                ivrLibaTemplateScriptVO.setIvrLibaScriptTargetoptionList(ivrLibaTemplateTargetoptions);
            }
        }
 
 
        return ivrLibaTemplateScriptVOS;
    }
 
 
}