init: 初始化 dpb 桃育种系统代码库
前后端 + 后端 FastAPI 全量源码、部署脚本与文档。
This commit is contained in:
@@ -0,0 +1,135 @@
|
||||
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 (
|
||||
TreeEvaluationCreateSchema,
|
||||
TreeEvaluationOutSchema,
|
||||
TreeEvaluationQueryParam,
|
||||
TreeEvaluationUpdateSchema,
|
||||
)
|
||||
from .service import TreeEvaluationService
|
||||
|
||||
TreeEvaluationRouter = APIRouter(route_class=OperationLogRoute, prefix="/tree_evaluation", tags=["单株评价"])
|
||||
|
||||
|
||||
@TreeEvaluationRouter.get("/detail/{id}", summary="获取单株评价详情", response_model=ResponseSchema[TreeEvaluationOutSchema])
|
||||
async def get_tree_evaluation__detail_controller(
|
||||
auth: Annotated[AuthSchema, Depends(AuthPermission(["module_bre:tree_evaluation:detail"]))],
|
||||
id: Annotated[int, Path(description="单株评价ID")],
|
||||
db: Annotated[AsyncSession, Depends(db_getter)],
|
||||
) -> JSONResponse:
|
||||
service = TreeEvaluationService(auth, db)
|
||||
result_dict = await service.detail(id=id)
|
||||
return SuccessResponse(data=result_dict, msg="获取单株评价详情成功")
|
||||
|
||||
|
||||
@TreeEvaluationRouter.get("/list", summary="分页查询单株评价", response_model=ResponseSchema[PageResultSchema[TreeEvaluationOutSchema]])
|
||||
async def get_tree_evaluation__list_controller(
|
||||
auth: Annotated[AuthSchema, Depends(AuthPermission(["module_bre:tree_evaluation:query"]))],
|
||||
page: Annotated[PaginationQueryParam, Depends()],
|
||||
search: Annotated[TreeEvaluationQueryParam, Query()],
|
||||
db: Annotated[AsyncSession, Depends(db_getter)],
|
||||
) -> JSONResponse:
|
||||
service = TreeEvaluationService(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="查询单株评价列表成功")
|
||||
|
||||
|
||||
@TreeEvaluationRouter.get("/options", summary="单株评价下拉选项")
|
||||
async def get_tree_evaluation__options_controller(
|
||||
auth: Annotated[AuthSchema, Depends(AuthPermission(["module_bre:tree_evaluation:query"]))],
|
||||
db: Annotated[AsyncSession, Depends(db_getter)],
|
||||
) -> JSONResponse:
|
||||
service = TreeEvaluationService(auth, db)
|
||||
options = await service.list_options()
|
||||
return SuccessResponse(data=options, msg="获取单株评价选项成功")
|
||||
|
||||
|
||||
@TreeEvaluationRouter.post("/create", summary="创建单株评价", response_model=ResponseSchema[TreeEvaluationOutSchema])
|
||||
async def create_tree_evaluation__controller(
|
||||
auth: Annotated[AuthSchema, Depends(AuthPermission(["module_bre:tree_evaluation:create"]))],
|
||||
data: Annotated[TreeEvaluationCreateSchema, Body(description="创建参数")],
|
||||
db: Annotated[AsyncSession, Depends(db_getter)],
|
||||
) -> JSONResponse:
|
||||
service = TreeEvaluationService(auth, db)
|
||||
result_dict = await service.create(data=data)
|
||||
return SuccessResponse(data=result_dict, msg="创建单株评价成功")
|
||||
|
||||
|
||||
@TreeEvaluationRouter.put("/update/{id}", summary="修改单株评价", response_model=ResponseSchema[TreeEvaluationOutSchema])
|
||||
async def update_tree_evaluation__controller(
|
||||
auth: Annotated[AuthSchema, Depends(AuthPermission(["module_bre:tree_evaluation:update"]))],
|
||||
id: Annotated[int, Path(description="单株评价ID")],
|
||||
data: Annotated[TreeEvaluationUpdateSchema, Body(description="修改参数")],
|
||||
db: Annotated[AsyncSession, Depends(db_getter)],
|
||||
) -> JSONResponse:
|
||||
service = TreeEvaluationService(auth, db)
|
||||
result_dict = await service.update(id=id, data=data)
|
||||
return SuccessResponse(data=result_dict, msg="修改单株评价成功")
|
||||
|
||||
|
||||
@TreeEvaluationRouter.delete("/delete", summary="删除单株评价", response_model=ResponseSchema[None])
|
||||
async def delete_tree_evaluation__controller(
|
||||
auth: Annotated[AuthSchema, Depends(AuthPermission(["module_bre:tree_evaluation:delete"]))],
|
||||
ids: Annotated[list[int], Body(description="ID列表")],
|
||||
db: Annotated[AsyncSession, Depends(db_getter)],
|
||||
) -> JSONResponse:
|
||||
service = TreeEvaluationService(auth, db)
|
||||
await service.delete(ids=ids)
|
||||
return SuccessResponse(msg="删除单株评价成功")
|
||||
|
||||
|
||||
@TreeEvaluationRouter.post("/export", summary="导出单株评价")
|
||||
async def export_tree_evaluation__controller(
|
||||
auth: Annotated[AuthSchema, Depends(AuthPermission(["module_bre:tree_evaluation:export"]))],
|
||||
search: Annotated[TreeEvaluationQueryParam, Query()],
|
||||
db: Annotated[AsyncSession, Depends(db_getter)],
|
||||
) -> StreamingResponse:
|
||||
service = TreeEvaluationService(auth, db)
|
||||
result_dict_list = await service.get_list(search=search)
|
||||
export_result = TreeEvaluationService.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')}"},
|
||||
)
|
||||
|
||||
|
||||
@TreeEvaluationRouter.post("/import", summary="导入单株评价", response_model=ResponseSchema[str])
|
||||
async def import_tree_evaluation__controller(
|
||||
file: Annotated[UploadFile, File(description="导入文件")],
|
||||
auth: Annotated[AuthSchema, Depends(AuthPermission(["module_bre:tree_evaluation:import"]))],
|
||||
db: Annotated[AsyncSession, Depends(db_getter)],
|
||||
) -> JSONResponse:
|
||||
service = TreeEvaluationService(auth, db)
|
||||
batch_import_result = await service.batch_import(file=file, update_support=True)
|
||||
return SuccessResponse(data=batch_import_result, msg="导入单株评价成功")
|
||||
|
||||
|
||||
@TreeEvaluationRouter.post("/download/template", summary="获取单株评价导入模板", dependencies=[Depends(AuthPermission(["module_bre:tree_evaluation:download"]))])
|
||||
async def download_tree_evaluation__template_controller() -> StreamingResponse:
|
||||
import_template_result = TreeEvaluationService.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",
|
||||
},
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user