service.py 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. # -*- coding: utf-8 -*-
  2. from app.core.base_schema import BatchSetAvailable
  3. from app.core.exceptions import CustomException
  4. from app.utils.common_util import (
  5. get_parent_id_map,
  6. get_parent_recursion,
  7. get_child_id_map,
  8. get_child_recursion,
  9. traversal_to_tree
  10. )
  11. from ..auth.schema import AuthSchema
  12. from .crud import DeptCRUD
  13. from .schema import (
  14. DeptCreateSchema,
  15. DeptUpdateSchema,
  16. DeptOutSchema,
  17. DeptQueryParam
  18. )
  19. class DeptService:
  20. """
  21. 部门管理模块服务层
  22. """
  23. @classmethod
  24. async def get_dept_detail_service(cls, auth: AuthSchema, id: int) -> dict:
  25. """
  26. 获取部门详情。
  27. 参数:
  28. - auth (AuthSchema): 认证对象。
  29. - id (int): 部门 ID。
  30. 返回:
  31. - dict: 部门详情对象。
  32. """
  33. dept = await DeptCRUD(auth).get_by_id_crud(id=id)
  34. result = DeptOutSchema.model_validate(dept).model_dump()
  35. if dept and dept.parent_id:
  36. parent = await DeptCRUD(auth).get(id=dept.parent_id)
  37. if parent:
  38. result['parent_name'] = parent.name
  39. return result
  40. @classmethod
  41. async def get_dept_tree_service(cls, auth: AuthSchema, search: DeptQueryParam | None= None, order_by: list[dict] | None = None) -> list[dict]:
  42. """
  43. 获取部门树形列表。
  44. 参数:
  45. - auth (AuthSchema): 认证对象。
  46. - search (DeptQueryParam | None): 查询参数对象。
  47. - order_by (list[dict] | None): 排序参数。
  48. 返回:
  49. - list[dict]: 部门树形列表对象。
  50. """
  51. # 使用树形结构查询,预加载children关系
  52. dept_list = await DeptCRUD(auth).get_tree_list_crud(search=search.__dict__, order_by=order_by)
  53. # 转换为字典列表
  54. dept_dict_list = [DeptOutSchema.model_validate(dept).model_dump() for dept in dept_list]
  55. # 使用traversal_to_tree构建树形结构
  56. return traversal_to_tree(dept_dict_list)
  57. @classmethod
  58. async def create_dept_service(cls, auth: AuthSchema, data: DeptCreateSchema) -> dict:
  59. """
  60. 创建部门。
  61. 参数:
  62. - auth (AuthSchema): 认证对象。
  63. - data (DeptCreateSchema): 部门创建对象。
  64. 返回:
  65. - dict: 新创建的部门对象。
  66. 异常:
  67. - CustomException: 当部门已存在时抛出。
  68. """
  69. dept = await DeptCRUD(auth).get(name=data.name)
  70. if dept:
  71. raise CustomException(msg='创建失败,该部门已存在')
  72. obj = await DeptCRUD(auth).get(code=data.code)
  73. if obj:
  74. raise CustomException(msg='创建失败,编码已存在')
  75. dept = await DeptCRUD(auth).create(data=data)
  76. return DeptOutSchema.model_validate(dept).model_dump()
  77. @classmethod
  78. async def update_dept_service(cls, auth: AuthSchema, id:int, data: DeptUpdateSchema) -> dict:
  79. """
  80. 更新部门。
  81. 参数:
  82. - auth (AuthSchema): 认证对象。
  83. - id (int): 部门 ID。
  84. - data (DeptUpdateSchema): 部门更新对象。
  85. 返回:
  86. - dict: 更新后的部门对象。
  87. 异常:
  88. - CustomException: 当部门不存在或名称重复时抛出。
  89. """
  90. dept = await DeptCRUD(auth).get_by_id_crud(id=id)
  91. if not dept:
  92. raise CustomException(msg='更新失败,该部门不存在')
  93. exist_dept = await DeptCRUD(auth).get(name=data.name)
  94. if exist_dept and exist_dept.id != id:
  95. raise CustomException(msg='更新失败,部门名称重复')
  96. dept = await DeptCRUD(auth).update(id=id, data=data)
  97. return DeptOutSchema.model_validate(dept).model_dump()
  98. @classmethod
  99. async def delete_dept_service(cls, auth: AuthSchema, ids: list[int]) -> None:
  100. """
  101. 删除部门。
  102. 参数:
  103. - auth (AuthSchema): 认证对象。
  104. - ids (List[int]): 部门 ID 列表。
  105. 返回:
  106. - None
  107. 异常:
  108. - CustomException: 当删除对象为空或部门不存在时抛出。
  109. """
  110. if len(ids) < 1:
  111. raise CustomException(msg='删除失败,删除对象不能为空')
  112. for id in ids:
  113. dept = await DeptCRUD(auth).get_by_id_crud(id=id)
  114. if not dept:
  115. raise CustomException(msg='删除失败,该部门不存在')
  116. # 校验是否存在子级部门,存在则禁止删除
  117. dept_list = await DeptCRUD(auth).get_list_crud()
  118. id_map = get_child_id_map(model_list=dept_list)
  119. for id in ids:
  120. descendants = get_child_recursion(id=id, id_map=id_map)
  121. if len(descendants) > 1:
  122. raise CustomException(msg='删除失败,存在子级部门,请先删除子级部门')
  123. await DeptCRUD(auth).delete(ids=ids)
  124. @classmethod
  125. async def batch_set_available_service(cls, auth: AuthSchema, data: BatchSetAvailable) -> None:
  126. """
  127. 批量设置部门可用状态。
  128. 参数:
  129. - auth (AuthSchema): 认证对象。
  130. - data (BatchSetAvailable): 批量设置可用状态对象。
  131. 返回:
  132. - None
  133. """
  134. dept_list = await DeptCRUD(auth).get_list_crud()
  135. total_ids = []
  136. if data.status:
  137. id_map = get_parent_id_map(model_list=dept_list)
  138. for dept_id in data.ids:
  139. enable_ids = get_parent_recursion(id=dept_id, id_map=id_map)
  140. total_ids.extend(enable_ids)
  141. else:
  142. id_map = get_child_id_map(model_list=dept_list)
  143. for dept_id in data.ids:
  144. disable_ids = get_child_recursion(id=dept_id, id_map=id_map)
  145. total_ids.extend(disable_ids)
  146. await DeptCRUD(auth).set_available_crud(ids=total_ids, status=data.status)