service.py 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. from app.api.v1.module_system.auth.schema import AuthSchema
  2. from app.core.exceptions import CustomException
  3. from app.utils.excel_util import ExcelUtil
  4. from .crud import OperationLogCRUD
  5. from .schema import (
  6. OperationLogCreateSchema,
  7. OperationLogOutSchema,
  8. OperationLogQueryParam,
  9. )
  10. class OperationLogService:
  11. """
  12. 日志模块服务层
  13. """
  14. @classmethod
  15. async def get_log_detail_service(cls, auth: AuthSchema, id: int) -> dict:
  16. """
  17. 获取日志详情
  18. 参数:
  19. - auth (AuthSchema): 认证信息模型
  20. - id (int): 日志 ID
  21. 返回:
  22. - dict: 日志详情字典
  23. """
  24. log = await OperationLogCRUD(auth).get_by_id_crud(id=id)
  25. log_dict = OperationLogOutSchema.model_validate(log).model_dump()
  26. return log_dict
  27. @classmethod
  28. async def get_log_list_service(
  29. cls,
  30. auth: AuthSchema,
  31. search: OperationLogQueryParam | None = None,
  32. order_by: list | None = None,
  33. ) -> list[dict]:
  34. """
  35. 获取日志列表
  36. 参数:
  37. - auth (AuthSchema): 认证信息模型
  38. - search (OperationLogQueryParam | None): 日志查询参数模型
  39. - order_by (list | None): 排序字段列表
  40. 返回:
  41. - list[dict]: 日志详情字典列表
  42. """
  43. log_list = await OperationLogCRUD(auth).get_list_crud(
  44. search=search.__dict__, order_by=order_by
  45. )
  46. log_dict_list = [OperationLogOutSchema.model_validate(log).model_dump() for log in log_list]
  47. return log_dict_list
  48. @classmethod
  49. async def get_log_page_service(
  50. cls,
  51. auth: AuthSchema,
  52. page_no: int,
  53. page_size: int,
  54. search: OperationLogQueryParam | None = None,
  55. order_by: list | None = None,
  56. ) -> dict:
  57. """
  58. 分页查询操作日志(数据库 OFFSET/LIMIT)。
  59. 参数:
  60. - auth (AuthSchema): 认证信息模型
  61. - page_no (int): 页码(从 1 开始)
  62. - page_size (int): 每页条数
  63. - search (OperationLogQueryParam | None): 查询条件
  64. - order_by (list | None): 排序字段列表
  65. 返回:
  66. - dict: 分页结果(结构由 `CRUD.page` 返回约定)
  67. """
  68. offset = (page_no - 1) * page_size
  69. return await OperationLogCRUD(auth).page(
  70. offset=offset,
  71. limit=page_size,
  72. order_by=order_by or [{"id": "asc"}],
  73. search=search.__dict__ if search else {},
  74. out_schema=OperationLogOutSchema,
  75. )
  76. @classmethod
  77. async def create_log_service(cls, auth: AuthSchema, data: OperationLogCreateSchema) -> dict:
  78. """
  79. 创建日志
  80. 参数:
  81. - auth (AuthSchema): 认证信息模型
  82. - data (OperationLogCreateSchema): 日志创建模型
  83. 返回:
  84. - dict: 日志详情字典
  85. """
  86. new_log = await OperationLogCRUD(auth).create(data=data)
  87. new_log_dict = OperationLogOutSchema.model_validate(new_log).model_dump()
  88. return new_log_dict
  89. @classmethod
  90. async def delete_log_service(cls, auth: AuthSchema, ids: list[int]) -> None:
  91. """
  92. 删除日志
  93. 参数:
  94. - auth (AuthSchema): 认证信息模型
  95. - ids (list[int]): 日志 ID 列表
  96. 返回:
  97. - None
  98. """
  99. if len(ids) < 1:
  100. raise CustomException(msg="删除失败,删除对象不能为空")
  101. await OperationLogCRUD(auth).delete(ids=ids)
  102. @classmethod
  103. async def export_log_list_service(cls, operation_log_list: list[dict]) -> bytes:
  104. """
  105. 导出日志信息
  106. 参数:
  107. - operation_log_list (list[dict]): 操作日志信息列表
  108. 返回:
  109. - bytes: 操作日志信息excel的二进制数据
  110. """
  111. # 操作日志字段映射
  112. mapping_dict = {
  113. "id": "编号",
  114. "type": "日志类型",
  115. "request_path": "请求URL",
  116. "request_method": "请求方式",
  117. "request_payload": "请求参数",
  118. "request_ip": "操作地址",
  119. "login_location": "登录位置",
  120. "request_os": "操作系统",
  121. "request_browser": "浏览器",
  122. "response_json": "返回参数",
  123. "response_code": "相应状态",
  124. "process_time": "处理时间",
  125. "description": "备注",
  126. "created_time": "创建时间",
  127. "updated_time": "更新时间",
  128. "created_id": "创建者ID",
  129. "updated_id": "更新者ID",
  130. }
  131. # 处理数据
  132. data = operation_log_list.copy()
  133. for item in data:
  134. # 处理状态
  135. item["response_code"] = "成功" if item.get("response_code") == 200 else "失败"
  136. # 处理日志类型 - 修正与schema.py保持一致
  137. item["type"] = "登录日志" if item.get("type") == 1 else "操作日志"
  138. item["creator"] = (
  139. item.get("creator", {}).get("name", "未知")
  140. if isinstance(item.get("creator"), dict)
  141. else "未知"
  142. )
  143. return ExcelUtil.export_list2excel(list_data=data, mapping_dict=mapping_dict)