Merge branch 'feature/RDMP-3845' into pre

# Conflicts:
#	workflow-engine-core/src/main/java/cn/axzo/workflow/core/engine/cmd/CustomGetProcessInstanceVariablesToObjectCmd.java
#	workflow-engine-core/src/main/java/cn/axzo/workflow/core/listener/AbstractBpmnEventListener.java
This commit is contained in:
wangli 2025-12-30 10:55:25 +08:00
commit 29895e355c
11 changed files with 225 additions and 10 deletions

View File

@ -23,6 +23,8 @@ public enum FormInstanceRespCode implements IModuleRespCode {
FORM_DATA_PARSE_ERROR_BY_IMAGE("008", "表单图片组件的数据解析异常"),
FORM_DATA_PARSE_ERROR_BY_CUSTOM_COMPONENT("009", "表单自定义组件的数据解析异常"),
FORM_DATA_PARSE_ERROR_BY_AMOUNT("010", "表单金额组件的数据解析异常"),
FORM_DATA_PARSE_ERROR_BY_CHECKBOX("011", "表单复选框组件的数据解析异常"),
FORM_DATA_PARSE_ERROR_BY_RADIO("012", "表单单选框组件的数据解析异常"),
;
private final String code;

View File

@ -3,8 +3,10 @@ package cn.axzo.workflow.common.enums;
import cn.axzo.workflow.common.model.dto.AmountFieldDTO;
import cn.axzo.workflow.common.model.dto.ContactsPersonDTO;
import cn.axzo.workflow.common.model.dto.UploadFieldDTO;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;
import java.util.Arrays;
import java.util.Collections;
@ -21,6 +23,7 @@ import java.util.Objects;
* @author wangli
* @since 2025-08-04 11:44
*/
@Slf4j
public enum FormFieldTypeEnum {
input("input", "文本", new TypeReference<String>() {
}),
@ -46,7 +49,10 @@ public enum FormFieldTypeEnum {
}),
decimal("decimal", "小数", new TypeReference<Map<String, Object>>() {
}),
;
checkbox("checkbox", "复选框", new TypeReference<List<String>>() {
}),
radio("radio", "单选框", new TypeReference<String>() {
});
private final String type;
private final String desc;
@ -113,7 +119,23 @@ public enum FormFieldTypeEnum {
}
return objectMapper.readValue(json, typeReference);
} catch (Exception e) {
throw new RuntimeException("字段值解析失败: " + fieldValue, e);
log.warn("字段值解析失败: {}", fieldValue, e);
return null;
}
}
public static void main(String[] args) throws JsonProcessingException {
String json = "";
FormFieldTypeEnum[] values = FormFieldTypeEnum.values();
ObjectMapper objectMapper = new ObjectMapper();
for (FormFieldTypeEnum value : values) {
try {
System.out.println(" type: " + value.getType());
Object o = objectMapper.readValue(json, value.getTypeReference());
System.out.println("o = " + o);
} catch (Exception e) {
System.out.println("e.getMessage() = " + e.getMessage());
}
}
}

View File

@ -22,7 +22,7 @@ import java.util.Map;
public class FormFieldDTO {
/**
* 字段类型, 如果是分组:FormContainer,其他表单组件:FormField
* 字段类型, 如果是分组:FormContainer,单选复选组件OptionFormField,其他表单组件:FormField,
*/
@ApiModelProperty(value = "表单字段类型")
@NotBlank(message = "字段类型不能为空")
@ -41,6 +41,9 @@ public class FormFieldDTO {
* { label: "变洽签单", value: "changeSignatureOrder" },
* { label: "通讯录", value: "contacts" },
* { label: "金额", value: "amount" },
* { label: "复选", value: "checkbox" },
* { label: "单选", value: "radio" },
*
*/
@ApiModelProperty(value = "前端的组件类型")
private String type;
@ -76,6 +79,14 @@ public class FormFieldDTO {
@ApiModelProperty(value = "该表单字段的其他扩展属性,期望 JSON 格式")
private Map<String, Object> params;
/**
* 单选复选框的选项
* <p>
* {@see org.flowable.form.model.OptionFormField}
*/
@ApiModelProperty(value = "单选复选框的选项,配合 fieldType=OptionFormField 使用")
private List<OptionDTO> options;
/**
* fieldType=FormContainer ,这里的代表内部的集合
*/

View File

@ -0,0 +1,24 @@
package cn.axzo.workflow.common.model.request.form.definition;
import io.swagger.annotations.ApiModel;
import lombok.Data;
/**
* 单选复选框的选项入参模型
*
* @author wangli
* @since 2025-11-20 10:57
*/
@ApiModel("单选复选框的选项入参模型")
@Data
public class OptionDTO {
/**
* 选项 ID
*/
private String id;
/**
* 选项名称
*/
private String name;
}

View File

@ -28,8 +28,14 @@ public class FormDataVO {
* @return
*/
public Object getFormatDecimalValue() {
if (Objects.isNull(fieldType)) {
return null;
}
switch (fieldType) {
case decimal:
if (Objects.isNull(fieldValue)) {
return null;
}
Map<String, Object> decimalMap = (Map<String, Object>) fieldValue;
Object value = decimalMap.getOrDefault("value", "");
if (Objects.isNull(value)) {

View File

@ -0,0 +1,52 @@
package cn.axzo.workflow.core.engine.cmd;
import com.alibaba.fastjson.JSON;
import org.flowable.common.engine.impl.interceptor.CommandContext;
import org.flowable.engine.impl.util.CommandContextUtil;
import org.flowable.form.api.FormEngineConfigurationApi;
import org.flowable.form.api.FormInfo;
import org.flowable.form.api.FormInstance;
import org.flowable.form.api.FormRepositoryService;
import org.flowable.form.api.FormService;
import org.springframework.util.CollectionUtils;
import java.io.Serializable;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* 通过审批实例 ID 获取对应的表单定义实体的自定义命令
*
* @author wangli
* @since 2025-11-20 10:18
*/
public class CustomGetFormModelByProcessInstanceIdCmd extends AbstractCommand<FormInfo> implements Serializable {
private final String processInstanceId;
public CustomGetFormModelByProcessInstanceIdCmd(String processInstanceId) {
this.processInstanceId = processInstanceId;
}
@Override
public String paramToJsonString() {
Map<String, Object> params = new HashMap<>();
params.put("processInstanceId", processInstanceId);
return JSON.toJSONString(params);
}
@Override
public FormInfo executeInternal(CommandContext commandContext) {
FormEngineConfigurationApi formEngineConfiguration = CommandContextUtil.getFormEngineConfiguration(commandContext);
FormService formService = formEngineConfiguration.getFormService();
List<FormInstance> list = formService.createFormInstanceQuery().processInstanceId(processInstanceId).orderBySubmittedDate().desc().list();
if (CollectionUtils.isEmpty(list)) {
return null;
}
String formDefinitionId = list.get(0).getFormDefinitionId();
FormRepositoryService formRepositoryService = formEngineConfiguration.getFormRepositoryService();
return formRepositoryService.getFormModelById(formDefinitionId);
}
}

View File

@ -17,6 +17,7 @@ import com.google.common.collect.Lists;
import lombok.extern.slf4j.Slf4j;
import org.flowable.bpmn.model.FlowElement;
import org.flowable.bpmn.model.Process;
import org.apache.commons.collections4.ListUtils;
import org.flowable.common.engine.api.FlowableObjectNotFoundException;
import org.flowable.common.engine.impl.interceptor.CommandContext;
import org.flowable.engine.HistoryService;
@ -30,6 +31,8 @@ import org.flowable.form.api.FormService;
import org.flowable.form.model.FormContainer;
import org.flowable.form.model.FormField;
import org.flowable.form.model.FormFieldTypes;
import org.flowable.form.model.Option;
import org.flowable.form.model.OptionFormField;
import org.flowable.form.model.SimpleFormModel;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;
@ -47,6 +50,8 @@ import java.util.Objects;
import java.util.function.Function;
import java.util.stream.Collectors;
import static cn.axzo.workflow.common.code.FormInstanceRespCode.FORM_DATA_PARSE_ERROR_BY_CHECKBOX;
import static cn.axzo.workflow.common.code.FormInstanceRespCode.FORM_DATA_PARSE_ERROR_BY_RADIO;
import static cn.axzo.workflow.common.code.FormModelRespCode.FORM_MODEL_NOT_EXISTS;
import static cn.axzo.workflow.common.constant.BpmnConstants.BPM_MODEL_CATEGORY;
import static cn.axzo.workflow.common.constant.BpmnConstants.INTERNAL_INITIATOR;
@ -79,7 +84,7 @@ public class CustomGetProcessInstanceVariablesToObjectCmd extends AbstractComman
private final String processInstanceId;
private final SimpleDateFormat sdf = new SimpleDateFormat("yyyy.MM.dd HH:mm:ss");
private static final List<String> SUPPORTED_FORM_TYPES = Lists.newArrayList("input", "date", "textarea", "image", "contacts", "amount", "decimal");
private static final List<String> SUPPORTED_FORM_TYPES = Lists.newArrayList("input", "date", "textarea", "image", "contacts", "amount", "decimal", "checkbox", "radio");
public CustomGetProcessInstanceVariablesToObjectCmd(String processInstanceId) {
this.processInstanceId = processInstanceId;
@ -223,6 +228,39 @@ public class CustomGetProcessInstanceVariablesToObjectCmd extends AbstractComman
.type(convert(field.getType()))
.build());
}
} else if (Objects.equals(field.getType(), "checkbox")) {
if (field instanceof OptionFormField) {
OptionFormField optionField = (OptionFormField) field;
if (StringUtils.hasText(fieldValue.toString())
&& fieldValue.toString().startsWith("[")
&& fieldValue.toString().endsWith("]")) {
List<String> selectedOptions = JSON.parseArray((String) fieldValue, String.class);
List<String> optionNames = ListUtils.emptyIfNull(optionField.getOptions()).stream().filter(i -> selectedOptions.contains(i.getId())).map(Option::getName).collect(Collectors.toList());
variables.add(VariableObjectDTO.builder()
.key(field.getId())
.desc(field.getName())
.value(StringUtils.collectionToDelimitedString(optionNames, ""))
.type(convert(field.getType()))
.build());
}
} else {
throw new WorkflowEngineException(FORM_DATA_PARSE_ERROR_BY_CHECKBOX);
}
} else if (Objects.equals(field.getType(), "radio")) {
if (field instanceof OptionFormField) {
OptionFormField optionField = (OptionFormField) field;
if (StringUtils.hasText(fieldValue.toString())) {
List<String> optionNames = ListUtils.emptyIfNull(optionField.getOptions()).stream().filter(i -> Objects.equals(fieldValue, i.getId())).map(Option::getName).collect(Collectors.toList());
variables.add(VariableObjectDTO.builder()
.key(field.getId())
.desc(field.getName())
.value(StringUtils.collectionToDelimitedString(optionNames, ""))
.type(convert(field.getType()))
.build());
}
} else {
throw new WorkflowEngineException(FORM_DATA_PARSE_ERROR_BY_RADIO);
}
} else {
variables.add(VariableObjectDTO.builder()
.key(field.getId())
@ -336,6 +374,8 @@ public class CustomGetProcessInstanceVariablesToObjectCmd extends AbstractComman
case "amount":
case "contacts":
case "decimal":
case "checkbox":
case "radio":
return VariableObjectDTO.Type.text;
case "image":
return VariableObjectDTO.Type.img;

View File

@ -74,4 +74,5 @@ public abstract class AbstractBpmnEventListener<T extends OperationContext> impl
.noneMatch(prefix -> entry.getKey().startsWith(prefix)))
.collect(HashMap::new, (m, e) -> m.put(e.getKey(), e.getValue()), HashMap::putAll);
}
}

View File

@ -3,6 +3,7 @@ package cn.axzo.workflow.form.service.converter;
import cn.axzo.workflow.common.model.request.form.definition.FormFieldDTO;
import org.flowable.form.model.FormContainer;
import org.flowable.form.model.FormField;
import org.flowable.form.model.OptionFormField;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
@ -34,6 +35,16 @@ public interface FormFieldConverter extends EntityConverter<FormFieldDTO, FormFi
@Mapping(target = "params", source = "entity.params")
FormFieldDTO toVo(FormField entity);
@Mapping(target = "fieldType", expression = "java(entity.getClass().getSimpleName())")
@Mapping(target = "id", source = "entity.id")
@Mapping(target = "name", source = "entity.name")
@Mapping(target = "type", source = "entity.type")
@Mapping(target = "value", expression = "java(ConversionUtils.convertObject(entity.getValue()))")
@Mapping(target = "placeholder", source = "entity.placeholder")
@Mapping(target = "params", source = "entity.params")
@Mapping(target = "options", source = "entity.options")
FormFieldDTO toVo(OptionFormField entity);
@Mapping(target = "fieldType", expression = "java(entity.getClass().getSimpleName())")
@Mapping(target = "id", source = "entity.id")
@Mapping(target = "name", source = "entity.name")
@ -50,7 +61,9 @@ public interface FormFieldConverter extends EntityConverter<FormFieldDTO, FormFi
for (FormField entity : entities) {
if(entity instanceof FormContainer) {
dtos.add(toVo((FormContainer) entity));
} else{
} else if (entity instanceof OptionFormField) {
dtos.add(toVo((OptionFormField) entity));
} else {
dtos.add(toVo((FormField) entity));
}
}

View File

@ -1,15 +1,19 @@
package cn.axzo.workflow.form.service.converter;
import cn.axzo.workflow.common.model.request.form.definition.FormFieldDTO;
import cn.axzo.workflow.common.model.request.form.definition.OptionDTO;
import cn.axzo.workflow.common.model.response.form.instance.FormInstanceVO;
import cn.axzo.workflow.common.model.response.form.model.FormModelVO;
import org.flowable.form.api.FormInstanceInfo;
import org.flowable.form.api.FormModel;
import org.flowable.form.model.FormContainer;
import org.flowable.form.model.FormField;
import org.flowable.form.model.Option;
import org.flowable.form.model.OptionFormField;
import org.flowable.form.model.SimpleFormModel;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.springframework.util.CollectionUtils;
import java.util.Arrays;
import java.util.List;
@ -64,6 +68,11 @@ public interface FormInstanceConverter extends EntityConverter<FormInstanceVO, F
formFieldDTO.setValue(ConversionUtils.convertObject(formField.getValue()));
formFieldDTO.setPlaceholder(formField.getPlaceholder());
formFieldDTO.setParams(formField.getParams());
if (formField instanceof OptionFormField) {
OptionFormField optionFormField = (OptionFormField) formField;
formFieldDTO.setFieldType(optionFormField.getClass().getSimpleName());
formFieldDTO.setOptions(optionsToOptionDTOs(optionFormField.getOptions()));
}
if (formField instanceof FormContainer) {
FormContainer formContainer = (FormContainer) formField;
formFieldDTO.setFieldType(formContainer.getClass().getSimpleName());
@ -72,6 +81,19 @@ public interface FormInstanceConverter extends EntityConverter<FormInstanceVO, F
return formFieldDTO;
}
default List<OptionDTO> optionsToOptionDTOs(List<Option> options) {
if (CollectionUtils.isEmpty(options)) {
return null;
}
return options.stream()
.map(option -> {
OptionDTO optionDTO = new OptionDTO();
optionDTO.setId(option.getId());
optionDTO.setName(option.getName());
return optionDTO;
})
.collect(Collectors.toList());
}
// 辅助方法用于处理List<FormContainer>到List<FormFieldDTO>的转换调用上面自定义的转换方法
default List<FormFieldDTO> formContainersToFormFieldDTOs(List<FormField> formContainers) {

View File

@ -39,6 +39,7 @@ import cn.axzo.workflow.common.model.response.print.ProcessLogPdfResultDTO;
import cn.axzo.workflow.core.common.utils.BpmnMetaParserHelper;
import cn.axzo.workflow.core.conf.SupportRefreshProperties;
import cn.axzo.workflow.core.engine.cmd.CustomGetFormInstanceLatestValuesCmd;
import cn.axzo.workflow.core.engine.cmd.CustomGetFormModelByProcessInstanceIdCmd;
import cn.axzo.workflow.core.engine.cmd.CustomGetProcessInstanceVariablesCmd;
import cn.axzo.workflow.core.repository.entity.ExtAxProcessLog;
import cn.axzo.workflow.core.service.BpmnProcessDefinitionService;
@ -68,6 +69,8 @@ import org.flowable.form.api.FormRepositoryService;
import org.flowable.form.model.FormContainer;
import org.flowable.form.model.FormField;
import org.flowable.form.model.FormFieldTypes;
import org.flowable.form.model.Option;
import org.flowable.form.model.OptionFormField;
import org.flowable.form.model.SimpleFormModel;
import org.flowable.spring.SpringProcessEngineConfiguration;
import org.slf4j.Logger;
@ -406,21 +409,30 @@ public class PrintAdminController implements PrintAdminApi {
// 生成系统字段的变量
generateSystemFieldVariables(processInstanceId, result);
FormInfo formInfo = commandExecutor.execute(new CustomGetFormModelByProcessInstanceIdCmd(processInstanceId));
// 将所有变量都转换成 JSON 对象返回
convertValueToObject(result);
convertValueToObject(result, formInfo);
return success(result);
}
private void convertValueToObject(Map<String, Object> result) {
if (CollectionUtils.isEmpty(result)) {
private void convertValueToObject(Map<String, Object> result, FormInfo formInfo) {
if (CollectionUtils.isEmpty(result) || Objects.isNull(formInfo)) {
return;
}
Map<String, FormField> stringFormFieldMap = ((SimpleFormModel) formInfo.getFormModel()).allFieldsAsMap();
result.forEach((key, value) -> {
if (!(value instanceof String)) {
return;
}
if (((String) value).startsWith("[")) {
if (stringFormFieldMap.containsKey(key) && stringFormFieldMap.get(key) instanceof OptionFormField) {
OptionFormField optionFormField = (OptionFormField) stringFormFieldMap.get(key);
if (Objects.equals("checkbox", optionFormField.getType())) {
result.put(key, buildOptionFieldValue(optionFormField, JSONObject.parseArray((String) value, String.class)));
} else if (Objects.equals("radio", optionFormField.getType())) {
result.put(key, buildOptionFieldValue(optionFormField, Lists.newArrayList((String) value)));
}
} else if (((String) value).startsWith("[")) {
result.put(key, JSONArray.parseArray((String) value));
} else if (((String) value).startsWith("{")) {
result.put(key, JSONObject.parseObject((String) value));
@ -428,6 +440,16 @@ public class PrintAdminController implements PrintAdminApi {
});
}
private String buildOptionFieldValue(OptionFormField optionFormField, List<String> selectedValues) {
if (CollectionUtils.isEmpty(optionFormField.getOptions())) {
return "";
}
return optionFormField.getOptions().stream()
.filter(i -> selectedValues.contains(i.getId()))
.map(Option::getName)
.collect(Collectors.joining(""));
}
private void generateSystemFieldVariables(String processInstanceId, Map<String, Object> result) {
CommandExecutor commandExecutor = processEngineConfiguration.getCommandExecutor();
Map<String, Object> variables = commandExecutor.execute(new CustomGetProcessInstanceVariablesCmd(processInstanceId));