修复:支持已确认退回的并发幂等

This commit is contained in:
焦龙言
2026-07-27 00:23:04 +08:00
parent 12260fdf83
commit 80c2288122
2 changed files with 142 additions and 23 deletions
@@ -446,14 +446,13 @@ class ReturnService:
pallet_ids: list[int],
source_site_id: int,
current_user: CurrentUserModel,
inventories: list[BizPalletInventory],
) -> tuple[
list[BizPalletInventory],
list[BizPallet],
dict[int, BizFillingRecord | None],
list[int] | None,
]:
ordered_ids = cls._validate_pallet_ids(pallet_ids)
inventories = await InventoryDao.get_inventories_for_update(query_db, ordered_ids)
inventory_by_id = {inventory.pallet_id: inventory for inventory in inventories}
if set(inventory_by_id) != set(ordered_ids):
cls._raise_pallet_not_accessible(current_user, '存在不存在或库存缺失的托盘')
@@ -461,13 +460,11 @@ class ReturnService:
ordered_inventories = [inventory_by_id[pallet_id] for pallet_id in ordered_ids]
if any(inventory.site_id != source_site_id for inventory in ordered_inventories):
cls._raise_pallet_not_accessible(current_user, '同一退回单托盘必须属于同一现场')
try:
await AccessService.assert_site_access(query_db, current_user, source_site_id)
except PermissionException:
cls._raise_pallet_not_accessible(current_user)
fresh_site_ids = await cls._site_scope(query_db, current_user)
if fresh_site_ids is not None and source_site_id not in fresh_site_ids:
cls._raise_pallet_not_accessible(current_user)
if any(
inventory.warehouse_id is not None or inventory.flow_state not in cls._REQUESTABLE_STATES
for inventory in ordered_inventories
):
raise ServiceException(message='托盘当前状态不允许发起退回')
pallets = await InventoryDao.get_pallets_by_ids(query_db, ordered_ids)
pallet_by_id = {pallet.pallet_id: pallet for pallet in pallets}
@@ -485,7 +482,7 @@ class ReturnService:
)
for filling_id in filling_ids:
filling_by_id[filling_id] = await OperationDao.get_active_filling_for_update(query_db, filling_id)
return ordered_inventories, ordered_pallets, filling_by_id, fresh_site_ids
return ordered_inventories, ordered_pallets, filling_by_id
@classmethod
def _validate_locked_request_pallets(
@@ -618,7 +615,7 @@ class ReturnService:
await query_db.rollback()
@classmethod
async def request_return_services( # noqa: PLR0915
async def request_return_services( # noqa: PLR0912, PLR0915
cls,
query_db: AsyncSession,
page_object: ReturnRequestModel,
@@ -672,12 +669,18 @@ class ReturnService:
await query_db.rollback()
return cls._success('申请成功', return_id)
inventories, pallets, filling_by_id, fresh_site_ids = await cls._lock_request_pallets(
query_db,
ordered_ids,
page_object.source_site_id,
current_user,
)
inventories = await InventoryDao.get_inventories_for_update(query_db, ordered_ids)
try:
await AccessService.assert_site_access(
query_db,
current_user,
page_object.source_site_id,
)
except PermissionException:
cls._raise_pallet_not_accessible(current_user)
fresh_site_ids = await cls._site_scope(query_db, current_user)
if fresh_site_ids is not None and page_object.source_site_id not in fresh_site_ids:
cls._raise_pallet_not_accessible(current_user)
fresh_warehouse_ids = None if fresh_site_ids is None else []
fresh_existing = await OperationDao.get_return_by_request_idempotency_key(
query_db,
@@ -693,6 +696,13 @@ class ReturnService:
await query_db.rollback()
return cls._success('申请成功', fresh_existing_id)
inventories, pallets, filling_by_id = await cls._lock_request_pallets(
query_db,
ordered_ids,
page_object.source_site_id,
current_user,
inventories,
)
await cls._lock_new_request_route(query_db, page_object, current_user)
cls._validate_locked_request_pallets(
inventories,