Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

1 ano atrás
1 ano atrás
1 ano atrás
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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. print("Creating new vehicle" + str(vehicle))
  8. vehicle_object = Vehicle(
  9. **vehicle.model_dump(),
  10. Fuel=0,
  11. Status="Inactive",
  12. CurrentLocation=["0.000", "0.000"]
  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. print("Updating vehicle geolocation" + str(location))
  20. vehicle_db = db.query(Vehicle).filter(Vehicle.Id == vehicle_id)
  21. vehicle_object = vehicle_db.first()
  22. if not vehicle_object:
  23. return "vehiclenotfound"
  24. print("Location: " + str(location.CurrentLocation))
  25. vehicle_object.CurrentLocation = location.CurrentLocation
  26. db.add(vehicle_object)
  27. db.commit()
  28. db.refresh(vehicle_object)
  29. return vehicle_object
  30. def assign_vehicle_driver(vehicle_id: int, driver_id: int, db: Session):
  31. print("Assigning vehicle " + str(vehicle_id) + " to driver " + str(driver_id))
  32. if driver_id != None:
  33. driver = (
  34. db.query(User)
  35. .filter((User.Id == driver_id) & (User.Role == "Driver"))
  36. .first()
  37. )
  38. vehicle = db.query(Vehicle).filter(Vehicle.Id == vehicle_id).first()
  39. if not vehicle:
  40. return "novehicle"
  41. if not driver:
  42. return "nodriver"
  43. driver.AssignedVehicle = vehicle_id
  44. else:
  45. vehicle = db.query(Vehicle).filter(Vehicle.Id == vehicle_id).first()
  46. if not vehicle:
  47. return "novehicle"
  48. if vehicle.Status == "Active":
  49. return "alreadyassigned"
  50. driver = get_car_driver(vehicle_id, db)
  51. if driver:
  52. driver.AssignedVehicle = None
  53. else:
  54. return "nodriver"
  55. db.add(driver)
  56. db.commit()
  57. return vehicle
  58. def list_vehicles(db: Session):
  59. print("Listing vehicles")
  60. vehicles = db.query(Vehicle).all()
  61. # assign AssignedDriver to all vehicles based on their drivers
  62. for vehicle in vehicles:
  63. driver = get_car_driver(vehicle.Id, db)
  64. if driver:
  65. vehicle.AssignedDriver = driver.__dict__
  66. else:
  67. vehicle.AssignedDriver = None
  68. return vehicles
  69. def get_vehicle_by_id(vehicle_id: int, db: Session):
  70. print("Retrieving vehicle by id: " + str(vehicle_id))
  71. vehicle = db.query(Vehicle).filter(Vehicle.Id == vehicle_id).first()
  72. if not vehicle:
  73. return None
  74. driver = get_car_driver(vehicle.Id, db)
  75. if driver:
  76. vehicle.AssignedDriver = driver.__dict__
  77. else:
  78. vehicle.AssignedDriver = None
  79. return vehicle
  80. def replace_vehicle_data(id: int, vehicle: UpdateVehicle, db: Session):
  81. print("Replacing vehicle data for vehicle " + str(id) + " with " + str(vehicle))
  82. vehicle_db = db.query(Vehicle).filter(Vehicle.Id == id)
  83. vehicle_object = vehicle_db.first()
  84. if not vehicle_object:
  85. return "vehiclenotfound"
  86. # vehicle_object.CurrentLocation = vehicle.CurrentLocation
  87. vehicle_object.Fuel = vehicle.Fuel
  88. vehicle_object.LicensePlate = vehicle.LicensePlate
  89. # vehicle_object.DriverHistory = vehicle.DriverHistory
  90. vehicle_object.Type = vehicle.Type
  91. vehicle_object.Mileage = vehicle.Mileage
  92. vehicle_object.Model = vehicle.Model
  93. vehicle_object.Status = vehicle.Status
  94. # res = assign_vehicle_driver(id, vehicle.AssignedDriver, db)
  95. # if res == "nodriver":
  96. # return "driverNotFound"
  97. vehicle_object.Capacity = vehicle.Capacity
  98. vehicle_object.Year = vehicle.Year
  99. db.add(vehicle_object)
  100. db.commit()
  101. # vehicle_object.AssignedDriver = vehicle.AssignedDriver
  102. return vehicle_object
  103. def delete_vehicle_data(id: int, db: Session):
  104. print("Deleting vehicle data for vehicle " + str(id))
  105. vehicle_db = db.query(Vehicle).filter(Vehicle.Id == id)
  106. vehicle_object = vehicle_db.first()
  107. if not vehicle_object:
  108. return "vehiclenotfound"
  109. db.delete(vehicle_object)
  110. db.commit()
  111. return vehicle_object