from __future__ import annotations from review_agent.application_form_fill.constants import WORKFLOW_TYPE as FORM_FILL_WORKFLOW_TYPE from review_agent.models import ( ApplicationFormFillBatch, ExportedSummaryFile, FileSummaryBatch, RegulatoryIssue, RegulatoryReviewBatch, ) from .context import NotificationContext def build_file_summary_context(batch: FileSummaryBatch) -> NotificationContext: status = batch.status abnormal_count = int(batch.failed_files or 0) + int(batch.unsupported_files or 0) + int(batch.uncertain_files or 0) return NotificationContext( workflow_type="file_summary", workflow_name="自动汇总", workflow_batch_id=batch.pk, workflow_batch_no=batch.batch_no, workflow_status=status, trigger_user_id=batch.user_id, trigger_username=batch.user.get_username(), title=f"自动汇总{_status_label(status)}", summary_lines=( f"文件总数 {batch.total_files} 个,成功 {batch.success_files} 个", f"异常/不支持/不确定 {abnormal_count} 个,总页数 {batch.total_pages}", _error_line(batch.error_message), ), next_step="查看文件目录、页数统计和导出结果", result_path=f"/api/review-agent/file-summary/{batch.pk}/status/", ) def build_regulatory_review_context(batch: RegulatoryReviewBatch) -> NotificationContext: summary = batch.risk_summary or _count_regulatory_issues(batch) return NotificationContext( workflow_type="regulatory_review", workflow_name="法规核查", workflow_batch_id=batch.pk, workflow_batch_no=batch.batch_no, workflow_status=batch.status, trigger_user_id=batch.user_id, trigger_username=batch.user.get_username(), title=f"法规核查{_status_label(batch.status)}", summary_lines=( f"阻断项 {int(summary.get('blocking') or 0)} 个,高风险 {int(summary.get('high') or 0)} 个", f"中风险 {int(summary.get('medium') or 0)} 个,低风险 {int(summary.get('low') or 0)} 个", _error_line(batch.error_message), ), next_step="查看风险报告并处理整改项", result_path=f"/api/review-agent/regulatory-review/{batch.pk}/status/", ) def build_application_form_fill_context(batch: ApplicationFormFillBatch) -> NotificationContext: export_count = ExportedSummaryFile.objects.filter( workflow_type=FORM_FILL_WORKFLOW_TYPE, workflow_batch_id=batch.pk, ).count() return NotificationContext( workflow_type=FORM_FILL_WORKFLOW_TYPE, workflow_name="自动填表", workflow_batch_id=batch.pk, workflow_batch_no=batch.batch_no, workflow_status=batch.status, trigger_user_id=batch.user_id, trigger_username=batch.user.get_username(), title=f"自动填表{_status_label(batch.status)}", summary_lines=( f"模板 {', '.join(batch.selected_templates or []) or '未识别'}", f"导出文件 {export_count} 个,冲突字段 {len(batch.conflict_summary or [])} 个", _error_line(batch.error_message), ), next_step="下载生成文件并检查字段冲突", result_path=f"/api/review-agent/application-form-fill/{batch.pk}/status/", ) def _count_regulatory_issues(batch: RegulatoryReviewBatch) -> dict[str, int]: return { severity: RegulatoryIssue.objects.filter(batch=batch, severity=severity).count() for severity in ["blocking", "high", "medium", "low", "info"] } def _status_label(status: str) -> str: labels = { "success": "完成", "partial_success": "部分完成", "failed": "失败", "cancelled": "已取消", } return labels.get(status, status) def _error_line(error_message: str) -> str: if not error_message: return "" return f"失败原因:{error_message[:160]}"