269 lines
14 KiB
Java
269 lines
14 KiB
Java
package com.xinelu.quartz.task;
|
||
|
||
import com.alibaba.fastjson2.JSONObject;
|
||
import com.xinelu.common.constant.AiobTaskTypeContant;
|
||
import com.xinelu.common.enums.NodeExecuteStatusEnum;
|
||
import com.xinelu.common.enums.PhoneDialMethodEnum;
|
||
import com.xinelu.common.enums.PhoneRedialTimesEnum;
|
||
import com.xinelu.common.enums.TaskExcuteTypeEnum;
|
||
import com.xinelu.common.utils.StringUtils;
|
||
import com.xinelu.manage.domain.patientinfo.PatientInfo;
|
||
import com.xinelu.manage.domain.patientvisitrecord.PatientVisitRecord;
|
||
import com.xinelu.manage.domain.scriptInfo.ScriptInfo;
|
||
import com.xinelu.manage.domain.scriptinfotaskinfo.ScriptInfoTaskInfo;
|
||
import com.xinelu.manage.domain.signpatientmanageroute.SignPatientManageRoute;
|
||
import com.xinelu.manage.domain.signpatientmanageroutenode.SignPatientManageRouteNode;
|
||
import com.xinelu.manage.domain.signpatientrecord.SignPatientRecord;
|
||
import com.xinelu.manage.dto.aiob.ActualTimeTaskDto;
|
||
import com.xinelu.manage.dto.aiob.CreateTaskDto;
|
||
import com.xinelu.manage.dto.aiob.CustomerInfoDto;
|
||
import com.xinelu.manage.dto.aiob.ImportTaskDto;
|
||
import com.xinelu.manage.dto.signpatientmanageroutenode.SignPatientManageRouteNodeDto;
|
||
import com.xinelu.manage.mapper.labelfieldcontent.LabelFieldContentMapper;
|
||
import com.xinelu.manage.mapper.patientinfo.PatientInfoMapper;
|
||
import com.xinelu.manage.mapper.patientvisitrecord.PatientVisitRecordMapper;
|
||
import com.xinelu.manage.mapper.scriptInfo.ScriptInfoMapper;
|
||
import com.xinelu.manage.mapper.scriptinfotaskinfo.ScriptInfoTaskInfoMapper;
|
||
import com.xinelu.manage.mapper.signpatientmanageroute.SignPatientManageRouteMapper;
|
||
import com.xinelu.manage.mapper.signpatientmanageroutenode.SignPatientManageRouteNodeMapper;
|
||
import com.xinelu.manage.mapper.signpatientrecord.SignPatientRecordMapper;
|
||
import com.xinelu.manage.service.aiob.IAIOBService;
|
||
import com.xinelu.manage.service.signpatientmanageroutenode.ISignPatientManageRouteNodeService;
|
||
import com.xinelu.manage.vo.aiob.ImportTaskVo;
|
||
import com.xinelu.manage.vo.labelfieldcontent.LabelFieldInfoContentVo;
|
||
import java.time.LocalDate;
|
||
import java.time.LocalDateTime;
|
||
import java.time.format.DateTimeFormatter;
|
||
import java.util.ArrayList;
|
||
import java.util.Arrays;
|
||
import java.util.List;
|
||
import java.util.Objects;
|
||
import javax.annotation.Resource;
|
||
import lombok.extern.slf4j.Slf4j;
|
||
import org.apache.commons.collections4.CollectionUtils;
|
||
import org.apache.commons.lang3.ObjectUtils;
|
||
import org.springframework.beans.factory.annotation.Value;
|
||
import org.springframework.stereotype.Component;
|
||
|
||
/**
|
||
* @description: 百度智能外呼上传任务定时任务
|
||
* @author: haown
|
||
* @create: 2024-08-19 13:58
|
||
**/
|
||
@Slf4j
|
||
@Component("UploadRobotPublishTask")
|
||
public class UploadRobotPublishTask {
|
||
|
||
@Value("${aiob.callBackUrl}")
|
||
private String callBackUrl;
|
||
@Resource
|
||
private SignPatientManageRouteNodeMapper signPatientManageRouteNodeMapper;
|
||
@Resource
|
||
private ISignPatientManageRouteNodeService signPatientManageRouteNodeService;
|
||
@Resource
|
||
private SignPatientManageRouteMapper signPatientManageRouteMapper;
|
||
@Resource
|
||
private SignPatientRecordMapper signPatientRecordMapper;
|
||
@Resource
|
||
private PatientVisitRecordMapper patientVisitRecordMapper;
|
||
@Resource
|
||
private ScriptInfoTaskInfoMapper scriptInfoTaskInfoMapper;
|
||
@Resource
|
||
private IAIOBService aiobService;
|
||
@Resource
|
||
private ScriptInfoMapper scriptInfoMapper;
|
||
@Resource
|
||
private PatientInfoMapper patientInfoMapper;
|
||
@Resource
|
||
private LabelFieldContentMapper labelFieldContentMapper;
|
||
|
||
/**
|
||
* @description 创建百度智能外呼任务并导入客户名单
|
||
* @Author haown
|
||
* @Date 2024-9-5 13:55
|
||
*/
|
||
public void uploadRobotPublishTask() {
|
||
log.info("开始执行百度智能外呼创建任务定时任务......");
|
||
// 查找需要当天执行的AI打电话任务(问卷或电话外呼类型)
|
||
SignPatientManageRouteNodeDto signPatientManageRouteNodeDto = new SignPatientManageRouteNodeDto();
|
||
//signPatientManageRouteNodeDto.setTaskNodeType(TaskNodeTypeEnum.PHONE_OUTBOUND.getInfo());
|
||
signPatientManageRouteNodeDto.setNodeExecuteStatus(NodeExecuteStatusEnum.UNEXECUTED.getInfo());
|
||
signPatientManageRouteNodeDto.setPhoneDialMethod(PhoneDialMethodEnum.AI.getInfo());
|
||
List<SignPatientManageRouteNode> nodeList = signPatientManageRouteNodeMapper.selectSignPatientManageRouteNodeList(signPatientManageRouteNodeDto);
|
||
|
||
List<SignPatientManageRouteNode> executeNodeList = new ArrayList<>();
|
||
nodeList.forEach(node -> {
|
||
SignPatientManageRoute signPatientManageRoute = signPatientManageRouteMapper.selectSignPatientManageRouteById(node.getManageRouteId());
|
||
SignPatientRecord signPatientRecord = signPatientRecordMapper.selectByPrimaryKey(signPatientManageRoute.getSignPatientRecordId());
|
||
PatientVisitRecord patientVisitRecord = patientVisitRecordMapper.selectPatientVisitRecordById(signPatientRecord.getPatientVisitRecordId());
|
||
LocalDateTime executeTime = signPatientManageRouteNodeService.getExecuteTime(node.getExecuteTime(), node.getRouteNodeName(), node.getRouteNodeDay(), patientVisitRecord);
|
||
if (node.getScriptInfoId() != null && (executeTime.toLocalDate().isBefore(LocalDate.now()) || executeTime.toLocalDate().isEqual(LocalDate.now()))) {
|
||
executeNodeList.add(node);
|
||
}
|
||
});
|
||
|
||
if (CollectionUtils.isNotEmpty(executeNodeList)) {
|
||
// 根据机器人id查询智能外呼系统的任务id
|
||
executeNodeList.forEach(node -> {
|
||
ScriptInfo scriptInfo = scriptInfoMapper.selectScriptInfoById(node.getScriptInfoId());
|
||
String taskId = scriptInfoTaskInfoMapper.getByNodeId(node.getId(), AiobTaskTypeContant.BATCHTASK);
|
||
if (StringUtils.isBlank(taskId)) {
|
||
// 没有任务则创建任务
|
||
if (ObjectUtils.isNotEmpty(scriptInfo)) {
|
||
log.info("创建任务......");
|
||
CreateTaskDto createTaskDto = new CreateTaskDto();
|
||
createTaskDto.setTaskName(LocalDate.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd")) + scriptInfo.getScriptName() + "(" +node.getId()+ ")");
|
||
createTaskDto.setRobotId(scriptInfo.getRobotPublishId());
|
||
createTaskDto.setDialStartDate(LocalDate.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd")));
|
||
createTaskDto.setDialEndDate(LocalDate.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd")));
|
||
createTaskDto.setForbidDialDate(Arrays.asList(99));
|
||
createTaskDto.setRetryTimes(StringUtils.isBlank(node.getPhoneRedialTimes()) ? 0 : PhoneRedialTimesEnum.getValueByInfo(node.getPhoneRedialTimes()).getValue());
|
||
createTaskDto.setRetryInterval(node.getPhoneTimeInterval());
|
||
createTaskDto.setNumTypeFilterList(Arrays.asList(1,2));
|
||
createTaskDto.setTaskDataCallback(true);
|
||
createTaskDto.setCallBackUrl(callBackUrl);
|
||
taskId = aiobService.createTask(createTaskDto);
|
||
// 开启任务
|
||
log.info("开启任务......");
|
||
aiobService.updateTaskStatus(taskId, 2);
|
||
|
||
ScriptInfoTaskInfo scriptInfoTaskInfo = new ScriptInfoTaskInfo();
|
||
scriptInfoTaskInfo.setScriptInfoId(node.getScriptInfoId());
|
||
scriptInfoTaskInfo.setTaskId(taskId);
|
||
scriptInfoTaskInfo.setRobotId(scriptInfo.getRobotPublishId());
|
||
scriptInfoTaskInfo.setCreateTime(LocalDateTime.now());
|
||
scriptInfoTaskInfo.setSignPatientManageRouteNodeId(node.getId());
|
||
scriptInfoTaskInfo.setAiobTaskType(AiobTaskTypeContant.BATCHTASK);
|
||
scriptInfoTaskInfoMapper.insertScriptInfoTaskInfo(scriptInfoTaskInfo);
|
||
}
|
||
}
|
||
// 客户名单list
|
||
List<CustomerInfoDto> customerInfoList = new ArrayList<>();
|
||
|
||
CustomerInfoDto customerInfoDto = new CustomerInfoDto();
|
||
customerInfoDto.setExtJson(node.getId() + "");
|
||
SignPatientManageRoute signPatientManageRoute = signPatientManageRouteMapper.selectSignPatientManageRouteById(node.getManageRouteId());
|
||
PatientInfo patientInfo = patientInfoMapper.selectPatientInfoById(signPatientManageRoute.getPatientId());
|
||
customerInfoDto.setMobile(patientInfo.getPatientPhone());
|
||
// 查询患者画像信息
|
||
List<LabelFieldInfoContentVo> labelFieldContentList = labelFieldContentMapper.selectByPatientId(patientInfo.getId());
|
||
// 处理变量
|
||
JSONObject jsonObject = new JSONObject();
|
||
if (StringUtils.isNotBlank(scriptInfo.getVariables())) {
|
||
List<String> variables = Arrays.asList(scriptInfo.getVariables().split("\\|"));
|
||
variables.forEach(variable -> {
|
||
LabelFieldInfoContentVo labelFieldContent = labelFieldContentList.stream().filter(s -> Objects.equals(s.getFieldCode(), variable.replaceAll("_", "").toUpperCase())).findFirst().orElse(null);
|
||
jsonObject.fluentPut(variable, ObjectUtils.isEmpty(labelFieldContent) ? "" : labelFieldContent.getFieldValue());
|
||
});
|
||
}
|
||
|
||
customerInfoDto.setVar(jsonObject);
|
||
customerInfoList.add(customerInfoDto);
|
||
|
||
// 上传名单
|
||
log.info("任务导入客户名单......");
|
||
ImportTaskDto importTaskDto = new ImportTaskDto();
|
||
importTaskDto.setTaskId(taskId);
|
||
importTaskDto.setSecretType(2);
|
||
importTaskDto.setCustomerInfoList(customerInfoList);
|
||
List<ImportTaskVo> importTaskList = aiobService.importTask(importTaskDto);
|
||
String finalTaskId = taskId;
|
||
importTaskList.forEach(importTaskVo -> {
|
||
// 名单导入成功后,sign_patient_manage_route_node表中设置taskId
|
||
if (importTaskVo.getStatus()) {
|
||
node.setTaskIdExt(finalTaskId);
|
||
node.setId(Long.valueOf(importTaskVo.getExtJson()));
|
||
signPatientManageRouteNodeMapper.updateSignPatientManageRouteNode(node);
|
||
}
|
||
});
|
||
});
|
||
}
|
||
log.info("百度智能外呼创建任务定时任务执行完成......");
|
||
}
|
||
|
||
/**
|
||
* @description 创建实时任务
|
||
* @return null
|
||
* @Author haown
|
||
* @Date 2024-9-23 10:41
|
||
*/
|
||
public void actualTimeTask() {
|
||
log.info("开始执行百度智能外呼创建实时任务定时任务......");
|
||
// 查找需要当天执行的AI打电话任务(问卷或电话外呼类型)
|
||
SignPatientManageRouteNodeDto signPatientManageRouteNodeDto = new SignPatientManageRouteNodeDto();
|
||
signPatientManageRouteNodeDto.setNodeExecuteStatus(NodeExecuteStatusEnum.UNEXECUTED.getInfo());
|
||
signPatientManageRouteNodeDto.setPhoneDialMethod(PhoneDialMethodEnum.AI.getInfo());
|
||
//只获取 任务执行类型为 单个执行的
|
||
signPatientManageRouteNodeDto.setTaskExcuteType(TaskExcuteTypeEnum.ACTUAL_TIME_TASK.getInfo());
|
||
List<SignPatientManageRouteNode> nodeList = signPatientManageRouteNodeMapper.selectSignPatientManageRouteNodeList(signPatientManageRouteNodeDto);
|
||
List<SignPatientManageRouteNode> executeNodeList = new ArrayList<>();
|
||
nodeList.forEach(node -> {
|
||
LocalDateTime executeTime ;
|
||
//如果有计划执行时间,则直接获取
|
||
if(node.getNodePlanTime()!=null)
|
||
{ executeTime = node.getNodePlanTime(); }
|
||
// 否则 ,则根据诊后/院后 第几天 计算
|
||
else {
|
||
SignPatientManageRoute signPatientManageRoute = signPatientManageRouteMapper.selectSignPatientManageRouteById(node.getManageRouteId());
|
||
SignPatientRecord signPatientRecord = signPatientRecordMapper.selectByPrimaryKey(signPatientManageRoute.getSignPatientRecordId());
|
||
PatientVisitRecord patientVisitRecord = patientVisitRecordMapper.selectPatientVisitRecordById(signPatientRecord.getPatientVisitRecordId());
|
||
executeTime = signPatientManageRouteNodeService.getExecuteTime(node.getExecuteTime(), node.getRouteNodeName(), node.getRouteNodeDay(), patientVisitRecord);
|
||
}
|
||
if (node.getScriptInfoId() != null && (executeTime.toLocalDate().isBefore(LocalDate.now()) || executeTime.toLocalDate().isEqual(LocalDate.now()))) {
|
||
executeNodeList.add(node);
|
||
}
|
||
});
|
||
|
||
// 查询需要重拨的任务
|
||
ScriptInfoTaskInfo scriptInfoTaskInfo = new ScriptInfoTaskInfo();
|
||
scriptInfoTaskInfo.setExecuteStatus(NodeExecuteStatusEnum.UNEXECUTED.getInfo());
|
||
scriptInfoTaskInfo.setAiobTaskType(AiobTaskTypeContant.ACTUALTIMETASK);
|
||
List<ScriptInfoTaskInfo> scriptInfoTaskInfoList = scriptInfoTaskInfoMapper.selectList(scriptInfoTaskInfo);
|
||
scriptInfoTaskInfoList.forEach(taskInfo -> {
|
||
SignPatientManageRouteNode node = signPatientManageRouteNodeMapper.selectSignPatientManageRouteNodeById(taskInfo.getSignPatientManageRouteNodeId());
|
||
if (taskInfo.getExecuteTime().isBefore(LocalDateTime.now()) || taskInfo.getExecuteTime().isEqual(LocalDateTime.now())) {
|
||
executeNodeList.add(node);
|
||
}
|
||
});
|
||
if (CollectionUtils.isNotEmpty(executeNodeList)) {
|
||
// 根据机器人id查询智能外呼系统的任务id
|
||
executeNodeList.forEach(node -> {
|
||
createTask(node);
|
||
});
|
||
}
|
||
log.info("百度智能外呼创建实时任务定时任务执行完成......");
|
||
}
|
||
|
||
private void createTask(SignPatientManageRouteNode node) {
|
||
ScriptInfo scriptInfo = scriptInfoMapper.selectScriptInfoById(node.getScriptInfoId());
|
||
SignPatientManageRoute signPatientManageRoute = signPatientManageRouteMapper.selectSignPatientManageRouteById(node.getManageRouteId());
|
||
PatientInfo patientInfo = patientInfoMapper.selectPatientInfoById(signPatientManageRoute.getPatientId());
|
||
// 没有任务则创建任务
|
||
if (ObjectUtils.isNotEmpty(scriptInfo)) {
|
||
log.info("创建任务......");
|
||
ActualTimeTaskDto actualTimeTaskDto = new ActualTimeTaskDto();
|
||
actualTimeTaskDto.setRobotId(scriptInfo.getRobotPublishId());
|
||
actualTimeTaskDto.setMobile(patientInfo.getPatientPhone());
|
||
actualTimeTaskDto.setSecretType(2);
|
||
actualTimeTaskDto.setStopDate(LocalDate.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd")) + " 20:00:00");
|
||
// 查询患者画像信息
|
||
List<LabelFieldInfoContentVo> labelFieldContentList = labelFieldContentMapper.selectByPatientId(patientInfo.getId());
|
||
// 处理变量
|
||
JSONObject jsonObject = new JSONObject();
|
||
if (StringUtils.isNotBlank(scriptInfo.getVariables())) {
|
||
List<String> variables = Arrays.asList(scriptInfo.getVariables().split("\\|"));
|
||
variables.forEach(variable -> {
|
||
LabelFieldInfoContentVo labelFieldContent = labelFieldContentList.stream().filter(s -> Objects.equals(s.getFieldCode(), variable.replaceAll("_", "").toUpperCase())).findFirst().orElse(null);
|
||
jsonObject.fluentPut(variable, ObjectUtils.isEmpty(labelFieldContent) ? "" : labelFieldContent.getFieldValue());
|
||
});
|
||
}
|
||
actualTimeTaskDto.setDialogVar(jsonObject);
|
||
actualTimeTaskDto.setCallBackUrl(callBackUrl);
|
||
actualTimeTaskDto.setExtJson(node.getId()+ "");
|
||
aiobService.createActualTimeTask(actualTimeTaskDto);
|
||
log.info("创建任务完成......");
|
||
}
|
||
}
|
||
|
||
}
|