|
|
|
|
@ -1,10 +1,11 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
from sqlalchemy import Select, Delete
|
|
|
|
|
from sqlalchemy import Select, Delete, func
|
|
|
|
|
|
|
|
|
|
from src.database.service import search_filter_sort_paginate
|
|
|
|
|
from src.overhaul_activity.model import OverhaulActivity
|
|
|
|
|
from src.scope_equipment.service import get_by_scope_name
|
|
|
|
|
from src.utils import time_now
|
|
|
|
|
from .model import OverhaulScope
|
|
|
|
|
from .schema import ScopeCreate, ScopeUpdate
|
|
|
|
|
from typing import Optional
|
|
|
|
|
@ -86,8 +87,84 @@ async def delete(*, db_session: DbSession, scope_id: str):
|
|
|
|
|
await db_session.commit()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# async def get_by_scope_name(*, db_session: DbSession, scope_name: str) -> Optional[OverhaulScope]:
|
|
|
|
|
# """Returns a document based on the given document id."""
|
|
|
|
|
# query = Select(OverhaulScope).filter(OverhaulScope.tyoe == scope_name)
|
|
|
|
|
# result = await db_session.execute(query)
|
|
|
|
|
# return result.scalars().one_or_none()
|
|
|
|
|
async def get_overview_overhaul(*, db_session: DbSession):
|
|
|
|
|
|
|
|
|
|
current_date = time_now().date()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# For ongoing overhaul with count
|
|
|
|
|
ongoing_query = Select(
|
|
|
|
|
OverhaulScope,
|
|
|
|
|
func.count(OverhaulActivity.id).label('equipment_count')
|
|
|
|
|
).outerjoin(
|
|
|
|
|
OverhaulScope.activity_equipments
|
|
|
|
|
).where(
|
|
|
|
|
OverhaulScope.start_date <= current_date,
|
|
|
|
|
OverhaulScope.end_date >= current_date,
|
|
|
|
|
OverhaulScope.status != 'Completed'
|
|
|
|
|
).group_by(
|
|
|
|
|
OverhaulScope.id
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
ongoing_result = await db_session.execute(ongoing_query)
|
|
|
|
|
# Use first() instead of scalar_one_or_none()
|
|
|
|
|
ongoing_result = ongoing_result.first()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if ongoing_result:
|
|
|
|
|
ongoing_overhaul, equipment_count = ongoing_result # Unpack the result tuple
|
|
|
|
|
return {
|
|
|
|
|
"status": "Ongoing",
|
|
|
|
|
"overhaul": {
|
|
|
|
|
"type": ongoing_overhaul.type,
|
|
|
|
|
"start_date": ongoing_overhaul.start_date,
|
|
|
|
|
"end_date": ongoing_overhaul.end_date,
|
|
|
|
|
"duration_oh": ongoing_overhaul.duration_oh,
|
|
|
|
|
"crew_number": ongoing_overhaul.crew_number,
|
|
|
|
|
"remaining_days": (ongoing_overhaul.end_date - current_date).days,
|
|
|
|
|
"equipment_count": equipment_count
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
# For upcoming overhaul with count
|
|
|
|
|
upcoming_query = Select(
|
|
|
|
|
OverhaulScope,
|
|
|
|
|
func.count(OverhaulActivity.id).label('equipment_count')
|
|
|
|
|
).outerjoin(
|
|
|
|
|
OverhaulScope.activity_equipments
|
|
|
|
|
).where(
|
|
|
|
|
OverhaulScope.start_date > current_date,
|
|
|
|
|
OverhaulScope.status == 'Upcoming'
|
|
|
|
|
).group_by(
|
|
|
|
|
OverhaulScope.id
|
|
|
|
|
).order_by(
|
|
|
|
|
OverhaulScope.start_date
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
upcoming_result = await db_session.execute(upcoming_query)
|
|
|
|
|
upcoming_result = upcoming_result.first()
|
|
|
|
|
|
|
|
|
|
if upcoming_result:
|
|
|
|
|
upcoming_overhaul, equipment_count = upcoming_result # Unpack the result tuple
|
|
|
|
|
days_until = (upcoming_overhaul.start_date - current_date).days
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
"status": "Upcoming",
|
|
|
|
|
"overhaul": {
|
|
|
|
|
"type": upcoming_overhaul.type,
|
|
|
|
|
"start_date": upcoming_overhaul.start_date,
|
|
|
|
|
"end_date": upcoming_overhaul.end_date,
|
|
|
|
|
"duration_oh": upcoming_overhaul.duration_oh,
|
|
|
|
|
"crew_number": upcoming_overhaul.crew_number,
|
|
|
|
|
"remaining_days": days_until,
|
|
|
|
|
"equipment_count": equipment_count
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
"status": "no_overhaul",
|
|
|
|
|
"overhaul": None
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|