fix(firstleg): retain declaration event idempotency history
This commit is contained in:
@@ -48,9 +48,16 @@ public interface OrderDeclarationSyncMapper {
|
||||
SnapshotRow lockSnapshot(@Param("tenantId") long tenantId, @Param("orderId") long orderId);
|
||||
|
||||
@Select("SELECT tenant_id tenantId,order_id orderId,event_id eventId,order_version orderVersion,request_hash requestHash "
|
||||
+ "FROM fl_order_declaration_snapshot WHERE tenant_id=#{tenantId} AND event_id=#{eventId}")
|
||||
+ "FROM fl_order_declaration_event WHERE tenant_id=#{tenantId} AND event_id=#{eventId}")
|
||||
SnapshotRow byEvent(@Param("tenantId") long tenantId, @Param("eventId") String eventId);
|
||||
|
||||
@Insert("INSERT INTO fl_order_declaration_event "
|
||||
+ "(tenant_id,event_id,order_id,order_version,request_hash,consumed_at) VALUES "
|
||||
+ "(#{tenantId},#{eventId},#{orderId},#{orderVersion},#{requestHash},CURRENT_TIMESTAMP(3))")
|
||||
int insertConsumedEvent(@Param("tenantId") long tenantId, @Param("eventId") String eventId,
|
||||
@Param("orderId") long orderId, @Param("orderVersion") int orderVersion,
|
||||
@Param("requestHash") String requestHash);
|
||||
|
||||
@Insert("""
|
||||
INSERT INTO fl_order_declaration_snapshot
|
||||
(tenant_id,order_id,event_id,order_version,request_hash,customer_remark,expected_arrival_at,
|
||||
|
||||
+20
-3
@@ -12,6 +12,7 @@ import jakarta.annotation.Resource;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
import org.springframework.util.StringUtils;
|
||||
import org.springframework.dao.DuplicateKeyException;
|
||||
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.security.MessageDigest;
|
||||
@@ -62,12 +63,16 @@ public class OrderDeclarationSyncService {
|
||||
return;
|
||||
}
|
||||
var current = mapper.lockSnapshot(event.tenantId(), event.orderId());
|
||||
if (current != null && event.version() < current.orderVersion()) return;
|
||||
if (current != null && event.version() < current.orderVersion()) {
|
||||
recordEvent(event, hash);
|
||||
return;
|
||||
}
|
||||
if (current != null && event.version() == current.orderVersion()) {
|
||||
throw conflict("同一订单版本已存在不同申报事件");
|
||||
}
|
||||
String cargoesJson = write(event.cargoes());
|
||||
String changesJson = write(event.changes());
|
||||
recordEvent(event, hash);
|
||||
int saved = current == null
|
||||
? mapper.insertSnapshot(event.tenantId(), event.orderId(), event.eventId(), event.version(), hash,
|
||||
event.customerRemark(), event.expectedArrivalAt(), normalizeChannel(event.channelCode()),
|
||||
@@ -83,6 +88,15 @@ public class OrderDeclarationSyncService {
|
||||
}
|
||||
}
|
||||
|
||||
private void recordEvent(UpdatedEvent event, String hash) {
|
||||
try {
|
||||
if (mapper.insertConsumedEvent(event.tenantId(), event.eventId(), event.orderId(),
|
||||
event.version(), hash) != 1) throw conflict("订单申报事件幂等记录未保存");
|
||||
} catch (DuplicateKeyException duplicate) {
|
||||
throw conflict("eventId已用于其他申报事件");
|
||||
}
|
||||
}
|
||||
|
||||
private static void requireValidationRequest(ChannelChangeValidationRequest request) {
|
||||
if (request == null || request.tenantId() <= 0 || request.orderId() <= 0 || request.orderVersion() < 0
|
||||
|| !StringUtils.hasText(request.channelCode()) || request.channelCode().length() > 64) {
|
||||
@@ -95,8 +109,11 @@ public class OrderDeclarationSyncService {
|
||||
|| event.eventId().length() > 128 || event.tenantId() <= 0 || event.orderId() <= 0
|
||||
|| event.version() < 0 || event.operatorId() <= 0 || event.occurredAt() == null
|
||||
|| !StringUtils.hasText(event.orderNo()) || !StringUtils.hasText(event.inboundCode())
|
||||
|| event.cargoes() == null || event.cargoes().isEmpty()
|
||||
|| !StringUtils.hasText(event.channelCode()) || !StringUtils.hasText(event.channelName())) {
|
||||
|| event.cargoes() == null || event.cargoes().isEmpty() || event.cargoes().size() > 500
|
||||
|| event.changes() == null || !StringUtils.hasText(event.channelCode()) || event.channelCode().length() > 64
|
||||
|| !StringUtils.hasText(event.channelName()) || event.channelName().length() > 128
|
||||
|| event.inboundCode().length() > 40
|
||||
|| event.customerRemark() != null && event.customerRemark().length() > 1000) {
|
||||
throw new BizException(ResultCode.BAD_REQUEST, "订单申报更新事件不完整");
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user