diff --git a/docs/Integration/operator-identity.md b/docs/Integration/operator-identity.md index 62901d3e..316487d0 100644 --- a/docs/Integration/operator-identity.md +++ b/docs/Integration/operator-identity.md @@ -249,9 +249,11 @@ def run(request) { | 表 | 新增列 | |----|--------| -| `t_flow_record` | `create_operator_identity`、`submit_operator_identity`、`current_operator_identity`、`forward_operator_identity`、`interfered_operator_identity` | +| `t_flow_record` | `create_operator_identity`、`submit_operator_identity`、`current_operator_identity`、`forward_operator_identity`、`interfered_operator_identity`、`handle_operator_identity` | | `t_flow_todo_record` | `create_operator_identity`、`submit_operator_identity`、`current_operator_identity` | +「办理人」另有两列非身份字段(issue #306):`handle_operator_id`、`handle_operator_name`,与 `handle_operator_identity` 一起记录**谁以什么身份办理了这条记录**。待办表不加这三列——待办尚未办理,没有办理人语义。 + 只存身份标识本身,**不落库任何附加属性**(避免为 5 组操作者各加一个 JSON 快照列,也避免身份名称被冻结在历史数据里)。展示名称仍取操作者姓名;需要展示身份名称时由宿主按 `identity` 自行映射。 升级脚本见 [sql/0.3.0-operator-identity.sql](./sql/0.3.0-operator-identity.sql)。 @@ -311,7 +313,23 @@ def run(request) { `OperatorManager`、操作者线程缓存、网关缓存的 `(userId, identity)` 键仍保持严格匹配——它们解决的是「取到哪一个带身份的操作者实例」,不是权限问题。 -留痕口径:被办理记录保留其归属身份(`currentOperatorIdentity`,由取人脚本决定),本次操作携带的身份写入下游新生成记录的 `submitOperatorIdentity`;`USER_ONLY` 下同人换身份不算「被干预」,因此不会写 `interferedOperator*`。 +留痕口径(`USER_ONLY` 下同人换身份不算「被干预」,因此不会写 `interferedOperator*`): + +| 字段 | 语义 | 写入时机 | +|------|------|----------| +| `currentOperatorId/Name/Identity` | 这条待办的**归属**人(由取人脚本决定) | 记录生成时;办结后**不变** | +| `submitOperatorId/Name/Identity` | **创建这条记录时**的上游提交人 | 记录生成时 | +| `handleOperatorId/Name/Identity` | **本次办理人**——谁、以什么身份办的 | 办结动作(通过 / 拒绝 / 退回 / 转办 / 委派);暂存与抄送刷新不写 | + +因此「办结」不会改写归属身份,只额外留下办理人三元组。 + +> **issue #306 之前的行为**:办理动作不写任何身份字段,最后一步办理人身份**无处落地**——没有下游记录可以承载 +> `submitOperator*`,于是「张三以 `department2` 审批通过」在库里只能看到 `department1`(待办归属)或默认身份。 +> 中间节点只能跳一条到下游记录的 `submitOperatorIdentity` 间接推断,且要求下游是同一个人。 +> 升级后请以 `handleOperator*` 作为「已办是谁办的」的唯一口径。 + +`FlowRecord#markHandleOperator(IFlowOperator)` 由办结动作显式调用,**不在 `update()` 内隐式写入**:`update()` 同时服务于 +暂存(`SaveAction`,`pass=false`)与抄送刷新(`notifyRecord`),隐式写入会把暂存人、抄送接收人误记成办理人。 ### 4.5 身份写入约束 @@ -353,6 +371,7 @@ codingapi.flow.identity.match-mode=USER_ONLY | 身份长度 | ≤ 64 字符(复合 key 用 `:` 拼接),入口与落库双处校验 | | 待办索引 | 实体未声明 `@Index`,大表请按 DDL 脚本手工加索引 | | 默认取人脚本 | issue #303 起新建节点的默认脚本返回 `OperatorKey`(创建者以发起身份参与);存量流程定义中的旧脚本不受影响,不迁移 | +| 办理人留痕 | issue #306 起 `handle_operator_*` 记录办理人与办理身份;存量记录为空,前端回退到 `current_operator_identity` 展示,行为与升级前一致;如需回填,按 `finish_time` 与下游 `submit_operator_*` 推断,引擎不做自动迁移 | | 不做的事 | 引擎不建组织模型、不做身份授权校验(「user1 能否以 department1 身份操作」由业务侧保证)、不冻结身份名称 | > 身份是一个**不透明字符串**,不要尝试在引擎里比较它的业务含义;需要「部门名称」等展示信息,由宿主维护 `identity → 名称` 的映射自行渲染(框架不提供附加属性字段)。 diff --git a/docs/Integration/sql/0.3.0-operator-identity.sql b/docs/Integration/sql/0.3.0-operator-identity.sql index 7a207227..e0b4b3ac 100644 --- a/docs/Integration/sql/0.3.0-operator-identity.sql +++ b/docs/Integration/sql/0.3.0-operator-identity.sql @@ -27,6 +27,18 @@ ALTER TABLE t_flow_record ADD COLUMN forward_operator_identity VARCHAR(64) NU ALTER TABLE t_flow_record ADD COLUMN interfered_operator_identity VARCHAR(64) NULL; +-- ----------------------------------------------------------------------------- +-- 1.1 流程记录表:办理人三列(issue #306) +-- ----------------------------------------------------------------------------- +-- 背景:此前记录被办结时不写任何身份字段,current_operator_identity 保留的是「待办归属身份」, +-- submit_operator_identity 只表示「创建该记录时的上游提交人」,最后一步办理人身份因此无处可落。 +-- 语义:handle_operator_* = 本次实际办理人的 (id, 名称, 身份),暂存与抄送刷新不写入。 +-- 兼容:与 current_operator_identity 并存、互不覆盖;历史数据为 NULL,前端可回退到 current_operator_identity。 +ALTER TABLE t_flow_record ADD COLUMN handle_operator_id BIGINT NULL; +ALTER TABLE t_flow_record ADD COLUMN handle_operator_name VARCHAR(255) NULL; +ALTER TABLE t_flow_record ADD COLUMN handle_operator_identity VARCHAR(64) NULL; + + -- ----------------------------------------------------------------------------- -- 2. 待办表:发起 / 提交 / 当前 三组操作者的身份 -- ----------------------------------------------------------------------------- diff --git a/flow-engine-framework/src/main/java/com/codingapi/flow/action/actions/DelegateAction.java b/flow-engine-framework/src/main/java/com/codingapi/flow/action/actions/DelegateAction.java index ccbd99f2..ecd87247 100644 --- a/flow-engine-framework/src/main/java/com/codingapi/flow/action/actions/DelegateAction.java +++ b/flow-engine-framework/src/main/java/com/codingapi/flow/action/actions/DelegateAction.java @@ -73,6 +73,7 @@ public void run(FlowSession flowSession) { FlowRecord currentRecord = flowSession.getCurrentRecord(); currentRecord.update(flowSession, true); + currentRecord.markHandleOperator(flowSession.getCurrentOperator()); recordList.add(currentRecord); flowEvents.add(new FlowRecordDoneEvent(currentRecord, flowSession.isMock())); diff --git a/flow-engine-framework/src/main/java/com/codingapi/flow/action/actions/PassAction.java b/flow-engine-framework/src/main/java/com/codingapi/flow/action/actions/PassAction.java index bca7e0c9..e39830c4 100644 --- a/flow-engine-framework/src/main/java/com/codingapi/flow/action/actions/PassAction.java +++ b/flow-engine-framework/src/main/java/com/codingapi/flow/action/actions/PassAction.java @@ -141,6 +141,7 @@ public void run(FlowSession flowSession) { boolean isFinish = currentNode.isFinish(flowSession); currentRecord.update(flowSession, true); + currentRecord.markHandleOperator(flowSession.getCurrentOperator()); // 添加流程结束事件 flowEvents.add(new FlowRecordDoneEvent(currentRecord, flowSession.isMock())); recordList.add(currentRecord); diff --git a/flow-engine-framework/src/main/java/com/codingapi/flow/action/actions/RejectAction.java b/flow-engine-framework/src/main/java/com/codingapi/flow/action/actions/RejectAction.java index ca7db511..5c06bfd7 100644 --- a/flow-engine-framework/src/main/java/com/codingapi/flow/action/actions/RejectAction.java +++ b/flow-engine-framework/src/main/java/com/codingapi/flow/action/actions/RejectAction.java @@ -100,6 +100,7 @@ public void run(FlowSession flowSession) { FlowRecord flowRecord = flowSession.getCurrentRecord(); flowRecord.update(flowSession, true); + flowRecord.markHandleOperator(flowSession.getCurrentOperator()); recordList.add(flowRecord); // 当前审批为已办状态 diff --git a/flow-engine-framework/src/main/java/com/codingapi/flow/action/actions/ReturnAction.java b/flow-engine-framework/src/main/java/com/codingapi/flow/action/actions/ReturnAction.java index 268cda80..14195421 100644 --- a/flow-engine-framework/src/main/java/com/codingapi/flow/action/actions/ReturnAction.java +++ b/flow-engine-framework/src/main/java/com/codingapi/flow/action/actions/ReturnAction.java @@ -54,6 +54,7 @@ public void run(FlowSession flowSession) { FlowRecord currentRecord = flowSession.getCurrentRecord(); currentRecord.update(flowSession, true); + currentRecord.markHandleOperator(flowSession.getCurrentOperator()); recordList.add(currentRecord); flowEvents.add(new FlowRecordDoneEvent(currentRecord,flowSession.isMock())); diff --git a/flow-engine-framework/src/main/java/com/codingapi/flow/action/actions/TransferAction.java b/flow-engine-framework/src/main/java/com/codingapi/flow/action/actions/TransferAction.java index 8d2a085b..0eec025e 100644 --- a/flow-engine-framework/src/main/java/com/codingapi/flow/action/actions/TransferAction.java +++ b/flow-engine-framework/src/main/java/com/codingapi/flow/action/actions/TransferAction.java @@ -82,6 +82,7 @@ public void run(FlowSession flowSession) { FlowRecord currentRecord = flowSession.getCurrentRecord(); currentRecord.update(flowSession, true); + currentRecord.markHandleOperator(flowSession.getCurrentOperator()); recordList.add(currentRecord); flowEvents.add(new FlowRecordDoneEvent(currentRecord,flowSession.isMock())); diff --git a/flow-engine-framework/src/main/java/com/codingapi/flow/pojo/response/FlowRecordContent.java b/flow-engine-framework/src/main/java/com/codingapi/flow/pojo/response/FlowRecordContent.java index c0e85522..38c5b5e4 100644 --- a/flow-engine-framework/src/main/java/com/codingapi/flow/pojo/response/FlowRecordContent.java +++ b/flow-engine-framework/src/main/java/com/codingapi/flow/pojo/response/FlowRecordContent.java @@ -100,6 +100,21 @@ public class FlowRecordContent { */ private String createOperatorIdentity; + /** + * 办理人Id(issue #306),记录办结时的实际办理人 + */ + private Long handleOperatorId; + + /** + * 办理人名称(issue #306) + */ + private String handleOperatorName; + + /** + * 办理人身份标识(issue #306),空串表示默认身份 + */ + private String handleOperatorIdentity; + /** * 合并记录id,当存在合并记录数据时待办记录数量不会增加,内容会更新至最新的待办数据信息。 * {@link FlowRecord#getTodoKey()} @@ -161,6 +176,9 @@ public static FlowRecordContent convert(FlowRecord record){ content.setCreateOperatorId(record.getCreateOperatorId()); content.setCreateOperatorName(record.getCreateOperatorName()); content.setCreateOperatorIdentity(OperatorIdentity.normalize(record.getCreateOperatorIdentity())); + content.setHandleOperatorId(record.getHandleOperatorId()); + content.setHandleOperatorName(record.getHandleOperatorName()); + content.setHandleOperatorIdentity(OperatorIdentity.normalize(record.getHandleOperatorIdentity())); content.setMergeable(record.isMergeable()); content.setRecordId(record.getId()); content.setTimeoutTime(record.getTimeoutTime()); diff --git a/flow-engine-framework/src/main/java/com/codingapi/flow/record/FlowRecord.java b/flow-engine-framework/src/main/java/com/codingapi/flow/record/FlowRecord.java index ebd6cd91..785886a3 100644 --- a/flow-engine-framework/src/main/java/com/codingapi/flow/record/FlowRecord.java +++ b/flow-engine-framework/src/main/java/com/codingapi/flow/record/FlowRecord.java @@ -303,6 +303,25 @@ public class FlowRecord { */ private String interferedOperatorIdentity; + /** + * 办理人Id(issue #306) + *

记录被办结时,本次实际办理人的用户 id;暂存与抄送刷新不写入。 + */ + private long handleOperatorId; + + /** + * 办理人名称(issue #306) + */ + private String handleOperatorName; + + /** + * 办理人身份标识(issue #306) + *

本次办理时使用的身份,空串表示默认身份。与「待办归属身份」 + * {@link FlowRecord#currentOperatorIdentity} 互不覆盖:办结不改变待办归属, + * 只额外留下「谁以什么身份办的」。 + */ + private String handleOperatorIdentity; + /** * 数据合并的依据,当开启时值为固定值,否则为随机数据 *

合并依据由 {@link RecordMergeStrategy.MergeType} 决定: @@ -401,6 +420,7 @@ public FlowRecord(FlowSession flowSession, int nodeOrder) { // 身份字段统一以空串兜底,避免出现 null 与空串两种「默认身份」表达 this.forwardOperatorIdentity = OperatorIdentity.DEFAULT; this.interferedOperatorIdentity = OperatorIdentity.DEFAULT; + this.handleOperatorIdentity = OperatorIdentity.DEFAULT; // 是否转交之后的人来审批的判断 if (currentOperator.equals(sourceOperator)) { this.forwardOperatorId = 0; @@ -648,6 +668,28 @@ public void update(FlowSession flowSession, boolean pass) { } } + /** + * 记录本次办理人(issue #306)。 + * + *

必须由「真正办结」的动作显式调用(通过 / 拒绝 / 退回 / 转办 / 委派)。 + * 暂存({@link com.codingapi.flow.action.actions.SaveAction})与抄送刷新 + * ({@link #notifyRecord(com.codingapi.flow.session.FlowSession)})只是刷新记录内容、 + * 并未产生办理结果,因此不能写入,否则会把暂存人、抄送接收人误记为办理人。 + * + *

不修改 {@link #currentOperatorId} / {@link #currentOperatorIdentity}: + * 待办归属身份在办结后仍需保留(见 docs/Integration/operator-identity.md §4.4)。 + * + * @param operator 本次实际办理人 + */ + public void markHandleOperator(IFlowOperator operator) { + if (operator == null) { + return; + } + this.handleOperatorId = operator.getUserId(); + this.handleOperatorName = operator.getName(); + this.handleOperatorIdentity = OperatorIdentity.of(operator); + } + public void clearNotify(){ this.readable = false; this.readTime = 0; @@ -818,6 +860,18 @@ public void newRecord() { this.setAdvice(null); this.setSignKey(null); this.setActionId(null); + // 新记录尚未被办理,不能继承上游记录的办理人信息(issue #306) + this.clearHandleOperator(); + } + + /** + * 清空办理人信息(issue #306)。 + *

记录回到「未办理」状态时调用,避免复用记录对象时把上一位办理人带到新的待办上。 + */ + public void clearHandleOperator() { + this.handleOperatorId = 0; + this.handleOperatorName = null; + this.handleOperatorIdentity = OperatorIdentity.DEFAULT; } /** diff --git a/flow-engine-framework/src/test/java/com/codingapi/flow/repository/CopyOnReadFlowRecordRepository.java b/flow-engine-framework/src/test/java/com/codingapi/flow/repository/CopyOnReadFlowRecordRepository.java index 2ee38d02..0d527738 100644 --- a/flow-engine-framework/src/test/java/com/codingapi/flow/repository/CopyOnReadFlowRecordRepository.java +++ b/flow-engine-framework/src/test/java/com/codingapi/flow/repository/CopyOnReadFlowRecordRepository.java @@ -147,6 +147,9 @@ private static FlowRecord copy(FlowRecord record) { record.getSubmitOperatorIdentity(), record.getCurrentOperatorIdentity(), record.getForwardOperatorIdentity(), - record.getInterferedOperatorIdentity()); + record.getInterferedOperatorIdentity(), + record.getHandleOperatorId(), + record.getHandleOperatorName(), + record.getHandleOperatorIdentity()); } } \ No newline at end of file diff --git a/flow-engine-framework/src/test/java/com/codingapi/flow/service/HandleOperatorIdentityFlowTest.java b/flow-engine-framework/src/test/java/com/codingapi/flow/service/HandleOperatorIdentityFlowTest.java new file mode 100644 index 00000000..72649123 --- /dev/null +++ b/flow-engine-framework/src/test/java/com/codingapi/flow/service/HandleOperatorIdentityFlowTest.java @@ -0,0 +1,290 @@ +package com.codingapi.flow.service; + +import com.codingapi.flow.action.ActionType; +import com.codingapi.flow.action.IFlowAction; +import com.codingapi.flow.action.actions.CustomAction; +import com.codingapi.flow.builder.ActionBuilder; +import com.codingapi.flow.builder.FormFieldPermissionsBuilder; +import com.codingapi.flow.builder.NodeStrategyBuilder; +import com.codingapi.flow.context.GatewayContext; +import com.codingapi.flow.factory.MyFlowServiceFactory; +import com.codingapi.flow.form.DataType; +import com.codingapi.flow.form.FlowForm; +import com.codingapi.flow.form.FlowFormBuilder; +import com.codingapi.flow.form.permission.PermissionType; +import com.codingapi.flow.node.nodes.ApprovalNode; +import com.codingapi.flow.node.nodes.EndNode; +import com.codingapi.flow.node.nodes.StartNode; +import com.codingapi.flow.operator.OperatorIdentity; +import com.codingapi.flow.operator.OperatorIdentityMatchMode; +import com.codingapi.flow.operator.OperatorIdentityPolicy; +import com.codingapi.flow.pojo.body.FlowAdviceBody; +import com.codingapi.flow.pojo.request.FlowActionRequest; +import com.codingapi.flow.pojo.request.FlowCreateRequest; +import com.codingapi.flow.record.FlowRecord; +import com.codingapi.flow.script.factory.FlowGroovyScriptFactory; +import com.codingapi.flow.strategy.node.FormFieldPermissionStrategy; +import com.codingapi.flow.strategy.node.OperatorLoadStrategy; +import com.codingapi.flow.strategy.node.RevokeStrategy; +import com.codingapi.flow.user.User; +import com.codingapi.flow.workflow.Workflow; +import com.codingapi.flow.workflow.WorkflowBuilder; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import java.util.Arrays; +import java.util.List; +import java.util.Map; +import java.util.stream.Collectors; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; + +/** + * 办理人身份留痕的流程级测试(issue #306)。 + * + *

核心回归:记录被办结后必须留下「本次办理人」的 {@code handleOperatorId/Name/Identity}, + * 与「待办归属身份」{@code currentOperatorIdentity} 互不覆盖。此前 {@code update()} 不写任何身份字段, + * 导致最后一步办理身份(没有下游记录可承载)彻底丢失。 + */ +class HandleOperatorIdentityFlowTest { + + private static final long INITIATOR = 1; + private static final long APPROVER = 2; + + private static final String DEPARTMENT_1 = "department1"; + private static final String DEPARTMENT_2 = "department2"; + + private MyFlowServiceFactory factory; + private final Map data = Map.of("name", "lorne"); + + @BeforeEach + void setUp() { + // issue #297:USER_ONLY 下同一人换身份可以办理自己的待办,与 issue #306 的复现场景一致 + OperatorIdentityPolicy.setMatchMode(OperatorIdentityMatchMode.USER_ONLY); + factory = new MyFlowServiceFactory(); + factory.userGateway.save(new User(INITIATOR, "initiator")); + factory.userGateway.save(new User(APPROVER, "approver")); + GatewayContext.getInstance().setFlowOperatorGateway(factory.userGateway); + } + + @AfterEach + void tearDown() { + OperatorIdentityPolicy.reset(); + } + + /** + * 测试目标:最后一步办理时,办理人身份必须留痕。 + * 前置条件:Start -> Approval(审批人固定为 APPROVER) -> End,开始节点归属身份为 department1、审批人归属为默认身份。 + * 执行步骤:department1 发起并提交,APPROVER 以 department2 身份审批通过(流程结束,无下游记录)。 + * 期望断言:该记录 currentOperatorIdentity 仍为空(归属身份不被改写), + * handleOperatorId=APPROVER、handleOperatorIdentity=department2。 + */ + @Test + void should_record_handle_operator_when_finishing_last_step() { + Workflow workflow = saveFlow("handle-identity-last", APPROVER); + + long startRecordId = createFlow(workflow, DEPARTMENT_1); + submit(workflow, startRecordId, DEPARTMENT_1); + + FlowRecord approvalRecord = todoRecordOf(APPROVER); + assertEquals(OperatorIdentity.DEFAULT, approvalRecord.getCurrentOperatorIdentity(), + "固定审批人脚本只返回 id,归属身份应为默认身份"); + + pass(workflow, approvalRecord.getId(), APPROVER, DEPARTMENT_2); + + FlowRecord done = factory.flowRecordRepository.get(approvalRecord.getId()); + assertTrue(done.isDone(), "记录应已办结"); + assertEquals(OperatorIdentity.DEFAULT, done.getCurrentOperatorIdentity(), + "归属身份不能被办理行为改写"); + assertEquals(APPROVER, done.getHandleOperatorId(), "办理人应留痕"); + assertEquals("approver", done.getHandleOperatorName(), "办理人名称应留痕"); + assertEquals(DEPARTMENT_2, done.getHandleOperatorIdentity(), "办理人身份应留痕"); + } + + /** + * 测试目标:中间节点办理同样留痕,且与下游记录的提交身份一致。 + * 前置条件:Start -> Approval1(APPROVER) -> Approval2(INITIATOR) -> End。 + * 执行步骤:department1 发起,APPROVER 以 department2 通过中间节点。 + * 期望断言:中间记录 handleOperatorIdentity=department2;下游待办的 submitOperatorIdentity=department2。 + */ + @Test + void should_record_handle_operator_when_finishing_middle_step() { + Workflow workflow = saveTwoApprovalFlow("handle-identity-middle"); + + long startRecordId = createFlow(workflow, DEPARTMENT_1); + submit(workflow, startRecordId, DEPARTMENT_1); + + FlowRecord middleRecord = todoRecordOf(APPROVER); + pass(workflow, middleRecord.getId(), APPROVER, DEPARTMENT_2); + + FlowRecord done = factory.flowRecordRepository.get(middleRecord.getId()); + assertEquals(DEPARTMENT_2, done.getHandleOperatorIdentity(), "中间节点办理身份应留痕"); + + FlowRecord nextRecord = todoRecordOf(INITIATOR); + assertEquals(DEPARTMENT_2, nextRecord.getSubmitOperatorIdentity(), + "下游记录的提交身份应与办理身份一致"); + } + + /** + * 测试目标:暂存不是办理,不得写入办理人。 + * 前置条件:同上单审批节点流程,APPROVER 存在待办。 + * 执行步骤:APPROVER 以 department2 身份执行「保存」动作。 + * 期望断言:记录仍为待办,handleOperator* 保持默认空值。 + */ + @Test + void should_not_record_handle_operator_when_saving_draft() { + Workflow workflow = saveFlow("handle-identity-save", APPROVER); + + long startRecordId = createFlow(workflow, DEPARTMENT_1); + submit(workflow, startRecordId, DEPARTMENT_1); + + FlowRecord approvalRecord = todoRecordOf(APPROVER); + save(workflow, approvalRecord.getId(), APPROVER, DEPARTMENT_2); + + FlowRecord saved = factory.flowRecordRepository.get(approvalRecord.getId()); + assertTrue(saved.isTodo(), "暂存后记录仍为待办"); + assertEquals(0L, saved.getHandleOperatorId(), "暂存不应写办理人 id"); + assertEquals(OperatorIdentity.DEFAULT, saved.getHandleOperatorIdentity(), "暂存不应写办理人身份"); + } + + // ==================== 流程构建 ==================== + + /** + * 构建 Start(可撤回) -> Approval(指定审批人) -> End 的流程。 + */ + private Workflow saveFlow(String code, long... approverIds) { + StartNode startNode = startNode(code); + ApprovalNode approvalNode = approvalNode(code, "审批", approverIds); + + Workflow workflow = WorkflowBuilder.builder() + .title("办理人身份留痕流程") + .code(code) + .createdOperator(new User(INITIATOR, "initiator")) + .form(form(code)) + .addNode(startNode) + .addNode(approvalNode) + .addNode(EndNode.builder().build()) + .build(); + factory.workflowService.saveWorkflow(workflow); + return workflow; + } + + /** + * 构建 Start(可撤回) -> Approval1(APPROVER) -> Approval2(INITIATOR) -> End 的流程。 + */ + private Workflow saveTwoApprovalFlow(String code) { + Workflow workflow = WorkflowBuilder.builder() + .title("办理人身份留痕流程") + .code(code) + .createdOperator(new User(INITIATOR, "initiator")) + .form(form(code)) + .addNode(startNode(code)) + .addNode(approvalNode(code, "一级审批", APPROVER)) + .addNode(approvalNode(code, "二级审批", INITIATOR)) + .addNode(EndNode.builder().build()) + .build(); + factory.workflowService.saveWorkflow(workflow); + return workflow; + } + + private StartNode startNode(String code) { + return StartNode.builder() + .strategies(NodeStrategyBuilder.builder() + .addStrategy(fieldPermission(code)) + .addStrategy(new RevokeStrategy(true, RevokeStrategy.Type.REVOKE_CURRENT)) + .build()) + .actions(ActionBuilder.builder() + .addAction(CustomAction.defaultAction()) + .build()) + .build(); + } + + private ApprovalNode approvalNode(String code, String name, long... approverIds) { + return ApprovalNode.builder() + .name(name) + .strategies(NodeStrategyBuilder.builder() + .addStrategy(fieldPermission(code)) + .addStrategy(new OperatorLoadStrategy(FlowGroovyScriptFactory + .createOperatorLoadScript("def run(request){return [" + joinIds(approverIds) + "]}") + .getKey())) + .build()) + .build(); + } + + private String joinIds(long... ids) { + return Arrays.stream(ids).mapToObj(String::valueOf).collect(Collectors.joining(",")); + } + + private FlowForm form(String code) { + return FlowFormBuilder.builder() + .name("办理人身份留痕流程") + .code(code) + .addField("请假人", "name", DataType.STRING) + .build(); + } + + private FormFieldPermissionStrategy fieldPermission(String code) { + return new FormFieldPermissionStrategy(FormFieldPermissionsBuilder.builder() + .addPermission(code, "name", PermissionType.WRITE) + .build()); + } + + // ==================== 动作入口 ==================== + + private long createFlow(Workflow workflow, String operatorIdentity) { + FlowCreateRequest request = new FlowCreateRequest(); + request.setWorkCode(workflow.getCode()); + request.setFormData(data); + request.setActionId(startAction(workflow).id()); + request.setOperatorId(INITIATOR); + request.setOperatorIdentity(operatorIdentity); + return factory.flowService.create(request); + } + + private void submit(Workflow workflow, long recordId, String operatorIdentity) { + doAction(workflow, recordId, startAction(workflow), INITIATOR, operatorIdentity); + } + + private void pass(Workflow workflow, long recordId, long operatorId, String operatorIdentity) { + IFlowAction passAction = approvalAction(workflow, ActionType.PASS); + doAction(workflow, recordId, passAction, operatorId, operatorIdentity); + } + + private void save(Workflow workflow, long recordId, long operatorId, String operatorIdentity) { + IFlowAction saveAction = approvalAction(workflow, ActionType.SAVE); + doAction(workflow, recordId, saveAction, operatorId, operatorIdentity); + } + + private void doAction(Workflow workflow, long recordId, IFlowAction action, long operatorId, String operatorIdentity) { + FlowAdviceBody advice = new FlowAdviceBody(action.id(), action.title(), operatorId); + advice.setOperatorIdentity(operatorIdentity); + FlowActionRequest request = new FlowActionRequest(); + request.setFormData(data); + request.setRecordId(recordId); + request.setAdvice(advice); + factory.flowService.action(request); + } + + private IFlowAction startAction(Workflow workflow) { + return workflow.getStartNode().actionManager().getActions().get(0); + } + + /** + * 取审批节点的指定类型动作(只取第一个审批节点,二级流程的审批动作保持一致)。 + */ + private IFlowAction approvalAction(Workflow workflow, ActionType actionType) { + return workflow.getNodes().stream() + .filter(node -> node instanceof ApprovalNode) + .findFirst() + .map(node -> node.actionManager().getActionByType(actionType.name())) + .orElseThrow(); + } + + private FlowRecord todoRecordOf(long operatorId) { + List records = factory.flowRecordRepository.findTodoByOperator(operatorId); + assertEquals(1, records.size(), "操作人应有且仅有一条待办"); + return records.get(0); + } +} diff --git a/flow-engine-starter-infra/src/main/java/com/codingapi/flow/infra/convert/FlowRecordContentConvertor.java b/flow-engine-starter-infra/src/main/java/com/codingapi/flow/infra/convert/FlowRecordContentConvertor.java index 3d44a16b..b1810559 100644 --- a/flow-engine-starter-infra/src/main/java/com/codingapi/flow/infra/convert/FlowRecordContentConvertor.java +++ b/flow-engine-starter-infra/src/main/java/com/codingapi/flow/infra/convert/FlowRecordContentConvertor.java @@ -69,6 +69,10 @@ public static FlowRecordContent convert(FlowRecordEntity record){ content.setRecordState(record.getRecordState()); content.setFlowState(record.getFlowState()); content.setNotify(record.getNotify()); + // issue #306:已办列表的办理人(历史数据为空,身份统一归一化为默认身份) + content.setHandleOperatorId(record.getHandleOperatorId()); + content.setHandleOperatorName(record.getHandleOperatorName()); + content.setHandleOperatorIdentity(OperatorIdentity.normalize(record.getHandleOperatorIdentity())); return content; } } diff --git a/flow-engine-starter-infra/src/main/java/com/codingapi/flow/infra/convert/FlowRecordConvertor.java b/flow-engine-starter-infra/src/main/java/com/codingapi/flow/infra/convert/FlowRecordConvertor.java index 3f956f12..02d35e71 100644 --- a/flow-engine-starter-infra/src/main/java/com/codingapi/flow/infra/convert/FlowRecordConvertor.java +++ b/flow-engine-starter-infra/src/main/java/com/codingapi/flow/infra/convert/FlowRecordConvertor.java @@ -68,7 +68,11 @@ public static FlowRecord convert(FlowRecordEntity entity) { OperatorIdentity.normalize(entity.getSubmitOperatorIdentity()), OperatorIdentity.normalize(entity.getCurrentOperatorIdentity()), OperatorIdentity.normalize(entity.getForwardOperatorIdentity()), - OperatorIdentity.normalize(entity.getInterferedOperatorIdentity())); + OperatorIdentity.normalize(entity.getInterferedOperatorIdentity()), + // issue #306:办理人(历史数据为空) + entity.getHandleOperatorId() == null ? 0L : entity.getHandleOperatorId(), + entity.getHandleOperatorName(), + OperatorIdentity.normalize(entity.getHandleOperatorIdentity())); } public static FlowRecordEntity convert(FlowRecord record) { @@ -133,6 +137,9 @@ public static FlowRecordEntity convert(FlowRecord record) { entity.setCurrentOperatorIdentity(OperatorIdentity.validate(record.getCurrentOperatorIdentity())); entity.setForwardOperatorIdentity(OperatorIdentity.validate(record.getForwardOperatorIdentity())); entity.setInterferedOperatorIdentity(OperatorIdentity.validate(record.getInterferedOperatorIdentity())); + entity.setHandleOperatorId(record.getHandleOperatorId()); + entity.setHandleOperatorName(record.getHandleOperatorName()); + entity.setHandleOperatorIdentity(OperatorIdentity.validate(record.getHandleOperatorIdentity())); return entity; } diff --git a/flow-engine-starter-infra/src/main/java/com/codingapi/flow/infra/entity/FlowRecordEntity.java b/flow-engine-starter-infra/src/main/java/com/codingapi/flow/infra/entity/FlowRecordEntity.java index 7e7a306a..af362361 100644 --- a/flow-engine-starter-infra/src/main/java/com/codingapi/flow/infra/entity/FlowRecordEntity.java +++ b/flow-engine-starter-infra/src/main/java/com/codingapi/flow/infra/entity/FlowRecordEntity.java @@ -239,6 +239,22 @@ public class FlowRecordEntity { @Column(length = OperatorIdentity.MAX_LENGTH) private String interferedOperatorIdentity; + /** + * 办理人Id(issue #306),记录办结时的实际办理人,暂存与抄送不写入 + */ + private Long handleOperatorId; + + /** + * 办理人名称(issue #306) + */ + private String handleOperatorName; + + /** + * 办理人身份标识(issue #306),空值等价默认身份 + */ + @Column(length = OperatorIdentity.MAX_LENGTH) + private String handleOperatorIdentity; + /** * 委托记录id */ diff --git a/flow-engine-starter-infra/src/test/java/com/codingapi/flow/infra/convert/FlowRecordContentConvertorTest.java b/flow-engine-starter-infra/src/test/java/com/codingapi/flow/infra/convert/FlowRecordContentConvertorTest.java new file mode 100644 index 00000000..013b6d93 --- /dev/null +++ b/flow-engine-starter-infra/src/test/java/com/codingapi/flow/infra/convert/FlowRecordContentConvertorTest.java @@ -0,0 +1,105 @@ +package com.codingapi.flow.infra.convert; + +import com.codingapi.flow.infra.entity.FlowRecordEntity; +import com.codingapi.flow.pojo.response.FlowRecordContent; +import com.codingapi.flow.record.FlowRecord; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertAll; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNull; + +/** + * 已办列表响应映射测试(issue #306)。 + * + *

查询层({@code FlowRecordQueryServiceImpl})把 JPA 实体直接交给 + * {@link FlowRecordContentConvertor#convert(FlowRecordEntity)} 组装列表数据, + * 不走领域对象链路。因此「办理人」三字段必须在该转换器里显式映射, + * 否则已办列表拿到的永远是 null,前端只能回退到待办归属人。 + */ +class FlowRecordContentConvertorTest { + + /** + * 构造字段完整的流程记录实体,避免转换时的拆箱 NPE 干扰断言。 + */ + private FlowRecordEntity newRecordEntity() { + FlowRecordEntity entity = new FlowRecordEntity(); + entity.setId(2L); + entity.setWorkRuntimeId(1L); + entity.setWorkTitle("身份标识验证流程"); + entity.setWorkCode("identity-demo"); + entity.setNodeId("approval"); + entity.setNodeType("APPROVAL"); + entity.setNodeName("审批节点"); + entity.setTitle("你有一条待办"); + entity.setFromId(0L); + entity.setParentId(0L); + entity.setReadTime(0L); + entity.setCurrentOperatorId(2L); + entity.setCurrentOperatorName("张三"); + entity.setSubmitOperatorId(1L); + entity.setForwardOperatorId(0L); + entity.setNodeOrder(0); + entity.setHidden(false); + entity.setRevoked(false); + entity.setNotify(false); + entity.setRecordState(FlowRecord.SATE_RECORD_DONE); + entity.setFlowState(FlowRecord.SATE_FLOW_FINISH); + entity.setUpdateTime(0L); + entity.setCreateTime(0L); + entity.setFinishTime(0L); + entity.setReadable(false); + entity.setCreateOperatorId(1L); + entity.setTimeoutTime(0L); + entity.setMergeable(false); + entity.setInterferedOperatorId(0L); + entity.setDelegateId(0L); + entity.setParallelBranchTotal(0); + return entity; + } + + /** + * 测试目标:已办记录的办理人(人 + 身份)必须出现在列表响应里。 + * 前置条件:实体带有本次办理人信息(张三以 department1 身份办理)。 + * 执行步骤:实体 → 列表响应对象。 + * 期望断言:办理人 id / 名称 / 身份与实体一致,且不与待办归属人混淆。 + */ + @Test + void should_expose_handle_operator_for_done_record() { + FlowRecordEntity entity = newRecordEntity(); + entity.setCurrentOperatorIdentity(""); + entity.setCreateOperatorIdentity(""); + entity.setHandleOperatorId(2L); + entity.setHandleOperatorName("张三"); + entity.setHandleOperatorIdentity("department1"); + + FlowRecordContent content = FlowRecordContentConvertor.convert(entity); + + assertAll( + () -> assertEquals(2L, content.getHandleOperatorId()), + () -> assertEquals("张三", content.getHandleOperatorName()), + () -> assertEquals("department1", content.getHandleOperatorIdentity()), + // 待办归属身份保持原样,办理身份不覆盖它 + () -> assertEquals("", content.getCurrentOperatorIdentity()) + ); + } + + /** + * 测试目标:升级前的存量已办记录(办理人列为空)不报错、不伪造数据。 + * 前置条件:实体 handle 三字段均为 null。 + * 执行步骤:实体 → 列表响应对象。 + * 期望断言:id / 名称保持 null,身份归一化为空串,前端据此回退待办归属人。 + */ + @Test + void should_keep_handle_operator_empty_for_legacy_done_record() { + FlowRecordEntity entity = newRecordEntity(); + + FlowRecordContent content = FlowRecordContentConvertor.convert(entity); + + assertAll( + () -> assertNull(content.getHandleOperatorId()), + () -> assertNull(content.getHandleOperatorName()), + () -> assertEquals("", content.getHandleOperatorIdentity()) + ); + } +}