Files
FastapiAdmin/backend/app/api/v1/module_system/dept/controller.py
T
zhangtao c2ca6d19ac refactor: 优化代码注释和文档字符串格式
style: 统一代码风格和格式

docs: 完善函数和方法的文档字符串

refactor(base_model): 移除冗余的表名和表参数生成方法

refactor(constant): 更新返回码注释格式

refactor(router_class): 添加路由处理器的详细文档

refactor(database): 完善数据库连接函数的文档

refactor(security): 添加认证类和方法的详细文档

refactor(validator): 更新验证器函数的文档格式

refactor(serialize): 优化序列化工具类的文档

refactor(response): 完善响应类的文档字符串

refactor(dependencies): 添加依赖函数的详细文档

refactor(initialize): 完善初始化脚本的文档

refactor(plugin): 添加生命周期和中间件注册的文档

refactor(service): 完善服务层方法的文档

refactor(controller): 添加控制器方法的详细文档

refactor(crud): 完善CRUD操作的文档字符串

refactor(schema): 简化模型类并移除冗余字段

refactor(param): 更新查询参数类的注释格式

refactor(template): 优化代码生成模板的格式

refactor(console): 添加控制台输出功能的实现

refactor(util): 完善工具函数的文档字符串
2025-10-18 16:31:28 +08:00

162 lines
5.2 KiB
Python

# -*- coding: utf-8 -*-
from fastapi import APIRouter, Body, Depends, Path
from fastapi.responses import JSONResponse
from app.common.response import SuccessResponse
from app.core.router_class import OperationLogRoute
from app.core.dependencies import AuthPermission
from app.core.base_schema import BatchSetAvailable
from app.core.logger import logger
from ..auth.schema import AuthSchema
from .param import DeptQueryParam
from .service import DeptService
from .schema import (
DeptCreateSchema,
DeptUpdateSchema
)
DeptRouter = APIRouter(route_class=OperationLogRoute, prefix="/dept", tags=["部门管理"])
@DeptRouter.get("/tree", summary="查询部门树", description="查询部门树")
async def get_dept_tree_controller(
search: DeptQueryParam = Depends(),
auth: AuthSchema = Depends(AuthPermission(["system:dept:query"]))
) -> JSONResponse:
"""
查询部门树
参数:
- search (DeptQueryParam): 查询参数模型
- auth (AuthSchema): 认证信息模型
返回:
- JSONResponse: 包含部门树的响应模型
异常:
- CustomException: 查询部门树失败时抛出异常。
"""
order_by = [{"order": "asc"}]
result_dict_list = await DeptService.get_dept_tree_service(search=search, auth=auth, order_by=order_by)
logger.info(f"查询部门树成功")
return SuccessResponse(data=result_dict_list, msg="查询部门树成功")
@DeptRouter.get("/detail/{id}", summary="查询部门详情", description="查询部门详情")
async def get_obj_detail_controller(
id: int = Path(..., description="部门ID"),
auth: AuthSchema = Depends(AuthPermission(["system:dept:query"]))
) -> JSONResponse:
"""
查询部门详情
参数:
- id (int): 部门ID
- auth (AuthSchema): 认证信息模型
返回:
- JSONResponse: 包含部门详情的响应模型
异常:
- CustomException: 查询部门详情失败时抛出异常。
"""
result_dict = await DeptService.get_dept_detail_service(id=id, auth=auth)
logger.info(f"查询部门详情成功 {id}")
return SuccessResponse(data=result_dict, msg="查询部门详情成功")
@DeptRouter.post("/create", summary="创建部门", description="创建部门")
async def create_obj_controller(
data: DeptCreateSchema,
auth: AuthSchema = Depends(AuthPermission(["system:dept:create"]))
) -> JSONResponse:
"""
创建部门
参数:
- data (DeptCreateSchema): 创建部门负载模型
- auth (AuthSchema): 认证信息模型
返回:
- JSONResponse: 包含创建部门结果的响应模型
异常:
- CustomException: 创建部门失败时抛出异常。
"""
result_dict = await DeptService.create_dept_service(data=data, auth=auth)
logger.info(f"创建部门成功: {result_dict}")
return SuccessResponse(data=result_dict, msg="创建部门成功")
@DeptRouter.put("/update/{id}", summary="修改部门", description="修改部门")
async def update_obj_controller(
data: DeptUpdateSchema,
id: int = Path(..., description="部门ID"),
auth: AuthSchema = Depends(AuthPermission(["system:dept:update"]))
) -> JSONResponse:
"""
修改部门
参数:
- data (DeptUpdateSchema): 修改部门负载模型
- id (int): 部门ID
- auth (AuthSchema): 认证信息模型
返回:
- JSONResponse: 包含修改部门结果的响应模型
异常:
- CustomException: 修改部门失败时抛出异常。
"""
result_dict = await DeptService.update_dept_service(auth=auth, id=id, data=data)
logger.info(f"修改部门成功: {result_dict}")
return SuccessResponse(data=result_dict, msg="修改部门成功")
@DeptRouter.delete("/delete", summary="删除部门", description="删除部门")
async def delete_obj_controller(
ids: list[int] = Body(..., description="ID列表"),
auth: AuthSchema = Depends(AuthPermission(["system:dept:delete"]))
) -> JSONResponse:
"""
删除部门
参数:
- ids (list[int]): 部门ID列表
- auth (AuthSchema): 认证信息模型
返回:
- JSONResponse: 包含删除部门结果的响应模型
异常:
- CustomException: 删除部门失败时抛出异常。
"""
await DeptService.delete_dept_service(ids=ids, auth=auth)
logger.info(f"删除部门成功: {ids}")
return SuccessResponse(msg="删除部门成功")
@DeptRouter.patch("/available/setting", summary="批量修改部门状态", description="批量修改部门状态")
async def batch_set_available_obj_controller(
data: BatchSetAvailable,
auth: AuthSchema = Depends(AuthPermission(["system:dept:patch"]))
) -> JSONResponse:
"""
批量修改部门状态
参数:
- data (BatchSetAvailable): 批量修改部门状态负载模型
- auth (AuthSchema): 认证信息模型
返回:
- JSONResponse: 包含批量修改部门状态结果的响应模型
异常:
- CustomException: 批量修改部门状态失败时抛出异常。
"""
await DeptService.batch_set_available_service(data=data, auth=auth)
logger.info(f"批量修改部门状态成功: {data.ids}")
return SuccessResponse(msg="批量修改部门状态成功")