M2-command.yaml
15.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
# M2 聚合行为命令模型 (v2.2:新增 Employee 的 CreateEmployee/ModifyEmployee;AuditOrder 的 bizSteps 用 IF 判断分支 +
# REMOTE_SET_FIELD/REMOTE_INCREMENT_FIELD 条件回写审核人统计;修正 totalAmount 派生为 quantity*itemPrice)
# 聚合操作命令入参定义;React页面提交时自动映射表单组件值至命令参数,无硬编码接口参数。
behaviors:
# 1.客户聚合行为
- aggregateId: CustomerAggregate
aggregateRoot: Customer
commands:
- cmdId: CreateCustomer
desc: 新增客户主数据
commandType: create
inputParams:
- name: customerName
type: string
- name: contactPhone
type: string
- name: customerLevel
type: string
validations:
- contactPhone非空、手机号格式校验
- customerLevel取值范围[普通,VIP,高级VIP]
emitEvents:
- CustomerCreated
sideEffect: 仅写入本聚合,不可修改其他聚合
- cmdId: ModifyCustomerInfo
desc: 修改客户名称、联系方式、等级
commandType: update
inputParams:
- name: customerId
type: string
- name: customerName
type: string
optional: true
- name: contactPhone
type: string
optional: true
- name: customerLevel
type: string
optional: true
validations:
- customerId必须存在
emitEvents:
- CustomerInfoModified
# 2.产品聚合行为
- aggregateId: ProductAggregate
aggregateRoot: Product
commands:
- cmdId: CreateProduct
desc: 新增商品档案
commandType: create
inputParams:
- name: productName
type: string
- name: skuCode
type: string
- name: category
type: string
- name: saleCurrency
type: string
- name: salePrice
type: decimal
- name: stockNum
type: int
- name: isOnSale
type: boolean
validations:
- skuCode全局唯一
- salePrice > 0
- stockNum >= 0
emitEvents:
- ProductCreated
- cmdId: ModifyProductPrice
desc: 修改售价与币种
commandType: update
inputParams:
- name: productId
type: string
- name: saleCurrency
type: string
optional: true
- name: salePrice
type: decimal
optional: true
validations:
- productId存在
- 修改后售价>0
emitEvents:
- ProductPriceChanged
- cmdId: StockDeduct
desc: 下单扣减库存(订单聚合远程调用)
commandType: update
inputParams:
- name: productId
type: string
- name: deductQty
type: int
validations:
- 可用库存 >= deductQty
# 结构化副作用:引擎据此通用执行"按 keyParam 定位、对 targetField 做 op 运算、amountParam 为量、guardField 保证不越界"
effect:
op: decrement
targetField: stockNum
keyParam: productId
amountParam: deductQty
guardField: stockNum
emitEvents:
- ProductStockDeducted
- cmdId: StockReturn
desc: 取消订单归还库存
commandType: update
inputParams:
- name: productId
type: string
- name: returnQty
type: int
validations: []
effect:
op: increment
targetField: stockNum
keyParam: productId
amountParam: returnQty
emitEvents:
- ProductStockReturned
# 3.订单聚合行为
- aggregateId: OrderAggregate
aggregateRoot: Order
commands:
- cmdId: CreateOrder
desc: 创建订单主单+明细+付款条件+送货地址
commandType: create
# 结构化派生:落库/风控前,引擎按明细 sum(quantity*itemPrice) 服务端重算 totalAmount,杜绝客户端篡改
derivations:
- targetField: totalAmount
sourceEntity: OrderItem
aggregate: sum
expression: "quantity * itemPrice"
inputParams:
- name: customerId
type: string
- name: totalCurrency
type: string
- name: totalAmount
type: decimal
- name: items
type: array
- name: paymentTerm
type: object
- name: deliveryAddress
type: object
validations:
- customerId 存在于CustomerAggregate
- 所有productId存在于ProductAggregate
- 订单明细金额累加 ≈ 订单总金额
- dueDays >= 0, depositRatio ∈ [0,1]
- 收货手机号格式合法
# bizSteps 现为“闭指令集”程序(CommandEngine 逐条通用解释,替代硬编码 create 分派)。
# create 语义:COMMIT 落库聚合根 + 组合子实体(totalAmount 已由 derivations 服务端重算);
# 收尾发 emitEvents,ME 规则 OrderCreated→StockDeduct 逐明细扣库存,任一失败超卖回滚。
bizSteps:
- stepNo: 1
desc: 落库 Order 主记录及 OrderItem/PaymentTerm/DeliveryAddress 组合子实体
instruction:
type: COMMIT_TRANSACTION
params: {}
emitEvents:
- OrderCreated
- OrderItemAdded
- OrderPaymentTermSet
- OrderDeliveryAddressSet
- cmdId: ModifyOrderDeliveryAddress
desc: 修改订单送货地址子实体
commandType: update
inputParams:
- name: orderId
type: string
- name: province
type: string
optional: true
- name: city
type: string
optional: true
- name: district
type: string
optional: true
- name: streetDetail
type: string
optional: true
- name: receiverName
type: string
optional: true
- name: receiverPhone
type: string
optional: true
validations:
- orderId存在
bizSteps:
- stepNo: 1
desc: 按 orderId 加载既有订单聚合根(不存在即失败)
instruction:
type: LOAD_AGGREGATE
params:
idSource: input.orderId
- stepNo: 2
desc: 更新命令携带的根字段与组合子实体(送货地址)后提交事务
instruction:
type: COMMIT_TRANSACTION
params: {}
emitEvents:
- OrderDeliveryAddressModified
- cmdId: CancelOrder
desc: 取消订单,归还库存
commandType: cancel
inputParams:
- name: orderId
type: string
validations:
- 订单状态允许取消
# cancel 语义:LOAD 校验存在 + COMMIT(无字段变更);收尾发 OrderCancelled,
# ME 规则 OrderCancelled→StockReturn 从库加载明细逐项回补库存。
bizSteps:
- stepNo: 1
desc: 按 orderId 加载既有订单聚合根(不存在即失败)
instruction:
type: LOAD_AGGREGATE
params:
idSource: input.orderId
- stepNo: 2
desc: 提交事务(标记作废;库存回补由 ME 一致性规则完成)
instruction:
type: COMMIT_TRANSACTION
params: {}
emitEvents:
- OrderCancelled
- cmdId: AuditOrder
desc: 审核订单:填审核人/时间/备注,按审核结果置订单状态(bizSteps 闭指令集程序)
commandType: update
inputParams:
- name: orderId
type: string
- name: auditorId
type: string
- name: auditRemark
type: string
optional: true
- name: auditResult
type: string
validations:
- orderId 存在
- auditResult ∈ [PASS, REJECT]
# ★ 展示“行为即数据”:这段程序由 CommandEngine 逐条通用解释,改步骤=改 YAML,无需改引擎。
# Step1:根据orderId加载完整订单聚合根
# Step2:系统当前时间自动赋值给auditTime
# Step3:前端传的auditorId赋值给订单的auditorId字段
# Step4:用auditorId去员工服务查审核人姓名,存到auditorName
# Step5:把审核备注赋值给auditRemark
# Step6:审核通过就把订单状态改成PASS,驳回就改成REJECT
# Step7:统一提交数据库事务
bizSteps:
- stepNo: 1
desc: 根据 orderId 加载完整订单聚合根
instruction:
type: LOAD_AGGREGATE
params:
idSource: input.orderId
- stepNo: 2
desc: 系统当前时间自动赋值给 auditTime
instruction:
type: AUTO_FILL_FIELD
params:
targetField: auditTime
func: now_datetime
- stepNo: 3
desc: 前端传的 auditorId 赋值给订单的 auditorId 字段
instruction:
type: SET_FIELD
params:
targetField: auditorId
source: input.auditorId
- stepNo: 4
desc: 用 auditorId 去员工聚合查审核人姓名,回填 auditorName
instruction:
type: REMOTE_QUERY_SET_FIELD
params:
remoteAggregate: EmployeeAggregate
queryKey: input.auditorId
sourceField: employeeName
targetField: auditorName
- stepNo: 5
desc: 把审核备注赋值给 auditRemark
instruction:
type: SET_FIELD
params:
targetField: auditRemark
source: input.auditRemark
- stepNo: 6
desc: 审核通过置 PASS,驳回置 REJECT
instruction:
type: CONDITION_SET_FIELD
params:
targetField: orderStatus
conditions:
- when: 'input.auditResult == "PASS"'
value: "PASS"
- when: 'input.auditResult == "REJECT"'
value: "REJECT"
# —— 命令级 bizSteps 现支持 IF 判断分支 + 跨聚合写回(类比 M4 flowSteps 的 condition)——
# 审核人(员工)统计随审核结果通用回写;when 走 Aviator,root.=订单现值 / input.=命令入参。
- stepNo: 7
desc: 订单金额 > 100 时,把本次审核备注覆盖写入审核人的备注字段(金额不足则不写)
instruction:
type: IF
params:
when: 'root.totalAmount > 100'
then:
- instruction:
type: REMOTE_SET_FIELD
params:
remoteAggregate: EmployeeAggregate
keySource: input.auditorId
targetField: remark
source: input.auditRemark
else: []
- stepNo: 8
desc: 审核通过(PASS)时,审核人累计通过数 +1
instruction:
type: IF
params:
when: 'input.auditResult == "PASS"'
then:
- instruction:
type: REMOTE_INCREMENT_FIELD
params:
remoteAggregate: EmployeeAggregate
keySource: input.auditorId
targetField: auditApproveCount
amount: 1
- stepNo: 9
desc: 审核驳回(REJECT)时,审核人累计驳回数 +1
instruction:
type: IF
params:
when: 'input.auditResult == "REJECT"'
then:
- instruction:
type: REMOTE_INCREMENT_FIELD
params:
remoteAggregate: EmployeeAggregate
keySource: input.auditorId
targetField: auditRejectCount
amount: 1
- stepNo: 10
desc: 记录审核人“上次审核时间”为本次审核时间
instruction:
type: REMOTE_SET_FIELD
params:
remoteAggregate: EmployeeAggregate
keySource: input.auditorId
targetField: lastAuditTime
source: root.auditTime
- stepNo: 11
desc: 统一提交数据库事务
instruction:
type: COMMIT_TRANSACTION
params: {}
emitEvents:
- OrderAudited
# 4.员工聚合行为(“新增/修改员工”场景;亦为 AuditOrder 跨聚合回写的目标聚合根)
- aggregateId: EmployeeAggregate
aggregateRoot: Employee
commands:
- cmdId: CreateEmployee
desc: 新增员工主数据(审核统计计数初始化为 0)
commandType: create
inputParams:
- name: employeeName
type: string
- name: department
type: string
validations:
- employeeName 非空
# bizSteps:把两个审核计数初始化为 0(后续 REMOTE_INCREMENT_FIELD 才能在非空值上累加),再落库
bizSteps:
- stepNo: 1
desc: 审核通过数初始化 0
instruction:
type: SET_FIELD
params:
targetField: auditApproveCount
value: 0
- stepNo: 2
desc: 审核驳回数初始化 0
instruction:
type: SET_FIELD
params:
targetField: auditRejectCount
value: 0
- stepNo: 3
desc: 落库新员工
instruction:
type: COMMIT_TRANSACTION
params: {}
emitEvents:
- EmployeeCreated
- cmdId: ModifyEmployee
desc: 修改员工姓名、部门、备注
commandType: update
inputParams:
- name: employeeId
type: string
- name: employeeName
type: string
optional: true
- name: department
type: string
optional: true
- name: remark
type: string
optional: true
validations:
- employeeId 必须存在
bizSteps:
- stepNo: 1
desc: 按 employeeId 加载既有员工聚合根(不存在即失败)
instruction:
type: LOAD_AGGREGATE
params:
idSource: input.employeeId
- stepNo: 2
desc: 更新命令携带的字段后提交事务
instruction:
type: COMMIT_TRANSACTION
params: {}
emitEvents:
- EmployeeModified
globalBehaviorConstraints:
- 命令只能操作所属聚合内部对象,跨聚合仅允许调用对方聚合根暴露Command,禁止直接操作对方内部实体
- 单个命令事务边界仅限当前聚合;跨聚合操作拆为分布式事件最终一致性
- 子实体只能被所属聚合根命令修改,外部无法直接操作
- 所有跨聚合依赖仅基于对方聚合根ID做校验,不依赖内部字段