import uuid from django.conf import settings from django.db import models from django.db.models import Q class WorkItemStatus(models.TextChoices): ACTIVE = "active", "待处理" COMPLETED = "completed", "已完成" CANCELLED = "cancelled", "已取消" class WorkItemType(models.TextChoices): TICKET_MISSING_INFO = "ticket_missing_info", "工单信息待补全" TICKET_INTAKE_FAILED = "ticket_intake_failed", "智能受理待人工接管" TICKET_CUSTOMER_REPLY = "ticket_customer_reply", "客户回复待确认" TICKET_CLOSE = "ticket_close", "工单待人工关闭" TICKET_CUSTOMER_CONFIRMATION = "ticket_customer_confirmation", "客户解决反馈待确认" SUPPORT_ANALYSIS_FAILED = "support_analysis_failed", "支持分析待人工接管" KNOWLEDGE_DISTILLATION_FAILED = ( "knowledge_distillation_failed", "知识蒸馏待人工接管", ) ASSET_REVIEW = "asset_review", "知识资产待审核" TOOL_CONFIRMATION = "tool_confirmation", "高风险工具待确认" class WorkItem(models.Model): public_id = models.UUIDField(default=uuid.uuid4, unique=True, editable=False) task_type = models.CharField(max_length=64, choices=WorkItemType.choices) status = models.CharField( max_length=24, choices=WorkItemStatus.choices, default=WorkItemStatus.ACTIVE, ) object_type = models.CharField(max_length=64) object_id = models.CharField(max_length=80) ticket = models.ForeignKey( "tickets.Ticket", null=True, blank=True, on_delete=models.CASCADE, related_name="work_items", ) title = models.CharField(max_length=200) description = models.TextField(blank=True) payload = models.JSONField(default=dict, blank=True) assignee = models.ForeignKey( settings.AUTH_USER_MODEL, null=True, blank=True, on_delete=models.SET_NULL, related_name="assigned_work_items", ) created_by = models.ForeignKey( settings.AUTH_USER_MODEL, null=True, blank=True, on_delete=models.SET_NULL, related_name="created_work_items", ) completed_by = models.ForeignKey( settings.AUTH_USER_MODEL, null=True, blank=True, on_delete=models.SET_NULL, related_name="completed_work_items", ) completed_at = models.DateTimeField(null=True, blank=True) due_at = models.DateTimeField(null=True, blank=True, db_index=True) created_at = models.DateTimeField(auto_now_add=True) updated_at = models.DateTimeField(auto_now=True) class Meta: ordering = ["-created_at"] constraints = [ models.UniqueConstraint( fields=["task_type", "object_type", "object_id"], condition=Q(status=WorkItemStatus.ACTIVE), name="unique_active_work_item_target", ) ] indexes = [ models.Index(fields=["status", "task_type", "created_at"]), models.Index(fields=["object_type", "object_id"]), models.Index(fields=["assignee", "status", "created_at"]), ]