feat(REQ-3340) - 新增覆盖更新指定流程中最后一次的表单中指定的表单项数据

This commit is contained in:
wangli 2025-02-08 13:38:34 +08:00
parent a5cd0761f7
commit d096ed9350
8 changed files with 192 additions and 9 deletions

View File

@ -13,6 +13,7 @@ import cn.axzo.workflow.common.model.request.bpmn.process.BpmnProcessInstanceCre
import cn.axzo.workflow.common.model.request.bpmn.process.BpmnProcessInstanceLogQueryDTO;
import cn.axzo.workflow.common.model.request.bpmn.process.BpmnProcessInstanceMyPageReqVO;
import cn.axzo.workflow.common.model.request.bpmn.process.BpmnProcessInstanceQueryDTO;
import cn.axzo.workflow.common.model.request.form.instance.FormVariablesUpdateDTO;
import cn.axzo.workflow.common.model.response.BpmPageResult;
import cn.axzo.workflow.common.model.response.bpmn.BatchOperationResultVO;
import cn.axzo.workflow.common.model.response.bpmn.process.BpmnProcessInstanceAdminPageItemVO;
@ -256,6 +257,17 @@ public interface ProcessInstanceApi {
@InvokeMode(SYNC)
CommonResponse<BpmnProcessInstanceLogVO> getProcessInstanceLogs(@Validated @RequestBody BpmnProcessInstanceLogQueryDTO dto);
/**
* 更新指定流程表单最后一次编辑的内容
*
* @param dto
* @return
*/
@Operation(summary = "更新指定流程表单最后一次编辑的内容")
@PostMapping("/api/process/instance/form/variable/update")
@InvokeMode(SYNC)
CommonResponse<Boolean> updateInstanceFormVariables(@Validated @RequestBody FormVariablesUpdateDTO dto);
// /**
// * 获取指定流程的变量集合, 用于打印模板中暂时系统字段
// *
@ -265,4 +277,6 @@ public interface ProcessInstanceApi {
// @PostMapping("/api/process/instance/variables")
// @InvokeMode(SYNC)
// CommonResponse<?> getProcessInstanceVariables();
}

View File

@ -0,0 +1,37 @@
package cn.axzo.workflow.common.model.request.form.instance;
import cn.axzo.workflow.common.model.dto.UploadFieldDTO;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import javax.validation.constraints.NotBlank;
import java.io.Serializable;
import java.util.Map;
/**
* 更新指定流程表单最后一次操作的表单内容的入参模型
*
* @author wangli
* @since 2025-02-08 10:04
*/
@ApiModel("更新指定流程表单最后一次操作的表单内容")
@Data
public class FormVariablesUpdateDTO implements Serializable {
private static final long serialVersionUID = 1L;
/**
* 流程实例 ID
*/
@ApiModelProperty(value = "流程实例ID")
@NotBlank(message = "流程实例 ID 不能为空")
private String processInstanceId;
/**
* todo 需要补充各种组件传入的模型文档
* <p>
* 附件类型@see {@link UploadFieldDTO}
*/
@ApiModelProperty(value = "通过表单创建流程时传入的初始表单数据")
private Map<String, Object> formVariables;
}

View File

@ -314,17 +314,19 @@ public class CustomGetFormInstanceModelCmd extends GetFormInstanceModelCmd {
} else if (FormFieldTypes.UPLOAD.equals(sf.getType())
|| FORM_FIELD_TYPE_IMAGE.equals(sf.getType())) {
if (rows.get(0).containsKey(sf.getId())) {
String uploadValue = (String) rows.get(0).get(sf.getId());
if (uploadValue != null) {
try {
try {
String updateValueJson = objectMapper.writeValueAsString(rows.get(0).get(sf.getId()));
if (updateValueJson != null) {
List<UploadFieldDTO> uploadFiles = formEngineConfiguration.getObjectMapper()
.readValue(uploadValue, new TypeReference<List<UploadFieldDTO>>() {
.readValue(updateValueJson, new TypeReference<List<UploadFieldDTO>>() {
});
sf.setValue(uploadFiles);
} catch (JsonProcessingException e) {
throw new FlowableException("Error parsing upload files json value: " + uploadValue, e);
}
}
} catch (JsonProcessingException e) {
throw new FlowableException("Error parsing upload files json ", e);
}
}
}
});

View File

@ -0,0 +1,106 @@
package cn.axzo.workflow.core.engine.cmd;
import cn.axzo.workflow.common.exception.WorkflowEngineException;
import com.alibaba.fastjson.JSON;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.fasterxml.jackson.databind.node.TextNode;
import lombok.extern.slf4j.Slf4j;
import org.flowable.common.engine.api.FlowableException;
import org.flowable.common.engine.impl.interceptor.CommandContext;
import org.flowable.form.api.FormInstance;
import org.flowable.form.api.FormService;
import org.flowable.form.engine.FormEngineConfiguration;
import org.flowable.form.engine.impl.persistence.entity.FormInstanceEntity;
import org.flowable.form.engine.impl.util.CommandContextUtil;
import org.springframework.util.CollectionUtils;
import java.io.Serializable;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import static cn.axzo.workflow.common.code.FormInstanceRespCode.FORM_INSTANCE_DATA_NOT_FOUND;
/**
* 覆写指定流程最后一次操作的表单内容
*
* @author wangli
* @since 2025-02-08 10:14
*/
@Slf4j
public class CustomOverrideFormVariablesByLatestInstanceCmd extends AbstractCommand<Void> implements Serializable {
private static final long serialVersionUID = 1L;
private final String processInstanceId;
private final Map<String, Object> formVariables;
public CustomOverrideFormVariablesByLatestInstanceCmd(String processInstanceId, Map<String, Object> formVariables) {
this.processInstanceId = processInstanceId;
this.formVariables = formVariables;
}
@Override
public String paramToJsonString() {
Map<String, Object> params = new HashMap<>();
params.put("processInstanceId", processInstanceId);
params.put("formVariables", formVariables);
return JSON.toJSONString(params);
}
@Override
public Void executeInternal(CommandContext commandContext) {
FormEngineConfiguration formEngineConfiguration = CommandContextUtil.getFormEngineConfiguration();
FormService formService = formEngineConfiguration.getFormService();
List<FormInstance> formInstances = formService.createFormInstanceQuery().processInstanceId(processInstanceId)
.orderBySubmittedDate().desc().listPage(0, 1);
if (CollectionUtils.isEmpty(formInstances)) {
throw new WorkflowEngineException(FORM_INSTANCE_DATA_NOT_FOUND, processInstanceId);
}
FormInstanceEntity formInstance = (FormInstanceEntity) formInstances.get(0);
ObjectMapper objectMapper = CommandContextUtil.getFormEngineConfiguration().getObjectMapper();
try {
JsonNode jsonNode = objectMapper.readTree(formInstance.getFormValueBytes());
if (Objects.isNull(jsonNode)) {
return null;
}
JsonNode valuesNode = jsonNode.get("values");
if (Objects.nonNull(valuesNode)) {
ObjectNode valuesObjectNode = (ObjectNode) valuesNode;
Iterator<String> fieldIdIterator = valuesNode.fieldNames();
while (fieldIdIterator.hasNext()) {
String fieldId = fieldIdIterator.next();
formVariables.forEach((k, v) -> {
if (Objects.equals(k, fieldId)) {
if (v instanceof Collection) {
try {
valuesObjectNode.set(k, new TextNode(objectMapper.writeValueAsString(v)));
} catch (JsonProcessingException e) {
throw new RuntimeException(e);
}
} else {
valuesObjectNode.set(k, new TextNode(v.toString()));
}
}
});
}
}
byte[] formValueBytes = objectMapper.writeValueAsBytes(jsonNode);
log.info("更新后表单内容: {}", new String(formInstance.getFormValueBytes()));
formInstance.setFormValueBytes(formValueBytes);
CommandContextUtil.getFormInstanceEntityManager().update(formInstance);
} catch (Exception e) {
throw new FlowableException("Error parsing form instance " + formInstance.getId(), e);
}
return null;
}
}

View File

@ -11,6 +11,7 @@ import cn.axzo.workflow.common.model.request.bpmn.process.BpmnProcessInstanceLog
import cn.axzo.workflow.common.model.request.bpmn.process.BpmnProcessInstanceMyPageReqVO;
import cn.axzo.workflow.common.model.request.bpmn.process.BpmnProcessInstanceQueryDTO;
import cn.axzo.workflow.common.model.request.bpmn.process.HistoricProcessInstanceSearchDTO;
import cn.axzo.workflow.common.model.request.form.instance.FormVariablesUpdateDTO;
import cn.axzo.workflow.common.model.response.BpmPageResult;
import cn.axzo.workflow.common.model.response.bpmn.BatchOperationResultVO;
import cn.axzo.workflow.common.model.response.bpmn.process.BpmnProcessInstanceAdminPageItemVO;
@ -177,4 +178,11 @@ public interface BpmnProcessInstanceService {
String getModelIdByProcessInstanceId(String processInstanceId);
String getModelIdByProcessDefinitionId(String processDefinitionId);
/**
* 覆盖指定流程表单的指定表单项
*
* @param dto
*/
void overrideFormVariables(FormVariablesUpdateDTO dto);
}

View File

@ -25,6 +25,7 @@ import cn.axzo.workflow.common.model.request.bpmn.process.HistoricProcessInstanc
import cn.axzo.workflow.common.model.request.bpmn.task.AttachmentDTO;
import cn.axzo.workflow.common.model.request.bpmn.task.BpmnTaskDelegateAssigner;
import cn.axzo.workflow.common.model.request.category.CategorySearchDTO;
import cn.axzo.workflow.common.model.request.form.instance.FormVariablesUpdateDTO;
import cn.axzo.workflow.common.model.response.BpmPageResult;
import cn.axzo.workflow.common.model.response.bpmn.BatchOperationItemResultVO;
import cn.axzo.workflow.common.model.response.bpmn.BatchOperationResultVO;
@ -46,6 +47,7 @@ import cn.axzo.workflow.core.engine.cmd.CustomCancelProcessInstanceAsyncCmd;
import cn.axzo.workflow.core.engine.cmd.CustomCancelProcessInstanceCmd;
import cn.axzo.workflow.core.engine.cmd.CustomCarbonCopyUserSelectorCmd;
import cn.axzo.workflow.core.engine.cmd.CustomForecastUserTaskAssigneeCmd;
import cn.axzo.workflow.core.engine.cmd.CustomOverrideFormVariablesByLatestInstanceCmd;
import cn.axzo.workflow.core.engine.listener.EngineExecutionStartListener;
import cn.axzo.workflow.core.repository.entity.ExtAxBpmnFormRelation;
import cn.axzo.workflow.core.repository.entity.ExtAxProcessLog;
@ -1456,4 +1458,10 @@ public class BpmnProcessInstanceServiceImpl implements BpmnProcessInstanceServic
}
return model.getId();
}
@Override
public void overrideFormVariables(FormVariablesUpdateDTO dto) {
CommandExecutor commandExecutor = springProcessEngineConfiguration.getCommandExecutor();
commandExecutor.execute(new CustomOverrideFormVariablesByLatestInstanceCmd(dto.getProcessInstanceId(), dto.getFormVariables()));
}
}

View File

@ -90,8 +90,7 @@ public class CustomGetVariablesFromFormSubmissionCmd extends GetVariablesFromFor
} else if (formField.getType().equals(FormFieldTypes.UPLOAD)) {
FormEngineConfiguration formEngineConfiguration = CommandContextUtil.getFormEngineConfiguration();
try {
String json = formEngineConfiguration.getObjectMapper().writeValueAsString(formFieldValue);
result = json;
result = formEngineConfiguration.getObjectMapper().writeValueAsString(formFieldValue);
} catch (JsonProcessingException e) {
result = null;
}

View File

@ -16,6 +16,7 @@ import cn.axzo.workflow.common.model.request.bpmn.process.BpmnProcessInstanceMyP
import cn.axzo.workflow.common.model.request.bpmn.process.BpmnProcessInstanceQueryDTO;
import cn.axzo.workflow.common.model.request.bpmn.process.HistoricProcessInstanceSearchDTO;
import cn.axzo.workflow.common.model.request.bpmn.task.BpmnTaskDelegateAssigner;
import cn.axzo.workflow.common.model.request.form.instance.FormVariablesUpdateDTO;
import cn.axzo.workflow.common.model.response.BpmPageResult;
import cn.axzo.workflow.common.model.response.bpmn.BatchOperationResultVO;
import cn.axzo.workflow.common.model.response.bpmn.process.BpmnProcessInstanceAdminPageItemVO;
@ -381,4 +382,12 @@ public class BpmnProcessInstanceController extends BasicPopulateAvatarController
request.setFileKeys(signUrls);
return RpcExternalUtil.rpcProcessor(() -> serverFileServiceApi.signUrlFetchDownload(request), "批量获取手写签私有访问地址", request);
}
@Operation(summary = "更新指定流程表单最后一次编辑的内容")
@PostMapping("/form/variable/update")
@Override
public CommonResponse<Boolean> updateInstanceFormVariables(FormVariablesUpdateDTO dto) {
bpmnProcessInstanceService.overrideFormVariables(dto);
return CommonResponse.success(true);
}
}