Files
ai-ticket-platform/backend/apps/tickets/management/commands/verify_ticket_intake.py
2026-07-16 19:37:32 +08:00

67 lines
3.0 KiB
Python

from django.contrib.auth import get_user_model
from django.core.management.base import BaseCommand, CommandError
from apps.tickets.draft_services import (
confirm_draft,
create_agent_run_for_draft,
execute_draft_analysis,
)
from apps.tickets.models import TicketDraft
class Command(BaseCommand):
help = "Run the fixed real-text ticket intake acceptance workflow."
def add_arguments(self, parser):
parser.add_argument("--username", default="admin")
def handle(self, *args, **options):
user = get_user_model().objects.filter(username=options["username"]).first()
if user is None:
raise CommandError("验收用户不存在")
draft = TicketDraft.objects.create(
created_by=user,
source_text=(
"生产环境中,客户 A 的采集任务 JOB-1024 从上午 10 点起没有更新,"
"影响单个客户,请技术支持排查。"
),
)
run = create_agent_run_for_draft(draft)
execute_draft_analysis(draft.id, run.id)
draft.refresh_from_db()
if draft.status != "ready":
raise CommandError(f"真实文本分析失败: {draft.error_code} {draft.error_message}")
fields = {
**draft.current_fields,
"title": draft.current_fields.get("title") or "生产采集任务未更新",
"description": draft.current_fields.get("description") or draft.source_text,
"category_code": "technical_issue",
"priority": draft.current_fields.get("priority")
if draft.current_fields.get("priority") in {"low", "medium", "high", "urgent"}
else "high",
"problem_performance": draft.current_fields.get("problem_performance")
or "采集进度长时间未更新",
"problem_object": draft.current_fields.get("problem_object") or "JOB-1024",
"submitter_name": draft.current_fields.get("submitter_name") or "验收用户",
"submitter_org": draft.current_fields.get("submitter_org") or "验收机构",
"environment": draft.current_fields.get("environment") or "生产环境",
"impact_scope": draft.current_fields.get("impact_scope") or "单个客户",
"related_system": draft.current_fields.get("related_system") or "数据采集平台",
}
draft.current_fields = fields
draft.revision += 1
draft.save(update_fields=["current_fields", "revision", "updated_at"])
revision = draft.revision
first = confirm_draft(draft.public_id, revision, user)
second = confirm_draft(draft.public_id, revision, user)
if first.confirmed_ticket_id != second.confirmed_ticket_id:
raise CommandError("重复确认创建了不同工单")
self.stdout.write(
self.style.SUCCESS(
"Ticket intake acceptance passed: "
f"draft={draft.public_id} run={run.public_id} "
f"ticket={first.confirmed_ticket.ticket_no} decision={first.decision}"
)
)