This commit is contained in:
Leo
2026-01-17 13:23:32 +08:00
parent 3336eeb6aa
commit 6257d816e9
2 changed files with 337 additions and 41 deletions

View File

@@ -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;