attachment.py 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. """附件CRUD"""
  2. from typing import Optional, List
  3. from sqlalchemy.orm import Session
  4. from sqlalchemy import and_
  5. from app.models.models import BizAttachment
  6. from app.crud.base import CRUDBase
  7. from app.core.utils import generate_id
  8. class AttachmentCRUD(CRUDBase[BizAttachment]):
  9. """附件CRUD操作"""
  10. def get_by_business(
  11. self,
  12. db: Session,
  13. business_type: str,
  14. business_id: str
  15. ) -> List[BizAttachment]:
  16. """根据业务类型和业务ID获取附件列表"""
  17. return db.query(self.model).filter(
  18. and_(
  19. self.model.business_type == business_type,
  20. self.model.business_id == business_id,
  21. self.model.is_del == 0
  22. )
  23. ).order_by(self.model.create_at.desc()).all()
  24. def create_attachment(
  25. self,
  26. db: Session,
  27. business_type: str,
  28. business_id: str,
  29. file_name: str,
  30. file_url: str,
  31. file_size: int = None,
  32. file_type: str = None,
  33. upload_by: str = None,
  34. upload_by_name: str = None
  35. ) -> BizAttachment:
  36. """创建附件记录"""
  37. data = {
  38. "id": generate_id(),
  39. "business_type": business_type,
  40. "business_id": business_id,
  41. "file_name": file_name,
  42. "file_url": file_url,
  43. "file_size": file_size,
  44. "file_type": file_type,
  45. "upload_by": upload_by,
  46. "upload_by_name": upload_by_name,
  47. "create_by": upload_by,
  48. "create_name": upload_by_name
  49. }
  50. return self.create(db, data)
  51. attachment_crud = AttachmentCRUD(BizAttachment)