1. 定义数据 model

本章创建后续教程依赖的三个 model。

Model用途
Lead入站线索,尚未达成资格判定
Account已资格化的客户组织
Opportunity挂在 Account 上的具体交易,含阶段与金额

预计耗时:20 分钟。

1.1 创建 Lead model

打开 Studio → Models → New model,填入:

  • Code:crm_lead
  • 显示名:Lead
  • Tenant:你的 tenant
  • 存储:standard(自动创建 mt_crm_lead 表)

添加以下 field:

CodedataType必填说明
namestring联系人姓名(constraints.maxLength: 80)
emailstring唯一索引(constraints.maxLength: 120)
phonestringconstraints.maxLength: 32
sourceenum取值:web / referral / event / cold
scoreinteger0–100
statusenum取值:new / qualified / disqualified(默认 new)
notestext

全字段类型示例

保存并发布。平台会自动创建动态表(参见 Model 与 Field)。

1.2 创建 Account model

# DSL 片段 —— 仅供参考,你可以在 Studio 中编辑。
model:
  code: crm_account
  fields:
    - { code: name,    dataType: string,    constraints: { required: true, maxLength: 120 } }
    - { code: domain,  dataType: string,    constraints: { maxLength: 80 } }
    - { code: tier,    dataType: enum,      dictCode: crm_account_tier }
    - { code: owner,   dataType: reference, extension: { referenceModelCode: sys_user } }

1.3 创建 Opportunity model

Opportunity 引用 Account,含 stage 与 amount。

model:
  code: crm_opportunity
  fields:
    - { code: title,    dataType: string,    constraints: { required: true, maxLength: 160 } }
    - { code: account,  dataType: reference, constraints: { required: true }, extension: { referenceModelCode: crm_account } }
    - { code: stage,    dataType: enum,      dictCode: crm_opp_stage, defaultValue: prospect }
    - { code: amount,   dataType: decimal,   extension: { precision: 2 } }
    - { code: close_at, dataType: date }
    - { code: owner,    dataType: reference, extension: { referenceModelCode: sys_user } }

1.4 校验规则

Studio → Models → Validation 中添加以下约束:

  • Lead.email —— tenant 内唯一
  • Opportunity.amount —— 非负
  • Opportunity.close_at —— 当 stage != won|lost 时必须为将来时间

1.5 验证

打开 Data → Tables,确认:

  • mt_crm_leadmt_crm_accountmt_crm_opportunity 表已存在
  • 每一行都自动写入 id(snowflake)与 pid(ULID)
  • 引用列 crm_opportunity.account_id 上有指向 mt_crm_account.id 的外键

模型管理界面 — Lead/Account/Opportunity

若任一项缺失,重新发布 model 并再次检查;平台会在 publish 时调用 SchemaManagementServiceImpl.createTableByModel

下一步

第 2 章 → 设计页面