weight.py 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. # -*- coding: utf-8 -*-
  2. from fastapi.encoders import jsonable_encoder
  3. from sqlalchemy.orm import Session
  4. from app.models.space.weight import VAVRoomWeight
  5. from app.schemas.sapce_weight import SpaceWeightCreate, SpaceWeightUpdate
  6. from app.utils.date import get_time_str
  7. def get_weights_by_space(db: Session, space_id: str) -> list[VAVRoomWeight]:
  8. return db.query(VAVRoomWeight).filter(VAVRoomWeight.space_id == space_id).all()
  9. def get_weights_by_vav(db: Session, vav_id: str) -> list[VAVRoomWeight]:
  10. return db.query(VAVRoomWeight).filter(VAVRoomWeight.vav_box_id == vav_id).all()
  11. def create_weight(db: Session, weight: SpaceWeightCreate) -> VAVRoomWeight:
  12. db_weight = VAVRoomWeight(
  13. default_weight=weight.default_weight,
  14. project_id=weight.project_id,
  15. space_id=weight.space_id,
  16. vav_box_id=weight.vav_box_id,
  17. )
  18. db.add(db_weight)
  19. db.commit()
  20. db.refresh(db_weight)
  21. return db_weight
  22. def update_weight(
  23. db: Session, db_weight: VAVRoomWeight, weight_in: SpaceWeightUpdate
  24. ) -> VAVRoomWeight:
  25. weight_data = jsonable_encoder(db_weight)
  26. update_data = weight_in.dict(exclude_unset=True)
  27. for field in weight_data:
  28. if field in update_data:
  29. setattr(db_weight, field, update_data[field])
  30. if field == "temporary_weight":
  31. db_weight.temporary_weight_update_time = get_time_str()
  32. db.add(db_weight)
  33. db.commit()
  34. db.refresh(db_weight)
  35. return db_weight