# 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做校验,不依赖内部字段