from typing import Annotated from fastapi import APIRouter, Depends, Path, Query from fastapi.responses import JSONResponse from app.api.v1.module_system.auth.schema import AuthSchema from app.common.response import ResponseSchema, SuccessResponse from app.core.dependencies import AuthPermission from app.core.logger import log from app.core.router_class import OperationLogRoute from .schema import ( DepartmentCreateSchema, DepartmentMoveSchema, DepartmentOutSchema, DepartmentTreeSchema, DepartmentUpdateSchema, ) from .service import DepartmentService DepartmentRouter = APIRouter( route_class=OperationLogRoute, prefix="/department", tags=["部门管理"], ) @DepartmentRouter.post( "", summary="创建部门", description="创建一个新的部门", response_model=ResponseSchema[DepartmentOutSchema], ) async def create_department_controller( data: DepartmentCreateSchema, auth: Annotated[AuthSchema, Depends(AuthPermission(["module_payment:department:create"]))], ) -> JSONResponse: """创建部门""" result = await DepartmentService.create_service(auth=auth, data=data) log.info(f"创建部门成功: {data.name}, department_id={result.department_id}") return SuccessResponse(data=result, msg="创建部门成功") @DepartmentRouter.get( "", summary="查询部门列表", description="分页查询部门列表(平铺)", response_model=ResponseSchema[DepartmentOutSchema], ) async def list_department_controller( auth: Annotated[AuthSchema, Depends(AuthPermission(["module_payment:department:list"]))], page_no: Annotated[int, Query(description="页码")] = 1, page_size: Annotated[int, Query(description="每页数量")] = 100, name: Annotated[str | None, Query(description="部门名称")] = None, code: Annotated[str | None, Query(description="部门编码")] = None, status: Annotated[str | None, Query(description="状态")] = None, ) -> JSONResponse: """查询部门列表""" search = {} if name: search["name"] = name if code: search["code"] = code if status: search["status"] = status result = await DepartmentService.list_service( auth=auth, page_no=page_no, page_size=page_size, search=search ) return SuccessResponse(data=result, msg="查询部门列表成功") @DepartmentRouter.get( "/tree", summary="获取部门树", description="获取部门树形结构", response_model=ResponseSchema[DepartmentTreeSchema], ) async def tree_department_controller( auth: Annotated[AuthSchema, Depends(AuthPermission(["module_payment:department:tree"]))], ) -> JSONResponse: """获取部门树形结构""" result = await DepartmentService.tree_service(auth=auth) return SuccessResponse(data=result, msg="获取部门树成功") @DepartmentRouter.get( "/{department_id}", summary="查询部门详情", description="根据 department_id 查询部门详情", response_model=ResponseSchema[DepartmentOutSchema], ) async def get_detail_controller( department_id: Annotated[str, Path(description="部门ID")], auth: Annotated[AuthSchema, Depends(AuthPermission(["module_payment:department:detail"]))], ) -> JSONResponse: """查询部门详情""" result = await DepartmentService.detail_service(auth=auth, department_id=department_id) log.info(f"查询部门详情成功: {department_id}") return SuccessResponse(data=result, msg="查询部门详情成功") @DepartmentRouter.get( "/{department_id}/children", summary="获取子部门", description="获取指定部门的子部门列表", response_model=ResponseSchema[DepartmentOutSchema], ) async def get_children_controller( department_id: Annotated[str, Path(description="部门ID")], auth: Annotated[AuthSchema, Depends(AuthPermission(["module_payment:department:children"]))], ) -> JSONResponse: """获取子部门列表""" result = await DepartmentService.children_service(auth=auth, department_id=department_id) return SuccessResponse(data=result, msg="获取子部门成功") @DepartmentRouter.get( "/options", summary="获取部门选项", description="获取部门选择选项,用于上级部门选择", response_model=ResponseSchema[list], ) async def get_department_options_controller( auth: Annotated[AuthSchema, Depends(AuthPermission(["module_payment:department:options"]))], ) -> JSONResponse: """获取部门选择选项""" result = await DepartmentService.options_service(auth=auth) return SuccessResponse(data=result, msg="获取部门选项成功") @DepartmentRouter.put( "/{department_id}", summary="更新部门", description="更新部门信息", response_model=ResponseSchema[DepartmentOutSchema], ) async def update_department_controller( department_id: Annotated[str, Path(description="部门ID")], data: DepartmentUpdateSchema, auth: Annotated[AuthSchema, Depends(AuthPermission(["module_payment:department:update"]))], ) -> JSONResponse: """更新部门""" result = await DepartmentService.update_service( auth=auth, department_id=department_id, data=data ) log.info(f"更新部门成功: {department_id}") return SuccessResponse(data=result, msg="更新部门成功") @DepartmentRouter.put( "/{department_id}/move", summary="移动部门", description="移动部门(变更父级)", response_model=ResponseSchema[DepartmentOutSchema], ) async def move_department_controller( department_id: Annotated[str, Path(description="部门ID")], data: DepartmentMoveSchema, auth: Annotated[AuthSchema, Depends(AuthPermission(["module_payment:department:move"]))], ) -> JSONResponse: """移动部门""" result = await DepartmentService.move_service( auth=auth, department_id=department_id, parent_id=data.parent_id ) log.info(f"移动部门成功: {department_id} -> {data.parent_id}") return SuccessResponse(data=result, msg="移动部门成功") @DepartmentRouter.delete( "/{department_id}", summary="删除部门", description="删除部门", response_model=ResponseSchema[None], ) async def delete_department_controller( department_id: Annotated[str, Path(description="部门ID")], auth: Annotated[AuthSchema, Depends(AuthPermission(["module_payment:department:delete"]))], ) -> JSONResponse: """删除部门""" await DepartmentService.delete_service(auth=auth, department_id=department_id) log.info(f"删除部门成功: {department_id}") return SuccessResponse(data=None, msg="删除部门成功")