1
This commit is contained in:
@@ -756,7 +756,7 @@ public class TencentDocController extends BaseController {
|
||||
successLogs.add(successLog);
|
||||
|
||||
// 手动执行时,batchId为null
|
||||
sendWeChatNotification(successLogs, 1, 0, 0, null, fileId, sheetId, true);
|
||||
sendWeChatNotification(successLogs, 1, 0, 0, null, fileId, sheetId, true, null, null);
|
||||
} catch (Exception e) {
|
||||
log.error("发送微信推送失败", e);
|
||||
// 不影响主流程,继续返回成功
|
||||
@@ -1205,6 +1205,12 @@ public class TencentDocController extends BaseController {
|
||||
|
||||
// 收集同步成功的详细日志(用于微信推送)
|
||||
List<Map<String, Object>> successLogs = new java.util.ArrayList<>();
|
||||
|
||||
// 收集物流链接更新的记录(用于推送消息说明)
|
||||
List<Map<String, Object>> logisticsLinkUpdatedLogs = new java.util.ArrayList<>();
|
||||
|
||||
// 收集错误详情日志(用于微信推送)
|
||||
List<Map<String, Object>> errorLogs = new java.util.ArrayList<>();
|
||||
|
||||
JSONArray updates = new JSONArray(); // 存储需要更新的行和值
|
||||
|
||||
@@ -1228,34 +1234,79 @@ public class TencentDocController extends BaseController {
|
||||
|
||||
// 检查物流链接列是否已有值(可能是别人手动填写的)
|
||||
String existingLogisticsLink = row.getString(logisticsLinkColumn);
|
||||
if (existingLogisticsLink != null && !existingLogisticsLink.trim().isEmpty()) {
|
||||
// 文档中已有物流链接,同步更新订单的推送状态
|
||||
String trimmedExistingLink = (existingLogisticsLink != null) ? existingLogisticsLink.trim() : "";
|
||||
|
||||
if (!trimmedExistingLink.isEmpty()) {
|
||||
// 文档中已有物流链接,需要比对数据库中的物流链接
|
||||
try {
|
||||
JDOrder existingOrder = jdOrderService.selectJDOrderByThirdPartyOrderNo(orderNo);
|
||||
if (existingOrder != null &&
|
||||
(existingOrder.getTencentDocPushed() == null || existingOrder.getTencentDocPushed() == 0)) {
|
||||
// 订单未标记为已推送,但文档中已有值,同步状态
|
||||
existingOrder.setTencentDocPushed(1);
|
||||
existingOrder.setTencentDocPushTime(new java.util.Date());
|
||||
int syncResult = jdOrderService.updateJDOrder(existingOrder);
|
||||
if (syncResult > 0) {
|
||||
log.info("✓ 同步订单状态成功 - 单号: {}, 行号: {}, 原因: 文档中已有物流链接(可能手动填写)",
|
||||
orderNo, excelRow);
|
||||
if (existingOrder != null) {
|
||||
String dbLogisticsLink = existingOrder.getLogisticsLink();
|
||||
String trimmedDbLink = (dbLogisticsLink != null) ? dbLogisticsLink.trim() : "";
|
||||
|
||||
// 比对物流链接,如果不一致则需要更新
|
||||
if (!trimmedDbLink.isEmpty() && !trimmedExistingLink.equals(trimmedDbLink)) {
|
||||
// 物流链接不一致,需要更新文档中的链接
|
||||
log.info("========== 检测到物流链接不一致 - 单号: {}, 行号: {}, 文档链接: {}, 数据库链接: {}, 开始更新 ==========",
|
||||
orderNo, excelRow, trimmedExistingLink, trimmedDbLink);
|
||||
|
||||
// 添加到更新列表
|
||||
JSONObject update = new JSONObject();
|
||||
update.put("row", excelRow);
|
||||
update.put("column", logisticsLinkColumn);
|
||||
update.put("orderNo", orderNo);
|
||||
update.put("logisticsLink", trimmedDbLink);
|
||||
update.put("isLinkUpdated", true); // 标记为物流链接更新
|
||||
update.put("oldLogisticsLink", trimmedExistingLink); // 旧链接
|
||||
update.put("newLogisticsLink", trimmedDbLink); // 新链接
|
||||
updates.add(update);
|
||||
|
||||
filledCount++; // 统计为填充数量
|
||||
|
||||
// 记录物流链接更新日志
|
||||
Map<String, Object> updatedLog = new java.util.HashMap<>();
|
||||
updatedLog.put("orderNo", orderNo);
|
||||
updatedLog.put("row", excelRow);
|
||||
updatedLog.put("oldLogisticsLink", trimmedExistingLink);
|
||||
updatedLog.put("newLogisticsLink", trimmedDbLink);
|
||||
logisticsLinkUpdatedLogs.add(updatedLog);
|
||||
|
||||
log.info("========== 物流链接不一致,已加入更新队列 - 单号: {}, 行号: {}, 旧链接: {}, 新链接: {} ==========",
|
||||
orderNo, excelRow, trimmedExistingLink, trimmedDbLink);
|
||||
} else {
|
||||
log.warn("⚠️ 同步订单状态返回0 - 单号: {}, 行号: {}, 可能未更新",
|
||||
orderNo, excelRow);
|
||||
}
|
||||
// 物流链接一致或数据库中没有链接,只需同步订单状态
|
||||
if (existingOrder.getTencentDocPushed() == null || existingOrder.getTencentDocPushed() == 0) {
|
||||
// 订单未标记为已推送,但文档中已有值,同步状态
|
||||
existingOrder.setTencentDocPushed(1);
|
||||
existingOrder.setTencentDocPushTime(new java.util.Date());
|
||||
int syncResult = jdOrderService.updateJDOrder(existingOrder);
|
||||
if (syncResult > 0) {
|
||||
log.info("✓ 同步订单状态成功 - 单号: {}, 行号: {}, 原因: 文档中已有物流链接(可能手动填写)",
|
||||
orderNo, excelRow);
|
||||
} else {
|
||||
log.warn("⚠️ 同步订单状态返回0 - 单号: {}, 行号: {}, 可能未更新",
|
||||
orderNo, excelRow);
|
||||
}
|
||||
|
||||
// 记录同步日志
|
||||
logOperation(batchId, fileId, sheetId, "BATCH_SYNC", orderNo, excelRow, existingLogisticsLink,
|
||||
"SKIPPED", "文档中已有物流链接,已同步订单状态");
|
||||
// 记录同步日志
|
||||
logOperation(batchId, fileId, sheetId, "BATCH_SYNC", orderNo, excelRow, trimmedExistingLink,
|
||||
"SKIPPED", "文档中已有物流链接,已同步订单状态");
|
||||
}
|
||||
skippedCount++; // 物流链接一致,跳过写入
|
||||
}
|
||||
} else {
|
||||
// 未找到订单,跳过
|
||||
skippedCount++;
|
||||
}
|
||||
} catch (Exception e) {
|
||||
log.error("❌ 同步订单状态失败 - 单号: {}, 行号: {}", orderNo, excelRow, e);
|
||||
log.error("❌ 处理已有物流链接时发生异常 - 单号: {}, 行号: {}", orderNo, excelRow, e);
|
||||
skippedCount++;
|
||||
}
|
||||
|
||||
if (!trimmedExistingLink.isEmpty()) {
|
||||
// 如果已经处理过(无论是更新还是跳过),都继续下一个
|
||||
continue;
|
||||
}
|
||||
|
||||
skippedCount++; // 已有物流链接,跳过写入
|
||||
continue;
|
||||
}
|
||||
|
||||
try {
|
||||
@@ -1265,6 +1316,15 @@ public class TencentDocController extends BaseController {
|
||||
if (order == null) {
|
||||
errorCount++;
|
||||
log.warn("未找到订单 - 单号: {}, 行号: {}", orderNo, excelRow);
|
||||
|
||||
// 记录错误详情
|
||||
Map<String, Object> errorLog = new java.util.HashMap<>();
|
||||
errorLog.put("orderNo", orderNo);
|
||||
errorLog.put("row", excelRow);
|
||||
errorLog.put("errorType", "订单不存在");
|
||||
errorLog.put("errorMessage", "未找到订单");
|
||||
errorLogs.add(errorLog);
|
||||
|
||||
continue;
|
||||
}
|
||||
|
||||
@@ -1320,10 +1380,26 @@ public class TencentDocController extends BaseController {
|
||||
} else {
|
||||
errorCount++;
|
||||
log.warn("订单物流链接为空 - 单号: {}, 行号: {}", orderNo, excelRow);
|
||||
|
||||
// 记录错误详情
|
||||
Map<String, Object> errorLog = new java.util.HashMap<>();
|
||||
errorLog.put("orderNo", orderNo);
|
||||
errorLog.put("row", excelRow);
|
||||
errorLog.put("errorType", "物流链接为空");
|
||||
errorLog.put("errorMessage", "订单物流链接为空");
|
||||
errorLogs.add(errorLog);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
errorCount++;
|
||||
log.error("处理订单失败 - 单号: {}, 行号: {}", orderNo, excelRow, e);
|
||||
|
||||
// 记录错误详情
|
||||
Map<String, Object> errorLog = new java.util.HashMap<>();
|
||||
errorLog.put("orderNo", orderNo);
|
||||
errorLog.put("row", excelRow);
|
||||
errorLog.put("errorType", "处理订单异常");
|
||||
errorLog.put("errorMessage", e.getMessage() != null ? e.getMessage() : e.getClass().getSimpleName());
|
||||
errorLogs.add(errorLog);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1359,6 +1435,15 @@ public class TencentDocController extends BaseController {
|
||||
if (verifyData == null || !verifyData.containsKey("values")) {
|
||||
log.warn("验证失败 - 无法读取行 {}", row);
|
||||
errorCount++;
|
||||
|
||||
// 记录错误详情
|
||||
Map<String, Object> errorLog = new java.util.HashMap<>();
|
||||
errorLog.put("orderNo", expectedOrderNo);
|
||||
errorLog.put("row", row);
|
||||
errorLog.put("errorType", "验证失败");
|
||||
errorLog.put("errorMessage", "无法读取行数据");
|
||||
errorLogs.add(errorLog);
|
||||
|
||||
continue;
|
||||
}
|
||||
|
||||
@@ -1366,6 +1451,15 @@ public class TencentDocController extends BaseController {
|
||||
if (verifyRows == null || verifyRows.isEmpty()) {
|
||||
log.warn("验证失败 - 行 {} 数据为空", row);
|
||||
errorCount++;
|
||||
|
||||
// 记录错误详情
|
||||
Map<String, Object> errorLog = new java.util.HashMap<>();
|
||||
errorLog.put("orderNo", expectedOrderNo);
|
||||
errorLog.put("row", row);
|
||||
errorLog.put("errorType", "验证失败");
|
||||
errorLog.put("errorMessage", "行数据为空");
|
||||
errorLogs.add(errorLog);
|
||||
|
||||
continue;
|
||||
}
|
||||
|
||||
@@ -1373,6 +1467,15 @@ public class TencentDocController extends BaseController {
|
||||
if (verifyRow == null || verifyRow.size() <= Math.max(orderNoColumn, logisticsLinkColumn)) {
|
||||
log.warn("验证失败 - 行 {} 列数不足", row);
|
||||
errorCount++;
|
||||
|
||||
// 记录错误详情
|
||||
Map<String, Object> errorLog = new java.util.HashMap<>();
|
||||
errorLog.put("orderNo", expectedOrderNo);
|
||||
errorLog.put("row", row);
|
||||
errorLog.put("errorType", "验证失败");
|
||||
errorLog.put("errorMessage", "行列数不足");
|
||||
errorLogs.add(errorLog);
|
||||
|
||||
continue;
|
||||
}
|
||||
|
||||
@@ -1381,16 +1484,34 @@ public class TencentDocController extends BaseController {
|
||||
if (currentOrderNo == null || !currentOrderNo.trim().equals(expectedOrderNo)) {
|
||||
log.warn("验证失败 - 行 {} 单号不匹配,预期: {}, 实际: {}", row, expectedOrderNo, currentOrderNo);
|
||||
errorCount++;
|
||||
|
||||
// 记录错误详情
|
||||
Map<String, Object> errorLog = new java.util.HashMap<>();
|
||||
errorLog.put("orderNo", expectedOrderNo);
|
||||
errorLog.put("row", row);
|
||||
errorLog.put("errorType", "验证失败");
|
||||
errorLog.put("errorMessage", String.format("单号不匹配,预期: %s, 实际: %s", expectedOrderNo, currentOrderNo));
|
||||
errorLogs.add(errorLog);
|
||||
|
||||
continue;
|
||||
}
|
||||
|
||||
// 验证2:物流链接列是否为空
|
||||
// 验证2:物流链接列是否为空(如果是物流链接更新,则允许覆盖)
|
||||
Boolean isLinkUpdated = update.getBoolean("isLinkUpdated");
|
||||
String currentLogisticsLink = verifyRow.getString(logisticsLinkColumn);
|
||||
if (currentLogisticsLink != null && !currentLogisticsLink.trim().isEmpty()) {
|
||||
if (!Boolean.TRUE.equals(isLinkUpdated) && currentLogisticsLink != null && !currentLogisticsLink.trim().isEmpty()) {
|
||||
log.info("跳过写入 - 行 {} 单号 {} 的物流链接列已有值: {}", row, expectedOrderNo, currentLogisticsLink);
|
||||
skippedCount++;
|
||||
continue;
|
||||
}
|
||||
|
||||
// 如果是物流链接更新,记录日志
|
||||
if (Boolean.TRUE.equals(isLinkUpdated)) {
|
||||
String oldLink = update.getString("oldLogisticsLink");
|
||||
String newLink = update.getString("newLogisticsLink");
|
||||
log.info("========== 开始更新物流链接 - 行: {}, 单号: {}, 旧链接: {}, 新链接: {} ==========",
|
||||
row, expectedOrderNo, oldLink, newLink);
|
||||
}
|
||||
|
||||
// 验证通过,执行写入
|
||||
// 使用 batchUpdate 一次性更新多个字段
|
||||
@@ -1474,6 +1595,18 @@ public class TencentDocController extends BaseController {
|
||||
if (phone != null) {
|
||||
successLog.put("phone", phone);
|
||||
}
|
||||
|
||||
// 检查是否为物流链接更新(复用之前的变量)
|
||||
if (Boolean.TRUE.equals(isLinkUpdated)) {
|
||||
String oldLink = update.getString("oldLogisticsLink");
|
||||
String newLink = update.getString("newLogisticsLink");
|
||||
successLog.put("isLinkUpdated", true);
|
||||
successLog.put("oldLogisticsLink", oldLink);
|
||||
successLog.put("newLogisticsLink", newLink);
|
||||
log.info("✓ 记录物流链接更新到成功日志 - 单号: {}, 旧链接: {}, 新链接: {}",
|
||||
expectedOrderNo, oldLink, newLink);
|
||||
}
|
||||
|
||||
successLogs.add(successLog);
|
||||
} catch (Exception e) {
|
||||
log.error("写入数据失败 - 行: {}", entry.getKey(), e);
|
||||
@@ -1484,6 +1617,14 @@ public class TencentDocController extends BaseController {
|
||||
String logisticsLink = entry.getValue().getString("logisticsLink");
|
||||
logOperation(batchId, fileId, sheetId, "BATCH_SYNC", orderNo, entry.getKey(), logisticsLink,
|
||||
"FAILED", "写入异常: " + e.getMessage());
|
||||
|
||||
// 记录错误详情
|
||||
Map<String, Object> errorLog = new java.util.HashMap<>();
|
||||
errorLog.put("orderNo", orderNo);
|
||||
errorLog.put("row", entry.getKey());
|
||||
errorLog.put("errorType", "写入失败");
|
||||
errorLog.put("errorMessage", e.getMessage() != null ? e.getMessage() : e.getClass().getSimpleName());
|
||||
errorLogs.add(errorLog);
|
||||
}
|
||||
|
||||
// 添加延迟,避免API调用频率过高
|
||||
@@ -1598,11 +1739,11 @@ public class TencentDocController extends BaseController {
|
||||
log.warn("⚠️ batchId 为空,无法更新批量推送记录");
|
||||
}
|
||||
|
||||
// 如果有成功记录,发送微信推送
|
||||
if (!successLogs.isEmpty()) {
|
||||
// 如果有成功记录或错误记录,发送微信推送
|
||||
if (!successLogs.isEmpty() || !errorLogs.isEmpty()) {
|
||||
try {
|
||||
// 传递batchId给推送方法,用于日志关联
|
||||
sendWeChatNotificationWithBatchId(successLogs, filledCount, skippedCount, errorCount, batchId, fileId, sheetId, false);
|
||||
// 传递batchId、物流链接更新日志和错误日志给推送方法,用于日志关联和说明
|
||||
sendWeChatNotificationWithBatchId(successLogs, filledCount, skippedCount, errorCount, batchId, fileId, sheetId, false, logisticsLinkUpdatedLogs, errorLogs);
|
||||
} catch (Exception e) {
|
||||
log.error("发送微信推送失败", e);
|
||||
// 不影响主流程,继续返回成功
|
||||
@@ -2559,9 +2700,11 @@ public class TencentDocController extends BaseController {
|
||||
* @param fileId 文档ID
|
||||
* @param sheetId 工作表ID
|
||||
* @param isManual 是否为手动执行
|
||||
* @param logisticsLinkUpdatedLogs 物流链接更新日志列表(可为null)
|
||||
* @param errorLogs 错误日志列表(可为null)
|
||||
*/
|
||||
private void sendWeChatNotificationWithBatchId(List<Map<String, Object>> successLogs, int filledCount, int skippedCount, int errorCount, String batchId, String fileId, String sheetId, boolean isManual) {
|
||||
sendWeChatNotification(successLogs, filledCount, skippedCount, errorCount, batchId, fileId, sheetId, isManual);
|
||||
private void sendWeChatNotificationWithBatchId(List<Map<String, Object>> successLogs, int filledCount, int skippedCount, int errorCount, String batchId, String fileId, String sheetId, boolean isManual, List<Map<String, Object>> logisticsLinkUpdatedLogs, List<Map<String, Object>> errorLogs) {
|
||||
sendWeChatNotification(successLogs, filledCount, skippedCount, errorCount, batchId, fileId, sheetId, isManual, logisticsLinkUpdatedLogs, errorLogs);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -2584,7 +2727,7 @@ public class TencentDocController extends BaseController {
|
||||
if (sheetId == null || sheetId.isEmpty()) {
|
||||
sheetId = tencentDocConfig.getSheetId();
|
||||
}
|
||||
sendWeChatNotification(successLogs, filledCount, skippedCount, errorCount, null, fileId, sheetId, isManual);
|
||||
sendWeChatNotification(successLogs, filledCount, skippedCount, errorCount, null, fileId, sheetId, isManual, null, null);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -2598,8 +2741,10 @@ public class TencentDocController extends BaseController {
|
||||
* @param fileId 文档ID
|
||||
* @param sheetId 工作表ID
|
||||
* @param isManual 是否为手动执行
|
||||
* @param logisticsLinkUpdatedLogs 物流链接更新日志列表(可为null)
|
||||
* @param errorLogs 错误日志列表(可为null)
|
||||
*/
|
||||
private void sendWeChatNotification(List<Map<String, Object>> successLogs, int filledCount, int skippedCount, int errorCount, String batchId, String fileId, String sheetId, boolean isManual) {
|
||||
private void sendWeChatNotification(List<Map<String, Object>> successLogs, int filledCount, int skippedCount, int errorCount, String batchId, String fileId, String sheetId, boolean isManual, List<Map<String, Object>> logisticsLinkUpdatedLogs, List<Map<String, Object>> errorLogs) {
|
||||
try {
|
||||
log.info("========== 开始发送微信推送通知 ==========");
|
||||
log.info("推送类型: {}, 成功: {} 条, 跳过: {} 条, 错误: {} 条",
|
||||
@@ -2625,6 +2770,14 @@ public class TencentDocController extends BaseController {
|
||||
if (errorCount > 0) {
|
||||
content.append(String.format("✗ 错误: %d 条\n", errorCount));
|
||||
}
|
||||
|
||||
// 如果有物流链接更新,添加说明
|
||||
int logisticsLinkUpdatedCount = 0;
|
||||
if (logisticsLinkUpdatedLogs != null && !logisticsLinkUpdatedLogs.isEmpty()) {
|
||||
logisticsLinkUpdatedCount = logisticsLinkUpdatedLogs.size();
|
||||
content.append(String.format("【物流链接已更新】: %d 条\n", logisticsLinkUpdatedCount));
|
||||
}
|
||||
|
||||
content.append("\n");
|
||||
|
||||
if (!successLogs.isEmpty()) {
|
||||
@@ -2643,7 +2796,21 @@ public class TencentDocController extends BaseController {
|
||||
if (phone != null && !phone.isEmpty()) {
|
||||
content.append(String.format(" 电话: %s\n", phone));
|
||||
}
|
||||
content.append(String.format(" 物流: %s\n", logisticsLink));
|
||||
|
||||
// 检查是否为物流链接更新
|
||||
Boolean isLinkUpdated = (Boolean) log.get("isLinkUpdated");
|
||||
if (Boolean.TRUE.equals(isLinkUpdated)) {
|
||||
String oldLink = (String) log.get("oldLogisticsLink");
|
||||
String newLink = (String) log.get("newLogisticsLink");
|
||||
content.append(String.format(" 【物流链接已更新】\n"));
|
||||
content.append(String.format(" 新链接: %s\n", newLink));
|
||||
if (oldLink != null && !oldLink.isEmpty()) {
|
||||
content.append(String.format(" 旧链接: %s\n", oldLink));
|
||||
}
|
||||
} else {
|
||||
content.append(String.format(" 物流: %s\n", logisticsLink));
|
||||
}
|
||||
|
||||
if (i < maxDisplay - 1) {
|
||||
content.append("\n");
|
||||
}
|
||||
@@ -2653,6 +2820,38 @@ public class TencentDocController extends BaseController {
|
||||
content.append(String.format("\n... 还有 %d 条记录未显示", successLogs.size() - maxDisplay));
|
||||
}
|
||||
}
|
||||
|
||||
// 如果有错误记录,添加错误详情
|
||||
if (errorLogs != null && !errorLogs.isEmpty()) {
|
||||
content.append("\n【错误详情】\n");
|
||||
// 最多显示20条错误记录,避免消息过长
|
||||
int maxErrorDisplay = Math.min(20, errorLogs.size());
|
||||
for (int i = 0; i < maxErrorDisplay; i++) {
|
||||
Map<String, Object> errorLog = errorLogs.get(i);
|
||||
String orderNo = (String) errorLog.get("orderNo");
|
||||
Integer row = (Integer) errorLog.get("row");
|
||||
String errorType = (String) errorLog.get("errorType");
|
||||
String errorMessage = (String) errorLog.get("errorMessage");
|
||||
|
||||
content.append(String.format("%d. 单号: %s\n", i + 1, orderNo != null ? orderNo : "未知"));
|
||||
if (row != null) {
|
||||
content.append(String.format(" 行号: %d\n", row));
|
||||
}
|
||||
if (errorType != null && !errorType.isEmpty()) {
|
||||
content.append(String.format(" 错误类型: %s\n", errorType));
|
||||
}
|
||||
if (errorMessage != null && !errorMessage.isEmpty()) {
|
||||
content.append(String.format(" 错误信息: %s\n", errorMessage));
|
||||
}
|
||||
if (i < maxErrorDisplay - 1) {
|
||||
content.append("\n");
|
||||
}
|
||||
}
|
||||
|
||||
if (errorLogs.size() > maxErrorDisplay) {
|
||||
content.append(String.format("\n... 还有 %d 条错误记录未显示", errorLogs.size() - maxErrorDisplay));
|
||||
}
|
||||
}
|
||||
|
||||
// 构建请求体
|
||||
JSONObject requestBody = new JSONObject();
|
||||
|
||||
@@ -5,6 +5,7 @@ import com.alibaba.fastjson2.JSONObject;
|
||||
import com.ruoyi.common.utils.http.HttpUtils;
|
||||
import com.ruoyi.jarvis.domain.JDOrder;
|
||||
import com.ruoyi.jarvis.service.ILogisticsService;
|
||||
import com.ruoyi.jarvis.service.IJDOrderService;
|
||||
import com.ruoyi.system.service.ISysConfigService;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
@@ -54,6 +55,9 @@ public class LogisticsServiceImpl implements ILogisticsService {
|
||||
@Resource
|
||||
private ISysConfigService sysConfigService;
|
||||
|
||||
@Resource
|
||||
private IJDOrderService jdOrderService;
|
||||
|
||||
@PostConstruct
|
||||
public void init() {
|
||||
externalApiUrlTemplate = logisticsBaseUrl + logisticsFetchPath + "?tracking_url=";
|
||||
@@ -276,6 +280,67 @@ public class LogisticsServiceImpl implements ILogisticsService {
|
||||
|
||||
logger.info("检测到waybill_no: {} - 订单ID: {}", waybillNo, orderId);
|
||||
|
||||
// 标记物流链接是否更新
|
||||
boolean logisticsLinkUpdated = false;
|
||||
String oldLogisticsLink = null;
|
||||
String newLogisticsLink = null;
|
||||
|
||||
// 检查并更新物流链接(如果返回的数据中包含新的物流链接)
|
||||
// 尝试多种可能的字段名
|
||||
if (dataObj.containsKey("tracking_url")) {
|
||||
newLogisticsLink = dataObj.getString("tracking_url");
|
||||
} else if (dataObj.containsKey("logistics_link")) {
|
||||
newLogisticsLink = dataObj.getString("logistics_link");
|
||||
} else if (dataObj.containsKey("logisticsLink")) {
|
||||
newLogisticsLink = dataObj.getString("logisticsLink");
|
||||
} else if (dataObj.containsKey("trackingUrl")) {
|
||||
newLogisticsLink = dataObj.getString("trackingUrl");
|
||||
}
|
||||
|
||||
// 如果获取到新的物流链接,与数据库中的进行比对
|
||||
if (newLogisticsLink != null && !newLogisticsLink.trim().isEmpty()) {
|
||||
String currentLogisticsLink = order.getLogisticsLink();
|
||||
String trimmedNewLink = newLogisticsLink.trim();
|
||||
String trimmedCurrentLink = (currentLogisticsLink != null) ? currentLogisticsLink.trim() : "";
|
||||
oldLogisticsLink = trimmedCurrentLink;
|
||||
|
||||
// 比对物流链接,如果不一致则更新数据库
|
||||
if (!trimmedNewLink.equals(trimmedCurrentLink)) {
|
||||
logger.info("========== 检测到物流链接发生变化 - 订单ID: {}, 订单号: {}, 旧链接: {}, 新链接: {}, 开始更新数据库 ==========",
|
||||
orderId, order.getOrderId(), trimmedCurrentLink, trimmedNewLink);
|
||||
|
||||
try {
|
||||
// 重新查询订单,确保获取最新数据
|
||||
JDOrder updateOrder = jdOrderService.selectJDOrderById(orderId);
|
||||
if (updateOrder != null) {
|
||||
updateOrder.setLogisticsLink(trimmedNewLink);
|
||||
int updateResult = jdOrderService.updateJDOrder(updateOrder);
|
||||
if (updateResult > 0) {
|
||||
logisticsLinkUpdated = true;
|
||||
logger.info("========== 物流链接更新成功 - 订单ID: {}, 订单号: {}, 旧链接: {}, 新链接: {} ==========",
|
||||
orderId, order.getOrderId(), trimmedCurrentLink, trimmedNewLink);
|
||||
// 更新内存中的order对象,以便后续使用最新数据
|
||||
order.setLogisticsLink(trimmedNewLink);
|
||||
} else {
|
||||
logger.warn("物流链接更新失败(影响行数为0) - 订单ID: {}, 订单号: {}, 新链接: {}",
|
||||
orderId, order.getOrderId(), trimmedNewLink);
|
||||
}
|
||||
} else {
|
||||
logger.warn("未找到订单,无法更新物流链接 - 订单ID: {}, 订单号: {}", orderId, order.getOrderId());
|
||||
}
|
||||
} catch (Exception e) {
|
||||
logger.error("更新物流链接时发生异常 - 订单ID: {}, 订单号: {}, 新链接: {}, 错误: {}",
|
||||
orderId, order.getOrderId(), trimmedNewLink, e.getMessage(), e);
|
||||
// 不中断主流程,继续处理
|
||||
}
|
||||
} else {
|
||||
logger.debug("物流链接未变化,无需更新 - 订单ID: {}, 订单号: {}, 链接: {}",
|
||||
orderId, order.getOrderId(), trimmedCurrentLink);
|
||||
}
|
||||
} else {
|
||||
logger.debug("返回数据中未包含物流链接字段 - 订单ID: {}, 订单号: {}", orderId, order.getOrderId());
|
||||
}
|
||||
|
||||
// 兼容处理:检查Redis中是否已有该订单的运单号记录
|
||||
// 如果存在且运单号一致,说明之前已经推送过了(可能是之前没有配置接收人但推送成功的情况)
|
||||
String redisKey = REDIS_WAYBILL_KEY_PREFIX + orderId;
|
||||
@@ -314,7 +379,7 @@ public class LogisticsServiceImpl implements ILogisticsService {
|
||||
}
|
||||
|
||||
// 调用企业应用推送,只有推送成功才记录状态
|
||||
boolean pushSuccess = sendEnterprisePushNotification(order, waybillNo);
|
||||
boolean pushSuccess = sendEnterprisePushNotification(order, waybillNo, logisticsLinkUpdated, oldLogisticsLink, newLogisticsLink);
|
||||
if (!pushSuccess) {
|
||||
logger.warn("企业微信推送未确认成功,稍后将重试 - 订单ID: {}, waybill_no: {}", orderId, waybillNo);
|
||||
return false;
|
||||
@@ -330,7 +395,14 @@ public class LogisticsServiceImpl implements ILogisticsService {
|
||||
stringRedisTemplate.opsForValue().set(redisKey, waybillNo, 30, TimeUnit.DAYS);
|
||||
}
|
||||
|
||||
logger.info("物流信息获取并推送成功 - 订单ID: {}, waybill_no: {}", orderId, waybillNo);
|
||||
// 记录最终处理结果
|
||||
if (logisticsLinkUpdated) {
|
||||
logger.info("========== 物流信息获取并推送成功(已更新物流链接) - 订单ID: {}, 订单号: {}, waybill_no: {}, 新链接: {} ==========",
|
||||
orderId, order.getOrderId(), waybillNo, newLogisticsLink);
|
||||
} else {
|
||||
logger.info("物流信息获取并推送成功 - 订单ID: {}, 订单号: {}, waybill_no: {}",
|
||||
orderId, order.getOrderId(), waybillNo);
|
||||
}
|
||||
return true;
|
||||
|
||||
} catch (Exception e) {
|
||||
@@ -351,8 +423,11 @@ public class LogisticsServiceImpl implements ILogisticsService {
|
||||
* 调用企业应用推送逻辑
|
||||
* @param order 订单信息
|
||||
* @param waybillNo 运单号
|
||||
* @param logisticsLinkUpdated 物流链接是否已更新
|
||||
* @param oldLogisticsLink 旧的物流链接(如果更新了)
|
||||
* @param newLogisticsLink 新的物流链接(如果更新了)
|
||||
*/
|
||||
private boolean sendEnterprisePushNotification(JDOrder order, String waybillNo) {
|
||||
private boolean sendEnterprisePushNotification(JDOrder order, String waybillNo, boolean logisticsLinkUpdated, String oldLogisticsLink, String newLogisticsLink) {
|
||||
try {
|
||||
// 构建推送消息内容
|
||||
StringBuilder pushContent = new StringBuilder();
|
||||
@@ -370,6 +445,17 @@ public class LogisticsServiceImpl implements ILogisticsService {
|
||||
pushContent.append("型号:").append(order.getModelNumber() != null ? order.getModelNumber() : "无").append("\n");
|
||||
// 收货地址
|
||||
pushContent.append("收货地址:").append(order.getAddress() != null ? order.getAddress() : "无").append("\n");
|
||||
|
||||
// 如果物流链接已更新,在推送消息中说明
|
||||
if (logisticsLinkUpdated && newLogisticsLink != null && !newLogisticsLink.trim().isEmpty()) {
|
||||
pushContent.append("【物流链接已更新】").append("\n");
|
||||
pushContent.append("新物流链接:").append(newLogisticsLink.trim()).append("\n");
|
||||
if (oldLogisticsLink != null && !oldLogisticsLink.trim().isEmpty()) {
|
||||
pushContent.append("旧物流链接:").append(oldLogisticsLink.trim()).append("\n");
|
||||
}
|
||||
pushContent.append("\n");
|
||||
}
|
||||
|
||||
// 运单号
|
||||
pushContent.append("运单号:").append("\n").append("\n").append("\n").append("\n").append(waybillNo).append("\n");
|
||||
|
||||
@@ -393,7 +479,13 @@ public class LogisticsServiceImpl implements ILogisticsService {
|
||||
|
||||
// 记录完整的推送参数(用于调试)
|
||||
String jsonBody = pushParam.toJSONString();
|
||||
logger.info("企业微信推送完整参数 - 订单ID: {}, JSON: {}", order.getId(), jsonBody);
|
||||
if (logisticsLinkUpdated) {
|
||||
logger.info("企业微信推送完整参数(已更新物流链接) - 订单ID: {}, 订单号: {}, 旧链接: {}, 新链接: {}, JSON: {}",
|
||||
order.getId(), order.getOrderId(), oldLogisticsLink, newLogisticsLink, jsonBody);
|
||||
} else {
|
||||
logger.info("企业微信推送完整参数 - 订单ID: {}, 订单号: {}, JSON: {}",
|
||||
order.getId(), order.getOrderId(), jsonBody);
|
||||
}
|
||||
|
||||
// 使用支持自定义header的HTTP请求
|
||||
String pushResult = sendPostWithHeaders(PUSH_URL, jsonBody, PUSH_TOKEN);
|
||||
@@ -404,11 +496,16 @@ public class LogisticsServiceImpl implements ILogisticsService {
|
||||
|
||||
boolean success = isPushResponseSuccess(pushResult);
|
||||
if (success) {
|
||||
logger.info("企业应用推送成功 - 订单ID: {}, waybill_no: {}, 推送结果: {}",
|
||||
order.getId(), waybillNo, pushResult);
|
||||
if (logisticsLinkUpdated) {
|
||||
logger.info("企业应用推送成功(已更新物流链接) - 订单ID: {}, 订单号: {}, waybill_no: {}, 新链接: {}, 推送结果: {}",
|
||||
order.getId(), order.getOrderId(), waybillNo, newLogisticsLink, pushResult);
|
||||
} else {
|
||||
logger.info("企业应用推送成功 - 订单ID: {}, 订单号: {}, waybill_no: {}, 推送结果: {}",
|
||||
order.getId(), order.getOrderId(), waybillNo, pushResult);
|
||||
}
|
||||
} else {
|
||||
logger.warn("企业应用推送响应未确认成功 - 订单ID: {}, waybill_no: {}, 响应: {}",
|
||||
order.getId(), waybillNo, pushResult);
|
||||
logger.warn("企业应用推送响应未确认成功 - 订单ID: {}, 订单号: {}, waybill_no: {}, 响应: {}",
|
||||
order.getId(), order.getOrderId(), waybillNo, pushResult);
|
||||
}
|
||||
return success;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user