优化
This commit is contained in:
+2
-2
@@ -26,7 +26,7 @@ public class InboundPalletAllocationRepository {
|
||||
Map<Long, String> reservationNos = mapper.selectList(new LambdaQueryWrapper<InboundPalletAllocationEntity>()
|
||||
.eq(InboundPalletAllocationEntity::getTenantId, tenantId)
|
||||
.eq(InboundPalletAllocationEntity::getInboundId, inboundId))
|
||||
.stream().collect(Collectors.toMap(InboundPalletAllocationEntity::getPalletId,
|
||||
.stream().collect(Collectors.toMap(InboundPalletAllocationEntity::getReservationId,
|
||||
InboundPalletAllocationEntity::getReservationNo, (left, right) -> left));
|
||||
mapper.delete(new LambdaQueryWrapper<InboundPalletAllocationEntity>()
|
||||
.eq(InboundPalletAllocationEntity::getTenantId, tenantId)
|
||||
@@ -58,7 +58,7 @@ public class InboundPalletAllocationRepository {
|
||||
value.setReservationId(source.reservationId());
|
||||
// 仓储确认响应只返回 reservationId;沿用预占阶段的预占单号快照。
|
||||
value.setReservationNo(source.reservationNo() != null ? source.reservationNo()
|
||||
: reservationNos.get(source.palletId()));
|
||||
: reservationNos.get(source.reservationId()));
|
||||
value.setAllocationStatus(normalize(source.state()));
|
||||
value.setCreatedAt(now);
|
||||
value.setUpdatedAt(now);
|
||||
|
||||
@@ -60,6 +60,109 @@ ALTER TABLE fl_ocean_booking
|
||||
MODIFY COLUMN customer_id BIGINT UNSIGNED NULL COMMENT '可选客户ID快照',
|
||||
MODIFY COLUMN customer_name VARCHAR(128) NULL COMMENT '可选客户名称快照';
|
||||
|
||||
-- Reconcile databases created before the direct-CONFIRMED contract. This is
|
||||
-- intentionally fail-closed: no legacy row is promoted and no transport term
|
||||
-- is guessed. Referenced legacy aggregates must be archived/deleted by the
|
||||
-- owning module first, then this block can be rerun safely.
|
||||
DROP TRIGGER IF EXISTS trg_ocean_booking_before_update;
|
||||
DROP TRIGGER IF EXISTS trg_ocean_party_before_insert;
|
||||
DROP TRIGGER IF EXISTS trg_ocean_party_before_update;
|
||||
DROP TRIGGER IF EXISTS trg_ocean_party_before_delete;
|
||||
DROP TRIGGER IF EXISTS trg_ocean_requirement_before_insert;
|
||||
DROP TRIGGER IF EXISTS trg_ocean_requirement_before_update;
|
||||
DROP TRIGGER IF EXISTS trg_ocean_requirement_before_delete;
|
||||
DROP TRIGGER IF EXISTS trg_ocean_history_before_delete;
|
||||
DELIMITER $$
|
||||
DROP PROCEDURE IF EXISTS reconcile_ocean_booking_legacy$$
|
||||
CREATE PROCEDURE reconcile_ocean_booking_legacy()
|
||||
BEGIN
|
||||
DECLARE v_blank_terms INT DEFAULT 0;
|
||||
DECLARE v_legacy INT DEFAULT 0;
|
||||
DECLARE v_external INT DEFAULT 0;
|
||||
DECLARE v_sql TEXT;
|
||||
|
||||
SELECT COUNT(*) INTO v_blank_terms
|
||||
FROM fl_ocean_booking
|
||||
WHERE NULLIF(TRIM(service_term),'') IS NULL
|
||||
OR NULLIF(TRIM(freight_term),'') IS NULL;
|
||||
IF v_blank_terms > 0 THEN
|
||||
SIGNAL SQLSTATE '45000'
|
||||
SET MESSAGE_TEXT = '订舱结构收敛暂停:存在 service_term/freight_term 为空的真实行,请按原始业务资料补齐后重跑,禁止猜值';
|
||||
END IF;
|
||||
|
||||
SELECT COUNT(*) INTO v_legacy
|
||||
FROM fl_ocean_booking
|
||||
WHERE status IN ('DRAFT','SUBMITTED','FAILED');
|
||||
IF v_legacy > 0 THEN
|
||||
-- Tables from later phase blocks may not exist on a module-only run. Use
|
||||
-- dynamic SQL so a clean initialization remains repeatable.
|
||||
IF EXISTS (SELECT 1 FROM information_schema.tables
|
||||
WHERE table_schema=DATABASE() AND table_name='fl_port_schedule') THEN
|
||||
SET @ocean_legacy_external := 0;
|
||||
SET v_sql = 'SELECT COUNT(*) INTO @ocean_legacy_external FROM fl_port_schedule p JOIN fl_ocean_booking b ON b.id=p.ocean_booking_id WHERE b.status IN (''DRAFT'',''SUBMITTED'',''FAILED'')';
|
||||
PREPARE stmt FROM v_sql; EXECUTE stmt; DEALLOCATE PREPARE stmt;
|
||||
SET v_external = v_external + COALESCE(@ocean_legacy_external,0);
|
||||
END IF;
|
||||
IF EXISTS (SELECT 1 FROM information_schema.tables
|
||||
WHERE table_schema=DATABASE() AND table_name='fl_carrier_schedule_query') THEN
|
||||
SET @ocean_legacy_external := 0;
|
||||
SET v_sql = 'SELECT COUNT(*) INTO @ocean_legacy_external FROM fl_carrier_schedule_query q JOIN fl_ocean_booking b ON b.id=q.ocean_booking_id WHERE b.status IN (''DRAFT'',''SUBMITTED'',''FAILED'')';
|
||||
PREPARE stmt FROM v_sql; EXECUTE stmt; DEALLOCATE PREPARE stmt;
|
||||
SET v_external = v_external + COALESCE(@ocean_legacy_external,0);
|
||||
END IF;
|
||||
IF v_external > 0 THEN
|
||||
SIGNAL SQLSTATE '45000'
|
||||
SET MESSAGE_TEXT = '订舱结构收敛暂停:旧订舱仍被港务船期/船司查询引用,请先按子表历史->主表归档顺序清理引用后重跑;不会伪造 CONFIRMED';
|
||||
END IF;
|
||||
|
||||
DELETE FROM fl_ocean_booking_party
|
||||
WHERE ocean_booking_id IN (SELECT id FROM fl_ocean_booking WHERE status IN ('DRAFT','SUBMITTED','FAILED'));
|
||||
DELETE FROM fl_ocean_booking_container_allocation
|
||||
WHERE ocean_booking_id IN (SELECT id FROM fl_ocean_booking WHERE status IN ('DRAFT','SUBMITTED','FAILED'));
|
||||
DELETE FROM fl_ocean_booking_container_requirement
|
||||
WHERE ocean_booking_id IN (SELECT id FROM fl_ocean_booking WHERE status IN ('DRAFT','SUBMITTED','FAILED'));
|
||||
DELETE FROM fl_ocean_booking_operation
|
||||
WHERE ocean_booking_id IN (SELECT id FROM fl_ocean_booking WHERE status IN ('DRAFT','SUBMITTED','FAILED'));
|
||||
DELETE FROM fl_ocean_booking_status_history
|
||||
WHERE ocean_booking_id IN (SELECT id FROM fl_ocean_booking WHERE status IN ('DRAFT','SUBMITTED','FAILED'));
|
||||
DELETE FROM fl_ocean_booking
|
||||
WHERE status IN ('DRAFT','SUBMITTED','FAILED');
|
||||
END IF;
|
||||
END$$
|
||||
CALL reconcile_ocean_booking_legacy()$$
|
||||
DROP PROCEDURE reconcile_ocean_booking_legacy$$
|
||||
DELIMITER ;
|
||||
|
||||
-- Existing CHECK constraints are not reconciled by CREATE TABLE IF NOT EXISTS.
|
||||
SET @ocean_drop_status_check = (
|
||||
SELECT IF(COUNT(*) > 0,
|
||||
'ALTER TABLE fl_ocean_booking DROP CHECK chk_ocean_booking_status',
|
||||
'SELECT 1')
|
||||
FROM information_schema.table_constraints
|
||||
WHERE table_schema=DATABASE() AND table_name='fl_ocean_booking'
|
||||
AND constraint_name='chk_ocean_booking_status'
|
||||
);
|
||||
PREPARE ocean_drop_status_stmt FROM @ocean_drop_status_check;
|
||||
EXECUTE ocean_drop_status_stmt;
|
||||
DEALLOCATE PREPARE ocean_drop_status_stmt;
|
||||
ALTER TABLE fl_ocean_booking
|
||||
MODIFY COLUMN service_term VARCHAR(32) NOT NULL COMMENT '运输服务条款快照',
|
||||
MODIFY COLUMN freight_term VARCHAR(32) NOT NULL COMMENT '海运费支付条款快照',
|
||||
ADD CONSTRAINT chk_ocean_booking_status CHECK
|
||||
(status IN ('DRAFT','CONFIRMED','RELEASED','CHANGE_PENDING','CANCELLED'));
|
||||
|
||||
SET @ocean_drop_failure_reason = (
|
||||
SELECT IF(COUNT(*) > 0,
|
||||
'ALTER TABLE fl_ocean_booking DROP COLUMN failure_reason',
|
||||
'SELECT 1')
|
||||
FROM information_schema.columns
|
||||
WHERE table_schema=DATABASE() AND table_name='fl_ocean_booking'
|
||||
AND column_name='failure_reason'
|
||||
);
|
||||
PREPARE ocean_drop_failure_stmt FROM @ocean_drop_failure_reason;
|
||||
EXECUTE ocean_drop_failure_stmt;
|
||||
DEALLOCATE PREPARE ocean_drop_failure_stmt;
|
||||
|
||||
CREATE TABLE IF NOT EXISTS fl_ocean_booking_party (
|
||||
id BIGINT UNSIGNED NOT NULL COMMENT '订舱提单关系方ID',
|
||||
ocean_booking_id BIGINT UNSIGNED NOT NULL COMMENT '所属海运订舱ID',
|
||||
|
||||
+13
@@ -42,6 +42,19 @@ class OceanBookingSqlContractTest {
|
||||
.contains("UNIQUE KEY uk_ocean_allocation_container (container_booking_id)");
|
||||
}
|
||||
|
||||
@Test
|
||||
void reconcilesExistingDatabasesWithoutGuessingLegacyBusinessData() throws Exception {
|
||||
assertThat(sql())
|
||||
.contains("reconcile_ocean_booking_legacy")
|
||||
.contains("service_term/freight_term 为空的真实行")
|
||||
.contains("status IN ('DRAFT','SUBMITTED','FAILED')")
|
||||
.contains("不会伪造 CONFIRMED")
|
||||
.contains("DROP CHECK chk_ocean_booking_status")
|
||||
.contains("ADD CONSTRAINT chk_ocean_booking_status CHECK")
|
||||
.contains("DROP COLUMN failure_reason")
|
||||
.doesNotContain("UPDATE fl_ocean_booking SET status='CONFIRMED'");
|
||||
}
|
||||
|
||||
@Test
|
||||
void declaresDedicatedPlatformPermissions() throws Exception {
|
||||
assertThat(sql())
|
||||
|
||||
+29
@@ -2,6 +2,7 @@ package com.baiyue.firstleg.repository;
|
||||
|
||||
import com.baiyue.firstleg.entity.InboundPalletAllocationEntity;
|
||||
import com.baiyue.firstleg.mapper.InboundPalletAllocationMapper;
|
||||
import com.baiyue.firstleg.client.WarehousePalletLocationClient;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
import org.mockito.InjectMocks;
|
||||
@@ -9,10 +10,14 @@ import org.mockito.Mock;
|
||||
import org.mockito.junit.jupiter.MockitoExtension;
|
||||
|
||||
import java.util.List;
|
||||
import java.math.BigDecimal;
|
||||
import org.mockito.ArgumentCaptor;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThatCode;
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.Mockito.when;
|
||||
import static org.mockito.Mockito.verify;
|
||||
|
||||
@ExtendWith(MockitoExtension.class)
|
||||
class InboundPalletAllocationRepositoryTest {
|
||||
@@ -42,6 +47,30 @@ class InboundPalletAllocationRepositoryTest {
|
||||
.doesNotThrowAnyException();
|
||||
}
|
||||
|
||||
@Test
|
||||
void restoresReservationNumbersByReservationIdWhenPalletIsReused() {
|
||||
InboundPalletAllocationEntity first = allocation(1L, 91L, "RESERVED", 1, 0);
|
||||
first.setPalletId(501L);
|
||||
first.setReservationNo("LR-91");
|
||||
InboundPalletAllocationEntity second = allocation(2L, 92L, "RESERVED", 1, 0);
|
||||
second.setPalletId(501L);
|
||||
second.setReservationNo("LR-92");
|
||||
when(mapper.selectList(any())).thenReturn(List.of(first, second));
|
||||
|
||||
var result = new WarehousePalletLocationClient.BatchResult(2, List.of(
|
||||
new WarehousePalletLocationClient.AllocationResult(91L, null, 1, 501L, "P1", 11L, "A1",
|
||||
new BigDecimal("1.2"), "OCCUPIED"),
|
||||
new WarehousePalletLocationClient.AllocationResult(92L, null, 2, 501L, "P1", 12L, "A2",
|
||||
new BigDecimal("1.2"), "OCCUPIED")));
|
||||
|
||||
repository.replace(10001L, 31L, 88L, 0L, new BigDecimal("1.2"), 2, result);
|
||||
|
||||
ArgumentCaptor<InboundPalletAllocationEntity> inserted = ArgumentCaptor.forClass(InboundPalletAllocationEntity.class);
|
||||
verify(mapper, org.mockito.Mockito.times(2)).insert(inserted.capture());
|
||||
assertThat(inserted.getAllValues()).extracting(InboundPalletAllocationEntity::getReservationNo)
|
||||
.containsExactly("LR-91", "LR-92");
|
||||
}
|
||||
|
||||
private static InboundPalletAllocationEntity allocation(long id, long reservationId, String status,
|
||||
int allocated, int picked) {
|
||||
InboundPalletAllocationEntity entity = new InboundPalletAllocationEntity();
|
||||
|
||||
+12
@@ -54,6 +54,18 @@ class Phase3FinalUnifiedSqlContractTest {
|
||||
.contains("trg_loading_pick_operation_before_delete");
|
||||
}
|
||||
|
||||
@Test
|
||||
void unifiedOceanBookingBlockIncludesExistingSchemaReconciliation() throws Exception {
|
||||
String sql = normalise(Files.readString(UNIFIED));
|
||||
assertThat(sql)
|
||||
.contains("reconcile_ocean_booking_legacy")
|
||||
.contains("DROP CHECK chk_ocean_booking_status")
|
||||
.contains("ADD CONSTRAINT chk_ocean_booking_status CHECK")
|
||||
.contains("DROP COLUMN failure_reason")
|
||||
.contains("不会伪造 CONFIRMED")
|
||||
.doesNotContain("UPDATE fl_ocean_booking SET status='CONFIRMED'");
|
||||
}
|
||||
|
||||
@Test
|
||||
void permissionRangesDoNotOverlapAndBothPlatformRolesAreGranted() throws Exception {
|
||||
String sql = Files.readString(UNIFIED, StandardCharsets.UTF_8);
|
||||
|
||||
Reference in New Issue
Block a user