{"id":2300,"date":"2026-05-01T06:16:52","date_gmt":"2026-04-30T22:16:52","guid":{"rendered":"https:\/\/www.appblog.cn\/index.php\/2026\/05\/01\/2300\/"},"modified":"2026-05-01T07:45:31","modified_gmt":"2026-04-30T23:45:31","slug":"workflow-framework-survey-payment-system-manual-approval-flow","status":"publish","type":"post","link":"https:\/\/www.appblog.cn\/index.php\/2026\/05\/01\/workflow-framework-survey-payment-system-manual-approval-flow\/","title":{"rendered":"\u5de5\u4f5c\u6d41\u6846\u67b6\u8c03\u7814\u6587\u6863 &#8211; \u652f\u4ed8\u7cfb\u7edf\u4eba\u5de5\u5ba1\u6279\u6d41\u6280\u672f\u9009\u578b\u53c2\u8003"},"content":{"rendered":"<p># \u5de5\u4f5c\u6d41\u6846\u67b6\u8c03\u7814\u6587\u6863<\/p>\n<p>> \u652f\u4ed8\u7cfb\u7edf\u4eba\u5de5\u5ba1\u6279\u6d41\u6280\u672f\u9009\u578b\u53c2\u8003<\/p>\n<p>&#8212;<\/p>\n<p>## \u76ee\u5f55<\/p>\n<p>1. [Spring State Machine \u7b80\u4ecb](#1-spring-state-machine-\u7b80\u4ecb)<br \/>\n2. [LiteFlow \u7b80\u4ecb](#2-liteflow-\u7b80\u4ecb)<br \/>\n3. [\u81ea\u5b9a\u4e49\u5ba1\u6279\u6d41\u65b9\u6848\uff08\u63a8\u8350\uff09](#3-\u81ea\u5b9a\u4e49\u5ba1\u6279\u6d41\u65b9\u6848\u63a8\u8350)<br \/>\n4. [\u65b9\u6848\u5bf9\u6bd4\u4e0e\u9009\u578b\u5efa\u8bae](#4-\u65b9\u6848\u5bf9\u6bd4\u4e0e\u9009\u578b\u5efa\u8bae)<\/p>\n<p>&#8212;<\/p>\n<p>## 1. Spring State Machine \u7b80\u4ecb<\/p>\n<p>Spring State Machine \u662f Spring \u5b98\u65b9\u51fa\u7684\u72b6\u6001\u673a\u6846\u67b6\uff0c\u57fa\u4e8e Spring Framework\uff0c\u7528\u4e8e\u6784\u5efa\u72b6\u6001\u673a\u5e94\u7528\u3002<\/p>\n<p>### \u6838\u5fc3\u6982\u5ff5<\/p>\n<p>**State (\u72b6\u6001) + Event (\u4e8b\u4ef6) + Transition (\u8f6c\u6362)**<\/p>\n<p>&#8211; **State**: \u72b6\u6001\uff0c\u5982 PENDING\u3001APPROVED<br \/>\n&#8211; **Event**: \u4e8b\u4ef6\uff0c\u5982 SUBMIT\u3001APPROVE<br \/>\n&#8211; **Transition**: \u72b6\u6001\u8f6c\u6362\uff0cA &#8211;Event&#8211;> B<br \/>\n&#8211; **Guard**: \u6761\u4ef6\u5224\u65ad\uff0c\u51b3\u5b9a\u662f\u5426\u80fd\u8f6c\u6362<br \/>\n&#8211; **Action**: \u8f6c\u6362\u65f6\u6267\u884c\u7684\u52a8\u4f5c<br \/>\n&#8211; **StateMachine**: \u72b6\u6001\u673a\u5b9e\u4f8b<\/p>\n<p>### \u4e09\u79cd\u6301\u4e45\u5316\u65b9\u5f0f<\/p>\n<p>| \u65b9\u5f0f | \u8bf4\u660e |<br \/>\n|&#8212;&#8212;|&#8212;&#8212;|<br \/>\n| Ephemeral | \u5185\u5b58\uff0c\u91cd\u542f\u4e22\u5931\uff08\u6d4b\u8bd5\u7528\uff09 |<br \/>\n| StateMachinePersist | \u81ea\u5b9a\u4e49\u6301\u4e45\u5316\uff08Redis\u3001DB\uff09 |<br \/>\n| JpaPersistingStateMachineListener | JPA \u6301\u4e45\u5316\uff08\u5b98\u65b9\u63a8\u8350\uff09 |<\/p>\n<p>### \u5feb\u901f\u793a\u4f8b<\/p>\n<p>**\u4f9d\u8d56\uff1a**<\/p>\n<p>&#8220;`xml<br \/>\n<dependency><br \/>\n    <groupId>org.springframework.statemachine<\/groupId><br \/>\n    <artifactId>spring-statemachine-starter<\/artifactId><br \/>\n    <version>4.0.0<\/version><br \/>\n<\/dependency><br \/>\n&#8220;`<\/p>\n<p>**1. \u5b9a\u4e49\u72b6\u6001\u548c\u4e8b\u4ef6\uff1a**<\/p>\n<p>&#8220;`java<br \/>\npublic enum PaymentState {<br \/>\n    PENDING, RISK_CHECK, MANUAL_REVIEW, APPROVED, REJECTED<br \/>\n}<\/p>\n<p>public enum PaymentEvent {<br \/>\n    SUBMIT, RISK_PASS, RISK_REJECT, NEED_MANUAL, APPROVE, REJECT<br \/>\n}<br \/>\n&#8220;`<\/p>\n<p>**2. \u914d\u7f6e\u72b6\u6001\u673a\uff1a**<\/p>\n<p>&#8220;`java<br \/>\n@Configuration<br \/>\n@EnableStateMachine<br \/>\npublic class PaymentStateConfig extends StateMachineConfigurerAdapter<PaymentState, PaymentEvent> {<\/p>\n<p>    @Override<br \/>\n    public void configure(StateMachineStateConfigurer<PaymentState, PaymentEvent> states) throws Exception {<br \/>\n        states<br \/>\n            .withStates()<br \/>\n            .initial(PaymentState.PENDING)<br \/>\n            .state(PaymentState.RISK_CHECK)<br \/>\n            .state(PaymentState.MANUAL_REVIEW)<br \/>\n            .end(PaymentState.APPROVED)<br \/>\n            .end(PaymentState.REJECTED);<br \/>\n    }<\/p>\n<p>    @Override<br \/>\n    public void configure(StateMachineTransitionConfigurer<PaymentState, PaymentEvent> transitions) throws Exception {<br \/>\n        transitions<br \/>\n            .withExternal().source(PaymentState.PENDING).target(PaymentState.RISK_CHECK)<br \/>\n                .event(PaymentEvent.SUBMIT)<br \/>\n                .action(ctx -> log.info(&#8220;\u63d0\u4ea4\u98ce\u63a7&#8221;))<br \/>\n            .and()<br \/>\n            .withExternal().source(PaymentState.RISK_CHECK).target(PaymentState.APPROVED)<br \/>\n                .event(PaymentEvent.RISK_PASS)<br \/>\n            .and()<br \/>\n            .withExternal().source(PaymentState.RISK_CHECK).target(PaymentState.MANUAL_REVIEW)<br \/>\n                .event(PaymentEvent.NEED_MANUAL)<br \/>\n            .and()<br \/>\n            .withExternal().source(PaymentState.RISK_CHECK).target(PaymentState.REJECTED)<br \/>\n                .event(PaymentEvent.RISK_REJECT)<br \/>\n            .and()<br \/>\n            .withExternal().source(PaymentState.MANUAL_REVIEW).target(PaymentState.APPROVED)<br \/>\n                .event(PaymentEvent.APPROVE)<br \/>\n            .and()<br \/>\n            .withExternal().source(PaymentState.MANUAL_REVIEW).target(PaymentState.REJECTED)<br \/>\n                .event(PaymentEvent.REJECT);<br \/>\n    }<br \/>\n}<br \/>\n&#8220;`<\/p>\n<p>**3. \u4f7f\u7528\u72b6\u6001\u673a\uff1a**<\/p>\n<p>&#8220;`java<br \/>\n@Service<br \/>\n@RequiredArgsConstructor<br \/>\npublic class PaymentService {<br \/>\n    private final StateMachine<PaymentState, PaymentEvent> stateMachine;<\/p>\n<p>    public void submitPayment(Long paymentId) {<br \/>\n        stateMachine.start();<br \/>\n        Message<PaymentEvent> message = MessageBuilder<br \/>\n            .withPayload(PaymentEvent.SUBMIT)<br \/>\n            .setHeader(&#8220;paymentId&#8221;, paymentId)<br \/>\n            .build();<br \/>\n        stateMachine.sendEvent(message);<br \/>\n        log.info(&#8220;\u5f53\u524d\u72b6\u6001: {}&#8221;, stateMachine.getState().getId());<br \/>\n    }<\/p>\n<p>    public void riskCheckComplete(Long paymentId, boolean pass) {<br \/>\n        PaymentEvent event = pass ? PaymentEvent.RISK_PASS : PaymentEvent.RISK_REJECT;<br \/>\n        stateMachine.sendEvent(event);<br \/>\n    }<br \/>\n}<br \/>\n&#8220;`<\/p>\n<p>### \u6ce8\u610f\u4e8b\u9879<\/p>\n<p>| \u95ee\u9898 | \u8bf4\u660e |<br \/>\n|&#8212;&#8212;|&#8212;&#8212;|<br \/>\n| \u6027\u80fd | \u5355\u673a\u5355\u5b9e\u4f8b\uff0c\u9ad8\u5e76\u53d1\u5efa\u8bae\u7528 Redis \u5206\u5e03\u5f0f\u9501 |<br \/>\n| \u6301\u4e45\u5316 | \u9ed8\u8ba4\u5185\u5b58\uff0c\u9700\u81ea\u884c\u5b9e\u73b0 StateMachinePersist \u6301\u4e45\u5316 |<br \/>\n| \u91cd\u8bd5 | \u5185\u7f6e RetryInterceptor\uff0c\u9700\u989d\u5916\u914d\u7f6e |<br \/>\n| \u8c03\u8bd5 | \u63d0\u4f9b @WithStateMachine \u6d4b\u8bd5\u652f\u6301 |<\/p>\n<p>### vs \u81ea\u5b9a\u4e49\u72b6\u6001\u673a<\/p>\n<p>| \u7ef4\u5ea6 | \u81ea\u5b9a\u4e49 | Spring State Machine |<br \/>\n|&#8212;&#8212;|&#8212;&#8212;&#8211;|&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;|<br \/>\n| \u4ee3\u7801\u91cf | \u5c11 | \u4e2d |<br \/>\n| \u53ef\u89c6\u5316\u72b6\u6001\u56fe | \u274c | \u2705\uff08Spring State Machine Visualizer\uff09 |<br \/>\n| \u6301\u4e45\u5316 | \u81ea\u884c\u5b9e\u73b0 | \u5185\u7f6e\u652f\u6301\uff08\u9700\u914d\u7f6e\uff09 |<br \/>\n| Guard\/Action | \u81ea\u884c\u5b9e\u73b0 | \u5185\u7f6e |<br \/>\n| \u5b66\u4e60\u6210\u672c | \u4f4e | \u4e2d |<\/p>\n<p>&#8212;<\/p>\n<p>## 2. LiteFlow \u7b80\u4ecb<\/p>\n<p>LiteFlow \u662f\u56fd\u4ea7\u7684\u89c4\u5219\u5f15\u64ce\u6846\u67b6\uff08\u7531 Yomahub \u51fa\u54c1\uff09\uff0c\u4e3b\u6253\u8f7b\u91cf + \u9ad8\u6027\u80fd\uff0c\u9002\u7528\u4e8e\u590d\u6742\u4e1a\u52a1\u89c4\u5219\u8def\u7531\uff0c\u800c\u975e\u4f20\u7edf\u5ba1\u6279\u6d41\u3002<\/p>\n<p>### \u5b9a\u4f4d<\/p>\n<p>| \u7ef4\u5ea6 | \u8bf4\u660e |<br \/>\n|&#8212;&#8212;|&#8212;&#8212;|<br \/>\n| \u5b9a\u4f4d | \u89c4\u5219\u5f15\u64ce \/ \u6d41\u7a0b\u7f16\u6392 |<br \/>\n| \u9002\u7528\u573a\u666f | \u89c4\u5219\u8def\u7531\u3001\u51b3\u7b56\u6811\u3001\u4e1a\u52a1\u89c4\u5219\u7f16\u6392 |<br \/>\n| \u4e0d\u64c5\u957f | \u9700\u8981\u4eba\u5de5\u5ba1\u6279\u3001\u591a\u5b9e\u4f8b\u534f\u4f5c\u7684\u957f\u6d41\u7a0b |<br \/>\n| \u8bb8\u53ef\u8bc1 | Apache 2.0\uff08\u514d\u8d39\uff09 |<\/p>\n<p>### \u6838\u5fc3\u6982\u5ff5<\/p>\n<p>**Chain (\u94fe) = Node (\u8282\u70b9) + Node (\u8282\u70b9) + Node (\u8282\u70b9)**<\/p>\n<p>&#8211; **SWITCH(x)**: \u6761\u4ef6\u5206\u652f\uff0c\u7c7b\u4f3c if\/else<br \/>\n&#8211; **WHEN(\u5e76\u884c)**: \u591a\u8282\u70b9\u5e76\u884c\u6267\u884c<br \/>\n&#8211; **THEN(\u4e32\u884c)**: \u4f9d\u6b21\u6267\u884c<br \/>\n&#8211; **BREAK**: \u7ec8\u6b62\u6d41\u7a0b<\/p>\n<p>### \u5feb\u901f\u793a\u4f8b<\/p>\n<p>**\u4f9d\u8d56\uff1a**<\/p>\n<p>&#8220;`xml<br \/>\n<dependency><br \/>\n    <groupId>com.yomahub<\/groupId><br \/>\n    <artifactId>liteflow-spring-boot-starter<\/artifactId><br \/>\n    <version>2.11.0<\/version><br \/>\n<\/dependency><br \/>\n&#8220;`<\/p>\n<p>**1. \u5b9a\u4e49\u6d41\u7a0b\u89c4\u5219\uff08flow.xml\uff09\uff1a**<\/p>\n<p>&#8220;`xml<br \/>\n<?xml version=\"1.0\" encoding=\"UTF-8\"?><br \/>\n<flow><br \/>\n    <chain name=\"paymentRoute\"><br \/>\n        THEN(<br \/>\n            &#8220;riskCheckNode&#8221;,<br \/>\n            SWITCH($payment.getType())<br \/>\n                .CASE(&#8220;NORMAL&#8221;, &#8220;normalPayNode&#8221;)<br \/>\n                .CASE(&#8220;HIGH_RISK&#8221;, &#8220;highRiskPayNode&#8221;)<br \/>\n                .CASE(&#8220;VIP&#8221;, &#8220;vipPayNode&#8221;),<br \/>\n            &#8220;notifyNode&#8221;<br \/>\n        );<br \/>\n    <\/chain><br \/>\n<\/flow><br \/>\n&#8220;`<\/p>\n<p>**2. \u5b9a\u4e49\u8282\u70b9\uff08Java\uff09\uff1a**<\/p>\n<p>&#8220;`java<br \/>\n@Component<br \/>\npublic class RiskCheckNode extends NodeComponent {<br \/>\n    @Override<br \/>\n    public void process() {<br \/>\n        Payment payment = this.getContextData(Payment.class);<\/p>\n<p>        if (blacklistService.isBlacklisted(payment.getUserId())) {<br \/>\n            this.setNextNodeId(&#8220;rejectNode&#8221;);<br \/>\n            return;<br \/>\n        }<\/p>\n<p>        int riskScore = riskService.calculateScore(payment);<br \/>\n        payment.setRiskScore(riskScore);<\/p>\n<p>        if (riskScore > 80) {<br \/>\n            this.setNextNodeId(&#8220;highRiskPayNode&#8221;);<br \/>\n        }<br \/>\n    }<br \/>\n}<br \/>\n&#8220;`<\/p>\n<p>**3. \u6267\u884c\u6d41\u7a0b\uff1a**<\/p>\n<p>&#8220;`java<br \/>\n@Service<br \/>\n@RequiredArgsConstructor<br \/>\npublic class PaymentService {<br \/>\n    private final FlowExecutor flowExecutor;<\/p>\n<p>    public PaymentResult routePayment(Payment payment) {<br \/>\n        DefaultContext context = new DefaultContext();<br \/>\n        context.set(&#8220;payment&#8221;, payment);<\/p>\n<p>        LiteFlowResponse response = flowExecutor.execute2Resp(<br \/>\n            &#8220;paymentRoute&#8221;, context<br \/>\n        );<\/p>\n<p>        if (response.isSuccess()) {<br \/>\n            return (PaymentResult) response.getContextData(&#8220;result&#8221;);<br \/>\n        } else {<br \/>\n            throw new PaymentException(&#8220;\u8def\u7531\u5931\u8d25&#8221;);<br \/>\n        }<br \/>\n    }<br \/>\n}<br \/>\n&#8220;`<\/p>\n<p>### vs Spring State Machine<\/p>\n<p>| \u7ef4\u5ea6 | LiteFlow | Spring State Machine |<br \/>\n|&#8212;&#8212;|&#8212;&#8212;&#8212;-|&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;|<br \/>\n| \u8bbe\u8ba1\u76ee\u6807 | \u89c4\u5219\u8def\u7531\u7f16\u6392 | \u72b6\u6001\u673a |<br \/>\n| \u914d\u7f6e\u65b9\u5f0f | XML\/YML\uff08\u53ef\u89c6\u5316\u53cb\u597d\uff09 | Java \u4ee3\u7801 |<br \/>\n| \u9002\u7528\u573a\u666f | \u89c4\u5219\u5f15\u64ce\u3001\u8def\u7531\u51b3\u7b56 | \u72b6\u6001\u6d41\u8f6c\u3001\u5ba1\u6279\u6d41 |<br \/>\n| \u4eba\u5de5\u5e72\u9884 | \u274c \u4e0d\u652f\u6301 | \u2705 \u652f\u6301\u4efb\u52a1\u9886\u53d6 |<br \/>\n| \u5e76\u884c\u6267\u884c | \u2705 WHEN \u539f\u751f\u652f\u6301 | \u26a0\ufe0f \u9700\u989d\u5916\u914d\u7f6e |<br \/>\n| \u6027\u80fd | \u9ad8\uff08\u65e0\u72b6\u6001\u8bbe\u8ba1\uff09 | \u4e2d |<br \/>\n| \u72b6\u6001\u6301\u4e45\u5316 | \u274c \u65e0\u5185\u7f6e | \u2705 \u53ef\u914d\u7f6e |<\/p>\n<p>### \u5178\u578b\u4f7f\u7528\u573a\u666f<\/p>\n<p>&#8211; **\u652f\u4ed8\u8def\u7531**: \u6839\u636e\u91d1\u989d\u3001\u7528\u6237\u7c7b\u578b\u3001\u98ce\u63a7\u5206\u6570\u9009\u62e9\u4e0d\u540c\u652f\u4ed8\u901a\u9053<br \/>\n&#8211; **\u98ce\u63a7\u51b3\u7b56**: \u591a\u89c4\u5219\u5e76\u884c\u68c0\u67e5\uff0c\u6700\u7ec8\u51b3\u7b56<br \/>\n&#8211; **\u8425\u9500\u89c4\u5219**: \u4f18\u60e0\u5238\u3001\u6298\u6263\u3001\u6ee1\u51cf\u7b49\u89c4\u5219\u7f16\u6392<br \/>\n&#8211; **\u8ba2\u5355\u5904\u7406**: \u6839\u636e\u8ba2\u5355\u7c7b\u578b\u3001\u5730\u533a\u3001\u5546\u54c1\u7c7b\u578b\u8d70\u4e0d\u540c\u5904\u7406\u6d41\u7a0b<\/p>\n<p>### \u603b\u7ed3<\/p>\n<p>**LiteFlow = \u89c4\u5219\u5f15\u64ce \u2260 \u5ba1\u6279\u6d41\u5f15\u64ce**<\/p>\n<p>\u5982\u679c\u4f60\u7684\u9700\u6c42\u662f\u6839\u636e\u6761\u4ef6\u9009\u62e9\u4e0d\u540c\u7684\u5904\u7406\u903b\u8f91\uff08\u8def\u7531\u3001\u51b3\u7b56\u3001\u89c4\u5219\u7f16\u6392\uff09\uff0cLiteFlow \u975e\u5e38\u5408\u9002\u3002<\/p>\n<p>\u5982\u679c\u4f60\u7684\u9700\u6c42\u662f\u9700\u8981\u4eba\u5de5\u5ba1\u6279\u3001\u591a\u6b65\u9aa4\u6d41\u8f6c\u3001\u4efb\u52a1\u7ba1\u7406\uff0cSpring State Machine \u6216\u81ea\u5b9a\u4e49\u72b6\u6001\u673a\u66f4\u5408\u9002\u3002<\/p>\n<p>&#8212;<\/p>\n<p>## 3. \u81ea\u5b9a\u4e49\u5ba1\u6279\u6d41\u65b9\u6848\uff08\u63a8\u8350\uff09<\/p>\n<p>\u9488\u5bf9\u4eba\u5de5\u5ba1\u6279 + \u591a\u6b65\u9aa4\u6d41\u8f6c\u573a\u666f\u7684\u81ea\u5b9a\u4e49\u8f7b\u91cf\u7ea7\u89e3\u51b3\u65b9\u6848\u3002<\/p>\n<p>### \u8bbe\u8ba1\u601d\u8def<\/p>\n<p>&#8220;`<br \/>\n\u250c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2510<br \/>\n\u2502                   \u5ba1\u6279\u6d41\u6838\u5fc3\u6a21\u578b                       \u2502<br \/>\n\u2502                                                      \u2502<br \/>\n\u2502  WorkflowDef (\u6d41\u7a0b\u5b9a\u4e49)                               \u2502<br \/>\n\u2502     \u2502                                                 \u2502<br \/>\n\u2502     \u251c\u2500\u2500\u25ba WorkflowStep (\u6b65\u9aa4\u5b9a\u4e49)                      \u2502<br \/>\n\u2502     \u2502       \u2502                                        \u2502<br \/>\n\u2502     \u2502       \u2514\u2500\u2500\u25ba 1. \u6b65\u9aa4\u987a\u5e8f                          \u2502<br \/>\n\u2502     \u2502       \u2514\u2500\u2500\u25ba 2. \u5ba1\u6279\u8005\u7c7b\u578b\uff08\u89d2\u8272\/\u7528\u6237\uff09            \u2502<br \/>\n\u2502     \u2502       \u2514\u2500\u2500\u25ba 3. \u901a\u8fc7\u6761\u4ef6                          \u2502<br \/>\n\u2502     \u2502                                                 \u2502<br \/>\n\u2502  WorkflowInstance (\u6d41\u7a0b\u5b9e\u4f8b) \u2190 \u4e1a\u52a1\u8868\u5173\u8054              \u2502<br \/>\n\u2502     \u2502                                                 \u2502<br \/>\n\u2502     \u251c\u2500\u2500\u25ba Task (\u5f85\u529e\u4efb\u52a1) \u2190 \u4eba\u5de5\u5ba1\u6279\u5165\u53e3                \u2502<br \/>\n\u2502     \u2502                                                 \u2502<br \/>\n\u2502     \u2514\u2500\u2500\u25ba TaskHistory (\u5ba1\u6279\u5386\u53f2) \u2190 \u5ba1\u8ba1\u8bb0\u5f55            \u2502<br \/>\n\u2514\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2518<br \/>\n&#8220;`<\/p>\n<p>### \u6838\u5fc3\u8868\u7ed3\u6784<\/p>\n<p>&#8220;`sql<br \/>\n&#8212; \u6d41\u7a0b\u5b9a\u4e49\u8868<br \/>\nCREATE TABLE workflow_def (<br \/>\n    id BIGINT PRIMARY KEY AUTO_INCREMENT,<br \/>\n    workflow_code VARCHAR(64) NOT NULL UNIQUE,  &#8212; &#8216;merchant_audit&#8217;<br \/>\n    name VARCHAR(128),                            &#8212; &#8216;\u5546\u6237\u5ba1\u6838\u6d41\u7a0b&#8217;<br \/>\n    version INT DEFAULT 1,<br \/>\n    status TINYINT DEFAULT 1,                     &#8212; 1\u542f\u7528 0\u7981\u7528<br \/>\n    created_at DATETIME,<br \/>\n    updated_at DATETIME<br \/>\n);<\/p>\n<p>&#8212; \u6b65\u9aa4\u5b9a\u4e49\u8868<br \/>\nCREATE TABLE workflow_step (<br \/>\n    id BIGINT PRIMARY KEY AUTO_INCREMENT,<br \/>\n    workflow_def_id BIGINT NOT NULL,<br \/>\n    step_code VARCHAR(64),                        &#8212; &#8216;risk_check&#8217;<br \/>\n    step_name VARCHAR(128),                        &#8212; &#8216;\u98ce\u63a7\u5ba1\u6838&#8217;<br \/>\n    step_order INT NOT NULL,                       &#8212; \u987a\u5e8f<br \/>\n    approver_type VARCHAR(32),                     &#8212; &#8216;ROLE&#8217; \/ &#8216;USER&#8217; \/ &#8216;EXPR&#8217;<br \/>\n    approver_value VARCHAR(256),                   &#8212; &#8216;risk_team&#8217; \/ &#8216;user_123&#8217; \/ &#8216;${initiator}&#8217;<br \/>\n    pass_condition VARCHAR(64),                    &#8212; &#8216;ALL_PASS&#8217; \/ &#8216;ONE_PASS&#8217;<br \/>\n    created_at DATETIME<br \/>\n);<\/p>\n<p>&#8212; \u6d41\u7a0b\u5b9e\u4f8b\u8868\uff08\u7ed1\u5b9a\u4e1a\u52a1\uff09<br \/>\nCREATE TABLE workflow_instance (<br \/>\n    id BIGINT PRIMARY KEY AUTO_INCREMENT,<br \/>\n    workflow_def_id BIGINT NOT NULL,<br \/>\n    biz_type VARCHAR(64),                         &#8212; &#8216;merchant&#8217;<br \/>\n    biz_id BIGINT NOT NULL,                       &#8212; merchant_audit.id<br \/>\n    initiator VARCHAR(64),                        &#8212; \u53d1\u8d77\u4eba<br \/>\n    current_step INT DEFAULT 1,                    &#8212; \u5f53\u524d\u6b65\u9aa4<br \/>\n    status VARCHAR(32),                            &#8212; RUNNING \/ APPROVED \/ REJECTED<br \/>\n    created_at DATETIME,<br \/>\n    updated_at DATETIME<br \/>\n);<\/p>\n<p>&#8212; \u5f85\u529e\u4efb\u52a1\u8868<br \/>\nCREATE TABLE workflow_task (<br \/>\n    id BIGINT PRIMARY KEY AUTO_INCREMENT,<br \/>\n    instance_id BIGINT NOT NULL,<br \/>\n    step_code VARCHAR(64),<br \/>\n    assignee VARCHAR(64),                          &#8212; \u5ba1\u6279\u4eba<br \/>\n    status VARCHAR(32),                           &#8212; PENDING \/ COMPLETED \/ REJECTED<br \/>\n    result VARCHAR(32),                           &#8212; PASS \/ REJECT<br \/>\n    comment TEXT,                                 &#8212; \u5ba1\u6279\u610f\u89c1<br \/>\n    created_at DATETIME,<br \/>\n    completed_at DATETIME<br \/>\n);<\/p>\n<p>&#8212; \u5ba1\u6279\u5386\u53f2\u8868<br \/>\nCREATE TABLE workflow_history (<br \/>\n    id BIGINT PRIMARY KEY AUTO_INCREMENT,<br \/>\n    instance_id BIGINT NOT NULL,<br \/>\n    step_code VARCHAR(64),<br \/>\n    operator VARCHAR(64),<br \/>\n    action VARCHAR(32),                           &#8212; SUBMIT \/ APPROVE \/ REJECT<br \/>\n    comment TEXT,<br \/>\n    created_at DATETIME<br \/>\n);<br \/>\n&#8220;`<\/p>\n<p>### \u6838\u5fc3\u4ee3\u7801<\/p>\n<p>**\u6d41\u7a0b\u72b6\u6001\u679a\u4e3e\uff1a**<\/p>\n<p>&#8220;`java<br \/>\npublic enum WorkflowStatus {<br \/>\n    PENDING,     \/\/ \u5f85\u5ba1\u6279<br \/>\n    RUNNING,     \/\/ \u5ba1\u6279\u4e2d<br \/>\n    PASSED,      \/\/ \u5ba1\u6279\u901a\u8fc7<br \/>\n    REJECTED,    \/\/ \u5ba1\u6279\u62d2\u7edd<br \/>\n    CANCELLED    \/\/ \u5df2\u53d6\u6d88<br \/>\n}<br \/>\n&#8220;`<\/p>\n<p>**\u6d41\u7a0b\u670d\u52a1\uff1a**<\/p>\n<p>&#8220;`java<br \/>\n@Service<br \/>\n@Slf4j<br \/>\npublic class WorkflowService {<\/p>\n<p>    @Autowired private WorkflowDefMapper workflowDefMapper;<br \/>\n    @Autowired private WorkflowStepMapper stepMapper;<br \/>\n    @Autowired private WorkflowInstanceMapper instanceMapper;<br \/>\n    @Autowired private WorkflowTaskMapper taskMapper;<br \/>\n    @Autowired private WorkflowHistoryMapper historyMapper;<\/p>\n<p>    \/** \u542f\u52a8\u6d41\u7a0b *\/<br \/>\n    public Long startWorkflow(String workflowCode, String bizType, Long bizId, String initiator) {<br \/>\n        \/\/ 1. \u83b7\u53d6\u6d41\u7a0b\u5b9a\u4e49<br \/>\n        WorkflowDef def = workflowDefMapper.findByCode(workflowCode);<br \/>\n        List<WorkflowStep> steps = stepMapper.findByDefId(def.getId());<\/p>\n<p>        \/\/ 2. \u521b\u5efa\u6d41\u7a0b\u5b9e\u4f8b<br \/>\n        WorkflowInstance instance = new WorkflowInstance();<br \/>\n        instance.setWorkflowDefId(def.getId());<br \/>\n        instance.setBizType(bizType);<br \/>\n        instance.setBizId(bizId);<br \/>\n        instance.setInitiator(initiator);<br \/>\n        instance.setCurrentStep(1);<br \/>\n        instance.setStatus(WorkflowStatus.RUNNING.name());<br \/>\n        instanceMapper.insert(instance);<\/p>\n<p>        \/\/ 3. \u521b\u5efa\u7b2c\u4e00\u4e2a\u4efb\u52a1<br \/>\n        createTask(instance, steps.get(0), initiator);<\/p>\n<p>        \/\/ 4. \u8bb0\u5f55\u5386\u53f2<br \/>\n        saveHistory(instance, null, &#8220;SUBMIT&#8221;, initiator);<\/p>\n<p>        log.info(&#8220;\u542f\u52a8\u6d41\u7a0b\u5b9e\u4f8b: {}, \u4e1a\u52a1: {}:{}&#8221;, workflowCode, bizType, bizId);<br \/>\n        return instance.getId();<br \/>\n    }<\/p>\n<p>    \/** \u5ba1\u6279\u4efb\u52a1 *\/<br \/>\n    public void approve(Long taskId, String operator, String result, String comment) {<br \/>\n        WorkflowTask task = taskMapper.findById(taskId);<br \/>\n        WorkflowInstance instance = instanceMapper.findById(task.getInstanceId());<br \/>\n        List<WorkflowStep> steps = stepMapper.findByDefId(instance.getWorkflowDefId());<\/p>\n<p>        \/\/ 1. \u5b8c\u6210\u5f53\u524d\u4efb\u52a1<br \/>\n        task.setStatus(&#8220;COMPLETED&#8221;);<br \/>\n        task.setResult(result);<br \/>\n        task.setComment(comment);<br \/>\n        task.setCompletedAt(LocalDateTime.now());<br \/>\n        taskMapper.update(task);<\/p>\n<p>        \/\/ 2. \u8bb0\u5f55\u5386\u53f2<br \/>\n        saveHistory(instance, task.getStepCode(), result, operator);<\/p>\n<p>        \/\/ 3. \u5224\u65ad\u662f\u5426\u901a\u8fc7<br \/>\n        WorkflowStep currentStep = steps.get(instance.getCurrentStep() &#8211; 1);<br \/>\n        boolean stepPass = &#8220;PASS&#8221;.equals(result);<\/p>\n<p>        if (!stepPass) {<br \/>\n            \/\/ \u6b65\u9aa4\u672a\u901a\u8fc7\uff0c\u6d41\u7a0b\u7ed3\u675f<br \/>\n            instance.setStatus(WorkflowStatus.REJECTED.name());<br \/>\n            instanceMapper.update(instance);<br \/>\n            log.info(&#8220;\u6d41\u7a0b\u62d2\u7edd: {}&#8221;, instance.getId());<br \/>\n            return;<br \/>\n        }<\/p>\n<p>        \/\/ 4. \u68c0\u67e5\u662f\u5426\u8fd8\u6709\u4e0b\u4e00\u6b65<br \/>\n        int nextStepIndex = instance.getCurrentStep();<br \/>\n        if (nextStepIndex >= steps.size()) {<br \/>\n            \/\/ \u6d41\u7a0b\u5b8c\u6210<br \/>\n            instance.setStatus(WorkflowStatus.PASSED.name());<br \/>\n            instanceMapper.update(instance);<br \/>\n            log.info(&#8220;\u6d41\u7a0b\u901a\u8fc7: {}&#8221;, instance.getId());<br \/>\n            return;<br \/>\n        }<\/p>\n<p>        \/\/ 5. \u8fdb\u5165\u4e0b\u4e00\u6b65<br \/>\n        instance.setCurrentStep(nextStepIndex + 1);<br \/>\n        instanceMapper.update(instance);<\/p>\n<p>        \/\/ 6. \u521b\u5efa\u4e0b\u4e00\u6b65\u4efb\u52a1<br \/>\n        WorkflowStep nextStep = steps.get(nextStepIndex);<br \/>\n        String nextAssignee = resolveAssignee(nextStep, instance);<br \/>\n        createTask(instance, nextStep, nextAssignee);<\/p>\n<p>        log.info(&#8220;\u6d41\u7a0b\u6d41\u8f6c\u81f3\u6b65\u9aa4: {}, \u4efb\u52a1\u5206\u914d\u7ed9: {}&#8221;, nextStep.getStepCode(), nextAssignee);<br \/>\n    }<\/p>\n<p>    \/** \u67e5\u8be2\u5f85\u529e\u4efb\u52a1 *\/<br \/>\n    public List<WorkflowTask> getPendingTasks(String assignee) {<br \/>\n        return taskMapper.findPendingByAssignee(assignee);<br \/>\n    }<\/p>\n<p>    private void createTask(WorkflowInstance instance, WorkflowStep step, String assignee) {<br \/>\n        WorkflowTask task = new WorkflowTask();<br \/>\n        task.setInstanceId(instance.getId());<br \/>\n        task.setStepCode(step.getStepCode());<br \/>\n        task.setAssignee(assignee);<br \/>\n        task.setStatus(&#8220;PENDING&#8221;);<br \/>\n        task.setCreatedAt(LocalDateTime.now());<br \/>\n        taskMapper.insert(task);<br \/>\n    }<\/p>\n<p>    private String resolveAssignee(WorkflowStep step, WorkflowInstance instance) {<br \/>\n        if (&#8220;INITIATOR&#8221;.equals(step.getApproverValue())) {<br \/>\n            return instance.getInitiator();<br \/>\n        }<br \/>\n        if (&#8220;EXPR&#8221;.equals(step.getApproverType())) {<br \/>\n            \/\/ \u52a8\u6001\u8868\u8fbe\u5f0f\uff0c\u5982 ${initiator} \u6216 ${amount > 10000 ? &#8216;admin&#8217; : &#8216;user&#8217;}<br \/>\n            return evaluateExpression(step.getApproverValue(), instance);<br \/>\n        }<br \/>\n        return step.getApproverValue();<br \/>\n    }<\/p>\n<p>    private void saveHistory(WorkflowInstance instance, String stepCode, String action, String operator) {<br \/>\n        WorkflowHistory history = new WorkflowHistory();<br \/>\n        history.setInstanceId(instance.getId());<br \/>\n        history.setStepCode(stepCode);<br \/>\n        history.setAction(action);<br \/>\n        history.setOperator(operator);<br \/>\n        history.setCreatedAt(LocalDateTime.now());<br \/>\n        historyMapper.insert(history);<br \/>\n    }<br \/>\n}<br \/>\n&#8220;`<\/p>\n<p>**\u4f7f\u7528\u793a\u4f8b\uff1a**<\/p>\n<p>&#8220;`java<br \/>\n@Service<br \/>\n@RequiredArgsConstructor<br \/>\npublic class MerchantAuditService {<\/p>\n<p>    private final WorkflowService workflowService;<\/p>\n<p>    \/** \u5546\u6237\u63d0\u4ea4\u5ba1\u6838 *\/<br \/>\n    public void submitAudit(MerchantAudit audit) {<br \/>\n        auditService.save(audit);<br \/>\n        workflowService.startWorkflow(&#8220;merchant_audit&#8221;, &#8220;merchant_audit&#8221;, audit.getId(), audit.getApplicant());<br \/>\n    }<\/p>\n<p>    \/** \u5ba1\u6279\u901a\u8fc7 *\/<br \/>\n    public void approve(Long taskId, String operator) {<br \/>\n        workflowService.approve(taskId, operator, &#8220;PASS&#8221;, &#8220;\u540c\u610f&#8221;);<br \/>\n    }<\/p>\n<p>    \/** \u5ba1\u6279\u62d2\u7edd *\/<br \/>\n    public void reject(Long taskId, String operator, String reason) {<br \/>\n        workflowService.approve(taskId, operator, &#8220;REJECT&#8221;, reason);<br \/>\n    }<\/p>\n<p>    \/** \u67e5\u8be2\u5f85\u5ba1\u6279\u5217\u8868 *\/<br \/>\n    public List<WorkflowTask> getMyTasks(String operator) {<br \/>\n        return workflowService.getPendingTasks(operator);<br \/>\n    }<br \/>\n}<br \/>\n&#8220;`<\/p>\n<p>### \u4f18\u70b9<\/p>\n<p>| \u4f18\u70b9 | \u8bf4\u660e |<br \/>\n|&#8212;&#8212;|&#8212;&#8212;|<br \/>\n| **\u96f6\u4f9d\u8d56** | \u4ec5 MySQL\uff0c\u65e0\u989d\u5916\u6846\u67b6 |<br \/>\n| **\u5b8c\u5168\u53ef\u63a7** | \u4ee3\u7801\u5373\u6587\u6863\uff0c\u53ef\u81ea\u884c\u6269\u5c55 |<br \/>\n| **\u8f7b\u91cf** | \u6838\u5fc3\u4ee3\u7801 < 300 \u884c |\n| **\u7075\u6d3b** | \u53ef\u52a0\u4f1a\u7b7e\u3001\u77e5\u4f1a\u3001\u52a0\u7b7e\u3001\u59d4\u6258\u7b49 |\n| **\u53ef\u89c2\u6d4b** | \u5b8c\u6574\u7684\u5ba1\u6279\u5386\u53f2\u8bb0\u5f55 |\n\n### \u53ef\u6269\u5c55\u529f\u80fd\n\n| \u529f\u80fd | \u96be\u5ea6 | \u8bf4\u660e |\n|------|------|------|\n| \u4f1a\u7b7e\uff08\u591a\u4eba\u5ba1\u6279\uff09 | \u2b50\u2b50 | \u540c\u4e00\u4efb\u52a1\u5206\u914d\u591a\u4eba\uff0c\u9700\u5168\u90e8\u901a\u8fc7 |\n| \u77e5\u4f1a\uff08\u975e\u5ba1\u6279\u4eba\uff09 | \u2b50 | \u53ea\u901a\u77e5\uff0c\u4e0d\u963b\u585e\u6d41\u7a0b |\n| \u52a0\u7b7e | \u2b50\u2b50 | \u5ba1\u6279\u4e2d\u8ffd\u52a0\u5ba1\u6279\u4eba |\n| \u59d4\u6258 | \u2b50 | \u5c06\u4efb\u52a1\u8f6c\u7ed9\u4ed6\u4eba |\n| \u50ac\u529e | \u2b50 | \u5b9a\u65f6\u63d0\u9192\u672a\u5ba1\u6279\u4efb\u52a1 |\n| \u9a73\u56de | \u2b50\u2b50 | \u9a73\u56de\u5230\u6307\u5b9a\u6b65\u9aa4 |\n\n---\n\n## 4. \u65b9\u6848\u5bf9\u6bd4\u4e0e\u9009\u578b\u5efa\u8bae\n\n### \u65b9\u6848\u5bf9\u6bd4\n\n| \u65b9\u6848 | \u4eba\u5de5\u5ba1\u6279 | \u591a\u6b65\u9aa4 | \u590d\u6742\u5ea6 | \u63a8\u8350\u573a\u666f | \u63a8\u8350\u5ea6 |\n|------|:--------:|:------:|--------|----------|:------:|\n| **\u81ea\u5b9a\u4e49\u72b6\u6001\u673a + \u4efb\u52a1\u8868** | \u2705 | \u2705 | \u4f4e | \u7b80\u5355\u6d41\u7a0b\uff08\u22645\u72b6\u6001\uff09 | \u2b50\u2b50\u2b50\u2b50\u2b50 |\n| **Spring State Machine** | \u2705 | \u2705 | \u4e2d | \u4e2d\u7b49\u6d41\u7a0b\uff0c\u6709\u6301\u4e45\u5316\u9700\u6c42 | \u2b50\u2b50\u2b50 |\n| **LiteFlow** | \u274c | \u26a0\ufe0f | \u4e2d | \u89c4\u5219\u8def\u7531\u3001\u51b3\u7b56\u6811 | \u2b50\u2b50 |\n| **Camunda 7** | \u2705 | \u2705 | \u9ad8 | \u590d\u6742\u4f01\u4e1a\u6d41\u7a0b | \u2b50\u2b50 |\n| **Activiti** | \u2705 | \u2705 | \u9ad8 | \u590d\u6742\u4f01\u4e1a\u6d41\u7a0b | \u2b50 |\n\n### \u9488\u5bf9\u652f\u4ed8\u7cfb\u7edf\u7684\u5efa\u8bae\n\n\u9488\u5bf9\u652f\u4ed8\u7cfb\u7edf\uff08\u98ce\u63a7\u5ba1\u6838\u3001\u5165\u8d26\u786e\u8ba4\uff09\u573a\u666f\uff0c\u63a8\u8350\u4f7f\u7528\u81ea\u5b9a\u4e49\u5ba1\u6279\u6d41\u65b9\u6848\uff1a\n\n| \u573a\u666f | \u63a8\u8350\u65b9\u6848 |\n|------|----------|\n| \u5546\u6237\u5ba1\u6838\uff08\u7b80\u5355\u591a\u7ea7\u5ba1\u6279\uff09 | \u81ea\u5b9a\u4e49\u72b6\u6001\u673a + \u5ba1\u6279\u4efb\u52a1\u8868 |\n| \u8ba2\u5355\u72b6\u6001\u6d41\u8f6c | \u679a\u4e3e + \u72b6\u6001\u673a |\n| \u9700\u8981\u6301\u4e45\u5316\/\u91cd\u542f\u6062\u590d | Spring State Machine + JPA |\n| \u8d39\u7387\/\u9650\u989d\u89c4\u5219\u8def\u7531 | LiteFlow |\n\n### \u4e0b\u4e00\u6b65\n\n\u5982\u9700\u8fdb\u4e00\u6b65\u96c6\u6210\u5230\u9879\u76ee\u4e2d\uff0c\u53ef\u63d0\u4f9b\u4ee5\u4e0b\u652f\u6301\uff1a\n\n- [ ] \u5b8c\u6574\u7684\u6570\u636e\u5e93\u8868\u7ed3\u6784\uff08SQL \u811a\u672c\uff09\n- [ ] Mapper\u3001Service\u3001Controller \u4ee3\u7801\u751f\u6210\n- [ ] \u5de5\u4f5c\u6d41\u5b9a\u4e49\u914d\u7f6e\uff08\u98ce\u63a7\u5ba1\u6838\u3001\u4eba\u5de5\u590d\u6838\u7b49\uff09\n- [ ] REST API \u63a5\u53e3\u8bbe\u8ba1\n\n---\n\n*\u6587\u6863\u751f\u6210\u65f6\u95f4: 2026-04-30*\n\n<\/p>\n","protected":false},"excerpt":{"rendered":"<p># \u5de5\u4f5c\u6d41\u6846\u67b6\u8c03\u7814\u6587\u6863 > \u652f\u4ed8\u7cfb\u7edf\u4eba\u5de5\u5ba1\u6279\u6d41\u6280\u672f\u9009\u578b\u53c2\u8003 &#8212; ## \u76ee\u5f55 1. [Spring  [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-2300","post","type-post","status-publish","format-standard","hentry","category-java"],"_links":{"self":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/2300","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/comments?post=2300"}],"version-history":[{"count":0,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/2300\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/media?parent=2300"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/categories?post=2300"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/tags?post=2300"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}