Browse Source

构建外箱标签 customFields:数组按QTY的当前下标取值,非数组字段由所有外箱共用

master
han\hanst 4 weeks ago
parent
commit
aaa8067a83
  1. 78
      src/main/java/com/gaotao/modules/base/service/Impl/ReportLabelListServiceImpl.java
  2. 1
      src/main/resources/i18n/messages_en_US.properties
  3. 1
      src/main/resources/i18n/messages_zh_CN.properties

78
src/main/java/com/gaotao/modules/base/service/Impl/ReportLabelListServiceImpl.java

@ -719,7 +719,7 @@ public class ReportLabelListServiceImpl extends ServiceImpl<ReportLabelListMappe
"netWeight": 14.9
}
}*/
// 则系统会替换customFields里的qty字段到ZPL代码里对应的位置
// customFields中的数组按QTY下标取值,非数组值由本次所有外箱标签共用
Map<String, Object> customFields = printRequest.getCustomFields();
// 2. 获取外箱标签打印机配置
UserLabelPrinterData outerPrinter = getRequiredLabelPrinter(printRequest.getUserId(), outerLabelSetting.getLabelType());
@ -730,13 +730,12 @@ public class ReportLabelListServiceImpl extends ServiceImpl<ReportLabelListMappe
if (hasCustomerId) {
// 传customerId时,按QTY数组逐项生成外箱任务
List<String> outerQtyList = extractRequiredOuterQtyList(customFields);
// boxPackCount与QTY等长,按下标取当前外箱的内包数,避免整段数组被印到每一张标签上
List<String> boxPackCountList = extractRequiredBoxPackCountList(customFields, outerQtyList.size());
// 所有数组字段必须与QTY等长,在入队前统一校验,避免部分标签已打印后才发现数据错位
validateOuterCustomFieldArrays(customFields, outerQtyList.size());
int outerBatchIndex = 1;
for (String outerQty : outerQtyList) {
String boxPackCount = boxPackCountList.get(outerBatchIndex - 1);
Map<String, Object> outerCustomFields = buildOuterCustomFields(
customFields, outerQty, boxPackCount, outerBatchIndex, outerQtyList.size());
customFields, outerQty, outerBatchIndex, outerQtyList.size());
queryParams.put("customFields", outerCustomFields);
// 生成当前QTY对应的外箱标签(同一queryParams会复用IFS缓存)
@ -752,8 +751,8 @@ public class ReportLabelListServiceImpl extends ServiceImpl<ReportLabelListMappe
Map<String, Object> labelData = i < labelDataList.size() ? labelDataList.get(i) : new HashMap<>();
Long taskId = enqueueSingleTask(outerPrinter.getIpAddress(), zplCode, outerRfidFlag, labelData, printRequest);
taskIds.add(taskId);
log.info("外箱标签任务已加入队列,batch={} / {}, index={} / {}, qty={}, boxPackCount={}, taskId={}",
outerBatchIndex, outerQtyList.size(), i + 1, totalLabels, outerQty, boxPackCount, taskId);
log.info("外箱标签任务已加入队列,batch={} / {}, index={} / {}, qty={}, taskId={}",
outerBatchIndex, outerQtyList.size(), i + 1, totalLabels, outerQty, taskId);
}
outerBatchIndex++;
}
@ -963,36 +962,31 @@ public class ReportLabelListServiceImpl extends ServiceImpl<ReportLabelListMappe
}
/**
* 构建外箱标签 customFields:保留原字段,并按当前外箱覆盖 qty、boxPackCount,同时写入箱序号和总箱数。
* 入参里的 QTY、boxPackCount 是等长数组,这里必须落成当前下标的单值,否则标签会印出整段数组。
* 构建外箱标签 customFields:数组按QTY的当前下标取值,非数组字段由所有外箱共用。
* 调用前统一校验数组长度;保留字段原名和数据类型,并写入当前箱序号和总箱数。
*/
private Map<String, Object> buildOuterCustomFields(Map<String, Object> sourceCustomFields, String outerQty,
String boxPackCount, int serialNumber, int outerBoxes) {
int serialNumber, int outerBoxes) {
Map<String, Object> outerCustomFields = new HashMap<>();
if (sourceCustomFields != null && !sourceCustomFields.isEmpty()) {
outerCustomFields.putAll(sourceCustomFields);
}
boolean qtyUpdated = false;
boolean boxPackCountUpdated = false;
for (String key : new ArrayList<>(outerCustomFields.keySet())) {
String normalizedKey = StringUtils.trimToEmpty(key);
if (StringUtils.equalsIgnoreCase(normalizedKey, "qty")) {
outerCustomFields.put(key, outerQty);
qtyUpdated = true;
} else if (StringUtils.equalsIgnoreCase(normalizedKey, "boxPackCount")) {
outerCustomFields.put(key, boxPackCount);
boxPackCountUpdated = true;
if (sourceCustomFields != null) {
for (Map.Entry<String, Object> entry : sourceCustomFields.entrySet()) {
Object value = entry.getValue();
if (value instanceof List<?> values) {
value = values.get(serialNumber - 1);
}
if (StringUtils.equalsIgnoreCase(StringUtils.trimToEmpty(entry.getKey()), "qty")) {
value = outerQty;
qtyUpdated = true;
}
outerCustomFields.put(entry.getKey(), value);
}
}
if (!qtyUpdated) {
outerCustomFields.put("qty", outerQty);
}
if (!boxPackCountUpdated) {
outerCustomFields.put("boxPackCount", boxPackCount);
}
outerCustomFields.put("serialNumber", serialNumber);
outerCustomFields.put("outerBoxes", outerBoxes);
outerCustomFields.put("boxPackCount", boxPackCount);
return outerCustomFields;
}
@ -1027,37 +1021,15 @@ public class ReportLabelListServiceImpl extends ServiceImpl<ReportLabelListMappe
}
/**
* 提取与QTY逐项对齐的boxPackCount。数量必须和QTY一致,当前外箱只取同一下标的值。
* 以QTY数量为准校验所有自定义数组字段,非数组字段无需逐箱配置。
*/
private List<String> extractRequiredBoxPackCountList(Map<String, Object> customFields, int expectedSize) {
if (customFields == null || customFields.isEmpty()) {
throw new RuntimeException(labelSettingI18n.text("label.error.boxPackCountRequired"));
}
Object packValue = null;
private void validateOuterCustomFieldArrays(Map<String, Object> customFields, int expectedSize) {
for (Map.Entry<String, Object> entry : customFields.entrySet()) {
if (StringUtils.equalsIgnoreCase(StringUtils.trimToEmpty(entry.getKey()), "boxPackCount")) {
packValue = entry.getValue();
break;
}
}
if (!(packValue instanceof List<?> packList) || packList.isEmpty()) {
throw new RuntimeException(labelSettingI18n.text("label.error.boxPackCountArrayRequired"));
}
if (packList.size() != expectedSize) {
throw new RuntimeException(labelSettingI18n.text(
"label.error.boxPackCountSizeMismatch", expectedSize, packList.size()));
}
List<String> boxPackCountList = new ArrayList<>();
int packIndex = 1;
for (Object packObj : packList) {
String packCount = packObj == null ? "" : packObj.toString().trim();
if (StringUtils.isBlank(packCount)) {
throw new RuntimeException(labelSettingI18n.text("label.error.boxPackCountItemRequired", packIndex));
if (entry.getValue() instanceof List<?> values && values.size() != expectedSize) {
throw new RuntimeException(labelSettingI18n.text(
"label.error.customFieldArraySizeMismatch", entry.getKey(), expectedSize, values.size()));
}
boxPackCountList.add(packCount);
packIndex++;
}
return boxPackCountList;
}
/**

1
src/main/resources/i18n/messages_en_US.properties

@ -96,6 +96,7 @@ label.error.printerIpMissing=Printer IP is not configured: labelType={0}
label.error.qtyRequired=customFields.QTY array is required for outer-box label printing.
label.error.qtyArrayRequired=customFields.QTY must be a non-empty array, for example ["100","200","300"]
label.error.qtyItemRequired=customFields.QTY item {0} cannot be empty.
label.error.customFieldArraySizeMismatch=customFields.{0} array length must match QTY. QTY has {1} items, but this field has {2}.
label.error.boxPackCountRequired=customFields.boxPackCount array is required for outer-box label printing.
label.error.boxPackCountArrayRequired=customFields.boxPackCount must be a non-empty array, for example ["5","5","1"]
label.error.boxPackCountSizeMismatch=customFields.boxPackCount length must match QTY. QTY has {0} items, boxPackCount has {1}.

1
src/main/resources/i18n/messages_zh_CN.properties

@ -96,6 +96,7 @@ label.error.printerIpMissing=打印机IP地址未配置:labelType={0}
label.error.qtyRequired=外箱标签打印必须传入customFields.QTY数组
label.error.qtyArrayRequired=customFields.QTY必须为非空数组,例如 ["100","200","300"]
label.error.qtyItemRequired=customFields.QTY第{0}项不能为空
label.error.customFieldArraySizeMismatch=customFields.{0}数组数量必须与QTY一致,QTY共{1}项,当前字段共{2}项
label.error.boxPackCountRequired=外箱标签打印必须传入customFields.boxPackCount数组
label.error.boxPackCountArrayRequired=customFields.boxPackCount必须为非空数组,例如 ["5","5","1"]
label.error.boxPackCountSizeMismatch=customFields.boxPackCount数量必须与QTY一致,QTY共{0}项,boxPackCount共{1}项

Loading…
Cancel
Save