jm_job_info.py 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. import time
  2. from typing import List
  3. from app import models, schemas
  4. from sqlalchemy.orm import Session
  5. from configs.globals import g
  6. def create_jm_job_info(db: Session, jm_job_info: models.JmJobInfo):
  7. db.add(jm_job_info)
  8. db.commit()
  9. db.refresh(jm_job_info)
  10. return jm_job_info
  11. def get_jm_job_infos(db: Session):
  12. res: List[models.JmJobInfo] = db.query(models.JmJobInfo)\
  13. .filter(models.JmJobInfo.project_id == g.project_id)\
  14. .filter(models.JmJobInfo.delete_status != 0)\
  15. .order_by(models.JmJobInfo.create_time.desc()).all()
  16. return res
  17. def get_jm_job_info(db: Session, jm_job_id: int):
  18. item = db.query(models.JmJobInfo)\
  19. .filter(models.JmJobInfo.id == jm_job_id)\
  20. .filter(models.JmJobInfo.delete_status != 0).first()
  21. if not item:
  22. raise Exception('未找到该定时任务')
  23. return item
  24. def update_jm_job_info(db: Session, item: models.JmJobInfo):
  25. db.commit()
  26. db.flush()
  27. db.refresh(item)
  28. return item
  29. def delete_jm_job_info(db: Session, jm_job_id: int):
  30. jm_job_info = db.query(models.JmJobInfo)\
  31. .filter(models.JmJobInfo.id == jm_job_id).first()
  32. if not jm_job_info:
  33. raise Exception('未找到该定时任务')
  34. if jm_job_info.status == 1:
  35. raise Exception('该任务未停用,不能删除')
  36. jm_job_info.delete_status = 0
  37. db.commit()
  38. db.flush()
  39. db.refresh(jm_job_info)
  40. return jm_job_info
  41. def update_jm_job_status(db: Session, jm_job_id: int, status: int):
  42. jm_job_info = db.query(models.JmJobInfo)\
  43. .filter(models.JmJobInfo.id == jm_job_id)\
  44. .filter(models.JmJobInfo.delete_status != 0).first()
  45. if not jm_job_info:
  46. raise Exception('未找到该定时任务')
  47. jm_job_info.status = status
  48. db.commit()
  49. db.flush()
  50. db.refresh(jm_job_info)
  51. return jm_job_info