优化表单设计器的保存和更新接口

This commit is contained in:
Wang 2026-01-28 11:12:31 +08:00
parent 68ca2e67e9
commit d5bfa598ec
6 changed files with 147 additions and 53 deletions

View File

@ -63,18 +63,17 @@ CREATE TABLE `mp_activity_form_execution` (
KEY `idx_form_id` (`form_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='活动表单执行实例表';
-- 创建表单变量表
DROP TABLE IF EXISTS `mp_activity_form_variable`;
CREATE TABLE `mp_activity_form_variable` (
`id` int NOT NULL AUTO_INCREMENT COMMENT '主键ID',
`execution_id` int NOT NULL COMMENT '表单执行实例ID',
`variable_name` varchar(255) NOT NULL COMMENT '变量名称',
`variable_code` varchar(100) NOT NULL COMMENT '变量代码',
`variable_value` text NOT NULL COMMENT '变量值',
`data_type` varchar(50) DEFAULT 'string' COMMENT '数据类型string, number, boolean, json等',
`variable_value` text COMMENT '变量值',
`data_type` varchar(50) NOT NULL DEFAULT 'string' COMMENT '数据类型string, number, boolean, json等',
`create_time` datetime NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
`update_time` datetime NULL DEFAULT CURRENT_TIMESTAMP COMMENT '修改时间',
`create_by` varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_german2_ci NULL DEFAULT NULL COMMENT '创建人',
`update_time` datetime NULL DEFAULT CURRENT_TIMESTAMP COMMENT '修改时间',
`update_by` varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_german2_ci NULL DEFAULT NULL COMMENT '修改人',
`deleted` bit(1) NULL DEFAULT b'0' COMMENT '是否删除',
`tenant_id` varchar(20) DEFAULT 'Default' COMMENT '租户id',

View File

@ -70,12 +70,12 @@ public class AppActivityFormExecutionController {
}
@Operation(summary = "获取提交表单详情")
@GetMapping("/{activityId}/{agentId}")
@GetMapping("/{activityId}/{agentId}/{formId}")
@SaCheckPermission("activity.form.execution.view")
public ResultBean<AppFormFieldWithValueResp> getFormFieldsWithValues(@PathVariable("activityId") Integer activityId, @PathVariable("agentId") Integer agentId) {
public ResultBean<AppFormFieldWithValueResp> getFormFieldsWithValues(@PathVariable("activityId") Integer activityId, @PathVariable("agentId") Integer agentId,@PathVariable("formId") Integer formId) {
if (StpUtil.isLogin()) {
Integer userId = StpUtil.getLoginIdAsInt();
AppFormFieldWithValueResp result = appActivityFormExecutionService.getExecutionWithFormData(activityId, agentId, userId);
AppFormFieldWithValueResp result = appActivityFormExecutionService.getExecutionWithFormData(activityId, agentId, formId, userId);
return ResultBean.success(result);
}
return ResultBean.error();

View File

@ -30,10 +30,11 @@ public interface IAppActivityFormExecutionService {
*
* @param activityId 活动ID
* @param agentId 代理商ID
* @param formId 表单ID
* @param userId 用户ID
* @return 表单字段及值详情
*/
AppFormFieldWithValueResp getExecutionWithFormData(Integer activityId, Integer agentId, Integer userId);
AppFormFieldWithValueResp getExecutionWithFormData(Integer activityId, Integer agentId,Integer formId, Integer userId);
/**
* 更新表单数据

View File

@ -1,6 +1,8 @@
package com.seer.teach.mp.app.service.impl;
import cn.hutool.core.collection.CollectionUtil;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.seer.teach.common.utils.OrderIdGenerator;
import com.seer.teach.mp.app.controller.req.ActivityFormSubmitReq;
import com.seer.teach.mp.app.controller.resp.AppActivityFormFieldResp;
@ -26,6 +28,7 @@ import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.function.Function;
import java.util.stream.Collectors;
/**
@ -45,38 +48,36 @@ public class AppActivityFormExecutionServiceImpl implements IAppActivityFormExec
private final IMpActivityFormVariableService activityFormVariableService;
private final IMpActivityFormFieldService activityFormFieldService;
private final IMpActivityFormRelationService activityFormRelationService;
private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper();
@Override
@Transactional
public String submitForm(ActivityFormSubmitReq req, Integer userId) {
// 生成执行编号
String executionNo = OrderIdGenerator.generateOrderId("EX");
// 创建表单执行实例
MpActivityFormExecutionEntity execution = new MpActivityFormExecutionEntity();
execution.setExecutionNo(executionNo);
execution.setActivityId(req.getActivityId());
execution.setAgentId(req.getAgentId());
execution.setFormId(req.getFormId());
execution.setSubmitterId(userId);
execution.setSubmitTime(LocalDateTime.now());
execution.setStatus("submitted");
// 保存表单执行实例
activityFormExecutionService.save(execution);
Integer activityId = req.getActivityId();
Integer agentId = req.getAgentId();
Integer formId = req.getFormId();
MpActivityFormExecutionEntity execution = null;
if(userId > 0 ){
execution = activityFormExecutionService.getOneByActivityIdAndAgentIdAndSubmitterIdAndFormId(activityId, agentId, formId, userId);
}
if(Objects.isNull(execution)){
// 创建表单执行实例
execution = new MpActivityFormExecutionEntity();
execution.setExecutionNo(executionNo);
execution.setActivityId(req.getActivityId());
execution.setAgentId(req.getAgentId());
execution.setFormId(req.getFormId());
execution.setSubmitterId(userId);
execution.setSubmitTime(LocalDateTime.now());
execution.setStatus("submitted");
}
activityFormExecutionService.saveOrUpdate(execution);
// 处理表单数据将其存储到变量表中
if (CollectionUtil.isNotEmpty(req.getFormData())) {
List<MpActivityFormVariableEntity> variables = req.getFormData().entrySet().stream()
.map(entry -> {
MpActivityFormVariableEntity variable = new MpActivityFormVariableEntity();
variable.setExecutionId(execution.getId());
variable.setVariableName(entry.getKey());
variable.setVariableCode(entry.getKey());
variable.setVariableValue(String.valueOf(entry.getValue()));
variable.setDataType(getDataType(entry.getValue()));
return variable;
}).collect(Collectors.toList());
List<MpActivityFormVariableEntity> variables = buildMpActivityFormVariables(req, execution);
activityFormVariableService.saveBatch(variables);
}
@ -84,6 +85,42 @@ public class AppActivityFormExecutionServiceImpl implements IAppActivityFormExec
return executionNo;
}
private List<MpActivityFormVariableEntity> buildMpActivityFormVariables(ActivityFormSubmitReq req, MpActivityFormExecutionEntity execution) {
// 获取表单字段定义用于确定数据类型
List<MpActivityFormFieldEntity> fieldDefinitions = activityFormFieldService.getFieldListByFormId(req.getFormId());
Map<String, MpActivityFormFieldEntity> fieldTypesMap = fieldDefinitions.stream().filter(item-> StringUtils.isNotBlank(item.getFieldCode()))
.collect(Collectors.toMap(key->key.getFieldCode().trim(),
Function.identity(),
(existing, replacement) -> existing
));
List<MpActivityFormVariableEntity> variables = req.getFormData().entrySet().stream()
.filter(entry -> Objects.nonNull(entry.getKey()) && Objects.nonNull(entry.getValue()))
.map(entry -> {
// 根据fieldType确定数据类型并处理值
String fieldCode = entry.getKey();
MpActivityFormFieldEntity mpActivityFormFieldEntity = fieldTypesMap.get(fieldCode);
if(Objects.nonNull(mpActivityFormFieldEntity)){
String fieldType = mpActivityFormFieldEntity.getFieldType();
MpActivityFormVariableEntity variable = new MpActivityFormVariableEntity();
variable.setExecutionId(execution.getId());
variable.setVariableName(mpActivityFormFieldEntity.getFieldName());
variable.setVariableCode(entry.getKey());
String processedValue = processValueByFieldType(entry.getValue(), fieldType);
String dataType = determineDataTypeByFieldType(fieldType);
variable.setVariableValue(processedValue);
variable.setDataType(dataType);
return variable;
}
return null;
})
.filter(Objects::nonNull) // 过滤掉可能产生的null值
.collect(Collectors.toList());
log.info("表单数据处理完成,变量列表:{}", variables);
return variables;
}
@Override
@Transactional
public boolean updateForm(Integer executionId, ActivityFormSubmitReq req, Integer userId) {
@ -117,16 +154,7 @@ public class AppActivityFormExecutionServiceImpl implements IAppActivityFormExec
// 重新插入新的表单数据
if (CollectionUtil.isNotEmpty(req.getFormData())) {
List<MpActivityFormVariableEntity> variables = req.getFormData().entrySet().stream()
.map(entry -> {
MpActivityFormVariableEntity variable = new MpActivityFormVariableEntity();
variable.setExecutionId(execution.getId());
variable.setVariableName(entry.getKey());
variable.setVariableCode(entry.getKey());
variable.setVariableValue(String.valueOf(entry.getValue()));
variable.setDataType(getDataType(entry.getValue()));
return variable;
}).collect(Collectors.toList());
List<MpActivityFormVariableEntity> variables = buildMpActivityFormVariables(req, execution);
boolean savedBatch = activityFormVariableService.saveBatch(variables);
log.info("保存表单变量结果:{}", savedBatch);
return savedBatch;
@ -136,14 +164,9 @@ public class AppActivityFormExecutionServiceImpl implements IAppActivityFormExec
}
@Override
public AppFormFieldWithValueResp getExecutionWithFormData(Integer activityId, Integer agentId, Integer userId) {
Integer formId = activityFormRelationService.getPrimaryFormIdByActivityId(activityId);
if (Objects.isNull(formId)) {
log.warn("未找到该活动对应的表单ID活动ID{}", activityId);
return null;
}
public AppFormFieldWithValueResp getExecutionWithFormData(Integer activityId, Integer agentId, Integer formId,Integer userId) {
log.info("获取表单执行信息活动ID{}代理ID{}用户ID{},表单ID{}", activityId, agentId, userId,formId);
MpActivityFormExecutionEntity execution = activityFormExecutionService.getOneByActivityIdAndAgentIdAndSubmitterId(activityId, agentId, userId);
MpActivityFormExecutionEntity execution = activityFormExecutionService.getOneByActivityIdAndAgentIdAndSubmitterIdAndFormId(activityId, agentId, userId,formId);
if (Objects.isNull(execution)) {
log.warn("未找到该用户的执行记录活动ID{}代理ID{}用户ID{}", activityId, agentId, userId);
return null;
@ -234,4 +257,71 @@ public class AppActivityFormExecutionServiceImpl implements IAppActivityFormExec
return "string";
}
}
/**
* 根据字段类型处理值
*/
private String processValueByFieldType(Object value, String fieldType) {
if (value == null) {
return null;
}
if (fieldType == null) {
return String.valueOf(value);
}
// 判断是否为复杂类型
if (isComplexType(fieldType)) {
// 对于复杂类型(cascader, upload等)尝试转换为JSON格式
if (value instanceof String) {
return (String) value;
} else {
// 将复杂对象序列化为JSON字符串
try {
return OBJECT_MAPPER.writeValueAsString(value);
} catch (JsonProcessingException e) {
log.warn("序列化复杂类型值为JSON失败: {}", e.getMessage());
return String.valueOf(value);
}
}
} else {
// 对于基本类型(password, radio, textarea等)直接转换为字符串
return String.valueOf(value);
}
}
/**
* 确定数据类型根据字段类型
*/
private String determineDataTypeByFieldType(String fieldType) {
if (fieldType == null) {
return "string";
}
if (isComplexType(fieldType)) {
return "json";
} else {
return "string";
}
}
/**
* 判断是否为复杂类型
*/
private boolean isComplexType(String fieldType) {
if (fieldType == null) {
return false;
}
// 定义复杂类型字段
String[] complexTypes = {"cascader", "upload", "checkbox-group", "select-multiple", "tree-select", "date-range", "time-picker", "image-upload", "file-upload", "rich-editor"};
for (String complexType : complexTypes) {
if (complexType.equalsIgnoreCase(fieldType)) {
return true;
}
}
return false;
}
}

View File

@ -19,8 +19,9 @@ public interface IMpActivityFormExecutionService extends IService<MpActivityForm
* @param activityId 活动ID
* @param agentId 代理ID
* @param submitterId 提交人ID
* @param formId 表单ID
* @return 最近一次提交的表单执行记录
*/
MpActivityFormExecutionEntity getOneByActivityIdAndAgentIdAndSubmitterId(Integer activityId, Integer agentId, Integer submitterId);
MpActivityFormExecutionEntity getOneByActivityIdAndAgentIdAndSubmitterIdAndFormId(Integer activityId, Integer agentId, Integer submitterId, Integer formId);
}

View File

@ -23,9 +23,12 @@ import org.springframework.stereotype.Service;
public class MpActivityFormExecutionServiceImpl extends ServiceImpl<MpActivityFormExecutionMapper, MpActivityFormExecutionEntity> implements IMpActivityFormExecutionService {
@Override
public MpActivityFormExecutionEntity getOneByActivityIdAndAgentIdAndSubmitterId(Integer activityId, Integer agentId, Integer submitterId) {
return super.getOne(new LambdaQueryWrapper<MpActivityFormExecutionEntity>().eq(MpActivityFormExecutionEntity::getActivityId, activityId)
public MpActivityFormExecutionEntity getOneByActivityIdAndAgentIdAndSubmitterIdAndFormId(Integer activityId, Integer agentId, Integer submitterId, Integer formId) {
return super.getOne(new LambdaQueryWrapper<MpActivityFormExecutionEntity>()
.eq(MpActivityFormExecutionEntity::getActivityId, activityId)
.eq(MpActivityFormExecutionEntity::getAgentId, agentId)
.eq(MpActivityFormExecutionEntity::getSubmitterId, submitterId).last("LIMIT 1"));
.eq(MpActivityFormExecutionEntity::getSubmitterId, submitterId)
.eq(MpActivityFormExecutionEntity::getFormId, formId)
.last("LIMIT 1"));
}
}