| | |
| | | @Slf4j |
| | | @Service |
| | | public class KafkaConsumerService { |
| | | private static final String CANCEL_ORDER_CONTEXT_PREFIX = "CANCEL_ORDER_CONTEXT:"; |
| | | private static final String REASSIGN_CANCEL_CONTEXT_PREFIX = "REASSIGN_CANCEL_CONTEXT:"; |
| | | |
| | | |
| | | @Resource |
| | |
| | | //已取消 |
| | | if(!orderId.contains("XU")){ |
| | | String taskId = orderStatusInfo.getTaskId(); |
| | | JSONObject cancelContext = getCancelOrderContext(taskId); |
| | | if (shouldSkipCancelForReassign(cancelContext)) { |
| | | JSONObject reassignCancelContext = resolveReassignCancelContext(orderId, orderStatusInfo); |
| | | if (Objects.nonNull(reassignCancelContext)) { |
| | | log.info("改派旧车取消回调,跳过订单取消处理,orderId: {}, taskId: {}, cancelScene: {}, context: {}", |
| | | orderId, taskId, cancelContext.getString("cancelScene"), cancelContext); |
| | | orderId, taskId, reassignCancelContext.getString("cancelScene"), reassignCancelContext); |
| | | //响应消息 |
| | | responseMessage(sequence, cancelContext.getString("vin")); |
| | | String responseVin = resolveReassignCancelResponseVin(reassignCancelContext); |
| | | if (StringUtils.isNotEmpty(responseVin)) { |
| | | responseMessage(sequence, responseVin); |
| | | } else { |
| | | log.warn("改派旧车取消回调未找到响应VIN,orderId: {}, taskId: {}, context: {}", |
| | | orderId, taskId, reassignCancelContext); |
| | | } |
| | | return; |
| | | } |
| | | if(orderId.contains("TEST")){ |
| | |
| | | //行程中取消订单,按照行程结束处理 |
| | | buildPushOrderStatus(orderId, OrderStatusEnum.SEE_OFF_GUESTS, sequence); |
| | | xpOrderClient.updateOrderStatus(Long.parseLong(orderId), OrderStatusEnum.SEE_OFF_GUESTS.getCode()); |
| | | redisService.setCacheObject("ORDER_STATUS:" + orderId.substring(4), OrderStatusEnum.SEE_OFF_GUESTS.getCode(), 24L, TimeUnit.HOURS); |
| | | redisService.setCacheObject("ORDER_STATUS:" + orderId, OrderStatusEnum.SEE_OFF_GUESTS.getCode(), 24L, TimeUnit.HOURS); |
| | | }else{ |
| | | buildPushOrderStatus(orderId, OrderStatusEnum.CANCELED, sequence); |
| | | xpOrderClient.updateOrderStatus(Long.parseLong(orderId), OrderStatusEnum.CANCELED.getCode()); |
| | | redisService.setCacheObject("ORDER_STATUS:" + orderId.substring(4), OrderStatusEnum.CANCELED.getCode(), 24L, TimeUnit.HOURS); |
| | | redisService.setCacheObject("ORDER_STATUS:" + orderId, OrderStatusEnum.CANCELED.getCode(), 24L, TimeUnit.HOURS); |
| | | } |
| | | } |
| | | } |
| | |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 根据taskId获取订单取消的上下文信息 |
| | | * @param taskId |
| | | * @return |
| | | */ |
| | | private JSONObject getCancelOrderContext(String taskId) { |
| | | if (StringUtils.isEmpty(taskId)) { |
| | | private JSONObject getReassignCancelContext(String orderId) { |
| | | if (StringUtils.isEmpty(orderId)) { |
| | | return null; |
| | | } |
| | | Object cacheObject = redisService.getCacheObject(CANCEL_ORDER_CONTEXT_PREFIX + taskId); |
| | | Object cacheObject = redisService.getCacheObject(REASSIGN_CANCEL_CONTEXT_PREFIX + orderId); |
| | | if (cacheObject == null) { |
| | | return null; |
| | | } |
| | | return JSON.parseObject(cacheObject.toString()); |
| | | } |
| | | |
| | | /** |
| | | * 判断是否是改派旧车取消回调,是则跳过订单取消处理 |
| | | * @param cancelContext |
| | | * @return |
| | | */ |
| | | private boolean shouldSkipCancelForReassign(JSONObject cancelContext) { |
| | | if (cancelContext == null) { |
| | | return false; |
| | | private JSONObject resolveReassignCancelContext(String orderId, OrderStatusInfo orderStatusInfo) { |
| | | if (OrderStateChangeSource.ORDER_STATE_CHANGE_SOURCE_VEHICLE != orderStatusInfo.getChangeSource()) { |
| | | return null; |
| | | } |
| | | return CancelOrderSceneEnum.REASSIGN_OLD_VEHICLE == CancelOrderSceneEnum.fromCode( |
| | | cancelContext.getString("cancelScene")); |
| | | JSONObject cancelContext = getReassignCancelContext(orderId); |
| | | if (cancelContext == null) { |
| | | return null; |
| | | } |
| | | if (CancelOrderSceneEnum.REASSIGN_OLD_VEHICLE != CancelOrderSceneEnum.fromCode( |
| | | cancelContext.getString("cancelScene"))) { |
| | | 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); |
| | | 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; |
| | | } |
| | | |
| | | 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(); |
| | | } |
| | | |
| | | /** |