|
|
|
@ -246,8 +246,17 @@ public class LabelDataProcessorServiceImpl implements LabelDataProcessorService |
|
|
|
// 检查是否是数据源字段引用 #{fieldName} |
|
|
|
if (reference.startsWith("#{") && reference.endsWith("}")) { |
|
|
|
String fieldName = reference.substring(2, reference.length() - 1); |
|
|
|
|
|
|
|
// 首先检查是否是系统变量 |
|
|
|
if (isSystemVariable(fieldName)) { |
|
|
|
replacement = getSystemVariableValue(fieldName); |
|
|
|
log.debug("元素组合中使用系统变量: {}={}", fieldName, replacement); |
|
|
|
} else { |
|
|
|
// 普通数据源字段 |
|
|
|
Object value = dataMap.get(fieldName); |
|
|
|
replacement = value != null ? value.toString() : ""; |
|
|
|
log.debug("元素组合中使用数据源字段: {}={}", fieldName, replacement); |
|
|
|
} |
|
|
|
} else { |
|
|
|
// 查找匹配的元素 |
|
|
|
ReportLabelList referencedElement = findElementByReference(reference, elementMap); |
|
|
|
@ -562,7 +571,7 @@ public class LabelDataProcessorServiceImpl implements LabelDataProcessorService |
|
|
|
|
|
|
|
@Override |
|
|
|
public String replaceDataSourceFields(String text, Map<String, Object> dataMap) { |
|
|
|
if (text == null || text.isEmpty() || dataMap == null || dataMap.isEmpty()) { |
|
|
|
if (text == null || text.isEmpty()) { |
|
|
|
return text; |
|
|
|
} |
|
|
|
|
|
|
|
@ -571,9 +580,15 @@ public class LabelDataProcessorServiceImpl implements LabelDataProcessorService |
|
|
|
|
|
|
|
while (matcher.find()) { |
|
|
|
String fieldName = matcher.group(1); |
|
|
|
Object value = dataMap.get(fieldName); |
|
|
|
|
|
|
|
String replacement; |
|
|
|
|
|
|
|
// 首先检查是否是系统变量 |
|
|
|
if (isSystemVariable(fieldName)) { |
|
|
|
replacement = getSystemVariableValue(fieldName); |
|
|
|
log.debug("替换系统变量: {}={}", fieldName, replacement); |
|
|
|
} else if (dataMap != null && !dataMap.isEmpty()) { |
|
|
|
// 然后检查数据源字段 |
|
|
|
Object value = dataMap.get(fieldName); |
|
|
|
if (value != null) { |
|
|
|
replacement = value.toString(); |
|
|
|
log.debug("替换数据源字段: {}={}", fieldName, replacement); |
|
|
|
@ -581,6 +596,10 @@ public class LabelDataProcessorServiceImpl implements LabelDataProcessorService |
|
|
|
replacement = "#{" + fieldName + "}"; // 保持原样,表示未找到数据 |
|
|
|
log.warn("未找到数据源字段的值: {}", fieldName); |
|
|
|
} |
|
|
|
} else { |
|
|
|
replacement = "#{" + fieldName + "}"; // 保持原样,表示未找到数据 |
|
|
|
log.warn("数据源为空,保持字段原样: {}", fieldName); |
|
|
|
} |
|
|
|
|
|
|
|
matcher.appendReplacement(result, Matcher.quoteReplacement(replacement)); |
|
|
|
} |
|
|
|
@ -971,7 +990,13 @@ public class LabelDataProcessorServiceImpl implements LabelDataProcessorService |
|
|
|
*/ |
|
|
|
private String processSerialNumberElement(ReportLabelList element, Map<String, Object> dataMap,Boolean nextSeqNotExist,Boolean printFlag) { |
|
|
|
try { |
|
|
|
log.debug("开始处理流水号元素: itemNo={}, data={}", element.getItemNo(), element.getData()); |
|
|
|
log.debug("开始处理流水号元素: itemNo={}, data={}, showSerialNumber={}", element.getItemNo(), element.getData(), element.getShowSerialNumber()); |
|
|
|
|
|
|
|
// 检查是否显示流水号(默认显示) |
|
|
|
if (element.getShowSerialNumber() != null && "false".equals(element.getShowSerialNumber())) { |
|
|
|
log.debug("流水号元素设置为不显示,跳过处理"); |
|
|
|
return ""; // 不显示时返回空字符串 |
|
|
|
} |
|
|
|
|
|
|
|
if (element.getParentSerialLabelNo() != null && StringUtils.isNotBlank(element.getParentSerialLabelNo())) { |
|
|
|
ReportLabelList parentElement = baseService.getReportLabelListByLabelNo(element.getParentSerialLabelNo()); |
|
|
|
@ -1054,11 +1079,20 @@ public class LabelDataProcessorServiceImpl implements LabelDataProcessorService |
|
|
|
continue; |
|
|
|
} |
|
|
|
|
|
|
|
// 检查是否是数据源字段 (格式: #{fieldName}) |
|
|
|
// 检查是否是数据源字段或系统变量 (格式: #{fieldName}) |
|
|
|
if (objectName.startsWith("#{") && objectName.endsWith("}")) { |
|
|
|
String fieldName = objectName.substring(2, objectName.length() - 1); |
|
|
|
String value = ""; |
|
|
|
|
|
|
|
// 首先检查是否是系统变量 |
|
|
|
if (isSystemVariable(fieldName)) { |
|
|
|
value = getSystemVariableValue(fieldName); |
|
|
|
} else { |
|
|
|
// 普通数据字段 |
|
|
|
Object fieldValue = dataMap.get(fieldName); |
|
|
|
String value = fieldValue != null ? fieldValue.toString() : ""; |
|
|
|
value = fieldValue != null ? fieldValue.toString() : ""; |
|
|
|
} |
|
|
|
|
|
|
|
if (!value.isEmpty()) { |
|
|
|
keyInfoBuilder.append(value); |
|
|
|
keyInfoBuilder.append("_"); |
|
|
|
@ -1197,13 +1231,23 @@ public class LabelDataProcessorServiceImpl implements LabelDataProcessorService |
|
|
|
serialNumberBuilder.append(formattedSeqNo); |
|
|
|
hasSerialNumberPlaceholder = true; |
|
|
|
} |
|
|
|
// 检查是否是数据源字段 (格式: #{fieldName}) - 使用真实数据 |
|
|
|
// 检查是否是数据源字段或系统变量 (格式: #{fieldName}) - 使用真实数据 |
|
|
|
else if (objectName.startsWith("#{") && objectName.endsWith("}")) { |
|
|
|
String fieldName = objectName.substring(2, objectName.length() - 1); |
|
|
|
String value = ""; |
|
|
|
|
|
|
|
// 首先检查是否是系统变量 |
|
|
|
if (isSystemVariable(fieldName)) { |
|
|
|
value = getSystemVariableValue(fieldName); |
|
|
|
log.debug("使用系统变量: {}={}", fieldName, value); |
|
|
|
} else { |
|
|
|
// 普通数据字段 |
|
|
|
Object fieldValue = dataMap.get(fieldName); |
|
|
|
String value = fieldValue != null ? fieldValue.toString() : ""; |
|
|
|
serialNumberBuilder.append(value); |
|
|
|
value = fieldValue != null ? fieldValue.toString() : ""; |
|
|
|
log.debug("使用真实数据字段: {}={}", fieldName, value); |
|
|
|
} |
|
|
|
|
|
|
|
serialNumberBuilder.append(value); |
|
|
|
} else { |
|
|
|
// 固定字符串,直接使用 |
|
|
|
serialNumberBuilder.append(objectName); |
|
|
|
@ -1288,4 +1332,145 @@ public class LabelDataProcessorServiceImpl implements LabelDataProcessorService |
|
|
|
throw new RuntimeException(e.getMessage()); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* 判断是否为系统变量 |
|
|
|
* |
|
|
|
* @param fieldName 字段名 |
|
|
|
* @return 是否为系统变量 |
|
|
|
*/ |
|
|
|
private boolean isSystemVariable(String fieldName) { |
|
|
|
return fieldName.startsWith("CURRENT_DATE_") || |
|
|
|
fieldName.startsWith("CURRENT_TIME_") || |
|
|
|
fieldName.equals("CURRENT_DATETIME"); |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* 获取系统变量的值 |
|
|
|
* |
|
|
|
* @param fieldName 系统变量名 |
|
|
|
* @return 系统变量的值 |
|
|
|
*/ |
|
|
|
private String getSystemVariableValue(String fieldName) { |
|
|
|
java.time.LocalDateTime now = java.time.LocalDateTime.now(); |
|
|
|
java.time.format.DateTimeFormatter formatter; |
|
|
|
|
|
|
|
switch (fieldName) { |
|
|
|
case "CURRENT_DATE_YYYYMMDD": |
|
|
|
formatter = java.time.format.DateTimeFormatter.ofPattern("yyyyMMdd"); |
|
|
|
return now.format(formatter); |
|
|
|
|
|
|
|
case "CURRENT_DATE_YYYY-MM-DD": |
|
|
|
formatter = java.time.format.DateTimeFormatter.ofPattern("yyyy-MM-dd"); |
|
|
|
return now.format(formatter); |
|
|
|
|
|
|
|
case "CURRENT_TIME_HHMMSS": |
|
|
|
formatter = java.time.format.DateTimeFormatter.ofPattern("HHmmss"); |
|
|
|
return now.format(formatter); |
|
|
|
|
|
|
|
case "CURRENT_DATETIME": |
|
|
|
formatter = java.time.format.DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); |
|
|
|
return now.format(formatter); |
|
|
|
|
|
|
|
default: |
|
|
|
log.warn("未知的系统变量: {}", fieldName); |
|
|
|
return ""; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* 测试系统变量功能 |
|
|
|
* 用于验证日期拼接功能的正确性 |
|
|
|
*/ |
|
|
|
public void testSystemVariables() { |
|
|
|
log.info("=== 系统变量功能测试开始 ==="); |
|
|
|
|
|
|
|
// 测试所有系统变量 |
|
|
|
String[] testVariables = { |
|
|
|
"CURRENT_DATE_YYYYMMDD", |
|
|
|
"CURRENT_DATE_YYYY-MM-DD", |
|
|
|
"CURRENT_TIME_HHMMSS", |
|
|
|
"CURRENT_DATETIME" |
|
|
|
}; |
|
|
|
|
|
|
|
for (String varName : testVariables) { |
|
|
|
boolean isSystemVar = isSystemVariable(varName); |
|
|
|
String value = getSystemVariableValue(varName); |
|
|
|
log.info("系统变量测试: {} -> 是否为系统变量: {}, 值: {}", varName, isSystemVar, value); |
|
|
|
} |
|
|
|
|
|
|
|
// 测试具体业务场景:P + site + YYYYMMDD + 8位流水号 |
|
|
|
log.info("=== 测试业务场景:P + site + YYYYMMDD + 流水号 ==="); |
|
|
|
|
|
|
|
java.util.List<com.gaotao.modules.base.entity.LabelContentSerialRuleData> businessRules = new java.util.ArrayList<>(); |
|
|
|
|
|
|
|
// 规则1:固定前缀 "P" |
|
|
|
com.gaotao.modules.base.entity.LabelContentSerialRuleData rule1 = new com.gaotao.modules.base.entity.LabelContentSerialRuleData(); |
|
|
|
rule1.setObjectName("P"); |
|
|
|
businessRules.add(rule1); |
|
|
|
|
|
|
|
// 规则2:数据源字段 site |
|
|
|
com.gaotao.modules.base.entity.LabelContentSerialRuleData rule2 = new com.gaotao.modules.base.entity.LabelContentSerialRuleData(); |
|
|
|
rule2.setObjectName("#{site}"); |
|
|
|
businessRules.add(rule2); |
|
|
|
|
|
|
|
// 规则3:当前日期 YYYYMMDD |
|
|
|
com.gaotao.modules.base.entity.LabelContentSerialRuleData rule3 = new com.gaotao.modules.base.entity.LabelContentSerialRuleData(); |
|
|
|
rule3.setObjectName("#{CURRENT_DATE_YYYYMMDD}"); |
|
|
|
businessRules.add(rule3); |
|
|
|
|
|
|
|
// 规则4:8位流水号 |
|
|
|
com.gaotao.modules.base.entity.LabelContentSerialRuleData rule4 = new com.gaotao.modules.base.entity.LabelContentSerialRuleData(); |
|
|
|
rule4.setObjectName("流水号"); |
|
|
|
businessRules.add(rule4); |
|
|
|
|
|
|
|
// 模拟数据源数据 |
|
|
|
java.util.Map<String, Object> businessDataMap = new java.util.HashMap<>(); |
|
|
|
businessDataMap.put("site", "SH001"); // 模拟站点数据 |
|
|
|
|
|
|
|
// 测试组装流水号(8位流水号) |
|
|
|
String businessSerialNumber = assembleFullSerialNumber(businessRules, businessDataMap, "00000001"); |
|
|
|
log.info("业务场景流水号组装结果: {}", businessSerialNumber); |
|
|
|
|
|
|
|
// 测试KeyInfo生成(用于区分不同条件下的流水号序列) |
|
|
|
String businessKeyInfo = generateSerialKeyInfo(businessRules, businessDataMap); |
|
|
|
log.info("业务场景KeyInfo生成结果: {}", businessKeyInfo); |
|
|
|
|
|
|
|
// 测试不同站点的情况 |
|
|
|
businessDataMap.put("site", "BJ002"); |
|
|
|
String anotherSerialNumber = assembleFullSerialNumber(businessRules, businessDataMap, "00000001"); |
|
|
|
String anotherKeyInfo = generateSerialKeyInfo(businessRules, businessDataMap); |
|
|
|
log.info("不同站点流水号组装结果: {}", anotherSerialNumber); |
|
|
|
log.info("不同站点KeyInfo生成结果: {}", anotherKeyInfo); |
|
|
|
|
|
|
|
// 测试真实数据预览场景 |
|
|
|
log.info("=== 测试真实数据预览场景 ==="); |
|
|
|
|
|
|
|
// 模拟真实数据预览的数据映射(包含site字段) |
|
|
|
java.util.Map<String, Object> realDataMap = new java.util.HashMap<>(); |
|
|
|
realDataMap.put("site", "SH001"); |
|
|
|
realDataMap.put("product_code", "ABC123"); |
|
|
|
realDataMap.put("batch_no", "B20241218"); |
|
|
|
|
|
|
|
// 测试replaceDataSourceFields方法是否支持系统变量 |
|
|
|
String testText1 = "P#{site}#{CURRENT_DATE_YYYYMMDD}"; |
|
|
|
String replacedText1 = replaceDataSourceFields(testText1, realDataMap); |
|
|
|
log.info("真实数据预览测试1: {} -> {}", testText1, replacedText1); |
|
|
|
|
|
|
|
String testText2 = "#{product_code}-#{CURRENT_DATE_YYYYMMDD}-#{batch_no}"; |
|
|
|
String replacedText2 = replaceDataSourceFields(testText2, realDataMap); |
|
|
|
log.info("真实数据预览测试2: {} -> {}", testText2, replacedText2); |
|
|
|
|
|
|
|
// 测试混合场景:固定字符串 + 数据源字段 + 系统变量 |
|
|
|
String testText3 = "PREFIX_#{site}_#{CURRENT_DATE_YYYYMMDD}_SUFFIX"; |
|
|
|
String replacedText3 = replaceDataSourceFields(testText3, realDataMap); |
|
|
|
log.info("真实数据预览测试3: {} -> {}", testText3, replacedText3); |
|
|
|
|
|
|
|
// 测试仅系统变量的场景 |
|
|
|
String testText4 = "#{CURRENT_DATE_YYYYMMDD}"; |
|
|
|
String replacedText4 = replaceDataSourceFields(testText4, null); // 空数据源 |
|
|
|
log.info("真实数据预览测试4(空数据源): {} -> {}", testText4, replacedText4); |
|
|
|
|
|
|
|
log.info("=== 系统变量功能测试完成 ==="); |
|
|
|
} |
|
|
|
} |