service.py 6.1 KB

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