slot_service.py 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. from sqlalchemy.orm import Session
  2. from sqlalchemy import asc, desc
  3. from app.models.slot import Slot
  4. from app.schemas.slot import SlotCreate
  5. from typing import List
  6. class SlotService:
  7. @staticmethod
  8. def report(db: Session, obj_in: SlotCreate) -> Slot:
  9. # 可以根据 submit_city + travel_country + visa_type 查重,决定是更新还是新增
  10. obj = db.query(Slot).filter(
  11. Slot.submit_city == obj_in.submit_city,
  12. Slot.travel_country == obj_in.travel_country,
  13. Slot.visa_type == obj_in.visa_type
  14. ).first()
  15. if obj:
  16. # 更新现有记录
  17. for k, v in obj_in.dict().items():
  18. setattr(obj, k, v)
  19. else:
  20. obj = VisaSlot(**obj_in.dict())
  21. db.add(obj)
  22. db.commit()
  23. db.refresh(obj)
  24. return obj
  25. @staticmethod
  26. def search(db: Session, submit_city: str, travel_country: str, visa_type: str, date_type: str):
  27. query = db.query(Slot).filter(
  28. Slot.submit_city == submit_city,
  29. Slot.travel_country == travel_country,
  30. Slot.visa_type == visa_type
  31. )
  32. if date_type == "latest":
  33. obj = query.order_by(desc(Slot.update_at)).first()
  34. return obj
  35. elif date_type == "earliest":
  36. obj = query.order_by(asc(Slot.lasted_slot_date)).first()
  37. return obj
  38. else:
  39. return query.all()