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 ( SelectionRuleCreateSchema, SelectionRuleOutSchema, SelectionRuleQueryParam, SelectionRuleUpdateSchema, ) from .service import SelectionRuleService SelectionRuleRouter = APIRouter(route_class=OperationLogRoute, prefix="/selection_rule", tags=["选择规则"]) @SelectionRuleRouter.get("/detail/{id}", summary="获取选择规则详情", response_model=ResponseSchema[SelectionRuleOutSchema]) async def get_selection_rule__detail_controller( auth: Annotated[AuthSchema, Depends(AuthPermission(["module_bre:selection_rule:detail"]))], id: Annotated[int, Path(description="选择规则ID")], db: Annotated[AsyncSession, Depends(db_getter)], ) -> JSONResponse: service = SelectionRuleService(auth, db) result_dict = await service.detail(id=id) return SuccessResponse(data=result_dict, msg="获取选择规则详情成功") @SelectionRuleRouter.get("/list", summary="分页查询选择规则", response_model=ResponseSchema[PageResultSchema[SelectionRuleOutSchema]]) async def get_selection_rule__list_controller( auth: Annotated[AuthSchema, Depends(AuthPermission(["module_bre:selection_rule:query"]))], page: Annotated[PaginationQueryParam, Depends()], search: Annotated[SelectionRuleQueryParam, Query()], db: Annotated[AsyncSession, Depends(db_getter)], ) -> JSONResponse: service = SelectionRuleService(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="查询选择规则列表成功") @SelectionRuleRouter.get("/options", summary="选择规则下拉选项") async def get_selection_rule__options_controller( auth: Annotated[AuthSchema, Depends(AuthPermission(["module_bre:selection_rule:query"]))], db: Annotated[AsyncSession, Depends(db_getter)], ) -> JSONResponse: service = SelectionRuleService(auth, db) options = await service.list_options() return SuccessResponse(data=options, msg="获取选择规则选项成功") @SelectionRuleRouter.post("/create", summary="创建选择规则", response_model=ResponseSchema[SelectionRuleOutSchema]) async def create_selection_rule__controller( auth: Annotated[AuthSchema, Depends(AuthPermission(["module_bre:selection_rule:create"]))], data: Annotated[SelectionRuleCreateSchema, Body(description="创建参数")], db: Annotated[AsyncSession, Depends(db_getter)], ) -> JSONResponse: service = SelectionRuleService(auth, db) result_dict = await service.create(data=data) return SuccessResponse(data=result_dict, msg="创建选择规则成功") @SelectionRuleRouter.put("/update/{id}", summary="修改选择规则", response_model=ResponseSchema[SelectionRuleOutSchema]) async def update_selection_rule__controller( auth: Annotated[AuthSchema, Depends(AuthPermission(["module_bre:selection_rule:update"]))], id: Annotated[int, Path(description="选择规则ID")], data: Annotated[SelectionRuleUpdateSchema, Body(description="修改参数")], db: Annotated[AsyncSession, Depends(db_getter)], ) -> JSONResponse: service = SelectionRuleService(auth, db) result_dict = await service.update(id=id, data=data) return SuccessResponse(data=result_dict, msg="修改选择规则成功") @SelectionRuleRouter.delete("/delete", summary="删除选择规则", response_model=ResponseSchema[None]) async def delete_selection_rule__controller( auth: Annotated[AuthSchema, Depends(AuthPermission(["module_bre:selection_rule:delete"]))], ids: Annotated[list[int], Body(description="ID列表")], db: Annotated[AsyncSession, Depends(db_getter)], ) -> JSONResponse: service = SelectionRuleService(auth, db) await service.delete(ids=ids) return SuccessResponse(msg="删除选择规则成功") @SelectionRuleRouter.post("/export", summary="导出选择规则") async def export_selection_rule__controller( auth: Annotated[AuthSchema, Depends(AuthPermission(["module_bre:selection_rule:export"]))], search: Annotated[SelectionRuleQueryParam, Query()], db: Annotated[AsyncSession, Depends(db_getter)], ) -> StreamingResponse: service = SelectionRuleService(auth, db) result_dict_list = await service.get_list(search=search) export_result = SelectionRuleService.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')}"}, ) @SelectionRuleRouter.post("/import", summary="导入选择规则", response_model=ResponseSchema[str]) async def import_selection_rule__controller( file: Annotated[UploadFile, File(description="导入文件")], auth: Annotated[AuthSchema, Depends(AuthPermission(["module_bre:selection_rule:import"]))], db: Annotated[AsyncSession, Depends(db_getter)], ) -> JSONResponse: service = SelectionRuleService(auth, db) batch_import_result = await service.batch_import(file=file, update_support=True) return SuccessResponse(data=batch_import_result, msg="导入选择规则成功") @SelectionRuleRouter.post("/download/template", summary="获取选择规则导入模板", dependencies=[Depends(AuthPermission(["module_bre:selection_rule:download"]))]) async def download_selection_rule__template_controller() -> StreamingResponse: import_template_result = SelectionRuleService.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", }, )