mine.py 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. from __future__ import annotations
  2. from sqlalchemy import select
  3. from sqlalchemy.orm import Session
  4. from app.db.models.mine import Mine, MineArea, Roadway, WorkFace
  5. def list_mines(db: Session, *, include_inactive: bool = False) -> list[Mine]:
  6. statement = select(Mine).order_by(Mine.is_default.desc(), Mine.name)
  7. if not include_inactive:
  8. statement = statement.where(Mine.status == "active")
  9. return list(db.scalars(statement).all())
  10. def get_mine(db: Session, mine_id: str) -> Mine | None:
  11. return db.get(Mine, mine_id)
  12. def get_mine_by_code(db: Session, code: str) -> Mine | None:
  13. return db.scalar(select(Mine).where(Mine.code == code))
  14. def get_default_mine(db: Session) -> Mine | None:
  15. return db.scalar(
  16. select(Mine)
  17. .where(Mine.status == "active")
  18. .order_by(Mine.is_default.desc(), Mine.created_at)
  19. )
  20. def list_areas(db: Session, mine_id: str) -> list[MineArea]:
  21. return list(
  22. db.scalars(
  23. select(MineArea)
  24. .where(MineArea.mine_id == mine_id)
  25. .order_by(MineArea.code)
  26. ).all()
  27. )
  28. def list_work_faces(db: Session, mine_id: str) -> list[WorkFace]:
  29. return list(
  30. db.scalars(
  31. select(WorkFace)
  32. .where(WorkFace.mine_id == mine_id)
  33. .order_by(WorkFace.code)
  34. ).all()
  35. )
  36. def list_roadways(db: Session, mine_id: str) -> list[Roadway]:
  37. return list(
  38. db.scalars(
  39. select(Roadway)
  40. .where(Roadway.mine_id == mine_id)
  41. .order_by(Roadway.code)
  42. ).all()
  43. )