Files
dpb/backend/scripts/_backup_query_param/planting__controller.py.bak
T
34047007@qq.com b95053c52c init: 初始化 dpb 桃育种系统代码库
前后端 + 后端 FastAPI 全量源码、部署脚本与文档。
2026-08-06 00:17:49 +08:00

136 lines
6.2 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 (
PlantingCreateSchema,
PlantingOutSchema,
PlantingQueryParam,
PlantingUpdateSchema,
)
from .service import PlantingService
PlantingRouter = APIRouter(route_class=OperationLogRoute, prefix="/planting", tags=["定植管理"])
@PlantingRouter.get("/detail/{id}", summary="获取定植管理详情", response_model=ResponseSchema[PlantingOutSchema])
async def get_planting__detail_controller(
auth: Annotated[AuthSchema, Depends(AuthPermission(["module_bre:planting:detail"]))],
id: Annotated[int, Path(description="定植管理ID")],
db: Annotated[AsyncSession, Depends(db_getter)],
) -> JSONResponse:
service = PlantingService(auth, db)
result_dict = await service.detail(id=id)
return SuccessResponse(data=result_dict, msg="获取定植管理详情成功")
@PlantingRouter.get("/list", summary="分页查询定植管理", response_model=ResponseSchema[PageResultSchema[PlantingOutSchema]])
async def get_planting__list_controller(
auth: Annotated[AuthSchema, Depends(AuthPermission(["module_bre:planting:query"]))],
page: Annotated[PaginationQueryParam, Depends()],
search: Annotated[PlantingQueryParam, Query()],
db: Annotated[AsyncSession, Depends(db_getter)],
) -> JSONResponse:
service = PlantingService(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="查询定植管理列表成功")
@PlantingRouter.get("/options", summary="定植管理下拉选项")
async def get_planting__options_controller(
auth: Annotated[AuthSchema, Depends(AuthPermission(["module_bre:planting:query"]))],
db: Annotated[AsyncSession, Depends(db_getter)],
) -> JSONResponse:
service = PlantingService(auth, db)
options = await service.list_options()
return SuccessResponse(data=options, msg="获取定植管理选项成功")
@PlantingRouter.post("/create", summary="创建定植管理", response_model=ResponseSchema[PlantingOutSchema])
async def create_planting__controller(
auth: Annotated[AuthSchema, Depends(AuthPermission(["module_bre:planting:create"]))],
data: Annotated[PlantingCreateSchema, Body(description="创建参数")],
db: Annotated[AsyncSession, Depends(db_getter)],
) -> JSONResponse:
service = PlantingService(auth, db)
result_dict = await service.create(data=data)
return SuccessResponse(data=result_dict, msg="创建定植管理成功")
@PlantingRouter.put("/update/{id}", summary="修改定植管理", response_model=ResponseSchema[PlantingOutSchema])
async def update_planting__controller(
auth: Annotated[AuthSchema, Depends(AuthPermission(["module_bre:planting:update"]))],
id: Annotated[int, Path(description="定植管理ID")],
data: Annotated[PlantingUpdateSchema, Body(description="修改参数")],
db: Annotated[AsyncSession, Depends(db_getter)],
) -> JSONResponse:
service = PlantingService(auth, db)
result_dict = await service.update(id=id, data=data)
return SuccessResponse(data=result_dict, msg="修改定植管理成功")
@PlantingRouter.delete("/delete", summary="删除定植管理", response_model=ResponseSchema[None])
async def delete_planting__controller(
auth: Annotated[AuthSchema, Depends(AuthPermission(["module_bre:planting:delete"]))],
ids: Annotated[list[int], Body(description="ID列表")],
db: Annotated[AsyncSession, Depends(db_getter)],
) -> JSONResponse:
service = PlantingService(auth, db)
await service.delete(ids=ids)
return SuccessResponse(msg="删除定植管理成功")
@PlantingRouter.post("/export", summary="导出定植管理")
async def export_planting__controller(
auth: Annotated[AuthSchema, Depends(AuthPermission(["module_bre:planting:export"]))],
search: Annotated[PlantingQueryParam, Query()],
db: Annotated[AsyncSession, Depends(db_getter)],
) -> StreamingResponse:
service = PlantingService(auth, db)
result_dict_list = await service.get_list(search=search)
export_result = PlantingService.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')}"},
)
@PlantingRouter.post("/import", summary="导入定植管理", response_model=ResponseSchema[str])
async def import_planting__controller(
file: Annotated[UploadFile, File(description="导入文件")],
auth: Annotated[AuthSchema, Depends(AuthPermission(["module_bre:planting:import"]))],
db: Annotated[AsyncSession, Depends(db_getter)],
) -> JSONResponse:
service = PlantingService(auth, db)
batch_import_result = await service.batch_import(file=file, update_support=True)
return SuccessResponse(data=batch_import_result, msg="导入定植管理成功")
@PlantingRouter.post("/download/template", summary="获取定植管理导入模板", dependencies=[Depends(AuthPermission(["module_bre:planting:download"]))])
async def download_planting__template_controller() -> StreamingResponse:
import_template_result = PlantingService.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",
},
)