"""客户现场使用接口。""" from typing import Annotated from fastapi import Path, Query, Request, Response from sqlalchemy.ext.asyncio import AsyncSession from common.annotation.log_annotation import Log from common.aspect.db_seesion import DBSessionDependency from common.aspect.interface_auth import UserInterfaceAuthDependency from common.aspect.pre_auth import CurrentUserDependency, PreAuthDependency from common.enums import BusinessType from common.router import APIRouterPro from common.vo import DataResponseModel, PageResponseModel from module_admin.entity.vo.user_vo import CurrentUserModel from module_business.dependency.test_batch_dependency import TestBatchScopeDependency from module_business.domain.test_batch_scope import TestBatchScope from module_business.entity.vo.operation_vo import ( BusinessOptionModel, PalletScanResponseModel, TunnelEntryCreateModel, TunnelEntrySuccessResponseModel, UsageCreateModel, UsageListItemModel, UsageModel, UsagePageQueryModel, UsageSiteBoardModel, UsageSiteOptionModel, UsageSuccessResponseModel, ) from module_business.service.usage_service import UsageService from utils.response_util import ResponseUtil usage_controller = APIRouterPro( prefix='/business/usage', tags=['业务管理-客户现场使用'], dependencies=[PreAuthDependency()], ) @usage_controller.get( '/list', summary='获取客户现场使用记录分页列表接口', description='按当前用户客户现场数据范围获取使用记录', response_model=PageResponseModel[UsageListItemModel], dependencies=[UserInterfaceAuthDependency('business:usage:list')], ) async def get_business_usage_list( request: Request, usage_page_query: Annotated[UsagePageQueryModel, Query()], query_db: Annotated[AsyncSession, DBSessionDependency()], current_user: Annotated[CurrentUserModel, CurrentUserDependency()], test_scope: Annotated[TestBatchScope, TestBatchScopeDependency()], ) -> Response: result = await UsageService.get_usage_list_services( query_db, usage_page_query, current_user, test_scope, is_page=True, ) return ResponseUtil.success(model_content=result) @usage_controller.get( '/line-options', summary='获取客户现场启用产线选项接口', description='仅返回当前用户有权访问的指定客户现场启用产线', response_model=DataResponseModel[list[BusinessOptionModel]], dependencies=[UserInterfaceAuthDependency('business:usage:query')], ) async def get_business_usage_line_options( request: Request, site_id: Annotated[int, Query(alias='siteId', gt=0, description='客户现场ID')], query_db: Annotated[AsyncSession, DBSessionDependency()], current_user: Annotated[CurrentUserModel, CurrentUserDependency()], test_scope: Annotated[TestBatchScope, TestBatchScopeDependency()], ) -> Response: result = await UsageService.get_line_options_services(query_db, site_id, current_user, test_scope) return ResponseUtil.success(data=result) @usage_controller.get( '/tunnel-options', summary='获取客户现场启用隧道选项接口', description='仅返回当前用户有权访问的指定客户现场启用隧道', response_model=DataResponseModel[list[BusinessOptionModel]], dependencies=[UserInterfaceAuthDependency('business:usage:query')], ) async def get_business_usage_tunnel_options( request: Request, site_id: Annotated[int, Query(alias='siteId', gt=0, description='客户现场ID')], query_db: Annotated[AsyncSession, DBSessionDependency()], current_user: Annotated[CurrentUserModel, CurrentUserDependency()], test_scope: Annotated[TestBatchScope, TestBatchScopeDependency()], ) -> Response: result = await UsageService.get_tunnel_options_services(query_db, site_id, current_user, test_scope) return ResponseUtil.success(data=result) @usage_controller.get( '/site-options', summary='获取客户现场使用的现场选项接口', description='返回当前用户有权访问且已启用的客户现场,附带启用隧道与产线数', response_model=DataResponseModel[list[UsageSiteOptionModel]], dependencies=[UserInterfaceAuthDependency('business:usage:list')], ) async def get_business_usage_site_options( request: Request, query_db: Annotated[AsyncSession, DBSessionDependency()], current_user: Annotated[CurrentUserModel, CurrentUserDependency()], test_scope: Annotated[TestBatchScope, TestBatchScopeDependency()], ) -> Response: result = await UsageService.get_site_options_services(query_db, current_user, test_scope) return ResponseUtil.success(data=result) @usage_controller.get( '/site-board', summary='获取客户现场看板接口', description='统计指定现场的托盘库存、今日用量、隧道使用与跳过先到托盘次数;不返回内部溯源号', response_model=DataResponseModel[UsageSiteBoardModel], dependencies=[UserInterfaceAuthDependency('business:usage:query')], ) async def get_business_usage_site_board( request: Request, site_id: Annotated[int, Query(alias='siteId', gt=0, description='客户现场ID')], query_db: Annotated[AsyncSession, DBSessionDependency()], current_user: Annotated[CurrentUserModel, CurrentUserDependency()], test_scope: Annotated[TestBatchScope, TestBatchScopeDependency()], ) -> Response: result = await UsageService.get_site_board_services(query_db, site_id, current_user, test_scope) return ResponseUtil.success(data=result) @usage_controller.post( '/enter-tunnel', summary='扫码入隧接口', description='将单个已到客户现场的托盘绑定到指定隧道,并生成或复用隧道内部溯源号', response_model=DataResponseModel[TunnelEntrySuccessResponseModel], dependencies=[UserInterfaceAuthDependency('business:usage:add')], ) @Log(title='扫码入隧', business_type=BusinessType.INSERT) async def enter_business_usage_tunnel( request: Request, entry_command: TunnelEntryCreateModel, query_db: Annotated[AsyncSession, DBSessionDependency()], current_user: Annotated[CurrentUserModel, CurrentUserDependency()], test_scope: Annotated[TestBatchScope, TestBatchScopeDependency(write=True)], ) -> Response: result = await UsageService.enter_tunnel_services(query_db, entry_command, current_user, test_scope) return ResponseUtil.success(msg=result.message, data=result.result) @usage_controller.get( '/scan/{qr_token:path}', summary='扫描客户现场待使用托盘接口', description='按托盘当前客户现场数据范围判断是否允许记录使用', response_model=DataResponseModel[PalletScanResponseModel], dependencies=[UserInterfaceAuthDependency('business:usage:query')], ) @Log( title='扫码操作', business_type=BusinessType.OTHER, request_log_mode='none', response_log_mode='summary', oper_url_mode='route', ) async def scan_business_usage_pallet( request: Request, qr_token: Annotated[ str, Path(min_length=1, max_length=128, pattern=r'\S', description='二维码固定标识'), ], query_db: Annotated[AsyncSession, DBSessionDependency()], current_user: Annotated[CurrentUserModel, CurrentUserDependency()], test_scope: Annotated[TestBatchScope, TestBatchScopeDependency()], ) -> Response: result = await UsageService.scan_usage_pallet_services(query_db, qr_token, current_user, test_scope) return ResponseUtil.success(data=result) @usage_controller.get( '/{usage_id}', summary='获取客户现场使用记录详情接口', description='按客户现场数据范围获取指定使用记录', response_model=DataResponseModel[UsageModel], dependencies=[UserInterfaceAuthDependency('business:usage:query')], ) async def query_detail_business_usage( request: Request, usage_id: Annotated[int, Path(gt=0, description='使用记录ID')], query_db: Annotated[AsyncSession, DBSessionDependency()], current_user: Annotated[CurrentUserModel, CurrentUserDependency()], test_scope: Annotated[TestBatchScope, TestBatchScopeDependency()], ) -> Response: result = await UsageService.usage_detail_services(query_db, usage_id, current_user, test_scope) return ResponseUtil.success(data=result) @usage_controller.post( '', summary='记录客户现场使用接口', description='扫描托盘并原子记录客户现场使用包数', response_model=DataResponseModel[UsageSuccessResponseModel], dependencies=[UserInterfaceAuthDependency('business:usage:add')], ) @Log(title='客户现场使用', business_type=BusinessType.INSERT) async def add_business_usage( request: Request, add_usage: UsageCreateModel, query_db: Annotated[AsyncSession, DBSessionDependency()], current_user: Annotated[CurrentUserModel, CurrentUserDependency()], test_scope: Annotated[TestBatchScope, TestBatchScopeDependency(write=True)], ) -> Response: result = await UsageService.record_usage_services(query_db, add_usage, current_user, test_scope) return ResponseUtil.success(msg=result.message, data=result.result)