from __future__ import annotations from review_agent.models import ApplicationFormFillBatch, ExportedSummaryFile, FileSummaryBatch, RegulatoryReviewBatch from .permissions import can_access_batch WORKFLOW_MODELS = { "file_summary": FileSummaryBatch, "regulatory_review": RegulatoryReviewBatch, "application_form_fill": ApplicationFormFillBatch, } def query_batch_summary(user, *, workflow_type: str | None = None, batch_no: str | None = None, latest: bool = False) -> dict: candidates = _candidate_batches(workflow_type) if batch_no: for current_workflow_type, model in candidates: batch = model.objects.filter(batch_no=batch_no).first() if batch: return _serialize_allowed_batch(user, current_workflow_type, batch) return {"ok": False, "permission_result": "not_found", "answer_summary": "未找到对应批次。"} if latest: for current_workflow_type, model in candidates: queryset = model.objects.all().order_by("-finished_at", "-created_at", "-id") for batch in queryset: if can_access_batch(user, batch): return _serialize_batch(current_workflow_type, batch, permission_result="allowed") return {"ok": False, "permission_result": "not_found", "answer_summary": "未找到可访问的批次。"} return {"ok": False, "permission_result": "not_found", "answer_summary": "请提供批次号,或询问最新/最近批次。"} def _candidate_batches(workflow_type: str | None): if workflow_type and workflow_type in WORKFLOW_MODELS: return [(workflow_type, WORKFLOW_MODELS[workflow_type])] return list(WORKFLOW_MODELS.items()) def _serialize_allowed_batch(user, workflow_type: str, batch) -> dict: if not can_access_batch(user, batch): return {"ok": False, "permission_result": "denied", "answer_summary": "无权限访问该批次。"} return _serialize_batch(workflow_type, batch, permission_result="allowed") def _serialize_batch(workflow_type: str, batch, *, permission_result: str) -> dict: summary = _summary_for_batch(workflow_type, batch) result_url = _result_url(workflow_type, batch.pk) answer = f"{batch.batch_no} 状态 {batch.status}。{summary}" return { "ok": True, "permission_result": permission_result, "workflow_type": workflow_type, "batch_id": batch.pk, "batch_no": batch.batch_no, "status": batch.status, "summary": summary, "result_url": result_url, "answer_summary": answer, } def _summary_for_batch(workflow_type: str, batch) -> str: if workflow_type == "file_summary": return f"文件 {batch.total_files} 个,成功 {batch.success_files} 个,失败 {batch.failed_files} 个。" if workflow_type == "regulatory_review": risk = batch.risk_summary or {} return f"阻断项 {int(risk.get('blocking') or 0)} 个,高风险 {int(risk.get('high') or 0)} 个。" if workflow_type == "application_form_fill": export_count = ExportedSummaryFile.objects.filter( workflow_type="application_form_fill", workflow_batch_id=batch.pk, ).count() return f"导出文件 {export_count} 个,冲突字段 {len(batch.conflict_summary or [])} 个。" return "" def _result_url(workflow_type: str, batch_id: int) -> str: paths = { "file_summary": f"/api/review-agent/file-summary/{batch_id}/status/", "regulatory_review": f"/api/review-agent/regulatory-review/{batch_id}/status/", "application_form_fill": f"/api/review-agent/application-form-fill/{batch_id}/status/", } return paths.get(workflow_type, "/")