upload project source code
This commit is contained in:
@@ -0,0 +1,2 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
@@ -0,0 +1,162 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
from fastapi import APIRouter, Body, Depends, Path
|
||||
from fastapi.responses import JSONResponse
|
||||
|
||||
from app.common.response import SuccessResponse
|
||||
from app.core.dependencies import AuthPermission
|
||||
from app.core.base_schema import BatchSetAvailable
|
||||
from app.core.logger import log
|
||||
from app.core.router_class import OperationLogRoute
|
||||
|
||||
from ..auth.schema import AuthSchema
|
||||
from .service import DeptService
|
||||
from .schema import (
|
||||
DeptCreateSchema,
|
||||
DeptUpdateSchema,
|
||||
DeptQueryParam
|
||||
)
|
||||
|
||||
|
||||
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(["module_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)
|
||||
log.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(["module_system:dept:query"]))
|
||||
) -> JSONResponse:
|
||||
"""
|
||||
查询部门详情
|
||||
|
||||
参数:
|
||||
- id (int): 部门ID
|
||||
- auth (AuthSchema): 认证信息模型
|
||||
|
||||
返回:
|
||||
- JSONResponse: 包含部门详情的响应模型
|
||||
|
||||
异常:
|
||||
- CustomException: 查询部门详情失败时抛出异常。
|
||||
"""
|
||||
result_dict = await DeptService.get_dept_detail_service(id=id, auth=auth)
|
||||
log.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(["module_system:dept:create"]))
|
||||
) -> JSONResponse:
|
||||
"""
|
||||
创建部门
|
||||
|
||||
参数:
|
||||
- data (DeptCreateSchema): 创建部门负载模型
|
||||
- auth (AuthSchema): 认证信息模型
|
||||
|
||||
返回:
|
||||
- JSONResponse: 包含创建部门结果的响应模型
|
||||
|
||||
异常:
|
||||
- CustomException: 创建部门失败时抛出异常。
|
||||
"""
|
||||
result_dict = await DeptService.create_dept_service(data=data, auth=auth)
|
||||
log.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(["module_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)
|
||||
log.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(["module_system:dept:delete"]))
|
||||
) -> JSONResponse:
|
||||
"""
|
||||
删除部门
|
||||
|
||||
参数:
|
||||
- ids (list[int]): 部门ID列表
|
||||
- auth (AuthSchema): 认证信息模型
|
||||
|
||||
返回:
|
||||
- JSONResponse: 包含删除部门结果的响应模型
|
||||
|
||||
异常:
|
||||
- CustomException: 删除部门失败时抛出异常。
|
||||
"""
|
||||
await DeptService.delete_dept_service(ids=ids, auth=auth)
|
||||
log.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(["module_system:dept:patch"]))
|
||||
) -> JSONResponse:
|
||||
"""
|
||||
批量修改部门状态
|
||||
|
||||
参数:
|
||||
- data (BatchSetAvailable): 批量修改部门状态负载模型
|
||||
- auth (AuthSchema): 认证信息模型
|
||||
|
||||
返回:
|
||||
- JSONResponse: 包含批量修改部门状态结果的响应模型
|
||||
|
||||
异常:
|
||||
- CustomException: 批量修改部门状态失败时抛出异常。
|
||||
"""
|
||||
await DeptService.batch_set_available_service(data=data, auth=auth)
|
||||
log.info(f"批量修改部门状态成功: {data.ids}")
|
||||
return SuccessResponse(msg="批量修改部门状态成功")
|
||||
@@ -0,0 +1,88 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
from typing import Sequence
|
||||
|
||||
from app.core.base_crud import CRUDBase
|
||||
|
||||
from ..auth.schema import AuthSchema
|
||||
from .model import DeptModel
|
||||
from .schema import DeptCreateSchema, DeptUpdateSchema
|
||||
|
||||
|
||||
class DeptCRUD(CRUDBase[DeptModel, DeptCreateSchema, DeptUpdateSchema]):
|
||||
"""部门模块数据层"""
|
||||
|
||||
def __init__(self, auth: AuthSchema) -> None:
|
||||
"""初始化部门CRUD"""
|
||||
self.auth = auth
|
||||
super().__init__(model=DeptModel, auth=auth)
|
||||
|
||||
async def get_by_id_crud(self, id: int, preload: list | None = None) -> DeptModel | None:
|
||||
"""
|
||||
根据 id 获取部门信息。
|
||||
|
||||
参数:
|
||||
- id (int): 部门 ID。
|
||||
- preload (list | None): 预加载关系,未提供时使用模型默认项
|
||||
|
||||
返回:
|
||||
- DeptModel | None: 部门信息,未找到返回 None。
|
||||
"""
|
||||
obj = await self.get(id=id, preload=preload)
|
||||
if not obj:
|
||||
return None
|
||||
return obj
|
||||
|
||||
async def get_list_crud(self, search: dict | None = None, order_by: list[dict] | None = None, preload: list | None = None) -> Sequence[DeptModel]:
|
||||
"""
|
||||
获取部门列表。
|
||||
|
||||
参数:
|
||||
- search (dict | None): 搜索条件。
|
||||
- order_by (list[dict] | None): 排序字段列表。
|
||||
- preload (list | None): 预加载关系,未提供时使用模型默认项
|
||||
|
||||
返回:
|
||||
- Sequence[DeptModel]: 部门列表。
|
||||
"""
|
||||
return await self.list(search=search, order_by=order_by, preload=preload)
|
||||
|
||||
async def get_tree_list_crud(self, search: dict | None = None, order_by: list[dict] | None = None, preload: list | None = None) -> Sequence[DeptModel]:
|
||||
"""
|
||||
获取部门树形列表。
|
||||
|
||||
参数:
|
||||
- search (dict | None): 搜索条件。
|
||||
- order_by (list[dict] | None): 排序字段列表。
|
||||
- preload (list | None): 预加载关系,未提供时使用模型默认项
|
||||
|
||||
返回:
|
||||
- Sequence[DeptModel]: 部门树形列表。
|
||||
"""
|
||||
return await self.tree_list(search=search, order_by=order_by, children_attr='children', preload=preload)
|
||||
|
||||
async def set_available_crud(self, ids: list[int], status: str) -> None:
|
||||
"""
|
||||
批量设置部门可用状态。
|
||||
|
||||
参数:
|
||||
- ids (list[int]): 部门 ID 列表。
|
||||
- status (str): 可用状态。
|
||||
|
||||
返回:
|
||||
- None
|
||||
"""
|
||||
await self.set(ids=ids, status=status)
|
||||
|
||||
async def get_name_crud(self, id: int) -> str | None:
|
||||
"""
|
||||
根据 id 获取部门名称。
|
||||
|
||||
参数:
|
||||
- id (int): 部门 ID。
|
||||
|
||||
返回:
|
||||
- str | None: 部门名称,未找到返回 None。
|
||||
"""
|
||||
obj = await self.get(id=id)
|
||||
return obj.name if obj else None
|
||||
@@ -0,0 +1,57 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
from typing import TYPE_CHECKING
|
||||
from sqlalchemy import String, Integer, ForeignKey
|
||||
from sqlalchemy.orm import relationship, Mapped, mapped_column
|
||||
|
||||
from app.core.base_model import ModelMixin
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from app.api.v1.module_system.role.model import RoleModel
|
||||
from app.api.v1.module_system.user.model import UserModel
|
||||
|
||||
|
||||
class DeptModel(ModelMixin):
|
||||
"""
|
||||
部门模型
|
||||
"""
|
||||
__tablename__: str = "sys_dept"
|
||||
__table_args__: dict[str, str] = ({'comment': '部门表'})
|
||||
|
||||
name: Mapped[str] = mapped_column(String(40), nullable=False, comment="部门名称")
|
||||
order: Mapped[int] = mapped_column(Integer, nullable=False, default=999, comment="显示排序")
|
||||
code: Mapped[str | None] = mapped_column(String(20), nullable=True, index=True, comment="部门编码")
|
||||
leader: Mapped[str | None] = mapped_column(String(32), default=None, comment='部门负责人')
|
||||
phone: Mapped[str | None] = mapped_column(String(11), default=None, comment='手机')
|
||||
email: Mapped[str | None] = mapped_column(String(64), default=None, comment='邮箱')
|
||||
|
||||
# 树形结构字段
|
||||
parent_id: Mapped[int | None] = mapped_column(
|
||||
Integer,
|
||||
ForeignKey("sys_dept.id", ondelete="SET NULL", onupdate="CASCADE"),
|
||||
default=None,
|
||||
index=True,
|
||||
comment="父级部门ID"
|
||||
)
|
||||
# 关联关系
|
||||
parent: Mapped["DeptModel | None"] = relationship(
|
||||
back_populates='children',
|
||||
remote_side="DeptModel.id",
|
||||
foreign_keys=[parent_id],
|
||||
uselist=False
|
||||
)
|
||||
children: Mapped[list["DeptModel"]] = relationship(
|
||||
back_populates='parent',
|
||||
foreign_keys=[parent_id],
|
||||
lazy="selectin"
|
||||
)
|
||||
roles: Mapped[list["RoleModel"]] = relationship(
|
||||
secondary="sys_role_depts",
|
||||
back_populates="depts",
|
||||
lazy="selectin"
|
||||
)
|
||||
users: Mapped[list["UserModel"]] = relationship(
|
||||
back_populates="dept",
|
||||
foreign_keys="UserModel.dept_id",
|
||||
lazy="selectin"
|
||||
)
|
||||
@@ -0,0 +1,77 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
from pydantic import BaseModel, ConfigDict, Field, field_validator
|
||||
from fastapi import Query
|
||||
|
||||
from app.core.validator import DateTimeStr
|
||||
from app.core.base_schema import BaseSchema
|
||||
|
||||
|
||||
class DeptCreateSchema(BaseModel):
|
||||
"""部门创建模型"""
|
||||
name: str = Field(..., max_length=40, description="部门名称")
|
||||
order: int = Field(default=1, ge=0, description="显示顺序")
|
||||
code: str | None = Field(default=None, max_length=60, description="部门编码")
|
||||
leader: str | None = Field(default=None, max_length=20, description="部门负责人")
|
||||
phone: str | None = Field(default=None, max_length=11, description="手机")
|
||||
email: str | None = Field(default=None, max_length=64, description="邮箱")
|
||||
parent_id: int | None = Field(default=None, ge=0, description="父部门ID")
|
||||
status: str = Field(default="0", description="是否启用(0:启用 1:禁用)")
|
||||
description: str | None = Field(default=None, max_length=255, description="备注说明")
|
||||
|
||||
@field_validator('name')
|
||||
@classmethod
|
||||
def validate_name(cls, value: str):
|
||||
if not value or len(value.strip()) == 0:
|
||||
raise ValueError("部门名称不能为空")
|
||||
value = value.replace(" ", "")
|
||||
return value
|
||||
|
||||
@field_validator('code')
|
||||
@classmethod
|
||||
def validate_code(cls, value: str | None):
|
||||
if value is None:
|
||||
return value
|
||||
v = value.strip()
|
||||
if v == "":
|
||||
return None
|
||||
import re
|
||||
if not re.match(r'^[A-Za-z][A-Za-z0-9_]*$', v):
|
||||
raise ValueError("部门编码必须以字母开头,且仅包含字母/数字/下划线")
|
||||
return v
|
||||
|
||||
|
||||
class DeptUpdateSchema(DeptCreateSchema):
|
||||
"""部门更新模型"""
|
||||
...
|
||||
|
||||
|
||||
class DeptOutSchema(DeptCreateSchema, BaseSchema):
|
||||
"""部门响应模型"""
|
||||
model_config = ConfigDict(from_attributes=True)
|
||||
|
||||
parent_name: str | None = Field(default=None, max_length=64, description="父部门名称")
|
||||
|
||||
|
||||
class DeptQueryParam:
|
||||
"""部门管理查询参数"""
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
name: str | None = Query(None, description="部门名称"),
|
||||
status: str | None = Query(None, description="部门状态(True正常 False停用)"),
|
||||
created_time: list[DateTimeStr] | None = Query(None, description="创建时间范围", examples=["2025-01-01 00:00:00", "2025-12-31 23:59:59"]),
|
||||
updated_time: list[DateTimeStr] | None = Query(None, description="更新时间范围", examples=["2025-01-01 00:00:00", "2025-12-31 23:59:59"]),
|
||||
) -> None:
|
||||
|
||||
# 模糊查询字段
|
||||
self.name = ("like", name)
|
||||
|
||||
# 精确查询字段
|
||||
self.status = status
|
||||
|
||||
# 时间范围查询
|
||||
if created_time and len(created_time) == 2:
|
||||
self.created_time = ("between", (created_time[0], created_time[1]))
|
||||
if updated_time and len(updated_time) == 2:
|
||||
self.updated_time = ("between", (updated_time[0], updated_time[1]))
|
||||
173
后端源码/yifan.action-ai.cn/app/api/v1/module_system/dept/service.py
Normal file
173
后端源码/yifan.action-ai.cn/app/api/v1/module_system/dept/service.py
Normal file
@@ -0,0 +1,173 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
from app.core.base_schema import BatchSetAvailable
|
||||
from app.core.exceptions import CustomException
|
||||
from app.utils.common_util import (
|
||||
get_parent_id_map,
|
||||
get_parent_recursion,
|
||||
get_child_id_map,
|
||||
get_child_recursion,
|
||||
traversal_to_tree
|
||||
)
|
||||
|
||||
from ..auth.schema import AuthSchema
|
||||
from .crud import DeptCRUD
|
||||
from .schema import (
|
||||
DeptCreateSchema,
|
||||
DeptUpdateSchema,
|
||||
DeptOutSchema,
|
||||
DeptQueryParam
|
||||
)
|
||||
|
||||
|
||||
class DeptService:
|
||||
"""
|
||||
部门管理模块服务层
|
||||
"""
|
||||
|
||||
@classmethod
|
||||
async def get_dept_detail_service(cls, auth: AuthSchema, id: int) -> dict:
|
||||
"""
|
||||
获取部门详情。
|
||||
|
||||
参数:
|
||||
- auth (AuthSchema): 认证对象。
|
||||
- id (int): 部门 ID。
|
||||
|
||||
返回:
|
||||
- dict: 部门详情对象。
|
||||
"""
|
||||
dept = await DeptCRUD(auth).get_by_id_crud(id=id)
|
||||
result = DeptOutSchema.model_validate(dept).model_dump()
|
||||
if dept and dept.parent_id:
|
||||
parent = await DeptCRUD(auth).get(id=dept.parent_id)
|
||||
if parent:
|
||||
result['parent_name'] = parent.name
|
||||
return result
|
||||
|
||||
@classmethod
|
||||
async def get_dept_tree_service(cls, auth: AuthSchema, search: DeptQueryParam | None= None, order_by: list[dict] | None = None) -> list[dict]:
|
||||
"""
|
||||
获取部门树形列表。
|
||||
|
||||
参数:
|
||||
- auth (AuthSchema): 认证对象。
|
||||
- search (DeptQueryParam | None): 查询参数对象。
|
||||
- order_by (list[dict] | None): 排序参数。
|
||||
|
||||
返回:
|
||||
- list[dict]: 部门树形列表对象。
|
||||
"""
|
||||
# 使用树形结构查询,预加载children关系
|
||||
dept_list = await DeptCRUD(auth).get_tree_list_crud(search=search.__dict__, order_by=order_by)
|
||||
# 转换为字典列表
|
||||
dept_dict_list = [DeptOutSchema.model_validate(dept).model_dump() for dept in dept_list]
|
||||
# 使用traversal_to_tree构建树形结构
|
||||
return traversal_to_tree(dept_dict_list)
|
||||
|
||||
@classmethod
|
||||
async def create_dept_service(cls, auth: AuthSchema, data: DeptCreateSchema) -> dict:
|
||||
"""
|
||||
创建部门。
|
||||
|
||||
参数:
|
||||
- auth (AuthSchema): 认证对象。
|
||||
- data (DeptCreateSchema): 部门创建对象。
|
||||
|
||||
返回:
|
||||
- dict: 新创建的部门对象。
|
||||
|
||||
异常:
|
||||
- CustomException: 当部门已存在时抛出。
|
||||
"""
|
||||
dept = await DeptCRUD(auth).get(name=data.name)
|
||||
if dept:
|
||||
raise CustomException(msg='创建失败,该部门已存在')
|
||||
obj = await DeptCRUD(auth).get(code=data.code)
|
||||
if obj:
|
||||
raise CustomException(msg='创建失败,编码已存在')
|
||||
dept = await DeptCRUD(auth).create(data=data)
|
||||
return DeptOutSchema.model_validate(dept).model_dump()
|
||||
|
||||
@classmethod
|
||||
async def update_dept_service(cls, auth: AuthSchema, id:int, data: DeptUpdateSchema) -> dict:
|
||||
"""
|
||||
更新部门。
|
||||
|
||||
参数:
|
||||
- auth (AuthSchema): 认证对象。
|
||||
- id (int): 部门 ID。
|
||||
- data (DeptUpdateSchema): 部门更新对象。
|
||||
|
||||
返回:
|
||||
- dict: 更新后的部门对象。
|
||||
|
||||
异常:
|
||||
- CustomException: 当部门不存在或名称重复时抛出。
|
||||
"""
|
||||
dept = await DeptCRUD(auth).get_by_id_crud(id=id)
|
||||
if not dept:
|
||||
raise CustomException(msg='更新失败,该部门不存在')
|
||||
exist_dept = await DeptCRUD(auth).get(name=data.name)
|
||||
if exist_dept and exist_dept.id != id:
|
||||
raise CustomException(msg='更新失败,部门名称重复')
|
||||
dept = await DeptCRUD(auth).update(id=id, data=data)
|
||||
return DeptOutSchema.model_validate(dept).model_dump()
|
||||
|
||||
@classmethod
|
||||
async def delete_dept_service(cls, auth: AuthSchema, ids: list[int]) -> None:
|
||||
"""
|
||||
删除部门。
|
||||
|
||||
参数:
|
||||
- auth (AuthSchema): 认证对象。
|
||||
- ids (List[int]): 部门 ID 列表。
|
||||
|
||||
返回:
|
||||
- None
|
||||
|
||||
异常:
|
||||
- CustomException: 当删除对象为空或部门不存在时抛出。
|
||||
"""
|
||||
if len(ids) < 1:
|
||||
raise CustomException(msg='删除失败,删除对象不能为空')
|
||||
for id in ids:
|
||||
dept = await DeptCRUD(auth).get_by_id_crud(id=id)
|
||||
if not dept:
|
||||
raise CustomException(msg='删除失败,该部门不存在')
|
||||
# 校验是否存在子级部门,存在则禁止删除
|
||||
dept_list = await DeptCRUD(auth).get_list_crud()
|
||||
id_map = get_child_id_map(model_list=dept_list)
|
||||
for id in ids:
|
||||
descendants = get_child_recursion(id=id, id_map=id_map)
|
||||
if len(descendants) > 1:
|
||||
raise CustomException(msg='删除失败,存在子级部门,请先删除子级部门')
|
||||
await DeptCRUD(auth).delete(ids=ids)
|
||||
|
||||
@classmethod
|
||||
async def batch_set_available_service(cls, auth: AuthSchema, data: BatchSetAvailable) -> None:
|
||||
"""
|
||||
批量设置部门可用状态。
|
||||
|
||||
参数:
|
||||
- auth (AuthSchema): 认证对象。
|
||||
- data (BatchSetAvailable): 批量设置可用状态对象。
|
||||
|
||||
返回:
|
||||
- None
|
||||
"""
|
||||
dept_list = await DeptCRUD(auth).get_list_crud()
|
||||
total_ids = []
|
||||
|
||||
if data.status:
|
||||
id_map = get_parent_id_map(model_list=dept_list)
|
||||
for dept_id in data.ids:
|
||||
enable_ids = get_parent_recursion(id=dept_id, id_map=id_map)
|
||||
total_ids.extend(enable_ids)
|
||||
else:
|
||||
id_map = get_child_id_map(model_list=dept_list)
|
||||
for dept_id in data.ids:
|
||||
disable_ids = get_child_recursion(id=dept_id, id_map=id_map)
|
||||
total_ids.extend(disable_ids)
|
||||
|
||||
await DeptCRUD(auth).set_available_crud(ids=total_ids, status=data.status)
|
||||
Reference in New Issue
Block a user