Files
ShiErFeng/shierfeng-fastapi-backend/module_business/service/usage_fifo_service.py
T
yujiangandClaude Opus 5.5 3050ccdc4e 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>
2026-10-18 05:03:10 -07:00

72 lines
3.7 KiB
Python

from sqlalchemy.ext.asyncio import AsyncSession
from exceptions.exception import ServiceException
from module_admin.entity.vo.user_vo import CurrentUserModel
from module_business.dao.master_dao import MasterDao
from module_business.dao.usage_fifo_dao import UsageFifoDao
from module_business.domain.fifo_policy import requires_fifo_skip_reason
from module_business.domain.test_batch_scope import TestBatchScope, assert_record_in_scope
from module_business.entity.vo.usage_fifo_vo import UsageFifoQueueModel, UsageFifoRecommendationModel
from module_business.service.access_service import AccessService
from module_business.service.usage_service import UsageService
class UsageFifoService:
@staticmethod
async def recommendation(
db: AsyncSession, identifier: str, user: CurrentUserModel, scope: TestBatchScope,
*, tunnel_id: int | None = None,
) -> UsageFifoRecommendationModel:
# 复用扫码入口的现场授权、当前批次与可用状态校验;客户端不指定查询现场。
pallet = await UsageService.scan_usage_pallet_services(db, identifier, user, scope)
inventory = await MasterDao.get_pallet_inventory(db, pallet.pallet_id, scope)
if inventory is None:
raise ServiceException(message='托盘库存不存在')
assert_record_in_scope(inventory, scope, '库存')
target_tunnel_id = pallet.tunnel_id
trace_no = pallet.trace_no
if tunnel_id is not None:
tunnel = await MasterDao.get_tunnel(db, tunnel_id, scope)
if tunnel is None or tunnel.site_id != pallet.site_id or tunnel.status != '0':
raise ServiceException(message='请选择托盘当前现场的启用隧道')
assert_record_in_scope(tunnel, scope, '隧道')
if pallet.tunnel_id is not None and pallet.tunnel_id != tunnel_id:
raise ServiceException(message='托盘已入其他隧道')
target_tunnel_id, trace_no = tunnel_id, tunnel.current_trace_no
candidate, unknown = await UsageFifoDao.recommendation(
db,
site_id=pallet.site_id,
strain_id=pallet.strain_id,
tunnel_id=target_tunnel_id,
trace_no=trace_no,
scope=scope,
)
message = '当前无可确定到货顺序的同品种托盘'
if candidate:
message = f'先到先用建议:托盘 {candidate["pallet_code"]},批次 {candidate["batch_no"]},余量 {candidate["remaining_packages"]} 包'
if unknown:
message += f';另有 {unknown} 托盘未登记实际收货时间,请人工核对先后顺序'
needs_reason = requires_fifo_skip_reason(pallet.pallet_id, inventory.site_arrived_at, candidate)
if needs_reason:
message += ';使用当前托盘需填写跳过先到托盘的原因'
return UsageFifoRecommendationModel(
recommended=candidate, unknown_arrival_count=unknown,
requires_skip_reason=needs_reason, message=message,
)
@staticmethod
async def queue(
db: AsyncSession,
site_id: int,
user: CurrentUserModel,
scope: TestBatchScope,
) -> UsageFifoQueueModel:
"""现场待使用队列(只读):与建议共用候选条件和排序,仅包含尚未入隧的托盘。"""
await AccessService.assert_site_access(db, user, site_id)
site = await MasterDao.get_site(db, site_id, scope)
if site is None:
raise ServiceException(message='客户现场不存在')
assert_record_in_scope(site, scope, '现场')
strains = await UsageFifoDao.queue(db, site_id=site_id, scope=scope)
return UsageFifoQueueModel.model_validate({'site_id': site_id, 'strains': strains})