Feat: 后端新增基础资料与业务管理只读汇总接口
为 Web 端改版提供名称、统计和看板数据。只新增只读查询、扩展列表与详情 返回字段;写接口、托盘状态机和幂等键规则均未改动,未新增权限标识。 - 基础资料:列表补充名称与汇总,客户详情返回现场树,托盘详情返回最近流转, 托盘列表支持按流转阶段筛选和二维码精确匹配;新增现场、产线、隧道的上级 资料选项接口;Excel 导入支持 preview 只校验不写入 - 发货出库:列表支持 keyword、receiptState 筛选并补充名称、托盘数、包数和 菌种构成;详情明细补充托盘号、菌种、批次 - 首次入库:列表与详情补充名称、托盘数、托盘当前状态;新增 /summary - 托盘退回:列表支持 keyword 并补充名称与托盘汇总,详情补充托盘号和隧道; 新增 /summary、/site-options - 工厂装填:列表支持 keyword 并补充托盘号、仓库、操作人;新增 /summary、 /batches、/batches/detail - 客户现场使用:列表支持 keyword、fifoSkipped 并补充名称和被跳过的先到托盘号; 新增 /site-options、/site-board 以及 usage-fifo 的 /queue - 先到先用 DAO 抽出公共候选条件与排序,建议查询生成的 SQL 与改动前一致 - 新增各汇总 DAO 的单元测试,同步更新路由权限与模型字段契约测试 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -15,6 +15,7 @@ from exceptions.exception import PermissionException, ServiceException
|
||||
from module_admin.entity.vo.user_vo import CurrentUserModel
|
||||
from module_business.dao.master_dao import MasterDao
|
||||
from module_business.domain.test_batch_scope import TestBatchScope
|
||||
from module_business.entity.vo.master_summary_vo import MasterImportPreviewModel, MasterImportPreviewRowModel
|
||||
from module_business.entity.vo.master_vo import (
|
||||
CustomerModel,
|
||||
LineModel,
|
||||
@@ -33,6 +34,11 @@ _MAX_IMPORT_BYTES = 5 * 1024 * 1024
|
||||
_MAX_IMPORT_ROWS = 5000
|
||||
_STATUS_TO_VALUE = {'正常': '0', '停用': '1'}
|
||||
_VALUE_TO_STATUS = {'0': '正常', '1': '停用'}
|
||||
_PREVIEW_MESSAGES: dict[ImportOutcome, str] = {
|
||||
'add': '校验通过,将新增',
|
||||
'update': '{code}已存在,将按文件内容更新',
|
||||
'skip': '{code}已存在,保持原资料不变',
|
||||
}
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
@@ -245,6 +251,65 @@ class MasterExcelService:
|
||||
raise ServiceException(message='导入失败,数据已全部回滚') from exc
|
||||
return CrudResponseModel(is_success=True, message=cls._summary(added, updated, skipped, details))
|
||||
|
||||
@classmethod
|
||||
async def preview_import(
|
||||
cls,
|
||||
db: AsyncSession,
|
||||
resource: MasterExcelResource,
|
||||
file: UploadFile,
|
||||
update_support: bool,
|
||||
current_user: CurrentUserModel,
|
||||
scope: TestBatchScope,
|
||||
) -> MasterImportPreviewModel:
|
||||
"""走与正式导入完全相同的逐行校验,最后整体回滚,返回每行将发生的结果。"""
|
||||
spec = _SPECS[resource]
|
||||
parsed_rows = await cls._read_upload(resource, file)
|
||||
rows: list[MasterImportPreviewRowModel] = []
|
||||
try:
|
||||
if resource == 'pallet':
|
||||
await PalletService._prepare_test_write(db, current_user, scope)
|
||||
else:
|
||||
await MasterService._prepare_test_write(db, current_user, scope)
|
||||
for row in parsed_rows:
|
||||
values = [row.values.get(header, '') for header in spec.headers]
|
||||
try:
|
||||
outcome, _ = await cls._apply_row(db, resource, row, update_support, current_user, scope)
|
||||
except ServiceException as exc:
|
||||
rows.append(
|
||||
MasterImportPreviewRowModel(
|
||||
excel_row=row.excel_row,
|
||||
outcome='error',
|
||||
message=exc.message or '校验失败',
|
||||
values=values,
|
||||
)
|
||||
)
|
||||
continue
|
||||
rows.append(
|
||||
MasterImportPreviewRowModel(
|
||||
excel_row=row.excel_row,
|
||||
outcome=outcome,
|
||||
message=_PREVIEW_MESSAGES[outcome].format(code=spec.code_header),
|
||||
values=values,
|
||||
)
|
||||
)
|
||||
except Exception as exc:
|
||||
if isinstance(exc, (ServiceException, PermissionException)):
|
||||
raise
|
||||
raise ServiceException(message='导入校验失败,请稍后重试') from exc
|
||||
finally:
|
||||
await db.rollback()
|
||||
counts = {outcome: sum(1 for row in rows if row.outcome == outcome) for outcome in _PREVIEW_MESSAGES}
|
||||
return MasterImportPreviewModel(
|
||||
headers=list(spec.headers),
|
||||
update_support=update_support,
|
||||
total_rows=len(rows),
|
||||
add_count=counts['add'],
|
||||
update_count=counts['update'],
|
||||
skip_count=counts['skip'],
|
||||
error_count=sum(1 for row in rows if row.outcome == 'error'),
|
||||
rows=rows,
|
||||
)
|
||||
|
||||
@classmethod
|
||||
async def _read_upload(cls, resource: MasterExcelResource, file: UploadFile) -> list[ParsedRow]:
|
||||
spec = _SPECS[resource]
|
||||
|
||||
Reference in New Issue
Block a user