""" backend/app/services/voucher_check/excel_rows.py 用途:把批次明细转换为通用 Excel 导出行与表头。 依赖:无。 逻辑:runner 返回 details;schedule excel_service / register_schedule_header 使用本模块表头。 """ from __future__ import annotations from typing import Any VOUCHER_CHECK_HEADER_ORDER = [ "period", "scope_code", "invoice_no", "invoice_date", "result", "folder_exists", "folder_path", "matched_files", "missing_materials", "material_fingerprint", "checked_at", "error_code", "error_message", ] VOUCHER_CHECK_HEADER_LABELS = { "period": "检核月份", "scope_code": "账套", "invoice_no": "发票号", "invoice_date": "发票日期", "result": "结论", "folder_exists": "发票目录是否存在", "folder_path": "共享盘相对路径", "matched_files": "已找到材料", "missing_materials": "缺失材料", "material_fingerprint": "目录元数据指纹", "checked_at": "实际检核时间", "error_code": "技术错误码", "error_message": "技术错误摘要", } _RESULT_LABELS = { "complete": "齐全", "missing": "缺资料", "skipped": "跳过", "technical_error": "技术异常", } def _fmt_matched(matched: dict[str, Any] | None) -> str: if not matched: return "" return "; ".join(f"{k}={v}" for k, v in matched.items()) def _fmt_missing(missing: list[dict[str, Any]] | None) -> str: if not missing: return "" parts = [] for item in missing: name = item.get("name") or item.get("code") or "" code = item.get("code") or "" parts.append(f"{name}({code})" if code and name != code else str(name or code)) return "; ".join(parts) def to_excel_detail_row(item: dict[str, Any], *, period: str, scope_code: str) -> dict[str, Any]: """ 将内存结果行转为 Excel details 行。 """ result = str(item.get("result") or "") checked_at = item.get("checked_at") invoice_date = item.get("invoice_date") return { "period": period, "scope_code": scope_code, "invoice_no": item.get("invoice_no"), "invoice_date": invoice_date.isoformat() if hasattr(invoice_date, "isoformat") else invoice_date, "result": _RESULT_LABELS.get(result, result), "folder_exists": "是" if item.get("folder_exists") else "否", "folder_path": item.get("folder_path") or "", "matched_files": _fmt_matched(item.get("matched_files")), "missing_materials": _fmt_missing(item.get("missing_materials")), "material_fingerprint": item.get("material_fingerprint") or "", "checked_at": checked_at.isoformat(sep=" ", timespec="seconds") if hasattr(checked_at, "isoformat") else (checked_at or ""), "error_code": item.get("error_code") or "", "error_message": item.get("error_message") or "", }