This commit is contained in:
2026-09-02 10:26:40 +08:00
parent 5f8b7baa06
commit 371aa3dbcb
3 changed files with 63 additions and 3 deletions
@@ -18,6 +18,11 @@ public interface FirstlegPricingClient {
@RequestHeader("X-Internal-Service-Token") String token,
@RequestBody OrderChannelChangeRequest request);
@PostMapping("/order-channel-changes/apply")
ApiResponse<OrderChannelChangeResponse> applyOrderChannelChange(
@RequestHeader("X-Internal-Service-Token") String token,
@RequestBody OrderChannelChangeRequest request);
record ChannelValidationRequest(long tenantId, long originWarehouseId, String channelCode,
String destinationType, boolean requireInboundCodePrefix) {}
record ChannelValidationResponse(String channelCode, String inboundCodePrefix) {}
@@ -4,6 +4,8 @@ import jakarta.annotation.Resource;
import com.baiyue.common.core.api.ApiResponse;
import com.baiyue.order.client.FirstlegInboundClient;
import com.baiyue.order.client.FirstlegPricingClient;
import com.baiyue.order.entity.OrderEntity;
import com.baiyue.order.entity.OutboxEventEntity;
import com.baiyue.order.dto.OrderDeclarationDtos;
import com.baiyue.order.repository.OrderRepository;
@@ -21,6 +23,7 @@ import java.time.LocalDateTime;
public class OrderInboundOutboxPublisher {
@Resource private OrderRepository repository;
@Resource private FirstlegInboundClient client;
@Resource private FirstlegPricingClient pricingClient;
@Resource private ObjectMapper json;
@Value("${baiyue.security.internal-service-token}") private String internalToken;
@@ -91,6 +94,25 @@ public class OrderInboundOutboxPublisher {
failPermanently(event, "declaration event ownership does not match payload");
return;
}
if (containsChannelChange(command)) {
OrderEntity order = repository.findOwned(event.getTenantId(), event.getAggregateId());
if (order == null) {
failPermanently(event, "order snapshot is unavailable for channel change");
return;
}
var pricingCommand = new FirstlegPricingClient.OrderChannelChangeRequest(
order.getTenantId(), order.getId(), command.version(), order.getReceivingWarehouseId(),
command.channelCode(), command.channelName(), order.getDestinationType(),
order.getDestinationWarehouseId(), order.getAmazonWarehouseCode(), order.getPricingTaxMode());
ApiResponse<FirstlegPricingClient.OrderChannelChangeResponse> pricingResponse =
pricingClient.applyOrderChannelChange(internalToken, pricingCommand);
if (pricingResponse == null || !pricingResponse.successful() || pricingResponse.data() == null
|| pricingResponse.data().orderId() != order.getId()
|| pricingResponse.data().orderVersion() != command.version()
|| !command.channelCode().equals(pricingResponse.data().channelCode())) {
throw new IllegalStateException("头程渠道重新计价同步失败");
}
}
ApiResponse<Void> response = client.updateDeclaration(internalToken, command);
if (response == null || !response.successful()) throw new IllegalStateException("头程申报同步失败");
repository.markOutboxPublished(event.getTenantId(), event.getId(), LocalDateTime.now());
@@ -101,6 +123,11 @@ public class OrderInboundOutboxPublisher {
}
}
private boolean containsChannelChange(OrderDeclarationDtos.UpdatedEvent command) {
return command.changes() != null && command.changes().stream()
.anyMatch(change -> "channelCode".equals(change.field()) || "channelName".equals(change.field()));
}
private boolean incomplete(FirstlegInboundClient.OrderApprovedInboundCommand command) {
return command.orderNo() == null || command.orderNo().isBlank()
|| command.inboundCode() == null || command.inboundCode().isBlank()