crud.py 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. # -*- coding: utf-8 -*-
  2. from typing import Sequence
  3. from app.core.base_crud import CRUDBase
  4. from .model import RoleModel
  5. from .schema import RoleCreateSchema, RoleUpdateSchema
  6. from ..auth.schema import AuthSchema
  7. from ..menu.crud import MenuCRUD
  8. from ..dept.crud import DeptCRUD
  9. class RoleCRUD(CRUDBase[RoleModel, RoleCreateSchema, RoleUpdateSchema]):
  10. """角色模块数据层"""
  11. def __init__(self, auth: AuthSchema) -> None:
  12. """
  13. 初始化角色模块数据层
  14. 参数:
  15. - auth (AuthSchema): 认证信息模型
  16. """
  17. self.auth = auth
  18. super().__init__(model=RoleModel, auth=auth)
  19. async def get_by_id_crud(self, id: int, preload: list | None = None) -> RoleModel | None:
  20. """
  21. 根据id获取角色信息
  22. 参数:
  23. - id (int): 角色ID
  24. - preload (list | None): 预加载选项
  25. 返回:
  26. - RoleModel | None: 角色模型对象
  27. """
  28. return await self.get(id=id, preload=preload)
  29. async def get_list_crud(self, search: dict | None = None, order_by: list | None = None, preload: list | None = None) -> Sequence[RoleModel]:
  30. """
  31. 获取角色列表
  32. 参数:
  33. - search (dict | None): 查询参数
  34. - order_by (list | None): 排序参数
  35. - preload (list | None): 预加载选项
  36. 返回:
  37. - Sequence[RoleModel]: 角色模型对象列表
  38. """
  39. return await self.list(search=search, order_by=order_by, preload=preload)
  40. async def set_role_menus_crud(self, role_ids: list[int], menu_ids: list[int]) -> None:
  41. """
  42. 设置角色的菜单权限
  43. 参数:
  44. - role_ids (List[int]): 角色ID列表
  45. - menu_ids (List[int]): 菜单ID列表
  46. 返回:
  47. - None
  48. """
  49. roles = await self.list(search={"id": ("in", role_ids)})
  50. menus = await MenuCRUD(self.auth).get_list_crud(search={"id": ("in", menu_ids)})
  51. for obj in roles:
  52. relationship = obj.menus
  53. relationship.clear()
  54. relationship.extend(menus)
  55. await self.auth.db.flush()
  56. async def set_role_data_scope_crud(self, role_ids: list[int], data_scope: int) -> None:
  57. """
  58. 设置角色的数据范围
  59. 参数:
  60. - role_ids (list[int]): 角色ID列表
  61. - data_scope (int): 数据范围
  62. 返回:
  63. - None
  64. """
  65. await self.set(ids=role_ids, data_scope=data_scope)
  66. async def set_role_depts_crud(self, role_ids: list[int], dept_ids: list[int]) -> None:
  67. """
  68. 设置角色的部门权限
  69. 参数:
  70. - role_ids (list[int]): 角色ID列表
  71. - dept_ids (list[int]): 部门ID列表
  72. 返回:
  73. - None
  74. """
  75. roles = await self.list(search={"id": ("in", role_ids)})
  76. depts = await DeptCRUD(self.auth).get_list_crud(search={"id": ("in", dept_ids)})
  77. for obj in roles:
  78. relationship = obj.depts
  79. relationship.clear()
  80. relationship.extend(depts)
  81. await self.auth.db.flush()
  82. async def set_available_crud(self, ids: list[int], status: str) -> None:
  83. """
  84. 设置角色的可用状态
  85. 参数:
  86. - ids (list[int]): 角色ID列表
  87. - status (str): 可用状态
  88. 返回:
  89. - None
  90. """
  91. await self.set(ids=ids, status=status)