service.py 4.2 KB

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