dingdanquanliucheng/backend/app/repositories/file_repository.py

50 lines
1.8 KiB
Python
Raw Normal View History

"""
文件附件数据访问层
负责封装文件附件FileAttachment模型的数据库查询操作
提供附件的创建和按业务类型/业务ID查询等功能
FileServiceOrderService 调用
"""
from sqlalchemy import select
from sqlalchemy.orm import Session
from backend.app.models.business import FileAttachment
class FileRepository:
"""文件附件数据访问层,封装文件附件表的数据库操作。
FileServiceOrderService 调用
"""
def create_attachment(self, session: Session, payload: dict) -> FileAttachment:
"""创建一条文件附件记录。
:param session: 数据库会话
:param payload: 附件字段字典包含 biz_type业务类型biz_id业务ID
file_namefile_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_filesOrderService 查询订单附件时调用
"""
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())