refactor(job): 优化工单任务中的JSON数组处理逻辑
- 移除无用的Stream导入 - 修改检查内容连接符从空格改为换行符 - 提取公共方法flattenJsonArray处理嵌套JSON数组 - 简化handleSopSchema和handleRelateProcessContent方法实现 - 新增递归展平嵌套JSON数组的功能方法 - 统一JSONArray处理逻辑,提高代码可读性
This commit is contained in:
@@ -23,7 +23,6 @@ import java.sql.SQLException;
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.*;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import static com.jeelowcode.tool.framework.common.util.date.DateUtils.FORMAT_YEAR_MONTH_DAY_HOUR_MINUTE_SECOND;
|
||||
|
||||
@@ -537,7 +536,7 @@ public class AlibabaWorkOrderJob implements JobHandler {
|
||||
List<String> checkContentList = nodeList.stream()
|
||||
.map(node -> new JSONObject(node).getStr("label"))
|
||||
.collect(Collectors.toList());
|
||||
return StrUtil.join(" ", checkContentList);
|
||||
return StrUtil.join("\n", checkContentList);
|
||||
} else {
|
||||
return StringUtils.EMPTY;
|
||||
}
|
||||
@@ -550,10 +549,7 @@ public class AlibabaWorkOrderJob implements JobHandler {
|
||||
* @return 解析并处理后的JSONArray对象
|
||||
*/
|
||||
private static JSONArray handleSopSchema(String sopSchema) {
|
||||
return new JSONArray(sopSchema).stream().flatMap(item -> {
|
||||
JSONArray itemArray = new JSONArray(item);
|
||||
return Stream.of(itemArray);
|
||||
}).collect(Collectors.toCollection(JSONArray::new));
|
||||
return new JSONArray(flattenJsonArray(sopSchema));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -569,13 +565,7 @@ public class AlibabaWorkOrderJob implements JobHandler {
|
||||
* @return 解析并处理后的JSONArray对象
|
||||
*/
|
||||
private static JSONArray handleRelateProcessContent(String processContent) {
|
||||
return new JSONArray(processContent).stream().flatMap(item -> {
|
||||
JSONArray itemArray = new JSONArray(item);
|
||||
return Stream.of(itemArray);
|
||||
}).flatMap(item -> {
|
||||
JSONArray itemArray = new JSONArray(item);
|
||||
return Stream.of(itemArray);
|
||||
}).collect(Collectors.toCollection(JSONArray::new));
|
||||
return flattenJsonArray(processContent);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -605,4 +595,38 @@ public class AlibabaWorkOrderJob implements JobHandler {
|
||||
}).collect(Collectors.toList());
|
||||
}
|
||||
|
||||
/**
|
||||
* 将嵌套的JSON数组转换为一维数组
|
||||
*
|
||||
* @param json 嵌套的JSON字符串
|
||||
* @return 一维JSONArray
|
||||
*/
|
||||
public static JSONArray flattenJsonArray(String json) {
|
||||
JSONArray inputArray = JSONUtil.parseArray(json);
|
||||
JSONArray resultArray = new JSONArray();
|
||||
flatten(inputArray, resultArray);
|
||||
return resultArray;
|
||||
}
|
||||
|
||||
/**
|
||||
* 递归展平嵌套的JSON数组
|
||||
*
|
||||
* @param input 输入的JSONArray
|
||||
* @param result 结果JSONArray
|
||||
*/
|
||||
private static void flatten(JSONArray input, JSONArray result) {
|
||||
for (Object item : input) {
|
||||
if (item instanceof JSONArray) {
|
||||
// 如果元素是数组,递归处理
|
||||
flatten((JSONArray) item, result);
|
||||
} else if (item instanceof JSONObject) {
|
||||
// 如果元素是对象,直接添加到结果中
|
||||
result.add(item);
|
||||
} else {
|
||||
// 如果是基本类型,也直接添加到结果中
|
||||
result.add(item);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user