147 lines
6.6 KiB
Python
147 lines
6.6 KiB
Python
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, ImportResultSchema
|
|
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 (
|
|
PlanCreateSchema,
|
|
PlanOutSchema,
|
|
PlanQueryParam,
|
|
PlanUpdateSchema,
|
|
)
|
|
from .service import PlanService
|
|
|
|
PlanRouter = APIRouter(route_class=OperationLogRoute, prefix="/plan", tags=["育种计划"])
|
|
|
|
|
|
@PlanRouter.get("/detail/{id}", summary="获取育种计划详情", response_model=ResponseSchema[PlanOutSchema])
|
|
async def get_plan__detail_controller(
|
|
auth: Annotated[AuthSchema, Depends(AuthPermission(["module_bre:plan:detail"]))],
|
|
id: Annotated[int, Path(description="育种计划ID")],
|
|
db: Annotated[AsyncSession, Depends(db_getter)],
|
|
) -> JSONResponse:
|
|
service = PlanService(auth, db)
|
|
result_dict = await service.detail(id=id)
|
|
return SuccessResponse(data=result_dict, msg="获取育种计划详情成功")
|
|
|
|
|
|
@PlanRouter.get("/list", summary="分页查询育种计划", response_model=ResponseSchema[PageResultSchema[PlanOutSchema]])
|
|
async def get_plan__list_controller(
|
|
auth: Annotated[AuthSchema, Depends(AuthPermission(["module_bre:plan:query"]))],
|
|
page: Annotated[PaginationQueryParam, Depends()],
|
|
search: Annotated[PlanQueryParam, Query()],
|
|
db: Annotated[AsyncSession, Depends(db_getter)],
|
|
) -> JSONResponse:
|
|
service = PlanService(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="查询育种计划列表成功")
|
|
|
|
|
|
@PlanRouter.get("/trace/{id}", summary="育种计划可追溯(计划→目标→组合→树 / 计划→试验→试验点→树)")
|
|
async def get_plan__trace_controller(
|
|
auth: Annotated[AuthSchema, Depends(AuthPermission(["module_bre:plan:query"]))],
|
|
id: Annotated[int, Path(description="育种计划ID")],
|
|
db: Annotated[AsyncSession, Depends(db_getter)],
|
|
) -> JSONResponse:
|
|
service = PlanService(auth, db)
|
|
result_dict = await service.trace(plan_id=id)
|
|
return SuccessResponse(data=result_dict, msg="获取育种计划可追溯链成功")
|
|
|
|
|
|
@PlanRouter.get("/options", summary="育种计划下拉选项")
|
|
async def get_plan__options_controller(
|
|
auth: Annotated[AuthSchema, Depends(AuthPermission(["module_bre:plan:query"]))],
|
|
db: Annotated[AsyncSession, Depends(db_getter)],
|
|
) -> JSONResponse:
|
|
service = PlanService(auth, db)
|
|
options = await service.list_options()
|
|
return SuccessResponse(data=options, msg="获取育种计划选项成功")
|
|
|
|
|
|
@PlanRouter.post("/create", summary="创建育种计划", response_model=ResponseSchema[PlanOutSchema])
|
|
async def create_plan__controller(
|
|
auth: Annotated[AuthSchema, Depends(AuthPermission(["module_bre:plan:create"]))],
|
|
data: Annotated[PlanCreateSchema, Body(description="创建参数")],
|
|
db: Annotated[AsyncSession, Depends(db_getter)],
|
|
) -> JSONResponse:
|
|
service = PlanService(auth, db)
|
|
result_dict = await service.create(data=data)
|
|
return SuccessResponse(data=result_dict, msg="创建育种计划成功")
|
|
|
|
|
|
@PlanRouter.put("/update/{id}", summary="修改育种计划", response_model=ResponseSchema[PlanOutSchema])
|
|
async def update_plan__controller(
|
|
auth: Annotated[AuthSchema, Depends(AuthPermission(["module_bre:plan:update"]))],
|
|
id: Annotated[int, Path(description="育种计划ID")],
|
|
data: Annotated[PlanUpdateSchema, Body(description="修改参数")],
|
|
db: Annotated[AsyncSession, Depends(db_getter)],
|
|
) -> JSONResponse:
|
|
service = PlanService(auth, db)
|
|
result_dict = await service.update(id=id, data=data)
|
|
return SuccessResponse(data=result_dict, msg="修改育种计划成功")
|
|
|
|
|
|
@PlanRouter.delete("/delete", summary="删除育种计划", response_model=ResponseSchema[None])
|
|
async def delete_plan__controller(
|
|
auth: Annotated[AuthSchema, Depends(AuthPermission(["module_bre:plan:delete"]))],
|
|
ids: Annotated[list[int], Body(description="ID列表")],
|
|
db: Annotated[AsyncSession, Depends(db_getter)],
|
|
) -> JSONResponse:
|
|
service = PlanService(auth, db)
|
|
await service.delete(ids=ids)
|
|
return SuccessResponse(msg="删除育种计划成功")
|
|
|
|
|
|
@PlanRouter.post("/export", summary="导出育种计划")
|
|
async def export_plan__controller(
|
|
auth: Annotated[AuthSchema, Depends(AuthPermission(["module_bre:plan:export"]))],
|
|
search: Annotated[PlanQueryParam, Query()],
|
|
db: Annotated[AsyncSession, Depends(db_getter)],
|
|
) -> StreamingResponse:
|
|
service = PlanService(auth, db)
|
|
result_dict_list = await service.get_list(search=search)
|
|
export_result = PlanService.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')}"},
|
|
)
|
|
|
|
|
|
@PlanRouter.post("/import", summary="导入育种计划", response_model=ResponseSchema[ImportResultSchema])
|
|
async def import_plan__controller(
|
|
file: Annotated[UploadFile, File(description="导入文件")],
|
|
auth: Annotated[AuthSchema, Depends(AuthPermission(["module_bre:plan:import"]))],
|
|
db: Annotated[AsyncSession, Depends(db_getter)],
|
|
) -> JSONResponse:
|
|
service = PlanService(auth, db)
|
|
batch_import_result = await service.batch_import(file=file, update_support=True)
|
|
return SuccessResponse(data=batch_import_result, msg="导入育种计划成功")
|
|
|
|
|
|
@PlanRouter.post("/download/template", summary="获取育种计划导入模板", dependencies=[Depends(AuthPermission(["module_bre:plan:download"]))])
|
|
async def download_plan__template_controller() -> StreamingResponse:
|
|
import_template_result = PlanService.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",
|
|
},
|
|
)
|
|
|