mirror of
https://github.com/fastapiadmin/FastapiAdmin.git
synced 2026-09-23 13:13:09 +00:00
- 将 ApplicationQueryParams 改为 ApplicationQueryParam - 同步更新相关导入和函数参数类型注解 - 修改 PaginationQueryParams 为 PaginationQueryParam refactor(demo): 重命名查询参数类以统一命名风格 - 将 DemoQueryParams 改为 DemoQueryParam - 同步更新相关导入和函数参数类型注解 - 修改 PaginationQueryParams 为 PaginationQueryParam refactor(gencode): 优化代码生成模块的模型和服务层结构 - 统一模型名称后缀为 Schema,调整相关引用 - 规范 Pydantic schema 的命名和定义 - 删除无用的 Python DAO 模板文件 - 调整导入路径,统一使用 app 目录下的模块路径 - 改进服务层方法签名,添加返回类型注解 - 使用自定义异常 CustomException 替代旧异常 - 统一成功响应格式为 SuccessResponse - 优化代码生成服务中的数据库操作 DAO 调用参数传递 - 优化代码生成业务表和字段模型的字段定义,添加注释和默认值 - 优化生成代码路径处理逻辑和异常信息提示 - 整合分页查询参数定义,统一分页模型 - 修正多个服务方法的参数类型和返回类型 - 删除无用的导入和多余注释,提升代码整洁度
95 lines
3.4 KiB
Python
95 lines
3.4 KiB
Python
# -*- coding: utf-8 -*-
|
|
|
|
from typing import Any, Dict, List
|
|
|
|
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:
|
|
"""获取日志详情"""
|
|
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: OperationLogQueryParam, order_by: List[Dict] = None) -> List[Dict]:
|
|
"""获取日志列表"""
|
|
if order_by:
|
|
order_by = eval(order_by)
|
|
else:
|
|
order_by = [{"created_at": "desc"}]
|
|
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:
|
|
"""创建日志"""
|
|
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:
|
|
"""删除日志"""
|
|
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:
|
|
"""
|
|
导出日志信息
|
|
|
|
Args:
|
|
operation_log_list: 操作日志信息列表
|
|
|
|
Returns:
|
|
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)
|