dingdanquanliucheng/backend/app/repositories/file_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

50 lines
1.8 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.

"""
文件附件数据访问层。
负责封装文件附件FileAttachment模型的数据库查询操作
提供附件的创建和按业务类型/业务ID查询等功能。
被 FileService、OrderService 调用。
"""
from sqlalchemy import select
from sqlalchemy.orm import Session
from backend.app.models.business import FileAttachment
class FileRepository:
"""文件附件数据访问层,封装文件附件表的数据库操作。
被 FileService、OrderService 调用。
"""
def create_attachment(self, session: Session, payload: dict) -> FileAttachment:
"""创建一条文件附件记录。
:param session: 数据库会话
:param payload: 附件字段字典,包含 biz_type业务类型、biz_id业务ID
file_name、file_path 等键
:return: 新创建的附件对象(含自增 ID
被 FileService.upload_file 调用,上传文件后保存附件元信息。
"""
attachment = FileAttachment(**payload)
session.add(attachment)
session.flush()
return attachment
def list_attachments(self, session: Session, biz_type: str, biz_id: int) -> list[FileAttachment]:
"""根据业务类型和业务 ID 查询关联的附件列表。
:param session: 数据库会话
:param biz_type: 业务类型标识(如 'order''product' 等)
:param biz_id: 业务主键 ID
:return: 该业务关联的附件列表,按 id 降序排列
被 FileService.list_files、OrderService 查询订单附件时调用。
"""
stmt = (
select(FileAttachment)
.where(FileAttachment.biz_type == biz_type, FileAttachment.biz_id == biz_id)
.order_by(FileAttachment.id.desc())
)
return list(session.execute(stmt).scalars())