Files
FastapiAdmin/backend/app/api/v1/module_system/log/service.py
T
zhangtao c2ca6d19ac refactor: 优化代码注释和文档字符串格式
style: 统一代码风格和格式

docs: 完善函数和方法的文档字符串

refactor(base_model): 移除冗余的表名和表参数生成方法

refactor(constant): 更新返回码注释格式

refactor(router_class): 添加路由处理器的详细文档

refactor(database): 完善数据库连接函数的文档

refactor(security): 添加认证类和方法的详细文档

refactor(validator): 更新验证器函数的文档格式

refactor(serialize): 优化序列化工具类的文档

refactor(response): 完善响应类的文档字符串

refactor(dependencies): 添加依赖函数的详细文档

refactor(initialize): 完善初始化脚本的文档

refactor(plugin): 添加生命周期和中间件注册的文档

refactor(service): 完善服务层方法的文档

refactor(controller): 添加控制器方法的详细文档

refactor(crud): 完善CRUD操作的文档字符串

refactor(schema): 简化模型类并移除冗余字段

refactor(param): 更新查询参数类的注释格式

refactor(template): 优化代码生成模板的格式

refactor(console): 添加控制台输出功能的实现

refactor(util): 完善工具函数的文档字符串
2025-10-18 16:31:28 +08:00

128 lines
4.2 KiB
Python

# -*- coding: utf-8 -*-
from typing import Any, Dict, List, Optional
from app.core.exceptions import CustomException
from app.utils.excel_util import ExcelUtil
from ..auth.schema import AuthSchema
from .param import OperationLogQueryParam
from .crud import OperationLogCRUD
from .schema import (
OperationLogCreateSchema,
OperationLogOutSchema
)
class OperationLogService:
"""
日志模块服务层
"""
@classmethod
async def get_log_detail_service(cls, auth: AuthSchema, id: int) -> Dict:
"""
获取日志详情
参数:
- auth (AuthSchema): 认证信息模型
- id (int): 日志 ID
返回:
- Dict: 日志详情字典
"""
log = await OperationLogCRUD(auth).get_by_id_crud(id=id)
log_dict = OperationLogOutSchema.model_validate(log).model_dump()
return log_dict
@classmethod
async def get_log_list_service(cls, auth: AuthSchema, search: Optional[OperationLogQueryParam], order_by: Optional[List[Dict]] = None) -> List[Dict]:
"""
获取日志列表
参数:
- auth (AuthSchema): 认证信息模型
- search (Optional[OperationLogQueryParam]): 日志查询参数模型
- order_by (Optional[List[Dict]]): 排序字段列表
返回:
- List[Dict]: 日志详情字典列表
"""
log_list = await OperationLogCRUD(auth).get_list_crud(search=search.__dict__, order_by=order_by)
log_dict_list = [OperationLogOutSchema.model_validate(log).model_dump() for log in log_list]
return log_dict_list
@classmethod
async def create_log_service(cls, auth: AuthSchema, data: OperationLogCreateSchema) -> Dict:
"""
创建日志
参数:
- auth (AuthSchema): 认证信息模型
- data (OperationLogCreateSchema): 日志创建模型
返回:
- Dict: 日志详情字典
"""
new_log = await OperationLogCRUD(auth).create(data=data)
new_log_dict = OperationLogOutSchema.model_validate(new_log).model_dump()
return new_log_dict
@classmethod
async def delete_log_service(cls, auth: AuthSchema, ids: list[int]) -> None:
"""
删除日志
参数:
- auth (AuthSchema): 认证信息模型
- ids (list[int]): 日志 ID 列表
返回:
- None
"""
if len(ids) < 1:
raise CustomException(msg='删除失败,删除对象不能为空')
await OperationLogCRUD(auth).delete(ids=ids)
@classmethod
async def export_log_list_service(cls, operation_log_list: List[Dict[str, Any]]) -> bytes:
"""
导出日志信息
参数:
- operation_log_list (List[Dict[str, Any]]): 操作日志信息列表
返回:
- bytes: 操作日志信息excel的二进制数据
"""
# 操作日志字段映射
mapping_dict = {
'id': '编号',
'type': '日志类型',
'request_path': '请求URL',
'request_method': '请求方式',
'request_payload': '请求参数',
'request_ip': '操作地址',
'login_location': '登录位置',
'request_os': '操作系统',
'request_browser': '浏览器',
'response_json': '返回参数',
'response_code': '相应状态',
'process_time': '处理时间',
'description': '备注',
'created_at': '创建时间',
'updated_at': '更新时间',
'creator_id': '创建者ID',
'creator': '创建者',
}
# 处理数据
data = operation_log_list.copy()
for item in data:
# 处理状态
item['response_code'] = '成功' if item.get('response_code') == 200 else '失败'
# 处理日志类型
item['type'] = '操作日志' if item.get('type') == 1 else '登录日志'
item['creator'] = item.get('creator', {}).get('name', '未知') if isinstance(item.get('creator'), dict) else '未知'
return ExcelUtil.export_list2excel(list_data=data, mapping_dict=mapping_dict)