add backend source code
This commit is contained in:
107
backend/app/api/v1/departments.py
Normal file
107
backend/app/api/v1/departments.py
Normal file
@@ -0,0 +1,107 @@
|
||||
"""
|
||||
API路由 - 科室管理
|
||||
"""
|
||||
from typing import Annotated, Optional
|
||||
from fastapi import APIRouter, Depends, HTTPException, Query
|
||||
from sqlalchemy.ext.asyncio import AsyncSession
|
||||
|
||||
from app.core.database import get_db
|
||||
from app.core.security import get_current_active_user, get_current_manager_user
|
||||
from app.schemas.schemas import (
|
||||
DepartmentCreate, DepartmentUpdate, DepartmentResponse,
|
||||
ResponseBase, PaginatedResponse
|
||||
)
|
||||
from app.services.department_service import DepartmentService
|
||||
from app.models.models import User
|
||||
|
||||
router = APIRouter(prefix="/departments", tags=["科室管理"])
|
||||
|
||||
|
||||
@router.get("", summary="获取科室列表")
|
||||
async def get_departments(
|
||||
dept_type: Optional[str] = Query(None, description="科室类型"),
|
||||
is_active: Optional[bool] = Query(None, description="是否启用"),
|
||||
page: int = Query(1, ge=1, description="页码"),
|
||||
page_size: int = Query(20, ge=1, le=100, description="每页数量"),
|
||||
db: AsyncSession = Depends(get_db),
|
||||
current_user: User = Depends(get_current_active_user)
|
||||
):
|
||||
"""获取科室列表"""
|
||||
departments, total = await DepartmentService.get_list(
|
||||
db, dept_type, is_active, page, page_size
|
||||
)
|
||||
return {
|
||||
"code": 200,
|
||||
"message": "success",
|
||||
"data": [DepartmentResponse.model_validate(d) for d in departments],
|
||||
"total": total,
|
||||
"page": page,
|
||||
"page_size": page_size
|
||||
}
|
||||
|
||||
|
||||
@router.get("/tree", summary="获取科室树形结构")
|
||||
async def get_department_tree(
|
||||
dept_type: Optional[str] = Query(None, description="科室类型"),
|
||||
db: AsyncSession = Depends(get_db),
|
||||
current_user: User = Depends(get_current_active_user)
|
||||
):
|
||||
"""获取科室树形结构"""
|
||||
tree = await DepartmentService.get_tree(db, dept_type)
|
||||
return {"code": 200, "message": "success", "data": tree}
|
||||
|
||||
|
||||
@router.get("/{dept_id}", summary="获取科室详情")
|
||||
async def get_department(
|
||||
dept_id: int,
|
||||
db: AsyncSession = Depends(get_db),
|
||||
current_user: User = Depends(get_current_active_user)
|
||||
):
|
||||
"""获取科室详情"""
|
||||
department = await DepartmentService.get_by_id(db, dept_id)
|
||||
if not department:
|
||||
raise HTTPException(status_code=404, detail="科室不存在")
|
||||
return {"code": 200, "message": "success", "data": DepartmentResponse.model_validate(department)}
|
||||
|
||||
|
||||
@router.post("", summary="创建科室")
|
||||
async def create_department(
|
||||
dept_data: DepartmentCreate,
|
||||
db: AsyncSession = Depends(get_db),
|
||||
current_user: Annotated[User, Depends(get_current_manager_user)] = None
|
||||
):
|
||||
"""创建科室(需要管理员或经理权限)"""
|
||||
# 检查编码是否已存在
|
||||
existing = await DepartmentService.get_by_code(db, dept_data.code)
|
||||
if existing:
|
||||
raise HTTPException(status_code=400, detail="科室编码已存在")
|
||||
|
||||
department = await DepartmentService.create(db, dept_data)
|
||||
return {"code": 200, "message": "创建成功", "data": DepartmentResponse.model_validate(department)}
|
||||
|
||||
|
||||
@router.put("/{dept_id}", summary="更新科室")
|
||||
async def update_department(
|
||||
dept_id: int,
|
||||
dept_data: DepartmentUpdate,
|
||||
db: AsyncSession = Depends(get_db),
|
||||
current_user: Annotated[User, Depends(get_current_manager_user)] = None
|
||||
):
|
||||
"""更新科室(需要管理员或经理权限)"""
|
||||
department = await DepartmentService.update(db, dept_id, dept_data)
|
||||
if not department:
|
||||
raise HTTPException(status_code=404, detail="科室不存在")
|
||||
return {"code": 200, "message": "更新成功", "data": DepartmentResponse.model_validate(department)}
|
||||
|
||||
|
||||
@router.delete("/{dept_id}", summary="删除科室")
|
||||
async def delete_department(
|
||||
dept_id: int,
|
||||
db: AsyncSession = Depends(get_db),
|
||||
current_user: Annotated[User, Depends(get_current_manager_user)] = None
|
||||
):
|
||||
"""删除科室(需要管理员或经理权限)"""
|
||||
success = await DepartmentService.delete(db, dept_id)
|
||||
if not success:
|
||||
raise HTTPException(status_code=400, detail="无法删除,科室下存在子科室")
|
||||
return {"code": 200, "message": "删除成功"}
|
||||
Reference in New Issue
Block a user