"""运输计划 · 现场链接读模型解析(安排运输 / 查看弹窗共用)""" from __future__ import annotations from sqlalchemy.orm import Session from app.models.pl_models import PlPlan, PlPlanProduct, PlTransport, PlTransportSiteLink from app.schemas.pl.transport import TransportSiteLinkRead from app.services.pl_dibang_bridge_service import plan_has_load_stop from app.services.pl_internal_customer import plan_requires_site_link from app.services.pl_io_order_service import build_io_order_site_url, get_active_io_order_for_plan from app.services.pl_site_link_read import build_transport_site_link_read_from_row from app.services.pl_transport_stop_feedback_service import build_transport_site_url def build_site_records_for_arranged_plan( db: Session, *, tenant_no: str, plan: PlPlan, transport: PlTransport, ) -> list[TransportSiteLinkRead]: """按当前计划装/送货客户判定,解析可复制的现场链接列表。""" if not plan_requires_site_link(db, tenant_no=tenant_no, plan=plan): return [] site_rows = ( db.query(PlTransportSiteLink) .filter( PlTransportSiteLink.tenant_no == tenant_no, PlTransportSiteLink.plan_id == plan.id, PlTransportSiteLink.transport_id == transport.id, PlTransportSiteLink.del_flag == 0, ) .order_by(PlTransportSiteLink.create_time.asc()) .all() ) if site_rows: product_name_by_id = { item.id: item.product_name or "" for item in db.query(PlPlanProduct) .filter(PlPlanProduct.plan_id == plan.id, PlPlanProduct.del_flag == 0) .all() } return [ build_transport_site_link_read_from_row( row, product_name=product_name_by_id.get(row.plan_product_id, row.product or ""), ) for row in site_rows ] if plan_has_load_stop(db, tenant_no, plan.id): token = (transport.site_access_token or "").strip() if token: return [ TransportSiteLinkRead( plan_product_id="", product_name=plan.plan_no or transport.transport_no, site_link_id=transport.id, plan_no=plan.plan_no or "", business_type="按段现场", site_access_token=token, site_url=build_transport_site_url(token), ) ] active_io = get_active_io_order_for_plan(db, tenant_no, plan) if active_io: kind, header = active_io token = (header.site_access_token or "").strip() if token and header.status != "voided": return [ TransportSiteLinkRead( plan_product_id="", product_name=header.order_no, site_link_id=header.id, plan_no=plan.plan_no or "", business_type="入库" if kind == "inbound" else "出库", site_access_token=token, site_url=build_io_order_site_url(kind, token), ) ] return []