您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
 
 

105 行
3.3 KiB

  1. from sqlalchemy.orm import Session
  2. from schemas.vehicle import CreateVehicle, UpdateVehicle, VehicleLocation
  3. from db.models.vehicle import Vehicle
  4. from db.base import User
  5. from db.repository.user import get_car_driver
  6. def create_new_vehicle(vehicle: CreateVehicle, db: Session):
  7. vehicle_object = Vehicle(
  8. **vehicle.model_dump(),
  9. Fuel=0,
  10. Status="Inactive",
  11. CurrentLocation=[],
  12. MaintenanceNotes=[]
  13. )
  14. db.add(vehicle_object)
  15. db.commit()
  16. db.refresh(vehicle_object)
  17. return vehicle_object
  18. def update_vehicle_geoloc(vehicle_id: int, location: VehicleLocation, db: Session):
  19. vehicle_db = db.query(Vehicle).filter(Vehicle.Id == vehicle_id)
  20. vehicle_object = vehicle_db.first()
  21. if not vehicle_object:
  22. return "vehiclenotfound"
  23. print("Location: " + str(location.CurrentLocation))
  24. vehicle_object.CurrentLocation = location.CurrentLocation
  25. db.add(vehicle_object)
  26. db.commit()
  27. db.refresh(vehicle_object)
  28. return vehicle_object
  29. def assign_vehicle_driver(vehicle_id: int, driver_id: int, db: Session):
  30. driver = (
  31. db.query(User).filter((User.Id == driver_id) & (User.Role == "Driver")).first()
  32. )
  33. print(driver)
  34. vehicle = db.query(Vehicle).filter(Vehicle.Id == vehicle_id).first()
  35. if not vehicle:
  36. return "novehicle"
  37. if not driver:
  38. return "nodriver"
  39. driver.AssignedVehicle = vehicle_id
  40. db.add(driver)
  41. db.commit()
  42. return vehicle
  43. def list_vehicles(db: Session):
  44. vehicles = db.query(Vehicle).all()
  45. # assign AssignedDriver to all vehicles based on their drivers
  46. for vehicle in vehicles:
  47. driver = get_car_driver(vehicle.Id, db)
  48. if driver:
  49. vehicle.AssignedDriver = driver.Id
  50. else:
  51. vehicle.AssignedDriver = None
  52. return vehicles
  53. def get_vehicle_by_id(vehicle_id: int, db: Session):
  54. vehicle = db.query(Vehicle).filter(Vehicle.Id == vehicle_id).first()
  55. driver = get_car_driver(vehicle.Id, db)
  56. if driver:
  57. vehicle.AssignedDriver = driver.Id
  58. else:
  59. vehicle.AssignedDriver = None
  60. return vehicle
  61. def replace_vehicle_data(id: int, vehicle: UpdateVehicle, db: Session):
  62. vehicle_db = db.query(Vehicle).filter(Vehicle.Id == id)
  63. vehicle_object = vehicle_db.first()
  64. if not vehicle_object:
  65. return "vehiclenotfound"
  66. vehicle_object.CurrentLocation = vehicle.CurrentLocation
  67. vehicle_object.Fuel = vehicle.Fuel
  68. vehicle_object.LicensePlate = vehicle.LicensePlate
  69. vehicle_object.MaintenanceNotes = vehicle.MaintenanceNotes
  70. vehicle_object.Mileage = vehicle.Mileage
  71. vehicle_object.Model = vehicle.Model
  72. vehicle_object.Status = vehicle.Status
  73. res = assign_vehicle_driver(id, vehicle.AssignedDriver, db)
  74. if res == "nodriver":
  75. return "driverNotFound"
  76. vehicle_object.Capacity = vehicle.Capacity
  77. vehicle_object.Year = vehicle.Year
  78. print(vehicle_object)
  79. db.add(vehicle_object)
  80. db.commit()
  81. vehicle_object.AssignedDriver = vehicle.AssignedDriver
  82. return vehicle_object
  83. def delete_vehicle_data(id: int, db: Session):
  84. vehicle_db = db.query(Vehicle).filter(Vehicle.Id == id)
  85. vehicle_object = vehicle_db.first()
  86. if not vehicle_object:
  87. return "vehiclenotfound"
  88. db.delete(vehicle_object)
  89. db.commit()
  90. return vehicle_object