# -*- coding: utf-8 -*- from fastapi.encoders import jsonable_encoder from sqlalchemy.orm import Session from app.models.space.weight import VAVRoomWeight from app.schemas.sapce_weight import SpaceWeightCreate, SpaceWeightUpdate from app.utils.date import get_time_str def get_weights_by_space(db: Session, space_id: str) -> list[VAVRoomWeight]: return db.query(VAVRoomWeight).filter(VAVRoomWeight.space_id == space_id).all() def get_weights_by_vav(db: Session, vav_id: str) -> list[VAVRoomWeight]: return db.query(VAVRoomWeight).filter(VAVRoomWeight.vav_box_id == vav_id).all() def create_weight(db: Session, weight: SpaceWeightCreate) -> VAVRoomWeight: db_weight = VAVRoomWeight( default_weight=weight.default_weight, project_id=weight.project_id, space_id=weight.space_id, vav_box_id=weight.vav_box_id, ) db.add(db_weight) db.commit() db.refresh(db_weight) return db_weight def update_weight( db: Session, db_weight: VAVRoomWeight, weight_in: SpaceWeightUpdate ) -> VAVRoomWeight: weight_data = jsonable_encoder(db_weight) update_data = weight_in.dict(exclude_unset=True) for field in weight_data: if field in update_data: setattr(db_weight, field, update_data[field]) if field == "temporary_weight": db_weight.temporary_weight_update_time = get_time_str() db.add(db_weight) db.commit() db.refresh(db_weight) return db_weight