sys_role_dept.py 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. # -*- coding: utf-8 -*-
  2. # @Author : YY
  3. from typing import List
  4. from sqlalchemy import delete, func, insert, select
  5. from ruoyi_admin.ext import db
  6. from ruoyi_common.sqlalchemy.transaction import Transactional
  7. from ruoyi_system.domain.entity import SysRoleDept
  8. from ruoyi_system.domain.po import SysRoleDeptPo
  9. class SysRoleDeptMapper:
  10. """
  11. 部门与角色相关联的数据访问层
  12. """
  13. @classmethod
  14. @Transactional(db.session)
  15. def batch_role_dept(cls, role_dept_list: List[SysRoleDept]) -> int:
  16. """
  17. 批量新增角色部门信息
  18. Args:
  19. role_dept_list (List[SysRoleDept]): 角色部门列表
  20. Returns:
  21. int: 操作影响的行数
  22. """
  23. role_dept_list = [
  24. row.model_dump(
  25. exclude_none=True,
  26. exclude_unset=True,
  27. ) for row in role_dept_list]
  28. stmt = insert(SysRoleDeptPo).values(role_dept_list)
  29. return db.session.execute(stmt).rowcount
  30. @classmethod
  31. @Transactional(db.session)
  32. def delete_role_dept_by_role_id(cls, role_id: int) -> int:
  33. """
  34. 根据角色ID,删除角色和部门关联数据
  35. Args:
  36. role_id (int): 角色ID
  37. Returns:
  38. int: 操作影响的行数
  39. """
  40. stmt = delete(SysRoleDeptPo).where(SysRoleDeptPo.role_id == role_id)
  41. return db.session.execute(stmt).rowcount
  42. @classmethod
  43. @Transactional(db.session)
  44. def delete_role_dept(cls, role_ids: List[int]) -> int:
  45. """
  46. 批量删除角色部门关联信息
  47. Args:
  48. role_ids (List[int]): 多个角色ID
  49. Returns:
  50. int: 操作影响的行数
  51. """
  52. stmt = delete(SysRoleDeptPo).where(SysRoleDeptPo.role_id.in_(role_ids))
  53. return db.session.execute(stmt).rowcount
  54. @classmethod
  55. def select_count_role_dept_by_dept_id(cls, dept_id: int) -> int:
  56. """
  57. 查询部门使用的角色数量
  58. Args:
  59. dept_id (int): 部门ID
  60. Returns:
  61. int: 角色数量
  62. """
  63. stmt = select(func.count()).select_from(SysRoleDeptPo) \
  64. .where(SysRoleDeptPo.dept_id == dept_id)
  65. return db.session.execute(stmt).scalar_one_or_none() or 0
  66. @classmethod
  67. def select_dept_ids_by_role_id(cls, role_id: int) -> List[int]:
  68. """
  69. 查询角色下的部门ID列表
  70. Args:
  71. role_id (int): 角色ID
  72. Returns:
  73. List[int]: 部门ID列表
  74. """
  75. stmt = select(SysRoleDeptPo.dept_id) \
  76. .where(SysRoleDeptPo.role_id == role_id)
  77. return db.session.execute(stmt).scalars().all()