| | |
| | | @Service |
| | | public class KafkaConsumerService { |
| | | private static final String REASSIGN_CANCEL_CONTEXT_PREFIX = "REASSIGN_CANCEL_CONTEXT:"; |
| | | private static final String REASSIGN_CANCEL_CONTEXT_PREFIX = "REASSIGN_CANCEL_CONTEXT:"; |
| | | private static final String CANCEL_ORDER_CONTEXT_PREFIX = "CANCEL_ORDER_CONTEXT:"; |
| | | |
| | | |
| | |
| | | log.info("改派旧车取消回调,跳过订单取消处理,orderId: {}, taskId: {}, cancelScene: {}, context: {}", |
| | | orderId, taskId, reassignCancelContext.getString("cancelScene"), reassignCancelContext); |
| | | //响应消息 |
| | | String responseVin = resolveReassignCancelResponseVin(reassignCancelContext); |
| | | String responseVin = resolveReassignCancelResponseVin(orderId, reassignCancelContext); |
| | | if (StringUtils.isNotEmpty(responseVin)) { |
| | | responseMessage(cmd, sequence, responseVin); |
| | | } else { |
| | |
| | | } |
| | | |
| | | /** |
| | | * 兼容按订单维度缓存的改派取消上下文。 |
| | | * 该键在历史实现中使用,保留读取能力避免线上旧数据失效。 |
| | | */ |
| | | private JSONObject getReassignCancelContext(String orderId) { |
| | | if (StringUtils.isEmpty(orderId)) { |
| | | return null; |
| | | } |
| | | Object cacheObject = redisService.getCacheObject(REASSIGN_CANCEL_CONTEXT_PREFIX + orderId); |
| | | if (cacheObject == null) { |
| | | return null; |
| | | } |
| | | return JSON.parseObject(cacheObject.toString()); |
| | | } |
| | | |
| | | /** |
| | | * 当前 cancelOrder 指令是按 taskId 记录取消上下文。 |
| | | * 改派旧车取消回调优先读取该上下文,避免依赖订单当前车/状态造成竞态误判。 |
| | | */ |
| | |
| | | return cancelContext; |
| | | } |
| | | |
| | | private String resolveReassignCancelResponseVin(JSONObject cancelContext) { |
| | | private String resolveReassignCancelResponseVin(String orderId, JSONObject cancelContext) { |
| | | if (cancelContext == null) { |
| | | return null; |
| | | } |
| | | String vin = cancelContext.getString("vin"); |
| | | if (StringUtils.isNotEmpty(vin)) { |
| | | return vin; |
| | | return null; |
| | | } |
| | | if (CancelOrderSceneEnum.REASSIGN_OLD_VEHICLE != CancelOrderSceneEnum.fromCode( |
| | | cancelContext.getString("cancelScene"))) { |
| | |
| | | boolean vehicleMatched = Objects.nonNull(newVehicleId) && Objects.equals(newVehicleId, xpOrder.getVehicleId()); |
| | | boolean statusMatched = Objects.nonNull(currentStatus) && currentStatus > 0 && currentStatus < 8; |
| | | if (vehicleMatched && statusMatched) { |
| | | return cancelContext; |
| | | return newVehicleId.toString(); |
| | | } |
| | | String oldVehicleVin = cancelContext.getString("oldVehicleVin"); |
| | | if (StringUtils.isNotEmpty(oldVehicleVin)) { |
| | |
| | | } |
| | | R<XpVehicle> vehicleResult = xpVehicleClient.queryById(oldVehicleId); |
| | | XpVehicle xpVehicle = vehicleResult == null ? null : vehicleResult.getData(); |
| | | return xpVehicle == null ? null : xpVehicle.getVehicleVin(); |
| | | log.info("改派取消回调未命中专用上下文,orderId: {}, currentVehicleId: {}, newVehicleId: {}, currentStatus: {}", |
| | | orderId, xpOrder.getVehicleId(), newVehicleId, currentStatus); |
| | | return null; |
| | | } |
| | | |
| | | private String resolveReassignCancelResponseVin(JSONObject cancelContext) { |
| | | if (cancelContext == null) { |
| | | return null; |
| | | } |
| | | String oldVehicleVin = cancelContext.getString("oldVehicleVin"); |
| | | if (StringUtils.isNotEmpty(oldVehicleVin)) { |
| | | return oldVehicleVin; |
| | | } |
| | | Long oldVehicleId = cancelContext.getLong("oldVehicleId"); |
| | | if (Objects.isNull(oldVehicleId)) { |
| | | return null; |
| | | } |
| | | R<XpVehicle> vehicleResult = xpVehicleClient.queryById(oldVehicleId); |
| | | XpVehicle xpVehicle = vehicleResult == null ? null : vehicleResult.getData(); |
| | | return xpVehicle == null ? null : xpVehicle.getVehicleVin(); |
| | | } |
| | | |