""" scripts/rpa/dsv_report_parameters.py 用途:填充 DSV 360 SSRS ReportViewer 报表参数(Owner / PutawayZone / Warehouse)。 依赖:Playwright Page/Frame;由 freight_rpa_worker._fill_dsv_filter_if_needed 委托调用。 逻辑:识别 multi-value checkbox 与 ValidValue select,在 report iframe 内操作并等待控件启用。 """ from __future__ import annotations import json import sys import time from typing import Any, Callable # BYAWSATL01 等 3lv 报表在 SSRS 中的默认控件 ID(与页面源码一致)。 _DSV_REPORT_PARAM_DEFAULTS: dict[str, dict[str, str]] = { "Owner": { "control": "multi_value", "text": "#ReportViewerMain_ctl08_ctl03_txtValue", "toggle": "#ReportViewerMain_ctl08_ctl03_ctl01", "dropdown": "#ReportViewerMain_ctl08_ctl03_divDropDown", }, "PutawayZone": { "control": "multi_value", "text": "#ReportViewerMain_ctl08_ctl07_txtValue", "toggle": "#ReportViewerMain_ctl08_ctl07_ctl01", "dropdown": "#ReportViewerMain_ctl08_ctl07_divDropDown", }, "Warehouse": { "control": "select", "selector": "#ReportViewerMain_ctl08_ctl17_ddValue", }, } def _iter_dsv_report_roots(page: Any) -> list[Any]: """返回包含 ReportViewerMain 的 frame;找不到时回退到全部 roots。""" matched: list[Any] = [] for frame in getattr(page, "frames", []): try: if frame.locator("#ReportViewerMain").count() > 0: matched.append(frame) except Exception: continue if matched: return matched return [page, *list(getattr(page, "frames", []))] def _find_locator(page: Any, selector: str) -> tuple[Any | None, Any | None]: """在 report roots 中查找 selector 对应的 locator。""" for root in _iter_dsv_report_roots(page): try: locator = root.locator(selector).first locator.wait_for(state="attached", timeout=1_000) return root, locator except Exception: continue return None, None def _warehouse_select_state(locator: Any, value: str) -> dict[str, Any]: """探测 Warehouse select 是否已有目标 option、是否已解除 disabled。""" try: result = locator.evaluate( """ (select, target) => { const normalize = (text) => (text || '').replace(/\\s+/g, ' ').trim().toLowerCase(); const wanted = normalize(target); const options = Array.from(select.options || []); const hasTarget = options.some((item) => normalize(item.textContent) === wanted); const realOptions = options.filter((item) => { const label = normalize(item.textContent); return label && label !== '