service.py 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. from typing import Any
  2. from app.api.v1.module_system.auth.schema import AuthSchema
  3. from app.core.base_schema import BatchSetAvailable
  4. from app.core.exceptions import CustomException
  5. from app.utils.common_util import (
  6. get_child_id_map,
  7. get_child_recursion,
  8. get_parent_id_map,
  9. get_parent_recursion,
  10. traversal_to_tree,
  11. )
  12. from .crud import MenuCRUD
  13. from .schema import (
  14. MenuCreateSchema,
  15. MenuOutSchema,
  16. MenuQueryParam,
  17. MenuUpdateSchema,
  18. )
  19. class MenuService:
  20. """
  21. 菜单模块服务层
  22. """
  23. @classmethod
  24. async def _validate_parent_child_type(
  25. cls, auth: AuthSchema, parent_id: int | None, child_type: int
  26. ) -> None:
  27. """
  28. 父子类型约束:目录下仅允许目录/菜单/外链;菜单下仅允许按钮;按钮与外链下不可挂子级。
  29. 无父级时仅允许目录、菜单、外链(与前端一致)。
  30. """
  31. if parent_id is None:
  32. if child_type not in (1, 2, 4):
  33. raise CustomException(msg="顶级菜单仅允许目录、菜单或外链类型")
  34. return
  35. parent = await MenuCRUD(auth).get_by_id_crud(id=parent_id)
  36. if not parent:
  37. raise CustomException(msg="父级菜单不存在")
  38. pt = parent.type
  39. if pt == 1:
  40. if child_type not in (1, 2, 4):
  41. raise CustomException(msg="目录下仅允许新增目录、菜单或外链")
  42. elif pt == 2:
  43. if child_type != 3:
  44. raise CustomException(msg="菜单下仅允许新增按钮")
  45. else:
  46. raise CustomException(msg="菜单或链接类型下不允许新增子菜单")
  47. @classmethod
  48. async def get_menu_detail_service(cls, auth: AuthSchema, id: int) -> dict:
  49. """
  50. 获取菜单详情。
  51. 参数:
  52. - auth (AuthSchema): 认证对象。
  53. - id (int): 菜单ID。
  54. 返回:
  55. - dict: 菜单详情对象。
  56. """
  57. menu = await MenuCRUD(auth).get_by_id_crud(id=id)
  58. # 创建实例后再设置parent_name属性
  59. menu_out = MenuOutSchema.model_validate(menu)
  60. if menu and menu.parent_id:
  61. parent = await MenuCRUD(auth).get_by_id_crud(id=menu.parent_id)
  62. if parent:
  63. menu_out.parent_name = parent.name
  64. return menu_out.model_dump()
  65. @classmethod
  66. async def get_menu_tree_service(
  67. cls,
  68. auth: AuthSchema,
  69. search: MenuQueryParam | None = None,
  70. order_by: list[dict] | None = None,
  71. ) -> list[dict]:
  72. """
  73. 获取菜单树形列表。
  74. 参数:
  75. - auth (AuthSchema): 认证对象。
  76. - search (MenuQueryParam | None): 查询参数对象。
  77. - order_by (list[dict] | None): 排序参数列表。
  78. 返回:
  79. - list[dict]: 菜单树形列表对象。
  80. """
  81. # 使用树形结构查询,预加载children关系
  82. menu_list = await MenuCRUD(auth).get_tree_list_crud(
  83. search=search.__dict__, order_by=order_by
  84. )
  85. # 转换为字典列表
  86. menu_dict_list = [MenuOutSchema.model_validate(menu).model_dump() for menu in menu_list]
  87. # 使用traversal_to_tree构建树形结构
  88. return traversal_to_tree(menu_dict_list)
  89. @classmethod
  90. async def create_menu_service(cls, auth: AuthSchema, data: MenuCreateSchema) -> dict:
  91. """
  92. 创建菜单。
  93. 参数:
  94. - auth (AuthSchema): 认证对象。
  95. - data (MenuCreateSchema): 创建参数对象。
  96. 返回:
  97. - dict: 创建的菜单对象。
  98. """
  99. search: dict[str, Any] = {"title": data.title}
  100. if data.parent_id is not None:
  101. search["parent_id"] = data.parent_id
  102. menu = await MenuCRUD(auth).get(**search)
  103. if menu:
  104. raise CustomException(msg="创建失败,该菜单已存在")
  105. await cls._validate_parent_child_type(auth, data.parent_id, data.type)
  106. new_menu = await MenuCRUD(auth).create(data=data)
  107. new_menu_dict = MenuOutSchema.model_validate(new_menu).model_dump()
  108. return new_menu_dict
  109. @classmethod
  110. async def update_menu_service(cls, auth: AuthSchema, id: int, data: MenuUpdateSchema) -> dict:
  111. """
  112. 更新菜单。
  113. 参数:
  114. - auth (AuthSchema): 认证对象。
  115. - id (int): 菜单ID。
  116. - data (MenuUpdateSchema): 更新参数对象。
  117. 返回:
  118. - dict: 更新的菜单对象。
  119. """
  120. menu = await MenuCRUD(auth).get_by_id_crud(id=id)
  121. if not menu:
  122. raise CustomException(msg="更新失败,该菜单不存在")
  123. await cls._validate_parent_child_type(auth, data.parent_id, data.type)
  124. search: dict[str, Any] = {"title": data.title}
  125. if data.parent_id is not None:
  126. search["parent_id"] = data.parent_id
  127. exist_menu = await MenuCRUD(auth).get(**search)
  128. if exist_menu and exist_menu.id != id:
  129. raise CustomException(msg="更新失败,菜单标题重复")
  130. if data.parent_id:
  131. parent_menu = await MenuCRUD(auth).get_by_id_crud(id=data.parent_id)
  132. if not parent_menu:
  133. raise CustomException(msg="更新失败,父级菜单不存在")
  134. data.parent_name = parent_menu.title
  135. new_menu = await MenuCRUD(auth).update(id=id, data=data)
  136. await cls.set_menu_available_service(
  137. auth=auth, data=BatchSetAvailable(ids=[id], status=data.status)
  138. )
  139. new_menu_dict = MenuOutSchema.model_validate(new_menu).model_dump()
  140. return new_menu_dict
  141. @classmethod
  142. async def delete_menu_service(cls, auth: AuthSchema, ids: list[int]) -> None:
  143. """
  144. 删除菜单。
  145. 参数:
  146. - auth (AuthSchema): 认证对象。
  147. - ids (list[int]): 菜单ID列表。
  148. 返回:
  149. - None
  150. """
  151. if len(ids) < 1:
  152. raise CustomException(msg="删除失败,删除对象不能为空")
  153. # 获取所有菜单列表,用于构建树形关系
  154. all_menus = await MenuCRUD(auth).get_list_crud()
  155. # 构建子菜单ID映射
  156. child_id_map = get_child_id_map(model_list=all_menus)
  157. # 收集所有需要删除的菜单ID,包括直接指定的ID和它们的所有子菜单ID
  158. delete_ids_set = set()
  159. for id in ids:
  160. # 递归获取该ID的所有子菜单ID
  161. all_descendants = get_child_recursion(id=id, id_map=child_id_map)
  162. delete_ids_set.update(all_descendants)
  163. # 将集合转换为列表
  164. delete_ids = list(delete_ids_set)
  165. # 执行批量删除操作
  166. await MenuCRUD(auth).delete(ids=delete_ids)
  167. @classmethod
  168. async def set_menu_available_service(cls, auth: AuthSchema, data: BatchSetAvailable) -> None:
  169. """
  170. 递归获取所有父、子级菜单,然后批量修改菜单可用状态。
  171. 参数:
  172. - auth (AuthSchema): 认证对象。
  173. - data (BatchSetAvailable): 批量设置可用参数对象。
  174. 返回:
  175. - None
  176. """
  177. menu_list = await MenuCRUD(auth).get_list_crud()
  178. total_ids = []
  179. if data.status == "0":
  180. # 激活,则需要把所有父级菜单都激活
  181. id_map = get_parent_id_map(model_list=menu_list)
  182. for menu_id in data.ids:
  183. enable_ids = get_parent_recursion(id=menu_id, id_map=id_map)
  184. total_ids.extend(enable_ids)
  185. else:
  186. # 禁止,则需要把所有子级菜单都禁止
  187. id_map = get_child_id_map(model_list=menu_list)
  188. for menu_id in data.ids:
  189. disable_ids = get_child_recursion(id=menu_id, id_map=id_map)
  190. total_ids.extend(disable_ids)
  191. await MenuCRUD(auth).set_available_crud(ids=total_ids, status=data.status)