from typing import Annotated, Optional 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, DepartmentUpdateSchema, DepartmentDetailOutSchema, DepartmentOperationOutSchema, ) from .service import DepartmentService DepartmentRouter = APIRouter( route_class=OperationLogRoute, prefix="/department", tags=["部门管理"], ) @DepartmentRouter.post( "", summary="创建部门", description="创建部门 (alipay.commerce.ec.department.create)", response_model=ResponseSchema[DepartmentOperationOutSchema], ) async def create_department_controller( data: DepartmentCreateSchema, auth: Annotated[AuthSchema, Depends(AuthPermission(["module_payment:department:create"]))], ) -> JSONResponse: """创建部门""" result = await DepartmentService.create_department_service(auth=auth, data=data) log.info(f"创建部门成功: {data.department_name}, department_id={result.department_id}") return SuccessResponse(data=result, msg="创建部门成功") @DepartmentRouter.delete( "/{department_id}", summary="删除部门", description="删除部门 (alipay.commerce.ec.department.delete)", response_model=ResponseSchema[DepartmentOperationOutSchema], ) async def delete_department_controller( department_id: Annotated[str, Path(description="部门ID")], enterprise_id: Annotated[str, Query(description="企业ID")], auth: Annotated[AuthSchema, Depends(AuthPermission(["module_payment:department:delete"]))], ) -> JSONResponse: """删除部门""" result = await DepartmentService.delete_department_service( auth=auth, department_id=department_id, enterprise_id=enterprise_id ) log.info(f"删除部门成功: department_id={department_id}") return SuccessResponse(data=result, msg="删除部门成功") @DepartmentRouter.put( "/{department_id}", summary="更新部门", description="更新部门 (alipay.commerce.ec.department.info.modify)", response_model=ResponseSchema[DepartmentOperationOutSchema], ) async def update_department_controller( department_id: Annotated[str, Path(description="部门ID")], enterprise_id: Annotated[str, Query(description="企业ID")], data: DepartmentUpdateSchema, auth: Annotated[AuthSchema, Depends(AuthPermission(["module_payment:department:update"]))], ) -> JSONResponse: """更新部门""" result = await DepartmentService.update_department_service( auth=auth, department_id=department_id, enterprise_id=enterprise_id, data=data ) log.info(f"更新部门成功: department_id={department_id}") return SuccessResponse(data=result, msg="更新部门成功") @DepartmentRouter.get( "/{department_id}", summary="查询部门详情", description="查询部门详情 (alipay.commerce.ec.department.info.query)", response_model=ResponseSchema[DepartmentDetailOutSchema], ) async def get_department_controller( department_id: Annotated[str, Path(description="部门ID")], enterprise_id: Annotated[str, Query(description="企业ID")], auth: Annotated[AuthSchema, Depends(AuthPermission(["module_payment:department:detail"]))], ) -> JSONResponse: """查询部门详情""" result = await DepartmentService.get_department_service( auth=auth, department_id=department_id, enterprise_id=enterprise_id ) log.info(f"查询部门详情成功: department_id={department_id}") return SuccessResponse(data=result, msg="查询部门详情成功") @DepartmentRouter.get( "/{department_id}/sub", summary="查询子部门列表", description="查询子部门列表 (alipay.commerce.ec.department.sublist.query)", ) async def get_sub_departments_controller( department_id: Annotated[str, Path(description="部门ID")], enterprise_id: Annotated[str, Query(description="企业ID")], auth: Annotated[AuthSchema, Depends(AuthPermission(["module_payment:department:list"]))], ) -> JSONResponse: """查询子部门列表""" result = await DepartmentService.get_sub_departments_service( auth=auth, parent_department_id=department_id, enterprise_id=enterprise_id ) log.info(f"查询子部门列表成功: department_id={department_id}") return SuccessResponse(data=result, msg="查询子部门列表成功") @DepartmentRouter.get( "", summary="查询部门列表", description="分页查询部门列表", ) 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="每页数量")] = 20, enterprise_id: Annotated[str | None, Query(description="企业ID")] = None, department_name: Annotated[str | None, Query(description="部门名称")] = None, department_code: Annotated[str | None, Query(description="部门编码")] = None, parent_department_id: Annotated[str | None, Query(description="上级部门ID")] = None, status: Annotated[str | None, Query(description="状态")] = None, ) -> JSONResponse: """查询部门列表""" search: dict = {} if enterprise_id: search["enterprise_id"] = enterprise_id if department_name: search["department_name"] = department_name if department_code: search["department_code"] = department_code if parent_department_id is not None: search["parent_department_id"] = parent_department_id if status: search["status"] = status result = await DepartmentService.list_service( auth=auth, page_no=page_no, page_size=page_size, search=search ) log.info(f"查询部门列表成功") return SuccessResponse(data=result, msg="查询部门列表成功") @DepartmentRouter.get( "/tree/list", summary="获取部门树形结构", description="获取部门树形结构", ) async def get_department_tree_controller( enterprise_id: Annotated[str, Query(description="企业ID")], auth: Annotated[AuthSchema, Depends(AuthPermission(["module_payment:department:list"]))], ) -> JSONResponse: """获取部门树形结构""" result = await DepartmentService.get_department_tree_service( auth=auth, enterprise_id=enterprise_id ) log.info(f"获取部门树形结构成功: enterprise_id={enterprise_id}") return SuccessResponse(data=result, msg="获取部门树形结构成功")