| | |
| | | @Service |
| | | public class KafkaConsumerService { |
| | | private static final String REASSIGN_CANCEL_CONTEXT_PREFIX = "REASSIGN_CANCEL_CONTEXT:"; |
| | | private static final String CANCEL_ORDER_CONTEXT_PREFIX = "CANCEL_ORDER_CONTEXT:"; |
| | | |
| | | |
| | | @Resource |
| | |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 兼容按订单维度缓存的改派取消上下文。 |
| | | * 该键在历史实现中使用,保留读取能力避免线上旧数据失效。 |
| | | */ |
| | | private JSONObject getReassignCancelContext(String orderId) { |
| | | if (StringUtils.isEmpty(orderId)) { |
| | | return null; |
| | |
| | | return JSON.parseObject(cacheObject.toString()); |
| | | } |
| | | |
| | | /** |
| | | * 当前 cancelOrder 指令是按 taskId 记录取消上下文。 |
| | | * 改派旧车取消回调优先读取该上下文,避免依赖订单当前车/状态造成竞态误判。 |
| | | */ |
| | | private JSONObject getCancelOrderContext(String taskId) { |
| | | if (StringUtils.isEmpty(taskId)) { |
| | | return null; |
| | | } |
| | | Object cacheObject = redisService.getCacheObject(CANCEL_ORDER_CONTEXT_PREFIX + taskId); |
| | | if (cacheObject == null) { |
| | | return null; |
| | | } |
| | | return JSON.parseObject(cacheObject.toString()); |
| | | } |
| | | |
| | | private JSONObject resolveReassignCancelContext(String orderId, OrderStatusInfo orderStatusInfo) { |
| | | if (OrderStateChangeSource.ORDER_STATE_CHANGE_SOURCE_VEHICLE != orderStatusInfo.getChangeSource()) { |
| | | return null; |
| | | } |
| | | JSONObject cancelContext = getReassignCancelContext(orderId); |
| | | String taskId = orderStatusInfo.getTaskId(); |
| | | // 先按 taskId 读取当前生效的取消上下文,再兼容历史按 orderId 存储的改派上下文。 |
| | | JSONObject cancelContext = getCancelOrderContext(taskId); |
| | | if (cancelContext == null) { |
| | | cancelContext = getReassignCancelContext(orderId); |
| | | } |
| | | if (cancelContext == null) { |
| | | log.info("改派取消回调未命中专用上下文,orderId: {}, taskId: {}, missReason: context_not_found", |
| | | orderId, taskId); |
| | | return null; |
| | | } |
| | | if (CancelOrderSceneEnum.REASSIGN_OLD_VEHICLE != CancelOrderSceneEnum.fromCode( |
| | | cancelContext.getString("cancelScene"))) { |
| | | String cancelScene = cancelContext.getString("cancelScene"); |
| | | if (CancelOrderSceneEnum.REASSIGN_OLD_VEHICLE != CancelOrderSceneEnum.fromCode(cancelScene)) { |
| | | log.info("改派取消回调未命中专用上下文,orderId: {}, taskId: {}, missReason: scene_not_match, cancelScene: {}, context: {}", |
| | | orderId, taskId, cancelScene, cancelContext); |
| | | return null; |
| | | } |
| | | R<XpOrder> orderResult = xpOrderClient.queryOrderById(Long.parseLong(orderId)); |
| | | XpOrder xpOrder = orderResult == null ? null : orderResult.getData(); |
| | | if (xpOrder == null) { |
| | | log.warn("改派取消回调识别失败,订单不存在,orderId: {}, context: {}", orderId, cancelContext); |
| | | String contextOrderId = cancelContext.getString("orderId"); |
| | | if (StringUtils.isNotEmpty(contextOrderId) && !Objects.equals(contextOrderId, orderId)) { |
| | | log.info("改派取消回调未命中专用上下文,orderId: {}, taskId: {}, missReason: order_not_match, contextOrderId: {}, context: {}", |
| | | orderId, taskId, contextOrderId, cancelContext); |
| | | return null; |
| | | } |
| | | Long newVehicleId = cancelContext.getLong("newVehicleId"); |
| | | Integer currentStatus = xpOrder.getStatus(); |
| | | boolean vehicleMatched = Objects.nonNull(newVehicleId) && Objects.equals(newVehicleId, xpOrder.getVehicleId()); |
| | | boolean statusMatched = Objects.nonNull(currentStatus) && currentStatus > 0 && currentStatus < 8; |
| | | if (vehicleMatched && statusMatched) { |
| | | return cancelContext; |
| | | } |
| | | log.info("改派取消回调未命中专用上下文,orderId: {}, currentVehicleId: {}, newVehicleId: {}, currentStatus: {}", |
| | | orderId, xpOrder.getVehicleId(), newVehicleId, currentStatus); |
| | | return null; |
| | | return cancelContext; |
| | | } |
| | | |
| | | private String resolveReassignCancelResponseVin(JSONObject cancelContext) { |
| | | if (cancelContext == null) { |
| | | return null; |
| | | } |
| | | String vin = cancelContext.getString("vin"); |
| | | if (StringUtils.isNotEmpty(vin)) { |
| | | return vin; |
| | | } |
| | | String oldVehicleVin = cancelContext.getString("oldVehicleVin"); |
| | | if (StringUtils.isNotEmpty(oldVehicleVin)) { |
| | | return oldVehicleVin; |