zhs
2025-04-24 b0b1e87fa3316d9db7b65d0330ad32a455479228
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
package com.smartor.service.impl;
 
import java.util.*;
 
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.gson.Gson;
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 com.ruoyi.common.utils.http.HttpUtils;
import org.apache.commons.lang3.ObjectUtils;
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.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import com.smartor.service.IServiceTaskService;
import org.springframework.transaction.annotation.Transactional;
 
/**
 * 语音任务Service业务层处理
 *
 * @author smartor
 * @date 2023-03-24
 */
@Slf4j
@Service
public class ServiceTaskServiceImpl implements IServiceTaskService {
    @Autowired
    private ServiceTaskMapper serviceTaskMapper;
 
    @Autowired
    private ServiceSubtaskMapper serviceSubtaskMapper;
 
    @Autowired
    private ServiceTaskdeptMapper serviceTaskdeptMapper;
 
    @Autowired
    private PatArchiveImportMapper patArchiveImportMapper;
 
 
    @Autowired
    private SvyTaskTemplateMapper svyTaskTemplateMapper;
 
    @Autowired
    private HeLibraryMapper heLibraryMapper;
 
    @Autowired
    private IvrLibaTemplateScriptMapper ivrLibaTemplateScriptMapper;
 
    @Autowired
    private SvyLibTemplateScriptMapper svyLibTemplateScriptMapper;
 
    @Autowired
    private PatArchiveMapper patArchiveMapper;
 
 
    @Autowired
    private IvrLibaTemplateTargetoptionMapper ivrLibaTemplateTargetOptionMapper;
 
    @Autowired
    private SvyLibTemplateTargetoptionMapper svyLibTemplateTargetoptionMapper;
 
    @Autowired
    private RedisCache redisCache;
 
    /**
     * 查询语音任务
     *
     * @param taskid 语音任务主键
     * @return 语音任务
     */
    @Override
    public ServiceTask selectServiceTaskByTaskid(Long taskid) {
        return serviceTaskMapper.selectServiceTaskByTaskid(taskid);
    }
 
    @Override
    public List<ServiceTask> selectServiceTaskAndtaskDeptList(ServiceTask serviceTask) {
        //如果为0不置空,会影响到我的查询
        if (CollectionUtils.isEmpty(serviceTask.getLeaveldeptcodes()) || serviceTask.getLeaveldeptcodes().size() == 0)
            serviceTask.setLeaveldeptcodes(new ArrayList<>());
        if (!CollectionUtils.isEmpty(serviceTask.getLeavehospitaldistrictcodes()) && serviceTask.getLeavehospitaldistrictcodes().size() > 0) {
            List<String> leaveldeptcodes = serviceTask.getLeaveldeptcodes();
            leaveldeptcodes.addAll(serviceTask.getLeavehospitaldistrictcodes());
            serviceTask.setLeaveldeptcodes(leaveldeptcodes);
        }
        return serviceTaskMapper.selectServiceTaskAndtaskDeptList(serviceTask);
    }
 
    /**
     * 查询语音任务列表
     *
     * @param serviceTask 语音任务
     * @return 语音任务
     */
    @Override
 
    public List<ServiceTask> selectServiceTaskList(ServiceTask serviceTask) {
        //如果为0不置空,会影响到我的查询
        if (CollectionUtils.isEmpty(serviceTask.getLeaveldeptcodes()) || serviceTask.getLeaveldeptcodes().size() == 0)
            serviceTask.setLeaveldeptcodes(null);
        if (CollectionUtils.isEmpty(serviceTask.getLeavehospitaldistrictcodes()) || serviceTask.getLeavehospitaldistrictcodes().size() == 0)
            serviceTask.setLeavehospitaldistrictcodes(null);
        return serviceTaskMapper.selectServiceTaskList(serviceTask);
    }
 
    /**
     * 新增语音任务
     *
     * @param serviceTask 语音任务
     * @return 结果
     */
    @Override
    public int insertServiceTask(ServiceTask serviceTask) {
        serviceTask.setCreateTime(DateUtils.getNowDate());
        serviceTask.setUpdateTime(DateUtils.getNowDate());
        return serviceTaskMapper.insertServiceTask(serviceTask);
    }
 
    /**
     * 修改语音任务
     *
     * @param serviceTask 语音任务
     * @return 结果
     */
    @Override
    public int updateServiceTask(ServiceTask serviceTask) {
        serviceTask.setUpdateTime(DateUtils.getNowDate());
        return serviceTaskMapper.updateServiceTask(serviceTask);
    }
 
    /**
     * 批量删除语音任务
     *
     * @param taskids 需要删除的语音任务主键
     * @return 结果
     */
    @Override
    public int deleteServiceTaskByTaskids(Long[] taskids) {
        return serviceTaskMapper.deleteServiceTaskByTaskids(taskids);
    }
 
    /**
     * 删除语音任务信息
     *
     * @param taskid 语音任务主键
     * @return 结果
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public Boolean deleteServiceTaskByTaskid(Long taskid) {
        ServiceSubtask serviceSubtask = new ServiceSubtask();
        serviceSubtask.setTaskid(taskid);
 
        //查询子任务
        ServiceSubtaskVO serviceSubtaskVO = DtoConversionUtils.sourceToTarget(serviceSubtask, ServiceSubtaskVO.class);
        List<ServiceSubtask> serviceSubtaskList = serviceSubtaskMapper.selectServiceSubtaskList(serviceSubtaskVO);
        for (ServiceSubtask ss : serviceSubtaskList) {
            log.error("sfSend需要移除的subId为:{}", ss.getId().toString());
            redisCache.removeElementFromList("cache-exist", ss.getId().toString());
            redisCache.removeElementFromList("cache-0", ss.getId().toString());
            redisCache.removeElementFromList("cache-1", ss.getId().toString());
            redisCache.removeElementFromList("cache-2", ss.getId().toString());
            redisCache.removeElementFromList("cache-3", ss.getId().toString());
            redisCache.removeElementFromList("cache-4", ss.getId().toString());
        }
 
        serviceSubtask.setDelFlag("1");
        Boolean aBoolean = serviceSubtaskMapper.updateServiceSubtaskByPatId(serviceSubtask);
 
        //关联表删除
        ServiceTaskdept serviceTaskdept = new ServiceTaskdept();
        serviceTaskdept.setTaskId(taskid);
        int i = serviceTaskdeptMapper.deleteServiceTaskdeptByTaskId(serviceTaskdept);
 
        Boolean isSuccess = false;
        if (!aBoolean || i <= 0) {
            new BaseException("任务删除失败");
        }
        isSuccess = serviceTaskMapper.deleteServiceTaskByTaskid(taskid);
        return isSuccess;
    }
 
    @Override
    public Map<String, Object> getScriptInfoByCondition(Long taskid, Long patid, Boolean isFinish, String patfrom) {
        Map<String, Object> map = new HashMap<>();
        ServiceSubtaskVO serviceSubtaskVO = new ServiceSubtaskVO();
        serviceSubtaskVO.setPatid(patid);
        serviceSubtaskVO.setTaskid(taskid);
//        List<ServiceSubtask> selectServiceSubtaskList = serviceSubtaskMapper.selectServiceSubtaskList(serviceSubtaskVO);
//        //查询患者是否重覆做题
//        if (CollectionUtils.isEmpty(selectServiceSubtaskList)) {
//            Long submit = selectServiceSubtaskList.get(0).getSubmit();
//            if (submit == 1L) {
//                map.put("submit", "1");
//                return map;
//            }
//        }
        //通过任务ID获取模板ID
        ServiceTask serviceTask = selectServiceTaskByTaskid(taskid);
        List info = new ArrayList();
        if (serviceTask.getType().equals("1")) {
            //随访
            log.error("文本随访问题");
            info = sfInfo(serviceTask, patid, patfrom);
        } else if (serviceTask.getType().equals("2")) {
            //问卷
            log.error("文本问卷问题");
            info = wjInfo(serviceTask, patid, isFinish, patfrom);
        } else {
            //宣教
            info = xjInfo(Long.valueOf(serviceTask.getLibtemplateid()), taskid, patid, isFinish);
            //只要打开了页面,就算成功
            if (isFinish) {
                ServiceSubtask ss = new ServiceSubtask();
                ss.setTaskid(taskid);
                ss.setPatid(patid);
                Integer integer = serviceSubtaskMapper.selectSendstateByCondition(ss);
                if (integer != null && integer != 6) {
                    ss.setSendstate(1L);
                    ss.setResult("success");
                    ss.setFinishtime(new Date());
                    serviceSubtaskMapper.updateServiceSubtaskByCondition(ss);
                }
            }
        }
 
        SvyTaskTemplate svyTaskTemplate = svyTaskTemplateMapper.selectSvyTaskTemplateBySvyid(serviceTask.getTemplateid());
        if (StringUtils.isNotEmpty(serviceTask.getKcb())) map.put("kcb", serviceTask.getKcb());
        map.put("script", info);
        if (StringUtils.isNotEmpty(serviceTask.getJsy())) map.put("jsy", serviceTask.getJsy());
        map.put("type", serviceTask.getType());
        map.put("taskName", serviceTask.getTaskName());
        map.put("submit", "0");
 
        return map;
    }
 
    /**
     * 随访信息
     *
     * @param serviceTask
     * @param patid
     * @return
     */
    public List sfInfo(ServiceTask serviceTask, Long patid, String patfrom) {
        //通过模板ID获取问题信息
        IvrLibaTemplateScript ivrLibaTemplateScript = new IvrLibaTemplateScript();
        ivrLibaTemplateScript.setTemplateid(Long.valueOf(serviceTask.getLibtemplateid()));
        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 = null;
        if (patfrom.equals("0")) patArchive = patArchiveMapper.selectPatArchiveByPatid(patid);
        else if (patfrom.equals("1")) {
            PatArchiveImport patArchiveImport = patArchiveImportMapper.selectPatArchiveImportById(patid);
            patArchive = DtoConversionUtils.sourceToTarget(patArchiveImport, PatArchive.class);
        }
 
        //通过患者信息和任务信息中的textParam对问题中的变量进行填充
        ObjectMapper objectMapper = new ObjectMapper();
        Map<String, Map<String, String>> serviceTaskMap = null;
        try {
            serviceTaskMap = objectMapper.readValue(serviceTask.getTextParam(), Map.class);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        List<Map<String, String>> mapList = new ArrayList<>();
 
        for (Map<String, String> map : serviceTaskMap.values()) {
            mapList.add(map);
        }
 
        //将模板问题话术里的通配符替换
        for (IvrLibaTemplateScriptVO ivrLibaTemplateScriptVO : ivrLibaTemplateScriptVOS) {
            if (StringUtils.isNotEmpty(ivrLibaTemplateScriptVO.getScriptContent())) {
                for (Map<String, String> map : mapList) {
                    for (String key : map.keySet()) {
 
                        ivrLibaTemplateScriptVO.setScriptContent(ivrLibaTemplateScriptVO.getScriptContent().replace(key, StringUtils.isNotEmpty(map.get(key)) ? map.get(key) : ""));
                    }
                }
                //替换患者个人信息数据
                ivrLibaTemplateScriptVO.setScriptContent(ivrLibaTemplateScriptVO.getScriptContent().replace("${name}", StringUtils.isNotEmpty(patArchive.getName()) ? patArchive.getName() : ""));
                ivrLibaTemplateScriptVO.setScriptContent(ivrLibaTemplateScriptVO.getScriptContent().replace("${dzz}", StringUtils.isNotEmpty(patArchive.getPlaceOfResidence()) ? patArchive.getPlaceOfResidence() : ""));
                ivrLibaTemplateScriptVO.setScriptContent(ivrLibaTemplateScriptVO.getScriptContent().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);
            }
        }
        Collections.sort(ivrLibaTemplateScriptVOS, Comparator.comparingInt(IvrLibaTemplateScriptVO::getSort));
 
        return ivrLibaTemplateScriptVOS;
    }
 
    /**
     * 问卷信息
     *
     * @param serviceTask
     * @param patid
     * @return
     */
    private List wjInfo(ServiceTask serviceTask, Long patid, Boolean isFinish, String patfrom) {
        //用户点击了外链,就算他领取了
        //这样做会有一个问题,如果固定任务,会出现一个任务里,有多个该患者
        ServiceSubtaskVO serviceSubtaskVO = new ServiceSubtaskVO();
        serviceSubtaskVO.setTaskid(serviceTask.getTaskid());
        serviceSubtaskVO.setPatid(patid);
        List<ServiceSubtask> serviceSubtaskList = serviceSubtaskMapper.selectServiceSubtaskList(serviceSubtaskVO);
        if (CollectionUtils.isNotEmpty(serviceSubtaskList) && serviceSubtaskList.size() == 1) {
            if (serviceSubtaskList.get(0).getSendstate() == 3 && isFinish == true) {
                ServiceSubtask serviceSubtask = new ServiceSubtask();
                serviceSubtask.setSendstate(1L);
                serviceSubtask.setPatid(patid);
                serviceSubtask.setTaskid(serviceTask.getTaskid());
                serviceSubtaskMapper.updateServiceSubtaskByPatId(serviceSubtask);
            }
        }
        //通过模板ID获取问题信息
        SvyLibTemplateScript svyLibTemplateScript = new SvyLibTemplateScript();
        svyLibTemplateScript.setSvyid(Long.valueOf(serviceTask.getLibtemplateid()));
        svyLibTemplateScript.setDelFlag("0");
        List<SvyLibTemplateScript> svyLibTemplateScripts = svyLibTemplateScriptMapper.selectSvyLibTemplateScriptList(svyLibTemplateScript);
        if (CollectionUtils.isEmpty(svyLibTemplateScripts) || svyLibTemplateScripts.size() == 0) {
            log.info("ivrLibaTemplateScripts为空了,请尽快联系管理员处理");
            return new ArrayList<>();
        }
        List<SvyLibTemplateScriptVO> svyLibTemplateScriptVOS = DtoConversionUtils.sourceToTarget(svyLibTemplateScripts, SvyLibTemplateScriptVO.class);
 
        //获取患者信息
        PatArchive patArchive = null;
        if (patfrom.equals("0")) patArchive = patArchiveMapper.selectPatArchiveByPatid(patid);
        else if (patfrom.equals("1")) {
            PatArchiveImport patArchiveImport = patArchiveImportMapper.selectPatArchiveImportById(patid);
            patArchive = DtoConversionUtils.sourceToTarget(patArchiveImport, PatArchive.class);
        }
 
 
        //通过患者信息和任务信息中的textParam对问题中的变量进行填充
        ObjectMapper objectMapper = new ObjectMapper();
        Map<String, Map<String, String>> ivrTaskMap = null;
        try {
            ivrTaskMap = objectMapper.readValue(serviceTask.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 (SvyLibTemplateScriptVO svyLibTemplateScriptVO : svyLibTemplateScriptVOS) {
            if (StringUtils.isNotEmpty(svyLibTemplateScriptVO.getScriptContent())) {
                for (Map<String, String> map : mapList) {
                    for (String key : map.keySet()) {
 
                        svyLibTemplateScriptVO.setScriptContent(svyLibTemplateScriptVO.getScriptContent().replace(key, StringUtils.isNotEmpty(map.get(key)) ? map.get(key) : ""));
                    }
                }
                //替换患者个人信息数据
                log.error("问题id:{},   问题内容:{}", svyLibTemplateScriptVO.getId(), svyLibTemplateScriptVO.getScriptContent());
                if (StringUtils.isNotEmpty(svyLibTemplateScriptVO.getScriptContent()) && ObjectUtils.isNotEmpty(patArchive)) {
                    svyLibTemplateScriptVO.setScriptContent(svyLibTemplateScriptVO.getScriptContent().replace("${name}", StringUtils.isNotEmpty(patArchive.getName()) ? patArchive.getName() : ""));
                    svyLibTemplateScriptVO.setScriptContent(svyLibTemplateScriptVO.getScriptContent().replace("${dzz}", StringUtils.isNotEmpty(patArchive.getPlaceOfResidence()) ? patArchive.getPlaceOfResidence() : ""));
                    svyLibTemplateScriptVO.setScriptContent(svyLibTemplateScriptVO.getScriptContent().replace("${dhh}", StringUtils.isNotEmpty(patArchive.getTelcode()) ? patArchive.getTelcode() : ""));
                }
                //获取问题选项
                SvyLibTemplateTargetoption svyLibTemplateTargetoption = new SvyLibTemplateTargetoption();
                svyLibTemplateTargetoption.setScriptid(svyLibTemplateScriptVO.getId());
                List<SvyLibTemplateTargetoption> svyLibTemplateTargetoptions = svyLibTemplateTargetoptionMapper.selectSvyLibTemplateTargetoptionList(svyLibTemplateTargetoption);
                svyLibTemplateScriptVO.setSvyLibTemplateTargetoptions(svyLibTemplateTargetoptions);
            }
        }
        Collections.sort(svyLibTemplateScriptVOS, Comparator.comparingLong(SvyLibTemplateScriptVO::getSort));
        return svyLibTemplateScriptVOS;
    }
 
    private List xjInfo(Long templateId, Long taskid, Long patid, Boolean isFinish) {
        if (isFinish == true) {
            ServiceSubtask serviceSubtask = new ServiceSubtask();
            serviceSubtask.setTaskid(taskid);
            serviceSubtask.setPatid(patid);
            Integer integer = serviceSubtaskMapper.selectSendstateByCondition(serviceSubtask);
            if (integer == 3) {
                serviceSubtask.setResult("完成");
                serviceSubtask.setFinishtime(new Date());
                serviceSubtask.setSendstate(6L);
                serviceSubtaskMapper.updateServiceSubtaskByCondition(serviceSubtask);
            }
        }
        HeLibrary heLibrary = heLibraryMapper.selectHeLibraryById(Long.valueOf(templateId));
        List list = new ArrayList();
        list.add(heLibrary);
        return list;
 
    }
}