sys_post.py 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. # -*- coding: utf-8 -*-
  2. # @Author : YY
  3. from typing import List, Literal, Optional
  4. from ruoyi_common.constant import UserConstants
  5. from ruoyi_common.exception import ServiceException
  6. from ruoyi_system.domain.entity import SysPost
  7. from ruoyi_system.domain.po import SysPostPo
  8. from ruoyi_system.mapper.sys_post import SysPostMapper
  9. from ruoyi_system.mapper.sys_user_post import SysUserPostMapper
  10. class SysPostService:
  11. @classmethod
  12. def select_post_list(cls, query:SysPost) -> List[SysPost]:
  13. """
  14. 查询岗位信息列表
  15. Args:
  16. query(SysPost): 包含查询条件的传输对象
  17. Returns:
  18. List[SysPost]: 岗位信息列表
  19. """
  20. return SysPostMapper.select_post_list(query)
  21. @classmethod
  22. def select_post_all(cls) -> List[SysPost]:
  23. """
  24. 查询所有岗位
  25. Returns:
  26. List[SysPost]: 所有岗位信息列表
  27. """
  28. return SysPostMapper.select_post_all()
  29. @classmethod
  30. def select_post_by_id(cls, id:int) -> Optional[SysPost]:
  31. """
  32. 通过岗位ID,查询岗位信息
  33. Args:
  34. id(int): 岗位ID
  35. Returns:
  36. Optional[SysPost]: 岗位信息
  37. """
  38. return SysPostMapper.select_post_by_id(id)
  39. @classmethod
  40. def select_post_list_by_user_id(cls, user_id:int) -> List[int]:
  41. """
  42. 根据用户ID,查询岗位选择框
  43. Args:
  44. user_id(int): 用户ID
  45. Returns:
  46. List[int]: 岗位ID列表
  47. """
  48. return SysPostMapper.select_post_list_by_user_id(user_id)
  49. @classmethod
  50. def check_post_name_unique(cls, body:SysPost) -> Literal["0","1"]:
  51. """
  52. 校验岗位名称是否唯一
  53. Args:
  54. body(SysPost): 包含岗位名称的传输对象
  55. Returns:
  56. str: 唯一或不唯一, 0-唯一, 1-不唯一
  57. """
  58. post_id = -1 if body.post_id is None else body.post_id
  59. eo:SysPost = SysPostMapper.check_post_name_unique(body.post_name)
  60. if eo is not None and eo.post_id != post_id:
  61. return UserConstants.NOT_UNIQUE
  62. return UserConstants.UNIQUE
  63. @classmethod
  64. def check_post_code_unique(cls, body:SysPost) -> Literal["0","1"]:
  65. """
  66. 校验岗位编码是否唯一
  67. Args:
  68. body(SysPost): 包含岗位编码的传输对象
  69. Returns:
  70. str: 唯一或不唯一, 0-唯一, 1-不唯一
  71. """
  72. post_code = -1 if body.post_code is None else body.post_code
  73. eo:SysPostPo = SysPostMapper.check_post_code_unique(body.post_code)
  74. if eo is not None and eo.post_code != post_code:
  75. return UserConstants.NOT_UNIQUE
  76. return UserConstants.UNIQUE
  77. @classmethod
  78. def count_user_post_by_id(cls, id:int) -> int:
  79. """
  80. 根据岗位ID,查询岗位使用数量
  81. Args:
  82. id(int): 岗位ID
  83. Returns:
  84. int: 岗位使用数量
  85. """
  86. return SysUserPostMapper.count_user_post_by_id(id)
  87. @classmethod
  88. def delete_post_by_id(cls, id:int) -> int:
  89. """
  90. 删除岗位信息
  91. Args:
  92. id(int): 岗位ID
  93. Returns:
  94. int: 删除的数量
  95. """
  96. return SysPostMapper.delete_post_by_id(id)
  97. @classmethod
  98. def delete_post_by_ids(cls, ids:List[int]) -> int:
  99. """
  100. 批量删除岗位信息
  101. Args:
  102. ids(List[int]): 岗位ID列表
  103. Returns:
  104. int: 删除的数量
  105. """
  106. for id in ids:
  107. po:SysPostPo = cls.select_post_by_id(id)
  108. if cls.count_user_post_by_id(id) > 0:
  109. raise ServiceException(f"{po.post_name}已分配,不能删除")
  110. return SysPostMapper.delete_post_by_ids(ids)
  111. @classmethod
  112. def insert_post(cls, body:SysPost) -> int:
  113. """
  114. 新增岗位信息
  115. Args:
  116. body(SysPost): 包含岗位信息的传输对象
  117. Returns:
  118. int: 新增的岗位ID
  119. """
  120. return SysPostMapper.insert_post(body)
  121. @classmethod
  122. def update_post(cls, body:SysPost) -> int:
  123. """
  124. 修改岗位信息
  125. Args:
  126. body(SysPost): 包含岗位信息的传输对象
  127. Returns:
  128. int: 修改的数量
  129. """
  130. return SysPostMapper.update_post(body)