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

74 行
2.2 KiB

  1. from sqlalchemy.orm import Session
  2. from schemas.maintenancejob import CreateMaintenanceJob
  3. from db.models.maintenancejob import MaintenanceJob
  4. from schemas.carpart import CreateCarPart
  5. from db.models.carpart import CarPart
  6. def create_new_maintenancejob(
  7. maintenancejob: CreateMaintenanceJob, maintenanceworker, db: Session
  8. ):
  9. maintenancejob_object = MaintenanceJob(
  10. MaintenanceWorker=maintenanceworker,
  11. Description=maintenancejob.Description,
  12. Date=maintenancejob.Date,
  13. )
  14. print("OBJECT CREATED")
  15. db.add(maintenancejob_object)
  16. db.commit()
  17. db.refresh(maintenancejob_object)
  18. return maintenancejob_object
  19. def create_car_part(car_part: CreateCarPart, db: Session):
  20. car_part_object = CarPart(
  21. ParentId=car_part.ParentId,
  22. Name=car_part.Name,
  23. Number=car_part.Number,
  24. Condition=car_part.Condition,
  25. ImageURL=car_part.ImageURL,
  26. Cost=car_part.Cost,
  27. )
  28. print("OBJECT CREATED")
  29. db.add(car_part_object)
  30. db.commit()
  31. db.refresh(car_part_object)
  32. return car_part_object
  33. def calculate_total_cost(car_parts: CarPart):
  34. total_cost = 0
  35. for part in car_parts:
  36. total_cost += part["Cost"]
  37. return total_cost
  38. def get_all_maintenance_jobs(db: Session):
  39. maintenancejobs = db.query(MaintenanceJob).all()
  40. result = []
  41. for job in maintenancejobs:
  42. job_dict = job.__dict__
  43. job_dict["CarParts"] = [part.__dict__ for part in job.CarParts]
  44. job_dict["TotalCost"] = calculate_total_cost(job.CarParts)
  45. result.append(job_dict)
  46. return maintenancejobs
  47. def get_maintenance_job(maintenancejob_id: int, db: Session):
  48. maintenancejob = (
  49. db.query(MaintenanceJob).filter(MaintenanceJob.Id == maintenancejob_id).first()
  50. )
  51. result = maintenancejob.__dict__
  52. result["CarParts"] = [part.__dict__ for part in maintenancejob.CarParts]
  53. return result
  54. def change_maintenance_status(maintenancejob_id: int, status: str, db: Session):
  55. maintenancejob = (
  56. db.query(MaintenanceJob).filter(MaintenanceJob.Id == maintenancejob_id).first()
  57. )
  58. if maintenancejob is None:
  59. return None
  60. maintenancejob.Status = status
  61. db.commit()
  62. db.refresh(maintenancejob)
  63. return maintenancejob