sys_post.py 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. # -*- coding: utf-8 -*-
  2. # @Author : YY
  3. from typing import List, Optional
  4. from flask import g
  5. from sqlalchemy import delete, insert, select, update
  6. from ruoyi_common.sqlalchemy.model import ColumnEntityList
  7. from ruoyi_common.sqlalchemy.transaction import Transactional
  8. from ruoyi_system.domain.entity import SysPost
  9. from ruoyi_admin.ext import db
  10. from ruoyi_system.domain.po import SysPostPo, SysUserPo, SysUserPostPo
  11. class SysPostMapper:
  12. """
  13. 岗位的数据访问层
  14. """
  15. default_fields = {
  16. 'post_id', 'post_name', 'post_code', 'post_sort', 'status',
  17. 'create_by', 'create_time', 'remark'
  18. }
  19. default_columns = ColumnEntityList(SysPostPo, default_fields, False)
  20. @classmethod
  21. def select_post_list(cls, post: SysPost) -> List[SysPost]:
  22. """
  23. 根据条件,查询岗位列表
  24. Args:
  25. post (SysPost): 岗位条件
  26. Returns:
  27. List[SysPost]: 岗位列表
  28. """
  29. criterions = []
  30. if post.post_code:
  31. criterions.append(SysPostPo.post_code.like(f'%{post.post_code}%'))
  32. if post.post_name:
  33. criterions.append(SysPostPo.post_name.like(f'%{post.post_name}%'))
  34. if post.status:
  35. criterions.append(SysPostPo.post_status == post.status)
  36. stmt = select(*cls.default_columns).where(*criterions)
  37. if "criterian_meta" in g and g.criterian_meta.page:
  38. g.criterian_meta.page.stmt = stmt
  39. rows = db.session.execute(stmt).all()
  40. return [cls.default_columns.cast(row, SysPost) for row in rows]
  41. @classmethod
  42. def select_post_all(cls) -> List[SysPost]:
  43. """
  44. 查询所有岗位
  45. Returns:
  46. List[SysPost]: 岗位列表
  47. """
  48. stmt = select(*cls.default_columns)
  49. rows = db.session.execute(stmt).all()
  50. return [cls.default_columns.cast(row, SysPost) for row in rows]
  51. @classmethod
  52. def select_post_by_id(cls, post_id: int) -> Optional[SysPost]:
  53. """
  54. 通过岗位ID查询岗位信息
  55. Args:
  56. post_id (int): 岗位ID
  57. Returns:
  58. Optional[SysPost]: 岗位信息
  59. """
  60. stmt = select(*cls.default_columns).where(SysPostPo.post_id == post_id)
  61. row = db.session.execute(stmt).one_or_none()
  62. return cls.default_columns.cast(row, SysPost) if row else None
  63. @classmethod
  64. def select_post_list_by_user_id(cls, user_id: int) -> List[int]:
  65. """
  66. 根据用户ID,查询岗位ID列表
  67. Args:
  68. user_id (int): 用户ID
  69. Returns:
  70. List[int]: 岗位ID列表
  71. """
  72. stmt = select(SysPostPo.post_id).select_from(SysPostPo) \
  73. .join(SysUserPostPo, SysUserPostPo.post_id == SysPostPo.post_id) \
  74. .join(SysUserPo, SysUserPo.user_id == SysUserPostPo.user_id) \
  75. .where(SysUserPo.user_id == user_id)
  76. return db.session.execute(stmt).scalars().all()
  77. @classmethod
  78. def select_posts_by_user_name(cls, user_name: str) -> List[SysPost]:
  79. """
  80. 根据用户名,查询岗位列表
  81. Args:
  82. user_name (str): 用户名
  83. Returns:
  84. List[SysPost]: 岗位列表
  85. """
  86. fields = {"post_id", "post_name", "post_code"}
  87. columns = ColumnEntityList(SysPostPo, fields, False)
  88. stmt = select(*columns).select_from(SysPostPo) \
  89. .join(SysUserPostPo, SysUserPostPo.post_id == SysPostPo.id) \
  90. .join(SysUserPo, SysUserPo.user_id == SysUserPostPo.user_id) \
  91. .where(SysUserPo.user_name == user_name)
  92. rows = db.session.execute(stmt).all()
  93. eos = list()
  94. for row in rows:
  95. eos.append(columns.cast(row, SysPost))
  96. return eos
  97. @classmethod
  98. @Transactional(db.session)
  99. def delete_post_by_id(cls, post_id: int) -> int:
  100. """
  101. 删除岗位信息
  102. Args:
  103. post_id (int): 岗位ID
  104. Returns:
  105. int: 影响行数
  106. """
  107. stmt = delete(SysPostPo).where(SysPostPo.post_id == post_id)
  108. num = db.session.execute(stmt).rowcount
  109. return num
  110. @classmethod
  111. @Transactional(db.session)
  112. def delete_post_by_ids(cls, post_ids: List[int]) -> int:
  113. """
  114. 批量删除岗位信息
  115. Args:
  116. post_ids (List[int]): 岗位ID列表
  117. Returns:
  118. int: 影响行数
  119. """
  120. stmt = delete(SysPostPo).where(SysPostPo.post_id.in_(post_ids))
  121. num = db.session.execute(stmt).rowcount
  122. return num
  123. @classmethod
  124. @Transactional(db.session)
  125. def update_post(cls, post: SysPost) -> int:
  126. """
  127. 修改岗位信息
  128. Args:
  129. post (SysPost): 岗位信息
  130. Returns:
  131. int: 影响行数
  132. """
  133. fields = {
  134. "post_name", "post_code", "post_sort", "status", "remark",
  135. "update_by", "update_time"
  136. }
  137. data = post.model_dump(
  138. include=fields,
  139. exclude_none=True,
  140. exclude_unset=True
  141. )
  142. stmt = update(SysPostPo).where(SysPostPo.post_id == post.post_id) \
  143. .values(data)
  144. return db.session.execute(stmt).rowcount
  145. @classmethod
  146. @Transactional(db.session)
  147. def insert_post(cls, post: SysPost) -> int:
  148. """
  149. 新增岗位信息
  150. Args:
  151. post (SysPost): 岗位信息
  152. Returns:
  153. int: 新增记录的ID
  154. """
  155. fields = {
  156. "post_id", "post_name", "post_code", "post_sort", "status",
  157. "remark", "create_by", "create_time"
  158. }
  159. data = post.model_dump(
  160. include=fields,
  161. exclude_none=True,
  162. exclude_unset=True
  163. )
  164. stmt = insert(SysPostPo).values(data)
  165. out = db.session.execute(stmt).inserted_primary_key
  166. return out[0] if out else 0
  167. @classmethod
  168. def check_post_name_unique(cls, post_name: str) -> Optional[SysPost]:
  169. """
  170. 校验岗位名称
  171. Args:
  172. post_name (str): 岗位名称
  173. Returns:
  174. Optional[SysPost]: 岗位信息
  175. """
  176. stmt = select(*cls.default_columns) \
  177. .where(SysPostPo.post_name == post_name) \
  178. .limit(1)
  179. row = db.session.execute(stmt).one_or_none()
  180. return cls.default_columns.cast(row, SysPost) if row else None
  181. @classmethod
  182. def check_post_code_unique(cls, post_code: str) -> Optional[SysPost]:
  183. """
  184. 校验岗位编码
  185. Args:
  186. post_code (str): 岗位编码
  187. Returns:
  188. Optional[SysPost]: 岗位信息
  189. """
  190. stmt = select(*cls.default_columns) \
  191. .where(SysPostPo.post_code == post_code) \
  192. .limit(1)
  193. row = db.session.execute(stmt).one_or_none()
  194. return cls.default_columns.cast(row, SysPost) if row else None
  195. @classmethod
  196. def delete_user_post_by_user_id(cls, user_id):
  197. stmt = delete(SysUserPostPo) \
  198. .where(SysUserPostPo.user_id == user_id)
  199. return db.session.execute(stmt).rowcount