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