56 lines
2.0 KiB
Python
56 lines
2.0 KiB
Python
from __future__ import annotations
|
|
|
|
from edkb_system.contracts import Context
|
|
|
|
|
|
COND_KWS = ["条件", "判定", "if", "IF"]
|
|
ACT_KWS = ["处理", "动作", "措施", "结果", "输出"]
|
|
OWN_KWS = ["责任", "负责人", "部门", "岗位"]
|
|
|
|
|
|
def _find_col(headers: list[str], kws: list[str]) -> int | None:
|
|
for i, h in enumerate(headers):
|
|
s = (h or "").lower()
|
|
if any(k.lower() in s for k in kws):
|
|
return i
|
|
return None
|
|
|
|
|
|
def _to_table_md(headers: list[str], rows: list[list[str]]) -> str:
|
|
hs = [h or "-" for h in headers]
|
|
out = ["| " + " | ".join(hs) + " |", "| " + " | ".join(["---"] * len(hs)) + " |"]
|
|
for r in rows:
|
|
vals = [r[i] if i < len(r) else "" for i in range(len(hs))]
|
|
out.append("| " + " | ".join(vals) + " |")
|
|
return "\n".join(out)
|
|
|
|
|
|
def run(input_data: list[dict] | dict, context: Context) -> str:
|
|
tables = input_data if isinstance(input_data, list) else [input_data]
|
|
parts: list[str] = []
|
|
|
|
for t in tables:
|
|
headers = [str(x).strip() for x in t.get("headers", [])]
|
|
rows = [[str(c).strip() for c in row] for row in t.get("rows", [])]
|
|
parts.append(f"## Sheet: {t.get('sheet', 'Sheet1')}")
|
|
|
|
cidx = _find_col(headers, COND_KWS)
|
|
aidx = _find_col(headers, ACT_KWS)
|
|
oidx = _find_col(headers, OWN_KWS)
|
|
if cidx is not None and aidx is not None:
|
|
for row in rows:
|
|
cond = row[cidx] if cidx < len(row) else "满足条件"
|
|
act = row[aidx] if aidx < len(row) else "执行处理"
|
|
owner = (
|
|
row[oidx] if oidx is not None and oidx < len(row) else "相关责任方"
|
|
)
|
|
parts.append(
|
|
f"- 当 {cond or '满足条件'} 时,{owner or '相关责任方'} 执行 {act or '执行处理'}。"
|
|
)
|
|
else:
|
|
parts.append(_to_table_md(headers, rows))
|
|
|
|
parts.append("")
|
|
|
|
return "\n".join(parts).strip() + "\n"
|