136 lines
6.4 KiB
Plaintext
136 lines
6.4 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 (
|
|
PollinationCreateSchema,
|
|
PollinationOutSchema,
|
|
PollinationQueryParam,
|
|
PollinationUpdateSchema,
|
|
)
|
|
from .service import PollinationService
|
|
|
|
PollinationRouter = APIRouter(route_class=OperationLogRoute, prefix="/pollination", tags=["授粉管理"])
|
|
|
|
|
|
@PollinationRouter.get("/detail/{id}", summary="获取授粉管理详情", response_model=ResponseSchema[PollinationOutSchema])
|
|
async def get_pollination__detail_controller(
|
|
auth: Annotated[AuthSchema, Depends(AuthPermission(["module_bre:pollination:detail"]))],
|
|
id: Annotated[int, Path(description="授粉管理ID")],
|
|
db: Annotated[AsyncSession, Depends(db_getter)],
|
|
) -> JSONResponse:
|
|
service = PollinationService(auth, db)
|
|
result_dict = await service.detail(id=id)
|
|
return SuccessResponse(data=result_dict, msg="获取授粉管理详情成功")
|
|
|
|
|
|
@PollinationRouter.get("/list", summary="分页查询授粉管理", response_model=ResponseSchema[PageResultSchema[PollinationOutSchema]])
|
|
async def get_pollination__list_controller(
|
|
auth: Annotated[AuthSchema, Depends(AuthPermission(["module_bre:pollination:query"]))],
|
|
page: Annotated[PaginationQueryParam, Depends()],
|
|
search: Annotated[PollinationQueryParam, Query()],
|
|
db: Annotated[AsyncSession, Depends(db_getter)],
|
|
) -> JSONResponse:
|
|
service = PollinationService(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="查询授粉管理列表成功")
|
|
|
|
|
|
@PollinationRouter.get("/options", summary="授粉管理下拉选项")
|
|
async def get_pollination__options_controller(
|
|
auth: Annotated[AuthSchema, Depends(AuthPermission(["module_bre:pollination:query"]))],
|
|
db: Annotated[AsyncSession, Depends(db_getter)],
|
|
) -> JSONResponse:
|
|
service = PollinationService(auth, db)
|
|
options = await service.list_options()
|
|
return SuccessResponse(data=options, msg="获取授粉管理选项成功")
|
|
|
|
|
|
@PollinationRouter.post("/create", summary="创建授粉管理", response_model=ResponseSchema[PollinationOutSchema])
|
|
async def create_pollination__controller(
|
|
auth: Annotated[AuthSchema, Depends(AuthPermission(["module_bre:pollination:create"]))],
|
|
data: Annotated[PollinationCreateSchema, Body(description="创建参数")],
|
|
db: Annotated[AsyncSession, Depends(db_getter)],
|
|
) -> JSONResponse:
|
|
service = PollinationService(auth, db)
|
|
result_dict = await service.create(data=data)
|
|
return SuccessResponse(data=result_dict, msg="创建授粉管理成功")
|
|
|
|
|
|
@PollinationRouter.put("/update/{id}", summary="修改授粉管理", response_model=ResponseSchema[PollinationOutSchema])
|
|
async def update_pollination__controller(
|
|
auth: Annotated[AuthSchema, Depends(AuthPermission(["module_bre:pollination:update"]))],
|
|
id: Annotated[int, Path(description="授粉管理ID")],
|
|
data: Annotated[PollinationUpdateSchema, Body(description="修改参数")],
|
|
db: Annotated[AsyncSession, Depends(db_getter)],
|
|
) -> JSONResponse:
|
|
service = PollinationService(auth, db)
|
|
result_dict = await service.update(id=id, data=data)
|
|
return SuccessResponse(data=result_dict, msg="修改授粉管理成功")
|
|
|
|
|
|
@PollinationRouter.delete("/delete", summary="删除授粉管理", response_model=ResponseSchema[None])
|
|
async def delete_pollination__controller(
|
|
auth: Annotated[AuthSchema, Depends(AuthPermission(["module_bre:pollination:delete"]))],
|
|
ids: Annotated[list[int], Body(description="ID列表")],
|
|
db: Annotated[AsyncSession, Depends(db_getter)],
|
|
) -> JSONResponse:
|
|
service = PollinationService(auth, db)
|
|
await service.delete(ids=ids)
|
|
return SuccessResponse(msg="删除授粉管理成功")
|
|
|
|
|
|
@PollinationRouter.post("/export", summary="导出授粉管理")
|
|
async def export_pollination__controller(
|
|
auth: Annotated[AuthSchema, Depends(AuthPermission(["module_bre:pollination:export"]))],
|
|
search: Annotated[PollinationQueryParam, Query()],
|
|
db: Annotated[AsyncSession, Depends(db_getter)],
|
|
) -> StreamingResponse:
|
|
service = PollinationService(auth, db)
|
|
result_dict_list = await service.get_list(search=search)
|
|
export_result = PollinationService.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')}"},
|
|
)
|
|
|
|
|
|
@PollinationRouter.post("/import", summary="导入授粉管理", response_model=ResponseSchema[str])
|
|
async def import_pollination__controller(
|
|
file: Annotated[UploadFile, File(description="导入文件")],
|
|
auth: Annotated[AuthSchema, Depends(AuthPermission(["module_bre:pollination:import"]))],
|
|
db: Annotated[AsyncSession, Depends(db_getter)],
|
|
) -> JSONResponse:
|
|
service = PollinationService(auth, db)
|
|
batch_import_result = await service.batch_import(file=file, update_support=True)
|
|
return SuccessResponse(data=batch_import_result, msg="导入授粉管理成功")
|
|
|
|
|
|
@PollinationRouter.post("/download/template", summary="获取授粉管理导入模板", dependencies=[Depends(AuthPermission(["module_bre:pollination:download"]))])
|
|
async def download_pollination__template_controller() -> StreamingResponse:
|
|
import_template_result = PollinationService.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",
|
|
},
|
|
)
|
|
|