Files

146 lines
4.9 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# =============================================================================
# 企微IT智能服务台 — 快速回复模板模型
# =============================================================================
# 说明:对应数据库 quick_reply_templates 表,存储坐席常用回复模板
# 分类:账号/网络/软件/硬件/通用
# 支持变量替换:如 {employee_name} 会被替换为实际员工姓名
# =============================================================================
import uuid
from datetime import datetime
from typing import Any, Dict, List
from sqlalchemy import DateTime, Index, Integer, JSON, String, Text
from sqlalchemy.orm import Mapped, mapped_column
from app.database import Base
class QuickReplyTemplate(Base):
"""快速回复模板模型 — 对应 quick_reply_templates 表。
坐席常用回复模板,按分类组织,支持变量替换。
变量如 {employee_name} 在使用时会被替换为实际值。
Attributes:
id: 模板唯一标识(UUID,数据库自动生成)
category: 分类(账号/网络/软件/硬件/通用)
title: 模板标题(简短描述,方便坐席快速识别)
content: 模板内容(支持 {employee_name} 等变量)
variables: 可用变量列表(JSONB,如 ["employee_name","department"]
sort_order: 排序权重(数值越小越靠前)
created_at: 创建时间
updated_at: 更新时间
"""
# 表名(必须和架构文档 DDL 一致)
__tablename__ = "quick_reply_templates"
# --------------------------------------------------------------------------
# 字段定义
# --------------------------------------------------------------------------
# 主键:UUIDPython端生成(兼容PostgreSQL和SQLite
id: Mapped[str] = mapped_column(
String(36),
primary_key=True,
default=lambda: str(uuid.uuid4()),
)
# 分类(用于按类别组织模板,在坐席端折叠展示)
category: Mapped[str] = mapped_column(
String(64),
nullable=False,
default="通用",
comment="分类:账号/网络/软件/硬件/通用",
)
# 模板标题(简短描述,方便坐席快速识别)
title: Mapped[str] = mapped_column(
String(128),
nullable=False,
comment="模板标题",
)
# 模板内容(支持变量替换)
# 示例:"您好{employee_name},您的密码重置链接已发送"
content: Mapped[str] = mapped_column(
Text,
nullable=False,
comment="模板内容,支持变量如 {employee_name}",
)
# 可用变量列表(JSON 格式,存储模板中可替换的变量名,兼容所有数据库)
# 示例:["employee_name", "department"]
variables: Mapped[List[str]] = mapped_column(
JSON,
nullable=False,
default=list,
comment="可用变量列表",
)
# 排序权重(数值越小越靠前,同一分类内排序)
sort_order: Mapped[int] = mapped_column(
Integer,
nullable=False,
default=0,
comment="排序权重",
)
# 状态(draft=草稿/pending_review=待审核/approved=已通过/rejected=已驳回)
# 审核流转:draft → pending_review → approved / rejected
status: Mapped[str] = mapped_column(
String(20),
nullable=False,
default="approved",
comment="状态:draft/pending_review/approved/rejected",
)
# 版本号(每次审核通过后 +1
version: Mapped[int] = mapped_column(
Integer,
nullable=False,
default=1,
comment="版本号,每次审核通过后 +1",
)
# 提交人 agent_id(提交审核的坐席ID,可为空表示系统创建)
submitted_by: Mapped[str] = mapped_column(
String(36),
nullable=True,
default=None,
comment="提交人 agent_id",
)
# 创建时间
created_at: Mapped[datetime] = mapped_column(
DateTime(timezone=True),
nullable=False,
default=datetime.now,
comment="创建时间",
)
# 更新时间
updated_at: Mapped[datetime] = mapped_column(
DateTime(timezone=True),
nullable=False,
default=datetime.now,
onupdate=datetime.now,
comment="更新时间",
)
# --------------------------------------------------------------------------
# 索引定义(和架构文档 DDL 严格一致)
# --------------------------------------------------------------------------
__table_args__ = (
# 按分类查询(如获取所有"账号"分类的模板)
Index("idx_qr_category", "category"),
)
def __repr__(self) -> str:
"""模板对象的字符串表示,方便调试。"""
return (
f"<QuickReplyTemplate(id={self.id}, category={self.category}, "
f"title={self.title})>"
)