|
|
|
@ -2,9 +2,10 @@
|
|
|
|
|
|
|
|
|
|
|
|
import asyncio
|
|
|
|
import asyncio
|
|
|
|
from uuid import UUID
|
|
|
|
from uuid import UUID
|
|
|
|
from sqlalchemy import Select, Delete, func, insert, select, update as sqlUpdate
|
|
|
|
from sqlalchemy import Select, Delete, func, select, update as sqlUpdate
|
|
|
|
from sqlalchemy.orm import joinedload
|
|
|
|
from sqlalchemy.orm import joinedload
|
|
|
|
from typing import List, Optional
|
|
|
|
from typing import List, Optional
|
|
|
|
|
|
|
|
from sqlalchemy.dialects.postgresql import insert
|
|
|
|
|
|
|
|
|
|
|
|
from src.overhaul_activity.utils import get_material_cost, get_service_cost
|
|
|
|
from src.overhaul_activity.utils import get_material_cost, get_service_cost
|
|
|
|
from src.overhaul_scope.model import OverhaulScope
|
|
|
|
from src.overhaul_scope.model import OverhaulScope
|
|
|
|
@ -88,41 +89,44 @@ async def create(*, db_session: DbSession, overhaul_activty_in: OverhaulActivity
|
|
|
|
return []
|
|
|
|
return []
|
|
|
|
|
|
|
|
|
|
|
|
# Get session and count in parallel
|
|
|
|
# Get session and count in parallel
|
|
|
|
session, equipment_count = await asyncio.gather(
|
|
|
|
session = await get_session(db_session=db_session, overhaul_session_id=overhaul_session_id)
|
|
|
|
get_session(db_session=db_session, overhaul_session_id=overhaul_session_id),
|
|
|
|
equipment_count = await db_session.scalar(
|
|
|
|
db_session.scalar(
|
|
|
|
select(func.count())
|
|
|
|
select(func.count())
|
|
|
|
.select_from(OverhaulActivity)
|
|
|
|
.select_from(OverhaulActivity)
|
|
|
|
.where(OverhaulActivity.overhaul_scope_id == overhaul_session_id)
|
|
|
|
.where(OverhaulActivity.overhaul_scope_id == overhaul_session_id)
|
|
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
# Calculate costs for all records
|
|
|
|
# Calculate costs for all records
|
|
|
|
total_equipment = equipment_count + len(assetnums)
|
|
|
|
total_equipment = equipment_count + len(assetnums)
|
|
|
|
material_cost = get_material_cost(scope=session.type, total_equipment=total_equipment)
|
|
|
|
material_cost = get_material_cost(
|
|
|
|
service_cost = get_service_cost(scope=session.type, total_equipment=total_equipment)
|
|
|
|
scope=session.type, total_equipment=total_equipment)
|
|
|
|
|
|
|
|
service_cost = get_service_cost(
|
|
|
|
# Bulk create new activities
|
|
|
|
scope=session.type, total_equipment=total_equipment)
|
|
|
|
overhaul_activities = [
|
|
|
|
|
|
|
|
OverhaulActivity(
|
|
|
|
# Create the insert statement
|
|
|
|
assetnum=assetnum,
|
|
|
|
stmt = insert(OverhaulActivity).values([
|
|
|
|
overhaul_scope_id=overhaul_session_id,
|
|
|
|
{
|
|
|
|
material_cost=material_cost,
|
|
|
|
'assetnum': assetnum,
|
|
|
|
service_cost=service_cost
|
|
|
|
'overhaul_scope_id': overhaul_session_id,
|
|
|
|
)
|
|
|
|
'material_cost': material_cost,
|
|
|
|
|
|
|
|
'service_cost': service_cost
|
|
|
|
|
|
|
|
}
|
|
|
|
for assetnum in assetnums
|
|
|
|
for assetnum in assetnums
|
|
|
|
]
|
|
|
|
])
|
|
|
|
|
|
|
|
|
|
|
|
# Add new records and update all existing records in parallel
|
|
|
|
# Add the ON CONFLICT DO NOTHING clause
|
|
|
|
await asyncio.gather(
|
|
|
|
stmt = stmt.on_conflict_do_nothing(
|
|
|
|
db_session.add_all(overhaul_activities),
|
|
|
|
index_elements=["assetnum", "overhaul_scope_id"]
|
|
|
|
db_session.execute(
|
|
|
|
|
|
|
|
update(OverhaulActivity)
|
|
|
|
|
|
|
|
.where(OverhaulActivity.overhaul_scope_id == overhaul_session_id)
|
|
|
|
|
|
|
|
.values(material_cost=material_cost, service_cost=service_cost)
|
|
|
|
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# Execute the statement
|
|
|
|
|
|
|
|
db_session.execute(stmt)
|
|
|
|
|
|
|
|
await db_session.execute(
|
|
|
|
|
|
|
|
sqlUpdate(OverhaulActivity)
|
|
|
|
|
|
|
|
.where(OverhaulActivity.overhaul_scope_id == overhaul_session_id)
|
|
|
|
|
|
|
|
.values(material_cost=material_cost, service_cost=service_cost)
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
await db_session.commit()
|
|
|
|
await db_session.commit()
|
|
|
|
return assetnums
|
|
|
|
return assetnums
|
|
|
|
|
|
|
|
|
|
|
|
|