service.py 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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 .crud import ApplicationCRUD
  5. from .plugin_manifest import list_plugin_infos
  6. from .schema import (
  7. ApplicationCreateSchema,
  8. ApplicationOutSchema,
  9. ApplicationQueryParam,
  10. ApplicationUpdateSchema,
  11. PluginInfoOut,
  12. )
  13. class ApplicationService:
  14. """
  15. 应用系统管理服务层
  16. """
  17. @classmethod
  18. async def list_plugins_service(cls) -> list[dict]:
  19. """
  20. 列出 ``app/plugin/module_*`` 插件元数据(含可选 ``plugin.toml``)。
  21. 返回:
  22. - list[dict]: ``PluginInfoOut`` 字典列表。
  23. """
  24. rows = list_plugin_infos()
  25. return [PluginInfoOut.model_validate(row).model_dump() for row in rows]
  26. @classmethod
  27. async def detail_service(cls, auth: AuthSchema, id: int) -> dict:
  28. """
  29. 获取应用详情
  30. 参数:
  31. - auth (AuthSchema): 认证信息模型
  32. - id (int): 应用ID
  33. 返回:
  34. - dict: 应用详情字典
  35. """
  36. obj = await ApplicationCRUD(auth).get_by_id_crud(id=id)
  37. if not obj:
  38. raise CustomException(msg="应用不存在")
  39. return ApplicationOutSchema.model_validate(obj).model_dump()
  40. @classmethod
  41. async def list_service(
  42. cls,
  43. auth: AuthSchema,
  44. search: ApplicationQueryParam | None = None,
  45. order_by: list[dict[str, str]] | None = None,
  46. ) -> list[dict]:
  47. """
  48. 获取应用列表
  49. 参数:
  50. - auth (AuthSchema): 认证信息模型
  51. - search (ApplicationQueryParam | None): 查询参数模型
  52. - order_by (list[dict[str, str]] | None): 排序参数,支持字符串或字典列表
  53. 返回:
  54. - list[dict]: 应用详情字典列表
  55. """
  56. search_dict = search.__dict__ if search else None
  57. obj_list = await ApplicationCRUD(auth).list_crud(
  58. search=search_dict, order_by=order_by
  59. )
  60. return [ApplicationOutSchema.model_validate(obj).model_dump() for obj in obj_list]
  61. @classmethod
  62. async def page_service(
  63. cls,
  64. auth: AuthSchema,
  65. page_no: int,
  66. page_size: int,
  67. search: ApplicationQueryParam | None = None,
  68. order_by: list[dict[str, str]] | None = None,
  69. ) -> dict:
  70. """
  71. 分页查询应用(数据库 OFFSET/LIMIT)。
  72. 参数:
  73. - auth (AuthSchema): 认证信息。
  74. - page_no (int): 页码。
  75. - page_size (int): 每页条数。
  76. - search (ApplicationQueryParam | None): 查询条件。
  77. - order_by (list[dict[str, str]] | None): 排序。
  78. 返回:
  79. - dict: 分页结果。
  80. """
  81. return await ApplicationCRUD(auth).page(
  82. offset=(page_no - 1) * page_size,
  83. limit=page_size,
  84. order_by=order_by or [{"id": "asc"}],
  85. search=search.__dict__ if search else {},
  86. out_schema=ApplicationOutSchema,
  87. )
  88. @classmethod
  89. async def create_service(cls, auth: AuthSchema, data: ApplicationCreateSchema) -> dict:
  90. """
  91. 创建应用
  92. 参数:
  93. - auth (AuthSchema): 认证信息模型
  94. - data (ApplicationCreateSchema): 应用创建模型
  95. 返回:
  96. - Dict: 应用详情字典
  97. """
  98. # 检查名称是否重复
  99. obj = await ApplicationCRUD(auth).get(name=data.name)
  100. if obj:
  101. raise CustomException(msg="创建失败,应用名称已存在")
  102. obj = await ApplicationCRUD(auth).create_crud(data=data)
  103. if not obj:
  104. raise CustomException(msg="创建应用失败")
  105. obj = await ApplicationCRUD(auth).get_by_id_crud(id=obj.id)
  106. if not obj:
  107. raise CustomException(msg="创建应用失败")
  108. return ApplicationOutSchema.model_validate(obj).model_dump()
  109. @classmethod
  110. async def update_service(cls, auth: AuthSchema, id: int, data: ApplicationUpdateSchema) -> dict:
  111. """
  112. 更新应用
  113. 参数:
  114. - auth (AuthSchema): 认证信息模型
  115. - id (int): 应用ID
  116. - data (ApplicationUpdateSchema): 应用更新模型
  117. 返回:
  118. - Dict: 应用详情字典
  119. """
  120. obj = await ApplicationCRUD(auth).get_by_id_crud(id=id)
  121. if not obj:
  122. raise CustomException(msg="更新失败,该应用不存在")
  123. # 检查名称重复
  124. exist_obj = await ApplicationCRUD(auth).get(name=data.name)
  125. if exist_obj and exist_obj.id != id:
  126. raise CustomException(msg="更新失败,应用名称重复")
  127. obj = await ApplicationCRUD(auth).update_crud(id=id, data=data)
  128. if not obj:
  129. raise CustomException(msg="更新失败,该应用不存在")
  130. obj = await ApplicationCRUD(auth).get_by_id_crud(id=obj.id)
  131. if not obj:
  132. raise CustomException(msg="更新应用失败")
  133. return ApplicationOutSchema.model_validate(obj).model_dump()
  134. @classmethod
  135. async def delete_service(cls, auth: AuthSchema, ids: list[int]) -> None:
  136. """
  137. 删除应用
  138. 参数:
  139. - auth (AuthSchema): 认证信息模型
  140. - ids (list[int]): 应用ID列表
  141. 返回:
  142. - None
  143. """
  144. if len(ids) < 1:
  145. raise CustomException(msg="删除失败,删除对象不能为空")
  146. for id in ids:
  147. obj = await ApplicationCRUD(auth).get_by_id_crud(id=id)
  148. if not obj:
  149. raise CustomException(msg=f"删除失败,应用 {id} 不存在")
  150. await ApplicationCRUD(auth).delete_crud(ids=ids)
  151. @classmethod
  152. async def set_available_service(cls, auth: AuthSchema, data: BatchSetAvailable) -> None:
  153. """
  154. 批量设置应用状态
  155. 参数:
  156. - auth (AuthSchema): 认证信息模型
  157. - data (BatchSetAvailable): 批量设置应用状态模型
  158. 返回:
  159. - None
  160. """
  161. await ApplicationCRUD(auth).set_available_crud(ids=data.ids, status=data.status)