136 lines
6.3 KiB
Plaintext
136 lines
6.3 KiB
Plaintext
import urllib.parse
|
|||
|
|
from typing import Annotated
|
||
|
|
|
||
|
|
from fastapi import APIRouter, Body, Depends, File, Path, Query, UploadFile
|
||
|
|
from fastapi.responses import JSONResponse, StreamingResponse
|
||
|
|
from sqlalchemy.ext.asyncio import AsyncSession
|
||
|
|
|
||
|
|
from app.common.response import ResponseSchema, StreamResponse, SuccessResponse
|
||
|
|
from app.core.base_schema import AuthSchema, PageResultSchema, PaginationQueryParam
|
||
|
|
from app.core.dependencies import AuthPermission, db_getter
|
||
|
|
from app.core.router_class import OperationLogRoute
|
||
|
|
from app.utils.common_util import bytes2file_response
|
||
|
|
|
||
|
|
from .schema import (
|
||
|
|
TrialStudyCreateSchema,
|
||
|
|
TrialStudyOutSchema,
|
||
|
|
TrialStudyQueryParam,
|
||
|
|
TrialStudyUpdateSchema,
|
||
|
|
)
|
||
|
|
from .service import TrialStudyService
|
||
|
|
|
||
|
|
TrialStudyRouter = APIRouter(route_class=OperationLogRoute, prefix="/trial_study", tags=["试验执行"])
|
||
|
|
|
||
|
|
|
||
|
|
@TrialStudyRouter.get("/detail/{id}", summary="获取试验执行详情", response_model=ResponseSchema[TrialStudyOutSchema])
|
||
|
|
async def get_trial_study__detail_controller(
|
||
|
|
auth: Annotated[AuthSchema, Depends(AuthPermission(["module_bre:trial_study:detail"]))],
|
||
|
|
id: Annotated[int, Path(description="试验执行ID")],
|
||
|
|
db: Annotated[AsyncSession, Depends(db_getter)],
|
||
|
|
) -> JSONResponse:
|
||
|
|
service = TrialStudyService(auth, db)
|
||
|
|
result_dict = await service.detail(id=id)
|
||
|
|
return SuccessResponse(data=result_dict, msg="获取试验执行详情成功")
|
||
|
|
|
||
|
|
|
||
|
|
@TrialStudyRouter.get("/list", summary="分页查询试验执行", response_model=ResponseSchema[PageResultSchema[TrialStudyOutSchema]])
|
||
|
|
async def get_trial_study__list_controller(
|
||
|
|
auth: Annotated[AuthSchema, Depends(AuthPermission(["module_bre:trial_study:query"]))],
|
||
|
|
page: Annotated[PaginationQueryParam, Depends()],
|
||
|
|
search: Annotated[TrialStudyQueryParam, Query()],
|
||
|
|
db: Annotated[AsyncSession, Depends(db_getter)],
|
||
|
|
) -> JSONResponse:
|
||
|
|
service = TrialStudyService(auth, db)
|
||
|
|
result_dict = await service.page(
|
||
|
|
page_no=page.page_no,
|
||
|
|
page_size=page.page_size,
|
||
|
|
search=search,
|
||
|
|
order_by=page.order_by,
|
||
|
|
)
|
||
|
|
return SuccessResponse(data=result_dict, msg="查询试验执行列表成功")
|
||
|
|
|
||
|
|
|
||
|
|
@TrialStudyRouter.get("/options", summary="试验执行下拉选项")
|
||
|
|
async def get_trial_study__options_controller(
|
||
|
|
auth: Annotated[AuthSchema, Depends(AuthPermission(["module_bre:trial_study:query"]))],
|
||
|
|
db: Annotated[AsyncSession, Depends(db_getter)],
|
||
|
|
) -> JSONResponse:
|
||
|
|
service = TrialStudyService(auth, db)
|
||
|
|
options = await service.list_options()
|
||
|
|
return SuccessResponse(data=options, msg="获取试验执行选项成功")
|
||
|
|
|
||
|
|
|
||
|
|
@TrialStudyRouter.post("/create", summary="创建试验执行", response_model=ResponseSchema[TrialStudyOutSchema])
|
||
|
|
async def create_trial_study__controller(
|
||
|
|
auth: Annotated[AuthSchema, Depends(AuthPermission(["module_bre:trial_study:create"]))],
|
||
|
|
data: Annotated[TrialStudyCreateSchema, Body(description="创建参数")],
|
||
|
|
db: Annotated[AsyncSession, Depends(db_getter)],
|
||
|
|
) -> JSONResponse:
|
||
|
|
service = TrialStudyService(auth, db)
|
||
|
|
result_dict = await service.create(data=data)
|
||
|
|
return SuccessResponse(data=result_dict, msg="创建试验执行成功")
|
||
|
|
|
||
|
|
|
||
|
|
@TrialStudyRouter.put("/update/{id}", summary="修改试验执行", response_model=ResponseSchema[TrialStudyOutSchema])
|
||
|
|
async def update_trial_study__controller(
|
||
|
|
auth: Annotated[AuthSchema, Depends(AuthPermission(["module_bre:trial_study:update"]))],
|
||
|
|
id: Annotated[int, Path(description="试验执行ID")],
|
||
|
|
data: Annotated[TrialStudyUpdateSchema, Body(description="修改参数")],
|
||
|
|
db: Annotated[AsyncSession, Depends(db_getter)],
|
||
|
|
) -> JSONResponse:
|
||
|
|
service = TrialStudyService(auth, db)
|
||
|
|
result_dict = await service.update(id=id, data=data)
|
||
|
|
return SuccessResponse(data=result_dict, msg="修改试验执行成功")
|
||
|
|
|
||
|
|
|
||
|
|
@TrialStudyRouter.delete("/delete", summary="删除试验执行", response_model=ResponseSchema[None])
|
||
|
|
async def delete_trial_study__controller(
|
||
|
|
auth: Annotated[AuthSchema, Depends(AuthPermission(["module_bre:trial_study:delete"]))],
|
||
|
|
ids: Annotated[list[int], Body(description="ID列表")],
|
||
|
|
db: Annotated[AsyncSession, Depends(db_getter)],
|
||
|
|
) -> JSONResponse:
|
||
|
|
service = TrialStudyService(auth, db)
|
||
|
|
await service.delete(ids=ids)
|
||
|
|
return SuccessResponse(msg="删除试验执行成功")
|
||
|
|
|
||
|
|
|
||
|
|
@TrialStudyRouter.post("/export", summary="导出试验执行")
|
||
|
|
async def export_trial_study__controller(
|
||
|
|
auth: Annotated[AuthSchema, Depends(AuthPermission(["module_bre:trial_study:export"]))],
|
||
|
|
search: Annotated[TrialStudyQueryParam, Query()],
|
||
|
|
db: Annotated[AsyncSession, Depends(db_getter)],
|
||
|
|
) -> StreamingResponse:
|
||
|
|
service = TrialStudyService(auth, db)
|
||
|
|
result_dict_list = await service.get_list(search=search)
|
||
|
|
export_result = TrialStudyService.batch_export(obj_list=[item.model_dump() for item in result_dict_list])
|
||
|
|
return StreamResponse(
|
||
|
|
data=bytes2file_response(export_result),
|
||
|
|
media_type="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
|
||
|
|
headers={"Content-Disposition": f"attachment; filename={urllib.parse.quote('试验执行管理.xlsx')}"},
|
||
|
|
)
|
||
|
|
|
||
|
|
|
||
|
|
@TrialStudyRouter.post("/import", summary="导入试验执行", response_model=ResponseSchema[str])
|
||
|
|
async def import_trial_study__controller(
|
||
|
|
file: Annotated[UploadFile, File(description="导入文件")],
|
||
|
|
auth: Annotated[AuthSchema, Depends(AuthPermission(["module_bre:trial_study:import"]))],
|
||
|
|
db: Annotated[AsyncSession, Depends(db_getter)],
|
||
|
|
) -> JSONResponse:
|
||
|
|
service = TrialStudyService(auth, db)
|
||
|
|
batch_import_result = await service.batch_import(file=file, update_support=True)
|
||
|
|
return SuccessResponse(data=batch_import_result, msg="导入试验执行成功")
|
||
|
|
|
||
|
|
|
||
|
|
@TrialStudyRouter.post("/download/template", summary="获取试验执行导入模板", dependencies=[Depends(AuthPermission(["module_bre:trial_study:download"]))])
|
||
|
|
async def download_trial_study__template_controller() -> StreamingResponse:
|
||
|
|
import_template_result = TrialStudyService.import_template_download()
|
||
|
|
return StreamResponse(
|
||
|
|
data=bytes2file_response(import_template_result),
|
||
|
|
media_type="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
|
||
|
|
headers={
|
||
|
|
"Content-Disposition": f"attachment; filename={urllib.parse.quote('试验执行管理导入模板.xlsx')}",
|
||
|
|
"Access-Control-Expose-Headers": "Content-Disposition",
|
||
|
|
},
|
||
|
|
)
|
||
|
|
|