Commit 15a56913 by zhouhaibao

feat(sop): 优化日期变量处理逻辑

- 在SopConditionEngine中添加日期字段截断功能,统一日期格式
- 新增WechatMessageActionExecutor中的变量值格式化方法
- 实现对Date、Long、Integer类型的日期变量自动格式化为yyyy-MM-dd格式
- 添加时间戳有效性验证机制,确保日期变量正确转换
- 支持基于变量名关键词识别的智能日期格式化功能
1 parent 5dba5f86
...@@ -4,6 +4,7 @@ import cn.com.glsx.dj.strategy.model.sop.SopStrategyBranch; ...@@ -4,6 +4,7 @@ import cn.com.glsx.dj.strategy.model.sop.SopStrategyBranch;
import cn.com.glsx.dj.strategy.model.sop.SopStrategyCondition; import cn.com.glsx.dj.strategy.model.sop.SopStrategyCondition;
import cn.com.glsx.dj.strategy.model.sop.SopStrategyConditionGroup; import cn.com.glsx.dj.strategy.model.sop.SopStrategyConditionGroup;
import cn.hutool.core.collection.CollUtil; import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateField;
import cn.hutool.core.date.DateUtil; import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil; import cn.hutool.core.util.StrUtil;
import java.math.BigDecimal; import java.math.BigDecimal;
...@@ -225,18 +226,20 @@ public class SopConditionEngine { ...@@ -225,18 +226,20 @@ public class SopConditionEngine {
if (value == null) { if (value == null) {
return null; return null;
} }
Date date;
if (value instanceof Date) { if (value instanceof Date) {
return (Date) value; date = (Date) value;
} else if (value instanceof String) {
date = DateUtil.parse(String.valueOf(value));
} else {
try {
date = new Date(Long.parseLong(String.valueOf(value)));
} catch (NumberFormatException e) {
log.warn("日期转换失败, value:{}", value);
return null;
}
} }
if (value instanceof String) { return DateUtil.truncate(date, DateField.of(java.util.Calendar.DATE));
return DateUtil.parse(String.valueOf(value));
}
try {
return new Date(Long.parseLong(String.valueOf(value)));
} catch (NumberFormatException e) {
log.warn("日期转换失败, value:{}", value);
return null;
}
} }
} }
\ No newline at end of file \ No newline at end of file
...@@ -14,6 +14,7 @@ import lombok.RequiredArgsConstructor; ...@@ -14,6 +14,7 @@ import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import java.util.Date;
import java.util.Map; import java.util.Map;
import java.util.Random; import java.util.Random;
import java.util.regex.Matcher; import java.util.regex.Matcher;
...@@ -217,7 +218,7 @@ public class WechatMessageActionExecutor implements SopActionExecutor { ...@@ -217,7 +218,7 @@ public class WechatMessageActionExecutor implements SopActionExecutor {
Object variableValue = variables.get(variableName); Object variableValue = variables.get(variableName);
if (variableValue != null) { if (variableValue != null) {
String valueStr = String.valueOf(variableValue); String valueStr = formatVariableValue(variableValue, variableName);
result = result.replace("{{" + variableName + "}}", valueStr); result = result.replace("{{" + variableName + "}}", valueStr);
log.info("替换变量: {} = {}", variableName, valueStr); log.info("替换变量: {} = {}", variableName, valueStr);
} else { } else {
...@@ -228,6 +229,37 @@ public class WechatMessageActionExecutor implements SopActionExecutor { ...@@ -228,6 +229,37 @@ public class WechatMessageActionExecutor implements SopActionExecutor {
return result; return result;
} }
private String formatVariableValue(Object value, String variableName) {
if (value instanceof Date) {
return DateUtil.format((Date) value, "yyyy-MM-dd");
}
if (value instanceof Long) {
Long timestamp = (Long) value;
if (isValidTimestamp(timestamp, variableName)) {
return DateUtil.format(new Date(timestamp), "yyyy-MM-dd");
}
}
if (value instanceof Integer) {
Integer timestamp = (Integer) value;
if (isValidTimestamp(timestamp.longValue(), variableName)) {
return DateUtil.format(new Date(timestamp.longValue() * 1000), "yyyy-MM-dd");
}
}
return String.valueOf(value);
}
private boolean isValidTimestamp(Long timestamp, String variableName) {
long minTimestamp = 946684800000L;
long maxTimestamp = 4102444800000L;
boolean isInRange = timestamp >= minTimestamp && timestamp <= maxTimestamp;
boolean isDateVariable = variableName != null &&
(variableName.contains("Date") || variableName.contains("date") ||
variableName.contains("Time") || variableName.contains("time"));
return isInRange || isDateVariable;
}
private String generateIdempotentKey(Integer actionId, Long customerInstanceId) { private String generateIdempotentKey(Integer actionId, Long customerInstanceId) {
return "sop:message:send:" + actionId + "_" + customerInstanceId; return "sop:message:send:" + actionId + "_" + customerInstanceId;
} }
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!