init: 初始化 dpb 桃育种系统代码库
前后端 + 后端 FastAPI 全量源码、部署脚本与文档。
This commit is contained in:
@@ -0,0 +1,134 @@
|
||||
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 (
|
||||
SeedLotCreateSchema,
|
||||
SeedLotOutSchema,
|
||||
SeedLotQueryParam,
|
||||
SeedLotUpdateSchema,
|
||||
)
|
||||
from .service import SeedLotService
|
||||
|
||||
SeedLotRouter = APIRouter(route_class=OperationLogRoute, prefix="/seed_lot", tags=["种子批管理"])
|
||||
|
||||
|
||||
@SeedLotRouter.get("/detail/{id}", summary="获取种子批详情", response_model=ResponseSchema[SeedLotOutSchema])
|
||||
async def get_seed_lot__detail_controller(
|
||||
auth: Annotated[AuthSchema, Depends(AuthPermission(["module_bre:seed_lot:detail"]))],
|
||||
id: Annotated[int, Path(description="种子批ID")],
|
||||
db: Annotated[AsyncSession, Depends(db_getter)],
|
||||
) -> JSONResponse:
|
||||
service = SeedLotService(auth, db)
|
||||
result_dict = await service.detail(id=id)
|
||||
return SuccessResponse(data=result_dict, msg="获取种子批详情成功")
|
||||
|
||||
|
||||
@SeedLotRouter.get("/list", summary="分页查询种子批", response_model=ResponseSchema[PageResultSchema[SeedLotOutSchema]])
|
||||
async def get_seed_lot__list_controller(
|
||||
auth: Annotated[AuthSchema, Depends(AuthPermission(["module_bre:seed_lot:query"]))],
|
||||
page: Annotated[PaginationQueryParam, Depends()],
|
||||
search: Annotated[SeedLotQueryParam, Query()],
|
||||
db: Annotated[AsyncSession, Depends(db_getter)],
|
||||
) -> JSONResponse:
|
||||
service = SeedLotService(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="查询种子批列表成功")
|
||||
|
||||
|
||||
@SeedLotRouter.get("/options", summary="种子批下拉选项")
|
||||
async def get_seed_lot__options_controller(
|
||||
auth: Annotated[AuthSchema, Depends(AuthPermission(["module_bre:seed_lot:query"]))],
|
||||
db: Annotated[AsyncSession, Depends(db_getter)],
|
||||
) -> JSONResponse:
|
||||
service = SeedLotService(auth, db)
|
||||
options = await service.list_options()
|
||||
return SuccessResponse(data=options, msg="获取种子批选项成功")
|
||||
|
||||
|
||||
@SeedLotRouter.post("/create", summary="创建种子批", response_model=ResponseSchema[SeedLotOutSchema])
|
||||
async def create_seed_lot__controller(
|
||||
auth: Annotated[AuthSchema, Depends(AuthPermission(["module_bre:seed_lot:create"]))],
|
||||
data: Annotated[SeedLotCreateSchema, Body(description="创建参数")],
|
||||
db: Annotated[AsyncSession, Depends(db_getter)],
|
||||
) -> JSONResponse:
|
||||
service = SeedLotService(auth, db)
|
||||
result_dict = await service.create(data=data)
|
||||
return SuccessResponse(data=result_dict, msg="创建种子批成功")
|
||||
|
||||
|
||||
@SeedLotRouter.put("/update/{id}", summary="修改种子批", response_model=ResponseSchema[SeedLotOutSchema])
|
||||
async def update_seed_lot__controller(
|
||||
auth: Annotated[AuthSchema, Depends(AuthPermission(["module_bre:seed_lot:update"]))],
|
||||
id: Annotated[int, Path(description="种子批ID")],
|
||||
data: Annotated[SeedLotUpdateSchema, Body(description="修改参数")],
|
||||
db: Annotated[AsyncSession, Depends(db_getter)],
|
||||
) -> JSONResponse:
|
||||
service = SeedLotService(auth, db)
|
||||
result_dict = await service.update(id=id, data=data)
|
||||
return SuccessResponse(data=result_dict, msg="修改种子批成功")
|
||||
|
||||
|
||||
@SeedLotRouter.delete("/delete", summary="删除种子批", response_model=ResponseSchema[None])
|
||||
async def delete_seed_lot__controller(
|
||||
auth: Annotated[AuthSchema, Depends(AuthPermission(["module_bre:seed_lot:delete"]))],
|
||||
ids: Annotated[list[int], Body(description="ID列表")],
|
||||
db: Annotated[AsyncSession, Depends(db_getter)],
|
||||
) -> JSONResponse:
|
||||
service = SeedLotService(auth, db)
|
||||
await service.delete(ids=ids)
|
||||
return SuccessResponse(msg="删除种子批成功")
|
||||
|
||||
|
||||
@SeedLotRouter.post("/export", summary="导出种子批")
|
||||
async def export_seed_lot__controller(
|
||||
auth: Annotated[AuthSchema, Depends(AuthPermission(["module_bre:seed_lot:export"]))],
|
||||
search: Annotated[SeedLotQueryParam, Query()],
|
||||
db: Annotated[AsyncSession, Depends(db_getter)],
|
||||
) -> StreamingResponse:
|
||||
service = SeedLotService(auth, db)
|
||||
result_dict_list = await service.get_list(search=search)
|
||||
export_result = SeedLotService.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')}"},
|
||||
)
|
||||
|
||||
|
||||
@SeedLotRouter.post("/import", summary="导入种子批", response_model=ResponseSchema[ImportResultSchema])
|
||||
async def import_seed_lot__controller(
|
||||
file: Annotated[UploadFile, File(description="导入文件")],
|
||||
auth: Annotated[AuthSchema, Depends(AuthPermission(["module_bre:seed_lot:import"]))],
|
||||
db: Annotated[AsyncSession, Depends(db_getter)],
|
||||
) -> JSONResponse:
|
||||
service = SeedLotService(auth, db)
|
||||
batch_import_result = await service.batch_import(file=file, update_support=True)
|
||||
return SuccessResponse(data=batch_import_result, msg="导入种子批成功")
|
||||
|
||||
|
||||
@SeedLotRouter.post("/download/template", summary="获取种子批导入模板", dependencies=[Depends(AuthPermission(["module_bre:seed_lot:download"]))])
|
||||
async def download_seed_lot__template_controller() -> StreamingResponse:
|
||||
import_template_result = SeedLotService.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