|
|
@@ -24,12 +24,12 @@ import java.util.*;
|
|
|
//@SuppressWarnings("all")
|
|
|
public abstract class AbstractWorkflowNode implements IWorkflowNode {
|
|
|
|
|
|
- private static final CompanyWorkflowNodeMapper companyWorkflowNodeMapper = SpringUtils.getBean(CompanyWorkflowNodeMapper.class);
|
|
|
- private static final CompanyAiWorkflowExecMapper companyAiWorkflowExecMapper = SpringUtils.getBean(CompanyAiWorkflowExecMapper.class);
|
|
|
- private static final CompanyAiWorkflowExecLogMapper companyAiWorkflowExecLogMapper = SpringUtils.getBean(CompanyAiWorkflowExecLogMapper.class);
|
|
|
- private static final CompanyWorkflowEdgeMapper companyWorkflowEdgeMapper = SpringUtils.getBean(CompanyWorkflowEdgeMapper.class);
|
|
|
- private static final CompanyVoiceRoboticBusinessMapper companyVoiceRoboticBusinessMapper = SpringUtils.getBean(CompanyVoiceRoboticBusinessMapper.class);
|
|
|
- private static final ObjectMapper objectMapper = new ObjectMapper();
|
|
|
+ public static final CompanyWorkflowNodeMapper companyWorkflowNodeMapper = SpringUtils.getBean(CompanyWorkflowNodeMapper.class);
|
|
|
+ public static final CompanyAiWorkflowExecMapper companyAiWorkflowExecMapper = SpringUtils.getBean(CompanyAiWorkflowExecMapper.class);
|
|
|
+ public static final CompanyAiWorkflowExecLogMapper companyAiWorkflowExecLogMapper = SpringUtils.getBean(CompanyAiWorkflowExecLogMapper.class);
|
|
|
+ public static final CompanyWorkflowEdgeMapper companyWorkflowEdgeMapper = SpringUtils.getBean(CompanyWorkflowEdgeMapper.class);
|
|
|
+ public static final CompanyVoiceRoboticBusinessMapper companyVoiceRoboticBusinessMapper = SpringUtils.getBean(CompanyVoiceRoboticBusinessMapper.class);
|
|
|
+ public static final ObjectMapper objectMapper = new ObjectMapper();
|
|
|
|
|
|
protected String nodeKey;
|
|
|
protected String nodeName;
|
|
|
@@ -61,15 +61,16 @@ public abstract class AbstractWorkflowNode implements IWorkflowNode {
|
|
|
} catch (Exception e) {
|
|
|
return handleExecutionError(e, context);
|
|
|
} finally {
|
|
|
- postExecute(context,result);
|
|
|
+ postExecute(context, result);
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
@Override
|
|
|
- public ExecutionResult continueExecute(ExecutionContext context){
|
|
|
- try{
|
|
|
+ public ExecutionResult continueExecute(ExecutionContext context) {
|
|
|
+ try {
|
|
|
CompanyAiWorkflowExec companyAiWorkflowExec = companyAiWorkflowExecMapper.selectByWorkflowInstanceId(context.getWorkflowInstanceId());
|
|
|
- if(!Integer.valueOf(ExecutionStatusEnum.WAITING.getValue()).equals(companyAiWorkflowExec.getStatus())){
|
|
|
- return handleExecutionError( new Exception("状态不符合"), context);
|
|
|
+ if (!Integer.valueOf(ExecutionStatusEnum.WAITING.getValue()).equals(companyAiWorkflowExec.getStatus())) {
|
|
|
+ return handleExecutionError(new Exception("状态不符合"), context);
|
|
|
}
|
|
|
return doContinue(context);
|
|
|
} catch (Exception e) {
|
|
|
@@ -92,16 +93,21 @@ public abstract class AbstractWorkflowNode implements IWorkflowNode {
|
|
|
/**
|
|
|
* 执行后的后处理
|
|
|
*/
|
|
|
- protected void postExecute(ExecutionContext context,ExecutionResult result) {
|
|
|
+ protected void postExecute(ExecutionContext context, ExecutionResult result) {
|
|
|
long endTime = System.currentTimeMillis();
|
|
|
context.setVariable("node_end_time_" + nodeKey, endTime);
|
|
|
log.info("Completed execution of node: {} ({})", nodeKey, nodeName);
|
|
|
//todo 写入执行日志等后置操作
|
|
|
+ Integer logStatus = 0;
|
|
|
if (result.isSuccess()) {
|
|
|
-
|
|
|
+ logStatus = ExecutionStatusEnum.SUCCESS.getValue();
|
|
|
} else {
|
|
|
+ logStatus = ExecutionStatusEnum.FAILURE.getValue();
|
|
|
updateWorkflowStatus(context.getWorkflowInstanceId(), ExecutionStatusEnum.FAILURE);
|
|
|
}
|
|
|
+ CompanyAiWorkflowExecLog logEntry = createLogEntry(context.getWorkflowInstanceId(), nodeKey, getType(), result, context);
|
|
|
+ logEntry.setStatus(logStatus);
|
|
|
+ logExecution(logEntry);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
@@ -120,52 +126,57 @@ public abstract class AbstractWorkflowNode implements IWorkflowNode {
|
|
|
}
|
|
|
|
|
|
public Boolean isAsync() {
|
|
|
- return false;
|
|
|
+ return false;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 根据流程配置& 条件判定 获取下一个执行节点
|
|
|
+ *
|
|
|
* @param workflowInstanceId
|
|
|
* @param nodeKey
|
|
|
* @return
|
|
|
*/
|
|
|
- public String getNextNodeKey(String workflowInstanceId, String nodeKey){
|
|
|
+ public String getNextNodeKey(String workflowInstanceId, String nodeKey) {
|
|
|
|
|
|
CompanyAiWorkflowExec companyAiWorkflowExec = companyAiWorkflowExecMapper.selectByWorkflowInstanceId(workflowInstanceId);
|
|
|
|
|
|
List<CompanyWorkflowEdge> companyWorkflowEdges =
|
|
|
companyWorkflowEdgeMapper.selectListByWorkflowIdAndNodeKey(companyAiWorkflowExec.getWorkflowId(), nodeKey);
|
|
|
//todo 判定条件满足
|
|
|
- if(null != companyWorkflowEdges && !companyWorkflowEdges.isEmpty()){
|
|
|
- if(companyWorkflowEdges.size() > 1){
|
|
|
-
|
|
|
+ if (null != companyWorkflowEdges && !companyWorkflowEdges.isEmpty()) {
|
|
|
+ if (companyWorkflowEdges.size() > 1) {
|
|
|
+ //存在多条件节点需重写此方法
|
|
|
+ return null;
|
|
|
} else {
|
|
|
return companyWorkflowEdges.get(0).getTargetNodeKey();
|
|
|
}
|
|
|
}
|
|
|
-
|
|
|
return companyWorkflowEdges.get(new Random().nextInt(companyWorkflowEdges.size())).getTargetNodeKey();
|
|
|
}
|
|
|
|
|
|
- public Boolean edgeConditionValidate(String condition){
|
|
|
+ public Boolean edgeConditionValidate(String condition) {
|
|
|
return true;
|
|
|
}
|
|
|
|
|
|
- public CompanyWorkflowNode getNodeByKey(String nodeKey){
|
|
|
+ public CompanyWorkflowNode getNodeByKey(String nodeKey) {
|
|
|
CompanyWorkflowNode companyWorkflowNode = companyWorkflowNodeMapper.selectNodeByNodeKey(nodeKey);
|
|
|
- if(null == companyWorkflowNode){
|
|
|
+ if (null == companyWorkflowNode) {
|
|
|
throw new RuntimeException("节点不存在");
|
|
|
}
|
|
|
return companyWorkflowNode;
|
|
|
}
|
|
|
|
|
|
- public CompanyVoiceRoboticBusiness getRoboticBusiness(String workflowInstanceId){
|
|
|
+ public CompanyVoiceRoboticBusiness getRoboticBusiness(String workflowInstanceId) {
|
|
|
CompanyVoiceRoboticBusiness companyVoiceRoboticBusiness = companyVoiceRoboticBusinessMapper.selectCompanyVoiceRoboticBusinessByWorkflowInstanceId(workflowInstanceId);
|
|
|
+ if(null == companyVoiceRoboticBusiness){
|
|
|
+ throw new RuntimeException("缺失业务数据");
|
|
|
+ }
|
|
|
return companyVoiceRoboticBusiness;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 记录日志
|
|
|
+ *
|
|
|
* @param logEntry
|
|
|
*/
|
|
|
public void logExecution(CompanyAiWorkflowExecLog logEntry) {
|
|
|
@@ -189,6 +200,7 @@ public abstract class AbstractWorkflowNode implements IWorkflowNode {
|
|
|
|
|
|
/**
|
|
|
* 创建执行日志
|
|
|
+ *
|
|
|
* @param workflowInstanceId
|
|
|
* @param nodeKey
|
|
|
* @param nodeType
|
|
|
@@ -196,20 +208,32 @@ public abstract class AbstractWorkflowNode implements IWorkflowNode {
|
|
|
* @return
|
|
|
*/
|
|
|
public CompanyAiWorkflowExecLog createLogEntry(String workflowInstanceId, String nodeKey,
|
|
|
- NodeTypeEnum nodeType, ExecutionResult result) {
|
|
|
+ NodeTypeEnum nodeType, ExecutionResult result, ExecutionContext context) {
|
|
|
try {
|
|
|
CompanyAiWorkflowExecLog logEntry = new CompanyAiWorkflowExecLog();
|
|
|
logEntry.setWorkflowInstanceId(workflowInstanceId);
|
|
|
logEntry.setNodeKey(nodeKey);
|
|
|
logEntry.setNodeType(nodeType.getValue());
|
|
|
logEntry.setStatus(result.getStatus().getValue());
|
|
|
-
|
|
|
logEntry.setOutputData(objectMapper.writeValueAsString(result.getOutputData()));
|
|
|
logEntry.setErrorMessage(result.getErrorMessage());
|
|
|
- logEntry.setStartTime(new Date());
|
|
|
- logEntry.setEndTime(new Date());
|
|
|
- logEntry.setDuration(0L); // 可以根据实际情况计算持续时间
|
|
|
-
|
|
|
+ Long startTime = context.getVariable("node_start_time_" + nodeKey, Long.class);
|
|
|
+ if (null != startTime) {
|
|
|
+ logEntry.setStartTime(new Date(startTime));
|
|
|
+ } else {
|
|
|
+ logEntry.setStartTime(new Date());
|
|
|
+ }
|
|
|
+ Long endTime = context.getVariable("node_end_time_" + nodeKey, Long.class);
|
|
|
+ if (null != endTime) {
|
|
|
+ logEntry.setEndTime(new Date(endTime));
|
|
|
+ } else {
|
|
|
+ logEntry.setEndTime(new Date());
|
|
|
+ }
|
|
|
+ long duration = 0;
|
|
|
+ if (null != startTime && null != endTime) {
|
|
|
+ duration = endTime - startTime;
|
|
|
+ }
|
|
|
+ logEntry.setDuration(duration);
|
|
|
return logEntry;
|
|
|
} catch (JsonProcessingException e) {
|
|
|
throw new RuntimeException(e);
|
|
|
@@ -219,6 +243,7 @@ public abstract class AbstractWorkflowNode implements IWorkflowNode {
|
|
|
|
|
|
/**
|
|
|
* 阻塞工作流
|
|
|
+ *
|
|
|
* @param workflowInstanceId
|
|
|
* @param nodeKey
|
|
|
* @param context
|
|
|
@@ -239,6 +264,7 @@ public abstract class AbstractWorkflowNode implements IWorkflowNode {
|
|
|
|
|
|
/**
|
|
|
* 更新工作流状态
|
|
|
+ *
|
|
|
* @param workflowInstanceId
|
|
|
* @param status
|
|
|
*/
|
|
|
@@ -250,4 +276,11 @@ public abstract class AbstractWorkflowNode implements IWorkflowNode {
|
|
|
companyAiWorkflowExecMapper.updateByWorkflowInstanceId(update);
|
|
|
}
|
|
|
|
|
|
+ public CompanyAiWorkflowExec getWorkflowExec(String workflowInstanceId) {
|
|
|
+ CompanyAiWorkflowExec companyAiWorkflowExec = companyAiWorkflowExecMapper.selectByWorkflowInstanceId(workflowInstanceId);
|
|
|
+ if (null == companyAiWorkflowExec) {
|
|
|
+ throw new RuntimeException("工作流不存在");
|
|
|
+ }
|
|
|
+ return companyAiWorkflowExec;
|
|
|
+ }
|
|
|
}
|