""" CRM 客户域 ORM 模型 —— 映射 crm_customers 表 """ from __future__ import annotations import uuid from datetime import datetime from sqlalchemy import Boolean, DateTime, ForeignKey, Numeric, SmallInteger, String, Text, func from sqlalchemy.dialects.postgresql import UUID, JSONB from sqlalchemy.orm import Mapped, mapped_column, relationship from app.models.base import Base class CrmCustomer(Base): __tablename__ = "crm_customers" id: Mapped[uuid.UUID] = mapped_column( UUID(as_uuid=True), primary_key=True, default=uuid.uuid4 ) name: Mapped[str] = mapped_column(String(200), nullable=False) level: Mapped[str] = mapped_column(String(1), default="C") industry: Mapped[str | None] = mapped_column(String(100), nullable=True) contact: Mapped[str | None] = mapped_column(String(50), nullable=True) phone: Mapped[str | None] = mapped_column(String(30), nullable=True) email: Mapped[str | None] = mapped_column(String(100), nullable=True) address: Mapped[str | None] = mapped_column(Text, nullable=True) ai_score: Mapped[float] = mapped_column(Numeric(5, 2), default=0) ai_persona: Mapped[dict | None] = mapped_column(JSONB, default=dict, nullable=True) billing_info: Mapped[dict | None] = mapped_column( JSONB, default=dict, nullable=True, comment="客户开票信息: company_name/tax_id/address/phone/bank_name/bank_account" ) health_score: Mapped[float] = mapped_column( Numeric(5, 2), default=0, comment="客户健康度评分 (AI 教练引擎计算)" ) meddic_status: Mapped[dict | None] = mapped_column( JSONB, default=dict, nullable=True, comment="MEDDIC 六维评估状态" ) owner_id: Mapped[uuid.UUID | None] = mapped_column( UUID(as_uuid=True), ForeignKey("sys_users.id"), nullable=True ) status: Mapped[int] = mapped_column(SmallInteger, default=1) created_at: Mapped[datetime] = mapped_column(DateTime, server_default=func.now()) updated_at: Mapped[datetime] = mapped_column( DateTime, server_default=func.now(), onupdate=func.now() ) is_deleted: Mapped[bool] = mapped_column(Boolean, default=False) # 关系 owner: Mapped["SysUser | None"] = relationship("SysUser", lazy="selectin") # noqa: F821 contacts: Mapped[list["CrmContact"]] = relationship("CrmContact", back_populates="customer", lazy="selectin") class CrmContact(Base): """客户联系人子表 (V5.0) — 映射 crm_contacts""" __tablename__ = "crm_contacts" id: Mapped[uuid.UUID] = mapped_column( UUID(as_uuid=True), primary_key=True, default=uuid.uuid4 ) customer_id: Mapped[uuid.UUID] = mapped_column( UUID(as_uuid=True), ForeignKey("crm_customers.id"), nullable=False ) name: Mapped[str] = mapped_column(String(100), nullable=False) phone: Mapped[str | None] = mapped_column(String(30), nullable=True) title: Mapped[str | None] = mapped_column(String(100), nullable=True) ai_buyer_persona: Mapped[dict | None] = mapped_column(JSONB, default=dict, nullable=True) is_deleted: Mapped[bool] = mapped_column(Boolean, default=False) created_at: Mapped[datetime] = mapped_column(DateTime, server_default=func.now()) updated_at: Mapped[datetime] = mapped_column( DateTime, server_default=func.now(), onupdate=func.now() ) # 关系 customer: Mapped["CrmCustomer"] = relationship("CrmCustomer", back_populates="contacts")