Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion AGENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

## 项目概述

Flow Engine 是一个企业级工作流引擎,基于 Java 17 和 Spring Boot 3.5.9 构建(当前版本 0.1.0-SNAPSHOT)。
Flow Engine 是一个企业级工作流引擎,基于 Java 17 和 Spring Boot 3.5.9 构建(当前版本 0.3.0-SNAPSHOT)。

### 核心模块

Expand Down
1 change: 1 addition & 0 deletions docs/Integration/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
|------|------|
| [快速开始](./quick-start.md) | 最小集成步骤:引入依赖 → 提供 Bean → 编写流程 → 发起流程 |
| [流程用户体系](./user-integration.md) | `FlowOperatorGateway` / `IFlowOperator` / `GatewayContext` / 线程缓存 / 当前登录人 |
| [多身份参与](./operator-identity.md) | 同一人员多身份:身份 SPI、请求声明、待办隔离、判定规则、升级 DDL |
| [流程默认脚本](./script-integration.md) | 12 种脚本类型、`GroovyScriptRequest` 完整 API、`$bind`、默认脚本替换、脚本生命周期 |
| [流程事件机制](./event-integration.md) | 9 种事件、`EventPusher`、`IHandler` 订阅、异步分发管道、事务变体、推送时机 |
| [仓储抽象与持久化](./repository-integration.md) | `IRepositoryHolder`、12 个仓储接口(含 11 个 JPA 实现 + 需自行实现的 `NodeViewJavaScriptRepository`)、脚本仓储、锁机制 |
Expand Down
305 changes: 305 additions & 0 deletions docs/Integration/operator-identity.md

Large diffs are not rendered by default.

85 changes: 85 additions & 0 deletions docs/Integration/sql/0.3.0-operator-identity.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
-- =============================================================================
-- flow-engine 0.3.0 升级脚本:操作者身份标识(issue #288)
-- =============================================================================
-- 背景:
-- 同一人员可同时隶属多个部门/岗位,以不同「身份」参与同一个流程。
-- 0.3.0 起框架在记录与待办上保存身份标识(不解释语义,由下游定义取值)。
--
-- 兼容性:
-- 1. 仅新增 nullable 列,不修改、不删除任何既有列,旧版本程序不映射新列,可安全回滚。
-- 2. 历史数据的新列为 NULL,引擎按「null 与空串等价 = 默认身份」读取,无需数据回填。
-- 3. 待办合并键在默认身份下保持历史格式,仅非空身份追加 "#identity" 后缀,
-- 升级瞬间仍在运行的流程不会出现待办重复或合并关系断裂。
--
-- 使用方式:
-- - 使用 ddl-auto=update 的环境会自动加列,本脚本用于「手工管理表结构」的生产环境;
-- - 索引请按实际数据量与慢查询情况选择性执行(见文末)。
-- =============================================================================


-- -----------------------------------------------------------------------------
-- 1. 流程记录表:发起 / 提交 / 当前 / 转交 / 干预 五组操作者的身份
-- -----------------------------------------------------------------------------
ALTER TABLE t_flow_record ADD COLUMN create_operator_identity VARCHAR(64) NULL;
ALTER TABLE t_flow_record ADD COLUMN submit_operator_identity VARCHAR(64) NULL;
ALTER TABLE t_flow_record ADD COLUMN current_operator_identity VARCHAR(64) NULL;
ALTER TABLE t_flow_record ADD COLUMN forward_operator_identity VARCHAR(64) NULL;
ALTER TABLE t_flow_record ADD COLUMN interfered_operator_identity VARCHAR(64) NULL;


-- -----------------------------------------------------------------------------
-- 2. 待办表:发起 / 提交 / 当前 三组操作者的身份
-- -----------------------------------------------------------------------------
ALTER TABLE t_flow_todo_record ADD COLUMN create_operator_identity VARCHAR(64) NULL;
ALTER TABLE t_flow_todo_record ADD COLUMN submit_operator_identity VARCHAR(64) NULL;
ALTER TABLE t_flow_todo_record ADD COLUMN current_operator_identity VARCHAR(64) NULL;


-- -----------------------------------------------------------------------------
-- 2.1 人员分配表:无需改表(issue #288 衍生 / #292)
-- -----------------------------------------------------------------------------
-- 「发起人设定 / 审批人设定」节点的分配关系存在 t_flow_operator_assignment.operator_ids (JSON) 中。
-- 身份编码在该 JSON 内部完成,无需新增列、无需回填:
-- 全部默认身份 -> [1,2,3] (保持历史格式)
-- 存在指定身份 -> [{"operatorId":2,"identity":"department1"}]
-- 读取端同时兼容两种格式;存量环境不执行本段语句即可正常升级。


-- -----------------------------------------------------------------------------
-- 3. 索引(可选,按需执行)
-- -----------------------------------------------------------------------------
-- 3.1 待办列表查询:where current_operator_id = ? and coalesce(current_operator_identity,'') = ?
-- 数据量超过 100 万的待办表建议加此联合索引;小表可暂不加。
-- 注意:coalesce 会导致该列无法命中普通索引,因此索引列顺序把 operator_id 放前面,
-- 由 operator_id 做范围收敛,身份列做回表过滤。
-- CREATE INDEX idx_todo_operator_identity
-- ON t_flow_todo_record (current_operator_id, current_operator_identity);

-- 3.2 已办 / 抄送列表查询(同上,命中 t_flow_record)
-- CREATE INDEX idx_record_operator_identity
-- ON t_flow_record (current_operator_id, current_operator_identity, record_state);


-- -----------------------------------------------------------------------------
-- 4. 达梦(DM)环境补充说明
-- -----------------------------------------------------------------------------
-- 达梦上 Hibernate 的 ddl-auto=update 对【加列】是可靠的(仅新增 nullable 列,不涉及序列),
-- 但生产环境仍建议按本脚本手工执行。达梦语法与上文一致,字段类型可用 VARCHAR(64);
-- 若字符集配置为 UTF-8,VARCHAR(64) 即 64 个字符,足以容纳「部门id」「部门id:岗位编码」等复合身份。
-- 达梦不支持 `coalesce` 走索引,索引策略同上(operator_id 在前)。


-- -----------------------------------------------------------------------------
-- 5. 升级后自检(可选)
-- -----------------------------------------------------------------------------
-- 4.1 身份列是否都已创建(不同数据库查询 information_schema 的写法略有差异)
-- SELECT table_name, column_name
-- FROM information_schema.columns
-- WHERE column_name LIKE '%operator_identity'
-- ORDER BY table_name, column_name;

-- 4.2 待办合并键格式抽查:默认身份应为 "id-runtime-node",非空身份应带 "#identity" 后缀
-- SELECT current_operator_id, current_operator_identity, todo_key
-- FROM t_flow_todo_record
-- ORDER BY id DESC
-- LIMIT 20;
8 changes: 8 additions & 0 deletions docs/Integration/user-integration.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,20 @@ package com.codingapi.flow.gateway;
/** 流程操作者防腐层 */
public interface FlowOperatorGateway {
IFlowOperator get(long id);

/** 按 (id, identity) 取用户,default 退化为 get(id);多身份场景需覆盖(见「多身份参与」) */
default IFlowOperator get(long id, String identity) {
return get(id);
}

List<IFlowOperator> findByIds(List<Long> ids);
}
```

引擎所有「按 id 取用户 / 批量取用户」的请求都汇聚到这个接口。**框架不提供默认实现**(`flow-engine-starter-infra` 也不提供),必须由业务方提供一个 Spring Bean。

> 同一人员以多个身份参与流程时(例如同时隶属两个部门),需要覆盖 `get(long, String)`,详见 [多身份参与](./operator-identity.md)。

### 1.2 `IFlowOperator`(流程操作人)

文件:`flow-engine-framework/src/main/java/com/codingapi/flow/operator/IFlowOperator.java`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import com.codingapi.springboot.script.annotation.ScriptType;
import jakarta.persistence.*;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.springframework.security.crypto.password.PasswordEncoder;

import java.util.List;
Expand Down Expand Up @@ -37,6 +38,19 @@ public class User implements IFlowOperator {
private String account;
private String password;

/**
* 当前身份标识(issue #288)。
*
* <p>同一个人员可能隶属多个部门/岗位,以不同身份参与流程。身份是**运行期选择**而非用户主数据,
* 因此不落库({@link Transient});由 {@code FlowOperatorGateway#get(id, identity)} 按身份返回
* 携带该标识的用户副本,示例前端通过请求参数 {@code operatorIdentity} 声明当前身份。
* 不参与 {@code equals/hashCode},避免同一 id 的两个身份副本被判定为不同用户。
*/
@Transient
@EqualsAndHashCode.Exclude
@ScriptField(name = "identity", description = "当前身份标识")
private String identity;


public static User admin(PasswordEncoder passwordEncoder) {
User user = new User();
Expand Down Expand Up @@ -75,6 +89,36 @@ public boolean isFlowManager() {
return flowManager!=null && flowManager ;
}

/**
* 当前身份标识,空串表示默认身份(issue #288)。
*/
@Override
@ScriptFunction(name = "getIdentity", description = "获取当前身份标识")
public String getIdentity() {
return identity;
}

/**
* 返回携带指定身份的副本。
*
* <p>必须复制而不是直接改写实体:JPA 返回的是持久化上下文中的共享实例,
* 直接写入 {@code identity} 会让同一 id 的两个身份互相覆盖(缓存串味)。
*
* @param identity 身份标识
* @return 带身份的副本
*/
public User withIdentity(String identity) {
User copy = new User();
copy.id = this.id;
copy.name = this.name;
copy.flowManager = this.flowManager;
copy.flowOperatorId = this.flowOperatorId;
copy.account = this.account;
copy.password = this.password;
copy.identity = identity;
return copy;
}

@Override
public IFlowOperator forwardOperator(GroovyScriptRequest request) {
if (flowOperatorId != null && flowOperatorId > 0) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package com.codingapi.example.gateway.impl;

import com.codingapi.example.entity.User;
import com.codingapi.example.repository.UserRepository;
import com.codingapi.flow.gateway.FlowOperatorGateway;
import com.codingapi.flow.operator.IFlowOperator;
import com.codingapi.flow.operator.OperatorIdentity;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Repository;

Expand All @@ -19,6 +21,22 @@ public IFlowOperator get(long id) {
return userRepository.getUserById(id);
}

/**
* 按身份获取操作者(issue #288)。
*
* <p>示例组织模型里身份就是「以哪个部门/岗位的身份参与流程」,不属于用户主数据,
* 因此这里返回携带身份标识的**用户副本**,而不改写持久化上下文中的实体实例。
* 真实项目应把身份替换为自己的组织模型(例如按部门 id 查询岗位任职关系)。
*/
@Override
public IFlowOperator get(long id, String identity) {
if (OperatorIdentity.isDefault(identity)) {
return get(id);
}
User user = userRepository.getUserById(id);
return user == null ? null : user.withIdentity(OperatorIdentity.normalize(identity));
}

@Override
public List<IFlowOperator> findByIds(List<Long> ids) {
return userRepository.findUserByIdIn(ids)
Expand All @@ -27,4 +45,22 @@ public List<IFlowOperator> findByIds(List<Long> ids) {
.toList();
}

/**
* 按统一身份批量获取操作者(issue #288 衍生 / #293)。
*
* <p>批量重载同样必须返回携带身份的副本:只覆盖单条 {@code get(id, identity)} 时,
* 流程树、人员设定等批量路径会把身份退化为默认身份。
*/
@Override
public List<IFlowOperator> findByIds(List<Long> ids, String identity) {
if (OperatorIdentity.isDefault(identity)) {
return findByIds(ids);
}
String normalized = OperatorIdentity.normalize(identity);
return userRepository.findUserByIdIn(ids)
.stream()
.map(user -> (IFlowOperator) user.withIdentity(normalized))
.toList();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,18 @@
import lombok.AllArgsConstructor;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.core.annotation.Order;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

/**
* 内置管理员账号初始化。
*
* <p>必须早于其它依赖 admin 的数据初始化执行(例如 {@code IdentityDemoInitializer} 用它作为流程创建人),
* 因此显式声明 {@link Order};未声明时优先级最低,会在其它 Initializer 之后执行。
*/
@Service
@Order(10)
@AllArgsConstructor
public class AdminInitializer implements ApplicationRunner {

Expand Down
Loading
Loading