This commit is contained in:
Leo
2025-12-14 13:43:26 +08:00
parent d78e77530b
commit 8572faf072

View File

@@ -143,7 +143,7 @@ public class JDUtil {
* <p> * <p>
* 订单号: * 订单号:
*/ */
private static final String WENAN_D = "单:\n" + "{单号} \n备注:{单的备注}\n" + "分销标记:{分销标记}\n" + "号:\n" + "{型号}" + "\n" + "链接\n" + "{链接}" + "\n" + "下单付款\n" + "\n" + "后返金额\n" + "\n" + "地址\n" + "{地址}" + "\n" + "物流链接:\n" + "\n" + "订单号\n" + "\n" + "下单人:\n" + "\n"; private static final String WENAN_D = "单:\n" + "{单号} \n" + "分销标记:{分销标记}\n" + "第三方单号:\n" + "—————————\n" + "下单链接(必须用这个):\n" + "{链接}\n" + "下单地址(注意带分机):\n" + "{地址}\n" + "—————————\n" + "型号:{型号}\n" + "\n" + "下单人(需填)\n" + "\n" + "下单付款(注意核对)\n" + "\n" + "后返金额(注意核对)\n" + "\n" + "订单号(需填):\n" + "\n" + "物流链接(需填)\n" + "\n" + "备注(下单号码有变动/没法带分机号的写这里)\n" + "{单的备注}\n" + "—————————\n" + "京粉实际价格:不用填";
final WXUtil wxUtil; final WXUtil wxUtil;
private final StringRedisTemplate redisTemplate; private final StringRedisTemplate redisTemplate;
@@ -2885,58 +2885,82 @@ public class JDUtil {
public JDOrder parseOrderFromText(String input) { public JDOrder parseOrderFromText(String input) {
// 清理多余的空白字符 // 清理空白字符,保持原始格式以便正确解析多行字段
input = input.replaceAll("\\s+", " ").trim();
Map<String, String> fields = new HashMap<>(); Map<String, String> fields = new HashMap<>();
// 定义正则表达式提取各个字段 // 适配新模板格式的字段提取
extractField(input, fields, "单:", "备注:"); // 新格式:单: -> 分销标记: -> 第三方单号: -> 下单链接(必须用这个): -> 下单地址(注意带分机): -> 型号: -> 下单人(需填): -> 下单付款(注意核对): -> 后返金额(注意核对): -> 订单号(需填): -> 物流链接(需填): -> 备注(下单号码有变动/没法带分机号的写这里):
extractField(input, fields, "备注:", "分销标记:"); // 使用更灵活的正则,支持分隔线"—————————"
extractField(input, fields, "分销标记", "型号"); extractFieldWithSeparator(input, fields, "", "分销标记");
extractField(input, fields, "型号", "链接"); extractFieldWithSeparator(input, fields, "分销标记", "第三方单号");
extractField(input, fields, "链接", "下单付款"); extractFieldWithSeparator(input, fields, "第三方单号", "下单链接(必须用这个)");
extractField(input, fields, "下单付款", "后返金额"); extractFieldWithSeparator(input, fields, "下单链接(必须用这个)", "下单地址(注意带分机)");
extractField(input, fields, "后返金额", "地址"); extractFieldWithSeparator(input, fields, "下单地址(注意带分机)", "型号");
extractField(input, fields, "地址", "物流链接"); extractFieldWithSeparator(input, fields, "型号", "下单人(需填)");
extractField(input, fields, "物流链接", "订单号"); extractFieldWithSeparator(input, fields, "下单人(需填)", "下单付款(注意核对)");
extractField(input, fields, "订单号", "下单人"); extractFieldWithSeparator(input, fields, "下单付款(注意核对)", "后返金额(注意核对)");
extractFieldWithSeparator(input, fields, "后返金额(注意核对):", "订单号(需填):");
// 手动提取"下单人" extractFieldWithSeparator(input, fields, "订单号(需填):", "物流链接(需填):");
Pattern buyerPattern = Pattern.compile("下单人:\\s*(.*?)\\s*(?=单:|\\Z)", Pattern.DOTALL); extractFieldWithSeparator(input, fields, "物流链接(需填):", "备注(下单号码有变动/没法带分机号的写这里):");
Matcher buyerMatcher = buyerPattern.matcher(input);
if (buyerMatcher.find()) { // 手动提取"备注"字段(最后一个字段,可能后面有分隔线或"京粉实际价格"
fields.put("下单人", buyerMatcher.group(1).trim()); Pattern remarkPattern = Pattern.compile("备注(下单号码有变动/没法带分机号的写这里):\\s*(.*?)\\s*(?=—————————|京粉实际价格|\\Z)", Pattern.DOTALL);
Matcher remarkMatcher = remarkPattern.matcher(input);
if (remarkMatcher.find()) {
String remarkValue = remarkMatcher.group(1).trim();
// 去掉可能的分隔线
remarkValue = remarkValue.replaceAll("^—————————\\s*", "").trim();
fields.put("备注", remarkValue);
} }
// 构建 JDOrder 对象 // 构建 JDOrder 对象
JDOrder order = new JDOrder(); JDOrder order = new JDOrder();
order.setRemark(fields.getOrDefault("", null)); // 单号格式2025-12-13 006
String orderNo = fields.getOrDefault("", "").trim();
order.setRemark(orderNo);
order.setDistributionMark(fields.getOrDefault("分销标记", null)); order.setDistributionMark(fields.getOrDefault("分销标记", null));
order.setModelNumber(fields.getOrDefault("型号", null)); order.setModelNumber(fields.getOrDefault("型号", null));
order.setLink(fields.getOrDefault("链接", null)); // 下单链接(必须用这个)
order.setLink(fields.getOrDefault("下单链接(必须用这个)", null));
// 备注字段
order.setStatus(fields.getOrDefault("备注", null)); order.setStatus(fields.getOrDefault("备注", null));
// 第三方单号
String thirdPartyOrderNo = fields.getOrDefault("第三方单号", null);
if (thirdPartyOrderNo != null && !thirdPartyOrderNo.trim().isEmpty()) {
order.setThirdPartyOrderNo(thirdPartyOrderNo.trim());
}
try { try {
order.setPaymentAmount(Double.parseDouble(fields.getOrDefault("下单付款", "0"))); String paymentStr = fields.getOrDefault("下单付款(注意核对)", "0").trim();
if (!paymentStr.isEmpty() && !paymentStr.equals("0")) {
order.setPaymentAmount(Double.parseDouble(paymentStr));
}
} catch (NumberFormatException ignored) { } catch (NumberFormatException ignored) {
} }
try { try {
order.setRebateAmount(Double.parseDouble(fields.getOrDefault("后返金额", "0"))); String rebateStr = fields.getOrDefault("后返金额(注意核对)", "0").trim();
if (!rebateStr.isEmpty() && !rebateStr.equals("0")) {
order.setRebateAmount(Double.parseDouble(rebateStr));
}
} catch (NumberFormatException ignored) { } catch (NumberFormatException ignored) {
} }
order.setAddress(fields.getOrDefault("地址", null)); // 下单地址(注意带分机)
order.setLogisticsLink(extractFirstUrl(fields.getOrDefault("物流链接", ""))); order.setAddress(fields.getOrDefault("下单地址(注意带分机)", null));
order.setOrderId(fields.getOrDefault("订单号", null)); order.setLogisticsLink(extractFirstUrl(fields.getOrDefault("物流链接(需填)", "")));
order.setBuyer(fields.getOrDefault("下单人", null)); order.setOrderId(fields.getOrDefault("订单号(需填)", null));
order.setBuyer(fields.getOrDefault("下单人(需填)", null));
// 设置下单时间,格式为 yyyy-MM-dd HH:mm:ss // 设置下单时间,格式为 yyyy-MM-dd HH:mm:ss
try { try {
String dateStr = fields.getOrDefault("", "").split(" ")[0]; if (!orderNo.isEmpty()) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); String dateStr = orderNo.split("\\s+")[0]; // 从"2025-12-13 006"中提取日期部分
order.setOrderTime(sdf.parse(dateStr)); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
order.setOrderTime(sdf.parse(dateStr));
} else {
order.setOrderTime(new Date());
}
} catch (Exception e) { } catch (Exception e) {
order.setOrderTime(new Date()); order.setOrderTime(new Date());
} }
@@ -2944,6 +2968,22 @@ public class JDUtil {
return order; return order;
} }
// 提取字段的方法(支持分隔线)
private void extractFieldWithSeparator(String input, Map<String, String> map, String startKeyword, String endKeyword) {
// 支持分隔线"—————————"作为字段分隔符
Pattern pattern = Pattern.compile(startKeyword + "\\s*(.*?)\\s*(?=" + Pattern.quote(endKeyword) + "|—————————|\\Z)", Pattern.DOTALL);
Matcher matcher = pattern.matcher(input);
if (matcher.find()) {
String value = matcher.group(1).trim();
// 去掉可能的分隔线
value = value.replaceAll("^—————————\\s*", "").replaceAll("\\s*—————————$", "").trim();
// 去掉字段标签本身(如果被匹配到了)
value = value.replaceAll("^" + Pattern.quote(startKeyword), "").trim();
map.put(startKeyword.replace("", ""), value);
}
}
// 提取字段的方法 // 提取字段的方法
private void extractField(String input, Map<String, String> map, String startKeyword, String endKeyword) { private void extractField(String input, Map<String, String> map, String startKeyword, String endKeyword) {
Pattern pattern = Pattern.compile(startKeyword + "\\s*(.*?)\\s*(?=" + endKeyword + "|\\Z)", Pattern.DOTALL); Pattern pattern = Pattern.compile(startKeyword + "\\s*(.*?)\\s*(?=" + endKeyword + "|\\Z)", Pattern.DOTALL);