from typing import Annotated from fastapi import Path, Request, Response from sqlalchemy.ext.asyncio import AsyncSession from common.annotation.log_annotation import Log, RequestLogFieldRoot 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 module_admin.entity.vo.user_vo import CurrentUserModel from module_business.entity.vo.test_batch_vo import ( AdoptionExecuteRequestModel, AdoptionPreviewRequestModel, PurgeExecuteRequestModel, TestBatchCreateModel, TestBatchEditModel, TestBatchUserAssignModel, ) from module_business.service.test_batch_adoption_service import TestBatchAdoptionService from module_business.service.test_batch_purge_service import TestBatchPurgeService from module_business.service.test_batch_service import TestBatchService from utils.response_util import ResponseUtil test_batch_controller = APIRouterPro( prefix='/system/test-batch', tags=['系统管理-测试数据批次'], dependencies=[PreAuthDependency()], ) @test_batch_controller.get('/options') async def list_test_batch_options( request: Request, query_db: Annotated[AsyncSession, DBSessionDependency()], current_user: Annotated[CurrentUserModel, CurrentUserDependency()], ) -> Response: result = await TestBatchService.list_available_batches(query_db, current_user) return ResponseUtil.success(data=result) @test_batch_controller.get( '/list', dependencies=[UserInterfaceAuthDependency('system:testbatch:list')], ) @Log( title='测试数据批次', business_type=BusinessType.OTHER, request_log_mode='summary', response_log_mode='summary', ) async def list_test_batches( request: Request, query_db: Annotated[AsyncSession, DBSessionDependency()], current_user: Annotated[CurrentUserModel, CurrentUserDependency()], ) -> Response: result = await TestBatchService.list_batches(query_db, current_user) return ResponseUtil.success(rows=result, dict_content={'total': len(result)}) @test_batch_controller.post( '', dependencies=[UserInterfaceAuthDependency('system:testbatch:add')], ) @Log( title='测试数据批次', business_type=BusinessType.INSERT, request_log_mode='summary', response_log_mode='summary', ) async def create_test_batch( request: Request, command: TestBatchCreateModel, query_db: Annotated[AsyncSession, DBSessionDependency()], current_user: Annotated[CurrentUserModel, CurrentUserDependency()], ) -> Response: result = await TestBatchService.create_batch(query_db, command, current_user) return ResponseUtil.success(msg=result.message) @test_batch_controller.put( '/{batch_id}', dependencies=[UserInterfaceAuthDependency('system:testbatch:edit')], ) @Log( title='测试数据批次', business_type=BusinessType.UPDATE, request_log_mode='summary', response_log_mode='summary', ) async def edit_test_batch( request: Request, batch_id: Annotated[int, Path(gt=0, description='测试批次ID')], command: TestBatchEditModel, query_db: Annotated[AsyncSession, DBSessionDependency()], current_user: Annotated[CurrentUserModel, CurrentUserDependency()], ) -> Response: result = await TestBatchService.edit_batch(query_db, batch_id, command, current_user) return ResponseUtil.success(msg=result.message) @test_batch_controller.put( '/{batch_id}/users', dependencies=[UserInterfaceAuthDependency('system:testbatch:assign')], ) @Log( title='测试数据批次', business_type=BusinessType.GRANT, request_log_mode='summary', response_log_mode='summary', ) async def assign_test_batch_users( request: Request, batch_id: Annotated[int, Path(gt=0, description='测试批次ID')], command: TestBatchUserAssignModel, query_db: Annotated[AsyncSession, DBSessionDependency()], current_user: Annotated[CurrentUserModel, CurrentUserDependency()], ) -> Response: result = await TestBatchService.replace_users(query_db, batch_id, command, current_user) return ResponseUtil.success(msg=result.message) @test_batch_controller.put( '/{batch_id}/close', dependencies=[UserInterfaceAuthDependency('system:testbatch:close')], ) @Log( title='测试数据批次', business_type=BusinessType.UPDATE, request_log_mode='summary', response_log_mode='summary', ) async def close_test_batch( request: Request, batch_id: Annotated[int, Path(gt=0, description='测试批次ID')], query_db: Annotated[AsyncSession, DBSessionDependency()], current_user: Annotated[CurrentUserModel, CurrentUserDependency()], ) -> Response: result = await TestBatchService.close_batch(query_db, batch_id, current_user) return ResponseUtil.success(msg=result.message) @test_batch_controller.put( '/{batch_id}/reopen', dependencies=[UserInterfaceAuthDependency('system:testbatch:close')], ) @Log( title='测试数据批次', business_type=BusinessType.UPDATE, request_log_mode='summary', response_log_mode='summary', ) async def reopen_test_batch( request: Request, batch_id: Annotated[int, Path(gt=0, description='测试批次ID')], query_db: Annotated[AsyncSession, DBSessionDependency()], current_user: Annotated[CurrentUserModel, CurrentUserDependency()], ) -> Response: result = await TestBatchService.reopen_batch(query_db, batch_id, current_user) return ResponseUtil.success(msg=result.message) @Log( title='测试数据批次', business_type=BusinessType.CLEAN, request_log_mode='include', request_include_fields=(RequestLogFieldRoot.PATH_PARAMS.field('batch_id'),), response_log_mode='summary', ) @test_batch_controller.post( '/{batch_id}/adoption/preview', dependencies=[UserInterfaceAuthDependency('system:testbatch:adopt')], ) async def preview_test_batch_adoption( request: Request, batch_id: Annotated[int, Path(gt=0, description='测试批次ID')], command: AdoptionPreviewRequestModel, query_db: Annotated[AsyncSession, DBSessionDependency()], current_user: Annotated[CurrentUserModel, CurrentUserDependency()], ) -> Response: result = await TestBatchAdoptionService.preview(query_db, batch_id, command, current_user) return ResponseUtil.success(data=result) @Log( title='测试数据批次', business_type=BusinessType.CLEAN, request_log_mode='include', request_include_fields=(RequestLogFieldRoot.PATH_PARAMS.field('batch_id'),), response_log_mode='summary', ) @test_batch_controller.post( '/{batch_id}/adoption/execute', dependencies=[UserInterfaceAuthDependency('system:testbatch:adopt')], ) async def execute_test_batch_adoption( request: Request, batch_id: Annotated[int, Path(gt=0, description='测试批次ID')], command: AdoptionExecuteRequestModel, query_db: Annotated[AsyncSession, DBSessionDependency()], current_user: Annotated[CurrentUserModel, CurrentUserDependency()], ) -> Response: result = await TestBatchAdoptionService.execute(query_db, batch_id, command, current_user) return ResponseUtil.success(msg=result.message) @Log( title='测试数据批次', business_type=BusinessType.CLEAN, request_log_mode='include', request_include_fields=(RequestLogFieldRoot.PATH_PARAMS.field('batch_id'),), response_log_mode='summary', ) @test_batch_controller.post( '/{batch_id}/purge/preview', dependencies=[UserInterfaceAuthDependency('system:testbatch:purge')], ) async def preview_test_batch_purge( request: Request, batch_id: Annotated[int, Path(gt=0, description='测试批次ID')], query_db: Annotated[AsyncSession, DBSessionDependency()], current_user: Annotated[CurrentUserModel, CurrentUserDependency()], ) -> Response: result = await TestBatchPurgeService.preview(query_db, batch_id, current_user) return ResponseUtil.success(data=result) @Log( title='测试数据批次', business_type=BusinessType.CLEAN, request_log_mode='include', request_include_fields=(RequestLogFieldRoot.PATH_PARAMS.field('batch_id'),), response_log_mode='summary', ) @test_batch_controller.post( '/{batch_id}/purge', dependencies=[UserInterfaceAuthDependency('system:testbatch:purge')], ) async def purge_test_batch( request: Request, batch_id: Annotated[int, Path(gt=0, description='测试批次ID')], command: PurgeExecuteRequestModel, query_db: Annotated[AsyncSession, DBSessionDependency()], current_user: Annotated[CurrentUserModel, CurrentUserDependency()], ) -> Response: result = await TestBatchPurgeService.purge(query_db, batch_id, command, current_user) return ResponseUtil.success(msg=result.message) @test_batch_controller.get( '/{batch_id}', dependencies=[UserInterfaceAuthDependency('system:testbatch:query')], ) async def get_test_batch( request: Request, batch_id: Annotated[int, Path(gt=0, description='测试批次ID')], query_db: Annotated[AsyncSession, DBSessionDependency()], current_user: Annotated[CurrentUserModel, CurrentUserDependency()], ) -> Response: result = await TestBatchService.get_batch_detail(query_db, batch_id, current_user) return ResponseUtil.success(data=result)