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:
| Code | dataType | 必填 | 说明 |
|---|---|---|---|
name | string | ✓ | 联系人姓名(constraints.maxLength: 80) |
email | string | ✓ | 唯一索引(constraints.maxLength: 120) |
phone | string | constraints.maxLength: 32 | |
source | enum | ✓ | 取值:web / referral / event / cold |
score | integer | 0–100 | |
status | enum | ✓ | 取值:new / qualified / disqualified(默认 new) |
notes | text |

保存并发布。平台会自动创建动态表(参见 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_lead、mt_crm_account、mt_crm_opportunity表已存在- 每一行都自动写入
id(snowflake)与pid(ULID) - 引用列
crm_opportunity.account_id上有指向mt_crm_account.id的外键

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