| | |
| | | package com.smartor.service.impl; |
| | | |
| | | import java.util.List; |
| | | import java.text.SimpleDateFormat; |
| | | import java.util.*; |
| | | |
| | | 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.exception.base.BaseException; |
| | | import com.ruoyi.common.utils.DateUtils; |
| | | import com.ruoyi.common.utils.DtoConversionUtils; |
| | | import com.ruoyi.common.utils.StringUtils; |
| | | import com.smartor.config.RabbitMqCallPhoneConfig; |
| | | 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.mapper.IvrTaskMapper; |
| | | import com.smartor.domain.IvrTask; |
| | | 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 |
| | | { |
| | | 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 RabbitMqCallPhoneConfig rabbitMqCallPhoneConfig; |
| | | |
| | | /** |
| | | * 查询语音任务 |
| | | * |
| | | * |
| | | * @param taskid 语音任务主键 |
| | | * @return 语音任务 |
| | | */ |
| | | @Override |
| | | public IvrTask selectIvrTaskByTaskid(Long taskid) |
| | | { |
| | | public IvrTask selectIvrTaskByTaskid(Long taskid) { |
| | | return ivrTaskMapper.selectIvrTaskByTaskid(taskid); |
| | | } |
| | | |
| | | /** |
| | | * 查询语音任务列表 |
| | | * |
| | | * |
| | | * @param ivrTask 语音任务 |
| | | * @return 语音任务 |
| | | */ |
| | | @Override |
| | | public List<IvrTask> selectIvrTaskList(IvrTask ivrTask) |
| | | { |
| | | public List<IvrTask> selectIvrTaskList(IvrTask ivrTask) { |
| | | return ivrTaskMapper.selectIvrTaskList(ivrTask); |
| | | } |
| | | |
| | | /** |
| | | * 新增语音任务 |
| | | * |
| | | * |
| | | * @param ivrTask 语音任务 |
| | | * @return 结果 |
| | | */ |
| | | @Override |
| | | public int insertIvrTask(IvrTask ivrTask) |
| | | { |
| | | public int insertIvrTask(IvrTask ivrTask) { |
| | | ivrTask.setCreateTime(DateUtils.getNowDate()); |
| | | return ivrTaskMapper.insertIvrTask(ivrTask); |
| | | } |
| | | |
| | | /** |
| | | * 修改语音任务 |
| | | * |
| | | * |
| | | * @param ivrTask 语音任务 |
| | | * @return 结果 |
| | | */ |
| | | @Override |
| | | public int updateIvrTask(IvrTask ivrTask) |
| | | { |
| | | public int updateIvrTask(IvrTask ivrTask) { |
| | | ivrTask.setUpdateTime(DateUtils.getNowDate()); |
| | | return ivrTaskMapper.updateIvrTask(ivrTask); |
| | | } |
| | | |
| | | /** |
| | | * 批量删除语音任务 |
| | | * |
| | | * |
| | | * @param taskids 需要删除的语音任务主键 |
| | | * @return 结果 |
| | | */ |
| | | @Override |
| | | public int deleteIvrTaskByTaskids(Long[] taskids) |
| | | { |
| | | public int deleteIvrTaskByTaskids(Long[] taskids) { |
| | | return ivrTaskMapper.deleteIvrTaskByTaskids(taskids); |
| | | } |
| | | |
| | | /** |
| | | * 删除语音任务信息 |
| | | * |
| | | * |
| | | * @param taskid 语音任务主键 |
| | | * @return 结果 |
| | | */ |
| | | @Transactional(rollbackFor = Exception.class) |
| | | @Override |
| | | public int deleteIvrTaskByTaskid(Long taskid) |
| | | { |
| | | return ivrTaskMapper.deleteIvrTaskByTaskid(taskid); |
| | | 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; |
| | | } |
| | | |
| | | /** |
| | | * 任务发送 |
| | | * |
| | | * @return |
| | | */ |
| | | @Override |
| | | public int taskSend(SendTaskVO sendTaskVO) { |
| | | if (sendTaskVO.getTaskId() == null) throw new BaseException("任务ID不能为空,请检查后,再进行执行"); |
| | | IvrTask ivrTask = selectIvrTaskByTaskid(sendTaskVO.getTaskId()); |
| | | if (sendTaskVO.getSendState() != null && sendTaskVO.getSendState() == 3 || sendTaskVO.getSendState() != null && sendTaskVO.getSendState() == 4) { |
| | | //任务暂停或终止 |
| | | IvrTask ivrTask3 = new IvrTask(); |
| | | ivrTask3.setTaskid(sendTaskVO.getTaskId()); |
| | | ivrTask3.setStopState(ivrTask.getStopState() + 1); |
| | | ivrTask3.setSendState(sendTaskVO.getSendState()); |
| | | updateIvrTask(ivrTask3); |
| | | return 0; |
| | | } |
| | | |
| | | //判断发送状态是否为空 |
| | | if (org.apache.commons.lang3.StringUtils.isEmpty(sendTaskVO.getSendType())) { |
| | | sendTaskVO.setSendType(ivrTask.getSendType().toString()); |
| | | } |
| | | //判断任务是否是立即发送 |
| | | if (org.apache.commons.lang3.StringUtils.isNotEmpty(sendTaskVO.getSendType()) && sendTaskVO.getSendType().equals("2")) { |
| | | IvrTaskcallMQ ivrTaskcallMQ = new IvrTaskcallMQ(); |
| | | ivrTaskcallMQ.setTaskid(sendTaskVO.getTaskId()); |
| | | ivrTaskcallMQ.setSendType(sendTaskVO.getSendType()); |
| | | ivrTaskcallMQ.setTemplateid(ivrTask.getTemplateid().toString()); |
| | | ivrTaskcallMQ.setPreachform(ivrTask.getPreachform()); |
| | | ivrTaskcallMQ.setStopState(ivrTask.getStopState()); |
| | | |
| | | String ivrTaskcallMQJson = JSON.toJSONString(ivrTaskcallMQ); |
| | | ivrTaskcallMQJson = ivrTaskcallMQJson.substring(1, ivrTaskcallMQJson.length() - 1); |
| | | |
| | | //立即发送 |
| | | rabbitMqCallPhoneConfig.sendMessage("phone_exchange", "phone.123", ivrTaskcallMQJson, 0L); |
| | | |
| | | //将任务状态修改成执行中 |
| | | IvrTask ivrTask2 = new IvrTask(); |
| | | ivrTask2.setTaskid(ivrTask.getTaskid()); |
| | | ivrTask2.setSendState(2); |
| | | updateIvrTask(ivrTask2); |
| | | } else if (org.apache.commons.lang3.StringUtils.isNotEmpty(sendTaskVO.getSendType()) && sendTaskVO.getSendType().equals("1")) { |
| | | //时间段发送 |
| | | ObjectMapper objectMapper = new ObjectMapper(); |
| | | if (org.apache.commons.lang3.StringUtils.isNotEmpty(ivrTask.getSendTimeSlot())) { |
| | | List<TaskSendTimeVO> taskSendTimeVOList = null; |
| | | try { |
| | | taskSendTimeVOList = objectMapper.readValue(ivrTask.getSendTimeSlot(), new TypeReference<List<TaskSendTimeVO>>() { |
| | | }); |
| | | } catch (JsonProcessingException e) { |
| | | log.error("JsonProcessingException报错了:{}", e.getMessage()); |
| | | } |
| | | for (TaskSendTimeVO taskSendTimeVO : taskSendTimeVOList) { |
| | | //去SendTimeslot中,获取所有的时间段 |
| | | List<TaskSendTimeVO> list = new ArrayList<>(); |
| | | list.add(taskSendTimeVO); |
| | | |
| | | //获取开始发送时间 |
| | | String begantime = taskSendTimeVO.getBegantime(); |
| | | SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); |
| | | //记录目前到发送时间的毫秒值 (发送时间的毫秒值 - 当前时间的毫秒值) |
| | | Long milliseconds = 1000L; |
| | | try { |
| | | Date date = sdf.parse(begantime); |
| | | milliseconds = date.getTime() - System.currentTimeMillis(); |
| | | System.out.println("日期毫秒数:" + milliseconds); |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | | } |
| | | IvrTaskcallMQ ivrTaskcallMQ = new IvrTaskcallMQ(); |
| | | ivrTaskcallMQ.setTaskid(sendTaskVO.getTaskId()); |
| | | ivrTaskcallMQ.setSendType("1"); |
| | | ivrTaskcallMQ.setTemplateid(ivrTask.getTemplateid().toString()); |
| | | ivrTaskcallMQ.setSendTimeslot(list); |
| | | ivrTaskcallMQ.setStopState(ivrTask.getStopState()); |
| | | ivrTaskcallMQ.setPreachform(ivrTask.getPreachform()); |
| | | |
| | | Long finalMilliseconds = milliseconds; |
| | | String ivrTaskcallMQJson = JSON.toJSONString(ivrTaskcallMQ); |
| | | ivrTaskcallMQJson = ivrTaskcallMQJson.substring(1, ivrTaskcallMQJson.length() - 1); |
| | | rabbitMqCallPhoneConfig.sendMessage("phone_exchange", "phone.123", ivrTaskcallMQJson, milliseconds); |
| | | } |
| | | //将任务状态修改成执行中 |
| | | IvrTask ivrTask2 = new IvrTask(); |
| | | ivrTask2.setTaskid(ivrTask.getTaskid()); |
| | | ivrTask2.setSendState(2); |
| | | updateIvrTask(ivrTask2); |
| | | } |
| | | } |
| | | return 1; |
| | | } |
| | | } |