fix(es): 电量消息归一化——value 支持字符串形态、createTime ISO 文本转本地格式
生产 _source 实测:value 索引为文本("28"/"100"),原 isNumber 判空 导致 value 恒 null;createTime 为 ISO 文本(2026-09-21T10:00:00.000+08:00), 原样输出与老系统 yyyy-MM-dd HH:mm:ss 口径不一致,统一归一。
This commit is contained in:
+33
-6
@@ -12,8 +12,10 @@ import org.springframework.stereotype.Component;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.time.Instant;
|
||||
import java.time.OffsetDateTime;
|
||||
import java.time.ZoneId;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
import java.time.format.DateTimeParseException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
@@ -93,23 +95,48 @@ public class ElectricityMessageEsGatewayImpl implements ElectricityMessageEsGate
|
||||
}
|
||||
|
||||
/**
|
||||
* createTime 在索引里是 date 类型,_source 里可能是 ISO 文本也可能是 epoch 毫秒数;
|
||||
* 两种形态都归一为 Asia/Shanghai yyyy-MM-dd HH:mm:ss 文本输出。
|
||||
* createTime 在 _source 里是 ISO 文本(如 2026-09-21T10:00:00.000+08:00)或 epoch 毫秒数;
|
||||
* 两种形态都归一为 Asia/Shanghai yyyy-MM-dd HH:mm:ss 文本输出(对齐老系统 Date 格式化口径)。
|
||||
*/
|
||||
private static String createTimeText(JsonNode value) {
|
||||
if (value == null || value.isNull()) {
|
||||
return null;
|
||||
}
|
||||
if (value.isTextual()) {
|
||||
return value.asText();
|
||||
}
|
||||
if (value.isNumber()) {
|
||||
return TIME_FORMATTER.format(Instant.ofEpochMilli(value.longValue()));
|
||||
}
|
||||
if (value.isTextual()) {
|
||||
String text = value.asText();
|
||||
if (text.indexOf('T') >= 0) {
|
||||
try {
|
||||
return TIME_FORMATTER.format(OffsetDateTime.parse(text));
|
||||
} catch (DateTimeParseException e) {
|
||||
return text;
|
||||
}
|
||||
}
|
||||
return text;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* value 在 _source 里可能是数字也可能是字符串(索引实际为 "28"/"100" 文本),
|
||||
* 统一解析为 Integer;非数值文本返回 null。
|
||||
*/
|
||||
private static Integer integerValue(JsonNode value) {
|
||||
return value != null && value.isNumber() ? value.intValue() : null;
|
||||
if (value == null || value.isNull()) {
|
||||
return null;
|
||||
}
|
||||
if (value.isNumber()) {
|
||||
return value.intValue();
|
||||
}
|
||||
if (value.isTextual()) {
|
||||
try {
|
||||
return Integer.valueOf(value.asText().trim());
|
||||
} catch (NumberFormatException e) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
+1
-1
@@ -37,7 +37,7 @@ class ElectricityMessageEsGatewayImplTest {
|
||||
+ "\"hits\":{\"total\":{\"value\":2,\"relation\":\"eq\"},"
|
||||
+ "\"hits\":["
|
||||
+ "{\"_index\":\"electricitymessage_202609\","
|
||||
+ "\"_source\":{\"createTime\":\"2026-09-21 10:00:00\",\"messageType\":0,\"value\":88}},"
|
||||
+ "\"_source\":{\"createTime\":\"2026-09-21T10:00:00.000+08:00\",\"messageType\":0,\"value\":\"88\"}},"
|
||||
+ "{\"_index\":\"electricitymessage_202609\","
|
||||
+ "\"_source\":{\"createTime\":" + EPOCH_MILLIS + ",\"messageType\":4,\"value\":3700}}"
|
||||
+ "]}}";
|
||||
|
||||
Reference in New Issue
Block a user