136 lines
6.2 KiB
Plaintext
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 (
|
|
SeedlingCreateSchema,
|
|
SeedlingOutSchema,
|
|
SeedlingQueryParam,
|
|
SeedlingUpdateSchema,
|
|
)
|
|
from .service import SeedlingService
|
|
|
|
SeedlingRouter = APIRouter(route_class=OperationLogRoute, prefix="/seedling", tags=["育苗管理"])
|
|
|
|
|
|
@SeedlingRouter.get("/detail/{id}", summary="获取育苗管理详情", response_model=ResponseSchema[SeedlingOutSchema])
|
|
async def get_seedling__detail_controller(
|
|
auth: Annotated[AuthSchema, Depends(AuthPermission(["module_bre:seedling:detail"]))],
|
|
id: Annotated[int, Path(description="育苗管理ID")],
|
|
db: Annotated[AsyncSession, Depends(db_getter)],
|
|
) -> JSONResponse:
|
|
service = SeedlingService(auth, db)
|
|
result_dict = await service.detail(id=id)
|
|
return SuccessResponse(data=result_dict, msg="获取育苗管理详情成功")
|
|
|
|
|
|
@SeedlingRouter.get("/list", summary="分页查询育苗管理", response_model=ResponseSchema[PageResultSchema[SeedlingOutSchema]])
|
|
async def get_seedling__list_controller(
|
|
auth: Annotated[AuthSchema, Depends(AuthPermission(["module_bre:seedling:query"]))],
|
|
page: Annotated[PaginationQueryParam, Depends()],
|
|
search: Annotated[SeedlingQueryParam, Query()],
|
|
db: Annotated[AsyncSession, Depends(db_getter)],
|
|
) -> JSONResponse:
|
|
service = SeedlingService(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="查询育苗管理列表成功")
|
|
|
|
|
|
@SeedlingRouter.get("/options", summary="育苗管理下拉选项")
|
|
async def get_seedling__options_controller(
|
|
auth: Annotated[AuthSchema, Depends(AuthPermission(["module_bre:seedling:query"]))],
|
|
db: Annotated[AsyncSession, Depends(db_getter)],
|
|
) -> JSONResponse:
|
|
service = SeedlingService(auth, db)
|
|
options = await service.list_options()
|
|
return SuccessResponse(data=options, msg="获取育苗管理选项成功")
|
|
|
|
|
|
@SeedlingRouter.post("/create", summary="创建育苗管理", response_model=ResponseSchema[SeedlingOutSchema])
|
|
async def create_seedling__controller(
|
|
auth: Annotated[AuthSchema, Depends(AuthPermission(["module_bre:seedling:create"]))],
|
|
data: Annotated[SeedlingCreateSchema, Body(description="创建参数")],
|
|
db: Annotated[AsyncSession, Depends(db_getter)],
|
|
) -> JSONResponse:
|
|
service = SeedlingService(auth, db)
|
|
result_dict = await service.create(data=data)
|
|
return SuccessResponse(data=result_dict, msg="创建育苗管理成功")
|
|
|
|
|
|
@SeedlingRouter.put("/update/{id}", summary="修改育苗管理", response_model=ResponseSchema[SeedlingOutSchema])
|
|
async def update_seedling__controller(
|
|
auth: Annotated[AuthSchema, Depends(AuthPermission(["module_bre:seedling:update"]))],
|
|
id: Annotated[int, Path(description="育苗管理ID")],
|
|
data: Annotated[SeedlingUpdateSchema, Body(description="修改参数")],
|
|
db: Annotated[AsyncSession, Depends(db_getter)],
|
|
) -> JSONResponse:
|
|
service = SeedlingService(auth, db)
|
|
result_dict = await service.update(id=id, data=data)
|
|
return SuccessResponse(data=result_dict, msg="修改育苗管理成功")
|
|
|
|
|
|
@SeedlingRouter.delete("/delete", summary="删除育苗管理", response_model=ResponseSchema[None])
|
|
async def delete_seedling__controller(
|
|
auth: Annotated[AuthSchema, Depends(AuthPermission(["module_bre:seedling:delete"]))],
|
|
ids: Annotated[list[int], Body(description="ID列表")],
|
|
db: Annotated[AsyncSession, Depends(db_getter)],
|
|
) -> JSONResponse:
|
|
service = SeedlingService(auth, db)
|
|
await service.delete(ids=ids)
|
|
return SuccessResponse(msg="删除育苗管理成功")
|
|
|
|
|
|
@SeedlingRouter.post("/export", summary="导出育苗管理")
|
|
async def export_seedling__controller(
|
|
auth: Annotated[AuthSchema, Depends(AuthPermission(["module_bre:seedling:export"]))],
|
|
search: Annotated[SeedlingQueryParam, Query()],
|
|
db: Annotated[AsyncSession, Depends(db_getter)],
|
|
) -> StreamingResponse:
|
|
service = SeedlingService(auth, db)
|
|
result_dict_list = await service.get_list(search=search)
|
|
export_result = SeedlingService.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')}"},
|
|
)
|
|
|
|
|
|
@SeedlingRouter.post("/import", summary="导入育苗管理", response_model=ResponseSchema[str])
|
|
async def import_seedling__controller(
|
|
file: Annotated[UploadFile, File(description="导入文件")],
|
|
auth: Annotated[AuthSchema, Depends(AuthPermission(["module_bre:seedling:import"]))],
|
|
db: Annotated[AsyncSession, Depends(db_getter)],
|
|
) -> JSONResponse:
|
|
service = SeedlingService(auth, db)
|
|
batch_import_result = await service.batch_import(file=file, update_support=True)
|
|
return SuccessResponse(data=batch_import_result, msg="导入育苗管理成功")
|
|
|
|
|
|
@SeedlingRouter.post("/download/template", summary="获取育苗管理导入模板", dependencies=[Depends(AuthPermission(["module_bre:seedling:download"]))])
|
|
async def download_seedling__template_controller() -> StreamingResponse:
|
|
import_template_result = SeedlingService.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",
|
|
},
|
|
)
|
|
|