sys_notice.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. # -*- coding: utf-8 -*-
  2. # @Author : YY
  3. from types import NoneType
  4. from typing import List, Optional
  5. from flask import g
  6. from sqlalchemy import delete, insert, select, update
  7. from ruoyi_common.sqlalchemy.model import ColumnEntityList
  8. from ruoyi_system.domain.entity import SysNotice
  9. from ruoyi_system.domain.po import SysNoticePo
  10. from ruoyi_admin.ext import db
  11. class SysNoticeMapper:
  12. """
  13. 公告的数据访问层
  14. """
  15. default_fields = {
  16. "notice_id", "notice_title", "notice_type", "notice_content", "status",
  17. "create_time", "update_time", "create_by", "update_by", "remark"
  18. }
  19. default_columns = ColumnEntityList(SysNoticePo, default_fields, False)
  20. @classmethod
  21. def select_notice_by_id(cls, id: int) -> Optional[SysNotice]:
  22. """
  23. 根据公告ID,查询公告信息
  24. Args:
  25. id (int): 公告ID
  26. Returns:
  27. Optional[SysNotice]: 公告信息
  28. """
  29. stmt = select(*cls.default_columns) \
  30. .where(SysNoticePo.notice_id == id)
  31. row = db.session.execute(stmt).one_or_none()
  32. return cls.default_columns.cast(row,SysNotice) if row else None
  33. @classmethod
  34. def select_notice_list(cls, notice: SysNotice|NoneType) -> List[SysNotice]:
  35. """
  36. 根据条件,查询公告列表
  37. Args:
  38. notice (SysNotice|NoneType): 公告信息
  39. Returns:
  40. List[SysNotice]: 公告列表
  41. """
  42. if notice is None:
  43. stmt = select(*cls.default_columns)
  44. else:
  45. criterions = []
  46. if notice.notice_title:
  47. criterions.append(
  48. SysNoticePo.notice_title.like(f'%{notice.notice_title}%')
  49. )
  50. if notice.notice_type:
  51. criterions.append(
  52. SysNoticePo.notice_type == notice.notice_type
  53. )
  54. if notice.create_by:
  55. criterions.append(
  56. SysNoticePo.create_by.like(f'%{notice.create_by}%')
  57. )
  58. stmt = select(*cls.default_columns) \
  59. .where(*criterions)
  60. if "criterian_meta" in g and g.criterian_meta.page:
  61. g.criterian_meta.page.stmt = stmt
  62. rows = db.session.execute(stmt).all()
  63. return [cls.default_columns.cast(row,SysNotice) for row in rows]
  64. @classmethod
  65. def insert_notice(cls, notice: SysNotice) -> int:
  66. """
  67. 新增一条公告记录
  68. Args:
  69. notice (SysNotice): 公告信息
  70. Returns:
  71. int: 新增记录的ID
  72. """
  73. fields = {
  74. "notice_title", "notice_type", "notice_content", "status",
  75. "create_by", "create_time", "remark"
  76. }
  77. data = notice.model_dump(
  78. include=fields, exclude_none=True, exclude_unset=True
  79. )
  80. stmt = insert(SysNoticePo).values(data)
  81. out = db.session.execute(stmt).inserted_primary_key
  82. return out[0] if out else 0
  83. @classmethod
  84. def update_notice(cls, notice: SysNotice) -> int:
  85. """
  86. 修改公告信息
  87. Args:
  88. notice (SysNotice): 公告信息
  89. Returns:
  90. int: 修改数据量
  91. """
  92. fields = {
  93. "notice_title", "notice_type", "notice_content", "status",
  94. "update_by", "update_time", "remark"
  95. }
  96. data = notice.model_dump(
  97. include=fields, exclude_none=True, exclude_unset=True
  98. )
  99. stmt = update(SysNoticePo) \
  100. .where(SysNoticePo.notice_id == notice.notice_id) \
  101. .values(data)
  102. return db.session.execute(stmt).rowcount
  103. @classmethod
  104. def delete_notice_by_id(cls, id: int) -> int:
  105. """
  106. 根据公告ID,删除公告
  107. Args:
  108. id (int): 公告ID
  109. Returns:
  110. int: 删除数据量
  111. """
  112. stmt = delete(SysNoticePo) \
  113. .where(SysNoticePo.notice_id == id)
  114. return db.session.execute(stmt).rowcount
  115. @classmethod
  116. def delete_notice_by_ids(cls, ids: List[int]) -> int:
  117. """
  118. 批量删除公告
  119. Args:
  120. ids (List[int]): 公告ID列表
  121. Returns:
  122. int: 删除数据量
  123. """
  124. stmt = delete(SysNoticePo) \
  125. .where(SysNoticePo.notice_id.in_(ids))
  126. return db.session.execute(stmt).rowcount