为基础资料维护页面增加导入导出

This commit is contained in:
2026-10-09 21:13:43 +08:00
parent a62803ccf0
commit a8af1f542b
20 changed files with 2134 additions and 15 deletions
@@ -187,6 +187,95 @@ class PalletService:
await db.rollback()
raise
@classmethod
async def stage_imported_pallet(
cls,
db: AsyncSession,
*,
pallet_code: str,
asset_status: Literal['0', '1'],
remark: str | None,
update_support: bool,
current_user: CurrentUserModel,
scope: TestBatchScope,
) -> Literal['add', 'update', 'skip']:
"""写入一条托盘导入。不提交事务,容量固定 100,且不改编号和二维码。"""
existing = await MasterDao.find_pallet_by_code(db, pallet_code)
if existing is None:
return await cls._stage_new_pallet(
db,
pallet_code=pallet_code,
asset_status=asset_status,
remark=remark,
current_user=current_user,
scope=scope,
)
current = await MasterDao.get_pallet(db, existing.pallet_id, scope, for_update=scope.mode == 'TEST')
if current is None:
raise ServiceException(message=f'托盘编号{pallet_code}已存在于其他数据范围')
if not update_support:
return 'skip'
_current_pallet, _inventory = await cls._lock_pallet_inventory(db, current.pallet_id, scope)
edit_model = PalletEditModel(
pallet_id=current.pallet_id,
capacity_packages=current.capacity_packages,
asset_status=asset_status,
remark=remark,
)
if current.capacity_packages == 100 and edit_model.capacity_packages != 100:
raise ServiceException(message='新规格托盘容量固定为 100 包')
if current.capacity_packages != edit_model.capacity_packages:
raise ServiceException(message='历史托盘容量不允许修改')
if edit_model.capacity_packages < _inventory.remaining_packages:
raise ServiceException(message='托盘容量不能小于当前剩余包数')
await MasterDao.update_pallet(
db,
edit_model,
scope,
update_by=current_user.user.user_name,
update_time=datetime.now(),
)
return 'update'
@classmethod
async def _stage_new_pallet(
cls,
db: AsyncSession,
*,
pallet_code: str,
asset_status: Literal['0', '1'],
remark: str | None,
current_user: CurrentUserModel,
scope: TestBatchScope,
) -> Literal['add']:
current_time = datetime.now()
user_name = current_user.user.user_name
pallet_record = BizPallet(
test_batch_id=scope.batch_id,
pallet_code=pallet_code,
qr_token=secrets.token_urlsafe(32),
capacity_packages=100,
asset_status=asset_status,
remark=remark,
create_by=user_name,
create_time=current_time,
update_by=user_name,
update_time=current_time,
)
pallet_record = await MasterDao.add_pallet(db, pallet_record)
inventory = BizPalletInventory(
pallet_id=pallet_record.pallet_id,
test_batch_id=scope.batch_id,
flow_state='UNSTOCKED',
remaining_packages=0,
version=0,
updated_at=current_time,
)
await MasterDao.add_pallet_inventory(db, inventory)
return 'add'
@classmethod
async def remove_pallet(
cls,