115 lines
4.1 KiB
Python
115 lines
4.1 KiB
Python
"""
|
|
backend/app/services/voucher_check/aggregator.py
|
|
|
|
用途:按单文件结果计算缺件双口径、invoice 行 Excel 值与批次状态。
|
|
依赖:无 IO。
|
|
逻辑:缺件文件数计 missing 条数;缺件 invoice 数对 missing 去重;技术失败/跳过/待归档不计缺件。
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
from collections import defaultdict
|
|
from typing import Any
|
|
|
|
EXCEL_PRESENT = "有"
|
|
EXCEL_MISSING = "缺件"
|
|
EXCEL_PENDING = "未检核"
|
|
INVOICE_COMPLETE = "完整"
|
|
INVOICE_MISSING = "缺件"
|
|
INVOICE_PENDING = "待处理"
|
|
|
|
_MISSING = "missing"
|
|
_ARCHIVED = "archived"
|
|
_SKIPPED = "skipped"
|
|
_TECH = "technical_error"
|
|
_STAGED = "staged"
|
|
|
|
|
|
def excel_cell_for_rule(statuses: list[str]) -> str:
|
|
"""一条规则在 Excel 中的单元格:缺件优先于有,其余为未检核。"""
|
|
if any(status == _MISSING for status in statuses):
|
|
return EXCEL_MISSING
|
|
if statuses and all(status == _ARCHIVED for status in statuses):
|
|
return EXCEL_PRESENT
|
|
return EXCEL_PENDING
|
|
|
|
|
|
def invoice_conclusion(cells: list[str]) -> str:
|
|
"""invoice 行总体状态。"""
|
|
if cells and all(cell == EXCEL_PRESENT for cell in cells):
|
|
return INVOICE_COMPLETE
|
|
if any(cell == EXCEL_MISSING for cell in cells):
|
|
return INVOICE_MISSING
|
|
return INVOICE_PENDING
|
|
|
|
|
|
def aggregate_file_results(
|
|
results: list[dict[str, Any]],
|
|
*,
|
|
rules: list[dict[str, Any]],
|
|
) -> dict[str, Any]:
|
|
"""
|
|
聚合单文件结果。
|
|
核心逻辑:只认 status 字段;同一 invoice 多条缺件文件计多份、invoice 只计一次。
|
|
"""
|
|
missing_files = [row for row in results if row.get("status") == _MISSING]
|
|
missing_invoices = {str(row.get("invoice_no") or "") for row in missing_files}
|
|
missing_invoices.discard("")
|
|
by_type: dict[str, dict[str, Any]] = {}
|
|
for rule in rules:
|
|
code = str(rule.get("code") or "")
|
|
type_rows = [row for row in results if row.get("rule_code") == code]
|
|
type_missing = [row for row in type_rows if row.get("status") == _MISSING]
|
|
by_type[code] = {
|
|
"rule_code": code,
|
|
"rule_name": rule.get("name") or code,
|
|
"missing_file_count": len(type_missing),
|
|
"missing_invoice_count": len({str(row.get("invoice_no") or "") for row in type_missing} - {""}),
|
|
}
|
|
|
|
invoices: dict[str, list[dict[str, Any]]] = defaultdict(list)
|
|
for row in results:
|
|
invoices[str(row.get("invoice_no") or "")].append(row)
|
|
|
|
excel_rows: list[dict[str, Any]] = []
|
|
for invoice_no, rows in invoices.items():
|
|
if not invoice_no:
|
|
continue
|
|
cells: dict[str, str] = {}
|
|
ordered_cells: list[str] = []
|
|
for rule in rules:
|
|
code = str(rule.get("code") or "")
|
|
cell = excel_cell_for_rule(
|
|
[str(item.get("status") or "") for item in rows if item.get("rule_code") == code]
|
|
)
|
|
cells[code] = cell
|
|
ordered_cells.append(cell)
|
|
excel_rows.append(
|
|
{
|
|
"invoice_no": invoice_no,
|
|
"invoice_date": next((item.get("invoice_date") for item in rows if item.get("invoice_date")), None),
|
|
"cells": cells,
|
|
"status": invoice_conclusion(ordered_cells),
|
|
}
|
|
)
|
|
excel_rows.sort(key=lambda item: str(item["invoice_no"]))
|
|
|
|
batch_status = "completed"
|
|
if missing_files:
|
|
batch_status = "missing"
|
|
elif any(row.get("status") == _TECH for row in results):
|
|
batch_status = "technical_failed"
|
|
|
|
return {
|
|
"subject_count": len([key for key in invoices if key]),
|
|
"archived_file_count": sum(1 for row in results if row.get("status") == _ARCHIVED),
|
|
"skipped_file_count": sum(1 for row in results if row.get("status") == _SKIPPED),
|
|
"missing_file_count": len(missing_files),
|
|
"missing_invoice_count": len(missing_invoices),
|
|
"technical_error_count": sum(1 for row in results if row.get("status") == _TECH),
|
|
"staged_file_count": sum(1 for row in results if row.get("status") == _STAGED),
|
|
"by_file_type": by_type,
|
|
"excel_rows": excel_rows,
|
|
"batch_status": batch_status,
|
|
}
|