Files
shanxia2.0/backend/app/services/db/pound_qr_public.py
T
wuzilongandCursor 10183dabc2 feat(backend): expose pound QR items on driver transport page
Enrich inventory links with business type/partner and attach
poundQrcodeItems for the public driver footer button.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-09-13 09:29:03 +08:00

86 lines
2.7 KiB
Python

from __future__ import annotations
from typing import Any, Optional
from app.schemas.db.public_inventory import PublicPoundQrRead
from app.schemas.pl.transport import PublicPoundQrcodeItemRead
from app.services.db.mappers import inventory_date_to_api
def build_pound_qr_label(business_type: str, partner_company: str) -> str:
bt = (business_type or "").strip()
pc = (partner_company or "").strip()
if bt and pc:
return f"{bt} · {pc}"
return bt or pc or "磅单"
def build_public_pound_qr_read(row: Any, *, quantity_unit: Optional[str] = None) -> PublicPoundQrRead:
unit = (quantity_unit or "").strip() or "吨"
return PublicPoundQrRead(
id=row.id,
date=inventory_date_to_api(row.date) or None,
business_type=(row.business_type or "").strip() or "",
vehicle_no=(row.vehicle_no or "").strip(),
partner_company=(row.partner_company or "").strip(),
product=(row.product or "").strip(),
specification=(row.specification or None),
quantity=float(row.quantity or 0),
quantity_unit=unit,
remarks=(row.remark or None) if getattr(row, "remark", None) else None,
)
def build_pound_qrcode_items_for_plan(
db,
*,
tenant_no: str,
plan_id: str,
transport_id: str,
) -> list[PublicPoundQrcodeItemRead]:
from app.models.db_models import DbInboundOutbound
from app.models.pl_models import PlTransportDbLink
links = (
db.query(PlTransportDbLink)
.filter(
PlTransportDbLink.tenant_no == tenant_no,
PlTransportDbLink.plan_id == plan_id,
PlTransportDbLink.transport_id == transport_id,
PlTransportDbLink.del_flag == 0,
)
.all()
)
items: list[PublicPoundQrcodeItemRead] = []
seen: set[str] = set()
for link in links:
inv_id = (link.db_inventory_id or "").strip()
if not inv_id or inv_id in seen:
continue
if not (link.db_purchase_plan_id or "").strip():
continue
inv = (
db.query(DbInboundOutbound)
.filter(
DbInboundOutbound.id == inv_id,
DbInboundOutbound.tenant_no == tenant_no,
DbInboundOutbound.del_flag == 0,
)
.first()
)
if not inv:
continue
seen.add(inv_id)
bt = (inv.business_type or "").strip()
pc = (inv.partner_company or "").strip()
items.append(
PublicPoundQrcodeItemRead(
inventory_id=inv.id,
business_type=bt,
partner_company=pc,
label=build_pound_qr_label(bt, pc),
)
)
return items