service.py 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. from app.api.v1.module_system.auth.schema import AuthSchema
  2. from app.core.base_schema import BatchSetAvailable
  3. from app.core.exceptions import CustomException
  4. from app.utils.excel_util import ExcelUtil
  5. from .crud import NoticeCRUD
  6. from .schema import (
  7. NoticeCreateSchema,
  8. NoticeOutSchema,
  9. NoticeQueryParam,
  10. NoticeUpdateSchema,
  11. )
  12. class NoticeService:
  13. """
  14. 公告管理模块服务层
  15. """
  16. @classmethod
  17. async def get_notice_detail_service(cls, auth: AuthSchema, id: int) -> dict:
  18. """
  19. 获取公告详情。
  20. 参数:
  21. - auth (AuthSchema): 认证信息模型。
  22. - id (int): 公告ID。
  23. 返回:
  24. - Dict: 公告详情字典。
  25. """
  26. notice_obj = await NoticeCRUD(auth).get_by_id_crud(id=id)
  27. return NoticeOutSchema.model_validate(notice_obj).model_dump()
  28. @classmethod
  29. async def get_notice_list_available_service(cls, auth: AuthSchema) -> list[dict]:
  30. """
  31. 获取可用的公告列表。
  32. 参数:
  33. - auth (AuthSchema): 认证信息模型。
  34. 返回:
  35. - list[dict]: 可用公告详情字典列表。
  36. """
  37. notice_obj_list = await NoticeCRUD(auth).get_list_crud(search={"status": "0"})
  38. return [
  39. NoticeOutSchema.model_validate(notice_obj).model_dump()
  40. for notice_obj in notice_obj_list
  41. ]
  42. @classmethod
  43. async def get_notice_list_service(
  44. cls,
  45. auth: AuthSchema,
  46. search: NoticeQueryParam | None = None,
  47. order_by: list[dict] | None = None,
  48. ) -> list[dict]:
  49. """
  50. 获取公告列表。
  51. 参数:
  52. - auth (AuthSchema): 认证信息模型。
  53. - search (NoticeQueryParam | None): 查询参数模型。
  54. - order_by (list[dict] | None): 排序参数列表。
  55. 返回:
  56. - list[dict]: 公告详情字典列表。
  57. """
  58. notice_obj_list = await NoticeCRUD(auth).get_list_crud(
  59. search=search.__dict__, order_by=order_by
  60. )
  61. return [
  62. NoticeOutSchema.model_validate(notice_obj).model_dump()
  63. for notice_obj in notice_obj_list
  64. ]
  65. @classmethod
  66. async def get_notice_page_service(
  67. cls,
  68. auth: AuthSchema,
  69. page_no: int,
  70. page_size: int,
  71. search: NoticeQueryParam | None = None,
  72. order_by: list[dict] | None = None,
  73. ) -> dict:
  74. """
  75. 分页查询公告(数据库 OFFSET/LIMIT)。
  76. 参数:
  77. - auth (AuthSchema): 认证信息模型
  78. - page_no (int): 页码(从 1 开始)
  79. - page_size (int): 每页条数
  80. - search (NoticeQueryParam | None): 查询条件
  81. - order_by (list[dict] | None): 排序字段列表
  82. 返回:
  83. - dict: 分页结果(结构由 `CRUD.page` 返回约定)
  84. """
  85. offset = (page_no - 1) * page_size
  86. return await NoticeCRUD(auth).page(
  87. offset=offset,
  88. limit=page_size,
  89. order_by=order_by or [{"id": "asc"}],
  90. search=search.__dict__ if search else {},
  91. out_schema=NoticeOutSchema,
  92. )
  93. @classmethod
  94. async def get_notice_available_page_service(cls, auth: AuthSchema) -> dict:
  95. """
  96. 已启用公告分页(与历史行为一致:固定第 1 页、每页 10 条)。
  97. 参数:
  98. - auth (AuthSchema): 认证信息模型
  99. 返回:
  100. - dict: 分页结果(结构由 `CRUD.page` 返回约定)
  101. """
  102. return await NoticeCRUD(auth).page(
  103. offset=0,
  104. limit=10,
  105. order_by=[{"id": "asc"}],
  106. search={"status": "0"},
  107. out_schema=NoticeOutSchema,
  108. )
  109. @classmethod
  110. async def create_notice_service(cls, auth: AuthSchema, data: NoticeCreateSchema) -> dict:
  111. """
  112. 创建公告。
  113. 参数:
  114. - auth (AuthSchema): 认证信息模型。
  115. - data (NoticeCreateSchema): 创建公告负载模型。
  116. 返回:
  117. - dict: 创建的公告详情字典。
  118. 异常:
  119. - CustomException: 创建失败,该公告通知已存在。
  120. """
  121. notice = await NoticeCRUD(auth).get(notice_title=data.notice_title)
  122. if notice:
  123. raise CustomException(msg="创建失败,该公告通知已存在")
  124. notice_obj = await NoticeCRUD(auth).create_crud(data=data)
  125. return NoticeOutSchema.model_validate(notice_obj).model_dump()
  126. @classmethod
  127. async def update_notice_service(
  128. cls, auth: AuthSchema, id: int, data: NoticeUpdateSchema
  129. ) -> dict:
  130. """
  131. 更新公告。
  132. 参数:
  133. - auth (AuthSchema): 认证信息模型。
  134. - id (int): 公告ID。
  135. - data (NoticeUpdateSchema): 更新公告负载模型。
  136. 返回:
  137. - dict: 更新的公告详情字典。
  138. 异常:
  139. - CustomException: 更新失败,该公告通知不存在或公告通知标题重复。
  140. """
  141. notice = await NoticeCRUD(auth).get_by_id_crud(id=id)
  142. if not notice:
  143. raise CustomException(msg="更新失败,该公告通知不存在")
  144. exist_notice = await NoticeCRUD(auth).get(notice_title=data.notice_title)
  145. if exist_notice and exist_notice.id != id:
  146. raise CustomException(msg="更新失败,公告通知标题重复")
  147. notice_obj = await NoticeCRUD(auth).update_crud(id=id, data=data)
  148. return NoticeOutSchema.model_validate(notice_obj).model_dump()
  149. @classmethod
  150. async def delete_notice_service(cls, auth: AuthSchema, ids: list[int]) -> None:
  151. """
  152. 删除公告。
  153. 参数:
  154. - auth (AuthSchema): 认证信息模型。
  155. - ids (list[int]): 删除的ID列表。
  156. 异常:
  157. - CustomException: 删除失败,删除对象不能为空或该公告通知不存在。
  158. 返回:
  159. - None
  160. """
  161. if len(ids) < 1:
  162. raise CustomException(msg="删除失败,删除对象不能为空")
  163. for id in ids:
  164. notice = await NoticeCRUD(auth).get_by_id_crud(id=id)
  165. if not notice:
  166. raise CustomException(msg="删除失败,该公告通知不存在")
  167. await NoticeCRUD(auth).delete_crud(ids=ids)
  168. @classmethod
  169. async def set_notice_available_service(cls, auth: AuthSchema, data: BatchSetAvailable) -> None:
  170. """
  171. 批量设置公告状态。
  172. 参数:
  173. - auth (AuthSchema): 认证信息模型。
  174. - data (BatchSetAvailable): 批量设置可用负载模型。
  175. 异常:
  176. - CustomException: 批量设置失败,该公告通知不存在。
  177. 返回:
  178. - None
  179. """
  180. await NoticeCRUD(auth).set_available_crud(ids=data.ids, status=data.status)
  181. @classmethod
  182. async def export_notice_service(cls, notice_list: list[dict]) -> bytes:
  183. """
  184. 导出公告列表。
  185. 参数:
  186. - notice_list (list[dict]): 公告详情字典列表。
  187. 返回:
  188. - bytes: Excel 文件的字节流。
  189. """
  190. mapping_dict = {
  191. "id": "编号",
  192. "notice_title": "公告标题",
  193. "notice_type": "公告类型(1通知 2公告)",
  194. "notice_content": "公告内容",
  195. "status": "状态",
  196. "description": "备注",
  197. "created_time": "创建时间",
  198. "updated_time": "更新时间",
  199. "created_id": "创建者ID",
  200. "updated_id": "更新者ID",
  201. }
  202. # 复制数据并转换状态
  203. data = notice_list.copy()
  204. for item in data:
  205. # 处理状态
  206. item["status"] = "启用" if item.get("status") == "0" else "停用"
  207. # 处理公告类型
  208. item["notice_type"] = "通知" if item.get("notice_type") == "1" else "公告"
  209. item["creator"] = (
  210. item.get("creator", {}).get("name", "未知")
  211. if isinstance(item.get("creator"), dict)
  212. else "未知"
  213. )
  214. return ExcelUtil.export_list2excel(list_data=data, mapping_dict=mapping_dict)