""" 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 _normalize_report_name(report_name: str) -> str: """报表名归一化:去首尾空白并折叠连续空白、统一小写,用于模板精确匹配。""" return " ".join(str(report_name or "").strip().lower().split()) # 报表名 → 参数控件模板。同名的 SSRS 参数在不同报表里的控件长相不同: # - 3lv 版(On Hand Inventory By Location 3lv,Bison/AWSBI):Warehouse 是原生 select(ddValue); # - JDA 版(On Hand Inventory by Location,Moerdijk 等):Warehouse 是 txtValue + toggle + divDropDown # 组成的多选框下拉(multi_value),历史 YAML 为该源配置的 selectors 即源于此。 # 模板按"归一化报表名"精确匹配;未命中时回退 _DSV_REPORT_PARAM_DEFAULTS(保持旧行为)。 _DSV_REPORT_PARAM_TEMPLATES: dict[str, dict[str, dict[str, str]]] = { "on hand inventory by location 3lv": { "Warehouse": { "control": "select", "selector": "#ReportViewerMain_ctl08_ctl17_ddValue", }, }, "on hand inventory by location": { "Warehouse": { "control": "multi_value", "text": "#ReportViewerMain_ctl08_ctl17_txtValue", "toggle": "#ReportViewerMain_ctl08_ctl17_ctl01", "dropdown": "#ReportViewerMain_ctl08_ctl17_divDropDown", }, }, } def resolve_parameter_controls(report_name: str | None, label: str) -> dict[str, str]: """ 按(报表名, 参数名)解析 SSRS 控件配置;报表模板优先,未命中返回空 dict。 Args: report_name: 当前报表名(如 navigation.report_name),允许为空。 label: 参数名(Owner / PutawayZone / Warehouse)。 Returns: dict[str, str]: 控件配置(control/selector 或 control/text/toggle/dropdown); 报表未登记时为空 dict,由调用方回退参数名默认值。 """ key = _normalize_report_name(report_name) template = _DSV_REPORT_PARAM_TEMPLATES.get(key) or {} return dict(template.get(str(label or "").strip()) or {}) 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 !== '