| 123456789101112131415161718192021222324252627282930313233343536373839404142 |
- from sqlalchemy.orm import Session
- from sqlalchemy import asc, desc
- from app.models.slot import Slot
- from app.schemas.slot import SlotCreate
- from typing import List
- class SlotService:
- @staticmethod
- def report(db: Session, obj_in: SlotCreate) -> Slot:
- # 可以根据 submit_city + travel_country + visa_type 查重,决定是更新还是新增
- obj = db.query(Slot).filter(
- Slot.submit_city == obj_in.submit_city,
- Slot.travel_country == obj_in.travel_country,
- Slot.visa_type == obj_in.visa_type
- ).first()
- if obj:
- # 更新现有记录
- for k, v in obj_in.dict().items():
- setattr(obj, k, v)
- else:
- obj = VisaSlot(**obj_in.dict())
- db.add(obj)
- db.commit()
- db.refresh(obj)
- return obj
- @staticmethod
- def search(db: Session, submit_city: str, travel_country: str, visa_type: str, date_type: str):
- query = db.query(Slot).filter(
- Slot.submit_city == submit_city,
- Slot.travel_country == travel_country,
- Slot.visa_type == visa_type
- )
- if date_type == "latest":
- obj = query.order_by(desc(Slot.update_at)).first()
- return obj
- elif date_type == "earliest":
- obj = query.order_by(asc(Slot.lasted_slot_date)).first()
- return obj
- else:
- return query.all()
|