dingdanquanliucheng/backend/app/repositories/audit_repository.py
taiyi 9b32afdbb4 为后端全部 86 个 Python 文件添加中文注释
覆盖所有模块:
- api 层:17 个路由文件,每个接口标注用途、参数、返回值、权限
- services 层:18 个服务文件,每个方法标注作用、参数、返回值、调用方
- repositories 层:13 个仓储文件,每个方法标注查询逻辑和被调用方
- schemas 层:11 个请求/响应体文件,每个字段标注业务含义
- core 层:config、security、exceptions、responses、error_codes
- models 层:19 个 ORM 模型类,每个表标注业务含义和关联关系
- scripts:bootstrap_data、smoke_check
- migrations:env.py 和版本迁移文件

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-05-30 07:23:33 +08:00

69 lines
2.4 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

"""审计日志模块 Repository 层
负责审计日志的创建和查询操作,记录系统中各业务模块的操作行为。
被 AuditService审计服务调用。
"""
from datetime import datetime
from sqlalchemy import select
from sqlalchemy.orm import Session
from backend.app.models.system import AuditLog
class AuditRepository:
"""审计日志数据仓储
封装审计日志的写入和多条件查询操作。
被 AuditService审计服务调用。
"""
def create_log(self, session: Session, payload: dict) -> AuditLog:
"""创建审计日志记录
参数:
session: 数据库会话
payload: 日志数据字典,包含 biz_type / biz_id / operator_name /
operate_type / operate_time / operate_content 等字段
返回:
新创建的审计日志对象
"""
log = AuditLog(**payload)
session.add(log)
session.flush()
return log
def list_logs(self, session: Session, filters: dict) -> list[AuditLog]:
"""查询审计日志列表
支持按业务类型、业务ID、操作人、操作类型、时间范围等条件过滤
按 id 倒序返回。
参数:
session: 数据库会话
filters: 过滤条件字典,支持 biz_type / biz_id / operator_name /
operate_type / start_time / end_time格式 "%Y-%m-%d %H:%M:%S"
返回:
符合条件的审计日志列表
"""
stmt = select(AuditLog)
if filters.get("biz_type"):
stmt = stmt.where(AuditLog.biz_type == filters["biz_type"])
if filters.get("biz_id") is not None:
stmt = stmt.where(AuditLog.biz_id == filters["biz_id"])
if filters.get("operator_name"):
stmt = stmt.where(AuditLog.operator_name.contains(filters["operator_name"]))
if filters.get("operate_type"):
stmt = stmt.where(AuditLog.operate_type == filters["operate_type"])
if filters.get("start_time"):
stmt = stmt.where(AuditLog.operate_time >= datetime.strptime(filters["start_time"], "%Y-%m-%d %H:%M:%S"))
if filters.get("end_time"):
stmt = stmt.where(AuditLog.operate_time <= datetime.strptime(filters["end_time"], "%Y-%m-%d %H:%M:%S"))
stmt = stmt.order_by(AuditLog.id.desc())
return list(session.execute(stmt).scalars())