修改mp模块下@SaCheckLogin,将其修改为@SaCheckPermission
This commit is contained in:
parent
a0106420e0
commit
956cc743f7
@ -2,7 +2,9 @@ package com.seer.teach.mp.mapper;
|
||||
|
||||
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
||||
import com.seer.teach.mp.entity.MpActivityFormVariableEntity;
|
||||
import org.apache.ibatis.annotations.Delete;
|
||||
import org.apache.ibatis.annotations.Mapper;
|
||||
import org.apache.ibatis.annotations.Param;
|
||||
|
||||
/**
|
||||
* <p>
|
||||
@ -15,4 +17,13 @@ import org.apache.ibatis.annotations.Mapper;
|
||||
@Mapper
|
||||
public interface MpActivityFormVariableMapper extends BaseMapper<MpActivityFormVariableEntity> {
|
||||
|
||||
/**
|
||||
* 根据执行id删除
|
||||
* @param executionId 执行id
|
||||
*
|
||||
* @return 删除数量
|
||||
*/
|
||||
@Delete("delete from mp_activity_form_variable where execution_id = #{executionId}")
|
||||
int deleteByExecutionId(@Param("executionId") Integer executionId);
|
||||
|
||||
}
|
||||
@ -1,28 +1,19 @@
|
||||
package com.seer.teach.mp.app.controller;
|
||||
|
||||
import cn.dev33.satoken.annotation.SaCheckPermission;
|
||||
import cn.dev33.satoken.stp.StpUtil;
|
||||
import com.seer.teach.common.PageListBean;
|
||||
import com.seer.teach.common.ResultBean;
|
||||
import com.seer.teach.common.annotation.DecryptionAnnotation;
|
||||
import com.seer.teach.common.annotation.EncryptionAnnotation;
|
||||
import com.seer.teach.common.annotation.LogPrint;
|
||||
import com.seer.teach.mp.app.controller.req.ActivityFormSubmitReq;
|
||||
import com.seer.teach.mp.app.controller.req.AppActivityFormExecutionQueryReq;
|
||||
import com.seer.teach.mp.app.controller.resp.AppActivityFormExecutionResp;
|
||||
import com.seer.teach.mp.app.controller.resp.AppActivityFormFieldResp;
|
||||
import com.seer.teach.mp.app.controller.resp.AppActivityFormResp;
|
||||
import com.seer.teach.mp.app.controller.resp.FormFieldWithValueResp;
|
||||
import com.seer.teach.mp.app.service.IAppActivityFormService;
|
||||
import io.swagger.v3.oas.annotations.Operation;
|
||||
import io.swagger.v3.oas.annotations.tags.Tag;
|
||||
import jakarta.validation.Valid;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
@ -51,56 +42,16 @@ public class AppActivityFormController {
|
||||
@Operation(summary = "获取活动可用的表单")
|
||||
@GetMapping("/{activityId}")
|
||||
@SaCheckPermission("activity.form.view")
|
||||
public ResultBean<AppActivityFormResp> getFormByActivity(@PathVariable Integer activityId) {
|
||||
public ResultBean<AppActivityFormResp> getFormByActivity(@PathVariable("activityId") Integer activityId) {
|
||||
AppActivityFormResp result = appActivityFormService.getFormByActivity(activityId);
|
||||
return ResultBean.success(result);
|
||||
}
|
||||
|
||||
@Operation(summary = "获取表单字段列表")
|
||||
@GetMapping("/field/{templateId}")
|
||||
@GetMapping("/field/{activityId}")
|
||||
@SaCheckPermission("activity.form.field.view")
|
||||
public ResultBean<List<AppActivityFormFieldResp>> getFieldList(@PathVariable Integer templateId) {
|
||||
List<AppActivityFormFieldResp> result = appActivityFormService.getFieldListByTemplate(templateId);
|
||||
return ResultBean.success(result);
|
||||
}
|
||||
|
||||
@Operation(summary = "获取用户提交的表单列表")
|
||||
@PostMapping("/execution/page-list")
|
||||
@SaCheckPermission("activity.form.execution.list")
|
||||
public ResultBean<PageListBean<AppActivityFormExecutionResp>> getExecutionList(@RequestBody @Valid AppActivityFormExecutionQueryReq query) {
|
||||
Integer userId = StpUtil.getLoginIdAsInt();
|
||||
PageListBean<AppActivityFormExecutionResp> result = appActivityFormService.getExecutionList(query, userId);
|
||||
return ResultBean.success(result);
|
||||
}
|
||||
|
||||
@Operation(summary = "提交表单")
|
||||
@PostMapping("/submit")
|
||||
@SaCheckPermission("activity.form.submit")
|
||||
public ResultBean<String> submitForm(@RequestBody @Valid ActivityFormSubmitReq req) {
|
||||
Integer userId = 0;
|
||||
if (StpUtil.isLogin()) {
|
||||
userId = StpUtil.getLoginIdAsInt();
|
||||
}
|
||||
String executionNo = appActivityFormService.submitForm(req, userId);
|
||||
return ResultBean.success(executionNo);
|
||||
}
|
||||
|
||||
@Operation(summary = "获取表单执行详情")
|
||||
@GetMapping("/execution/{id}")
|
||||
@SaCheckPermission("activity.form.execution.detail")
|
||||
public ResultBean<AppActivityFormExecutionResp> getExecutionDetail(@PathVariable Integer id) {
|
||||
return ResultBean.success(appActivityFormService.getExecutionWithFormData(id));
|
||||
}
|
||||
|
||||
@Operation(summary = "获取提交表单详情")
|
||||
@GetMapping("/execution/{activityId}/{agentId}")
|
||||
@SaCheckPermission("activity.form.view")
|
||||
public ResultBean<FormFieldWithValueResp> getFormFieldsWithValues(@PathVariable Integer activityId, @PathVariable Integer agentId) {
|
||||
Integer userId = 0;
|
||||
if (StpUtil.isLogin()) {
|
||||
userId = StpUtil.getLoginIdAsInt();
|
||||
}
|
||||
FormFieldWithValueResp result = appActivityFormService.getFormFieldsWithValues(activityId, agentId,userId);
|
||||
public ResultBean<List<AppActivityFormFieldResp>> getFieldList(@PathVariable("activityId") Integer activityId) {
|
||||
List<AppActivityFormFieldResp> result = appActivityFormService.getFieldListByActivityId(activityId);
|
||||
return ResultBean.success(result);
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,83 @@
|
||||
package com.seer.teach.mp.app.controller;
|
||||
|
||||
import cn.dev33.satoken.annotation.SaCheckPermission;
|
||||
import cn.dev33.satoken.stp.StpUtil;
|
||||
import com.seer.teach.common.PageListBean;
|
||||
import com.seer.teach.common.ResultBean;
|
||||
import com.seer.teach.common.annotation.DecryptionAnnotation;
|
||||
import com.seer.teach.common.annotation.EncryptionAnnotation;
|
||||
import com.seer.teach.common.annotation.LogPrint;
|
||||
import com.seer.teach.mp.app.controller.req.ActivityFormSubmitReq;
|
||||
import com.seer.teach.mp.app.controller.req.AppActivityFormExecutionQueryReq;
|
||||
import com.seer.teach.mp.app.controller.resp.AppActivityFormExecutionResp;
|
||||
import com.seer.teach.mp.app.controller.resp.AppFormFieldWithValueResp;
|
||||
import com.seer.teach.mp.app.service.IAppActivityFormExecutionService;
|
||||
import io.swagger.v3.oas.annotations.Operation;
|
||||
import io.swagger.v3.oas.annotations.tags.Tag;
|
||||
import jakarta.validation.Valid;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.PutMapping;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
/**
|
||||
* <p>
|
||||
* APP端活动表单执行器控制器
|
||||
* </p>
|
||||
*
|
||||
* @author Lingma
|
||||
* @since 2026-01-10
|
||||
*/
|
||||
@Tag(name = "APP - 活动表单执行器")
|
||||
@RestController
|
||||
@RequestMapping("/app/activity/form/execution")
|
||||
@LogPrint
|
||||
@EncryptionAnnotation
|
||||
@DecryptionAnnotation
|
||||
@RequiredArgsConstructor
|
||||
@Slf4j
|
||||
public class AppActivityFormExecutionController {
|
||||
|
||||
private final IAppActivityFormExecutionService appActivityFormExecutionService;
|
||||
|
||||
@Operation(summary = "提交表单")
|
||||
@PostMapping("/submit")
|
||||
@SaCheckPermission("activity.form.execution.submit")
|
||||
public ResultBean<String> submitForm(@RequestBody @Valid ActivityFormSubmitReq req) {
|
||||
Integer userId = 0;
|
||||
if (StpUtil.isLogin()) {
|
||||
userId = StpUtil.getLoginIdAsInt();
|
||||
}
|
||||
String executionNo = appActivityFormExecutionService.submitForm(req, userId);
|
||||
return ResultBean.success(executionNo);
|
||||
}
|
||||
|
||||
@Operation(summary = "更新表单数据")
|
||||
@PutMapping("/{executionId}")
|
||||
@SaCheckPermission("activity.form.execution.update")
|
||||
public ResultBean<Boolean> updateForm(@PathVariable("executionId") Integer executionId, @RequestBody @Valid ActivityFormSubmitReq req) {
|
||||
if (StpUtil.isLogin()) {
|
||||
Integer userId = StpUtil.getLoginIdAsInt();
|
||||
boolean result = appActivityFormExecutionService.updateForm(executionId, req, userId);
|
||||
return ResultBean.success(result);
|
||||
}
|
||||
return ResultBean.error();
|
||||
}
|
||||
|
||||
@Operation(summary = "获取提交表单详情")
|
||||
@GetMapping("/{activityId}/{agentId}")
|
||||
@SaCheckPermission("activity.form.execution.view")
|
||||
public ResultBean<AppFormFieldWithValueResp> getFormFieldsWithValues(@PathVariable("activityId") Integer activityId, @PathVariable("agentId") Integer agentId) {
|
||||
if (StpUtil.isLogin()) {
|
||||
Integer userId = StpUtil.getLoginIdAsInt();
|
||||
AppFormFieldWithValueResp result = appActivityFormExecutionService.getExecutionWithFormData(activityId, agentId, userId);
|
||||
return ResultBean.success(result);
|
||||
}
|
||||
return ResultBean.error();
|
||||
}
|
||||
}
|
||||
@ -13,9 +13,9 @@ import java.util.List;
|
||||
* @author Lingma
|
||||
* @since 2026-01-23
|
||||
*/
|
||||
@Schema(name = "FormFieldWithValueResp", description = "表单字段与值的响应参数")
|
||||
@Schema(name = "AppFormFieldWithValueResp", description = "表单字段与值的响应参数")
|
||||
@Data
|
||||
public class FormFieldWithValueResp {
|
||||
public class AppFormFieldWithValueResp {
|
||||
|
||||
@Schema(description = "表单执行ID")
|
||||
private Integer executionId;
|
||||
@ -26,8 +26,8 @@ public class FormFieldWithValueResp {
|
||||
@Schema(description = "活动ID")
|
||||
private Integer activityId;
|
||||
|
||||
@Schema(description = "表单模板ID")
|
||||
private Integer templateId;
|
||||
@Schema(description = "表单ID")
|
||||
private Integer formId;
|
||||
|
||||
@Schema(description = "提交者ID")
|
||||
private Integer submitterId;
|
||||
@ -0,0 +1,47 @@
|
||||
package com.seer.teach.mp.app.service;
|
||||
|
||||
import com.seer.teach.common.PageListBean;
|
||||
import com.seer.teach.mp.app.controller.req.ActivityFormSubmitReq;
|
||||
import com.seer.teach.mp.app.controller.req.AppActivityFormExecutionQueryReq;
|
||||
import com.seer.teach.mp.app.controller.resp.AppActivityFormExecutionResp;
|
||||
import com.seer.teach.mp.app.controller.resp.AppFormFieldWithValueResp;
|
||||
|
||||
/**
|
||||
* <p>
|
||||
* APP端活动表单服务接口
|
||||
* </p>
|
||||
*
|
||||
* @author Lingma
|
||||
* @since 2026-01-10
|
||||
*/
|
||||
public interface IAppActivityFormExecutionService {
|
||||
|
||||
/**
|
||||
* 提交表单
|
||||
*
|
||||
* @param req 表单提交请求
|
||||
* @param userId 用户ID
|
||||
* @return 执行编号
|
||||
*/
|
||||
String submitForm(ActivityFormSubmitReq req, Integer userId);
|
||||
|
||||
/**
|
||||
* 获取表单字段及提交值详情
|
||||
*
|
||||
* @param activityId 活动ID
|
||||
* @param agentId 代理商ID
|
||||
* @param userId 用户ID
|
||||
* @return 表单字段及值详情
|
||||
*/
|
||||
AppFormFieldWithValueResp getExecutionWithFormData(Integer activityId, Integer agentId, Integer userId);
|
||||
|
||||
/**
|
||||
* 更新表单数据
|
||||
*
|
||||
* @param executionId 执行ID
|
||||
* @param req 表单提交请求
|
||||
* @param userId 用户ID
|
||||
* @return 是否更新成功
|
||||
*/
|
||||
boolean updateForm(Integer executionId, ActivityFormSubmitReq req, Integer userId);
|
||||
}
|
||||
@ -1,12 +1,7 @@
|
||||
package com.seer.teach.mp.app.service;
|
||||
|
||||
import com.seer.teach.common.PageListBean;
|
||||
import com.seer.teach.mp.app.controller.req.ActivityFormSubmitReq;
|
||||
import com.seer.teach.mp.app.controller.req.AppActivityFormExecutionQueryReq;
|
||||
import com.seer.teach.mp.app.controller.resp.AppActivityFormExecutionResp;
|
||||
import com.seer.teach.mp.app.controller.resp.AppActivityFormResp;
|
||||
import com.seer.teach.mp.app.controller.resp.AppActivityFormFieldResp;
|
||||
import com.seer.teach.mp.app.controller.resp.FormFieldWithValueResp;
|
||||
import com.seer.teach.mp.app.controller.resp.AppActivityFormResp;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@ -21,54 +16,19 @@ import java.util.List;
|
||||
public interface IAppActivityFormService {
|
||||
|
||||
/**
|
||||
* 根据活动ID获取表单模板
|
||||
* 根据活动ID获取表单表单
|
||||
*
|
||||
* @param activityId 活动ID
|
||||
* @return 表单模板响应对象
|
||||
* @return 表单表单响应对象
|
||||
*/
|
||||
AppActivityFormResp getFormByActivity(Integer activityId);
|
||||
|
||||
/**
|
||||
* 根据模板ID获取字段列表
|
||||
* 根据表单ID获取字段列表
|
||||
*
|
||||
* @param templateId 模板ID
|
||||
* @param activityId 表单ID
|
||||
* @return 字段列表
|
||||
*/
|
||||
List<AppActivityFormFieldResp> getFieldListByTemplate(Integer templateId);
|
||||
List<AppActivityFormFieldResp> getFieldListByActivityId(Integer activityId);
|
||||
|
||||
/**
|
||||
* 提交表单
|
||||
*
|
||||
* @param req 表单提交请求
|
||||
* @param userId 用户ID
|
||||
* @return 执行编号
|
||||
*/
|
||||
String submitForm(ActivityFormSubmitReq req, Integer userId);
|
||||
|
||||
/**
|
||||
* 根据执行ID获取完整表单数据
|
||||
*
|
||||
* @param executionId 执行ID
|
||||
* @return 完整表单数据
|
||||
*/
|
||||
AppActivityFormExecutionResp getExecutionWithFormData(Integer executionId);
|
||||
|
||||
/**
|
||||
* 根据查询条件获取执行列表
|
||||
*
|
||||
* @param query 查询条件
|
||||
* @param userId 用户ID
|
||||
* @return 执行记录分页列表
|
||||
*/
|
||||
PageListBean<AppActivityFormExecutionResp> getExecutionList(AppActivityFormExecutionQueryReq query, Integer userId);
|
||||
|
||||
/**
|
||||
* 获取表单字段及提交值详情
|
||||
*
|
||||
* @param activityId 活动ID
|
||||
* @param agentId 代理商ID
|
||||
* @param userId 用户ID
|
||||
* @return 表单字段及值详情
|
||||
*/
|
||||
FormFieldWithValueResp getFormFieldsWithValues(Integer activityId, Integer agentId, Integer userId);
|
||||
}
|
||||
@ -0,0 +1,217 @@
|
||||
package com.seer.teach.mp.app.service.impl;
|
||||
|
||||
import cn.hutool.core.collection.CollectionUtil;
|
||||
import com.seer.teach.common.utils.OrderIdGenerator;
|
||||
import com.seer.teach.mp.app.controller.req.ActivityFormSubmitReq;
|
||||
import com.seer.teach.mp.app.controller.resp.AppFormFieldWithValueResp;
|
||||
import com.seer.teach.mp.app.convert.AppActivityFormConvert;
|
||||
import com.seer.teach.mp.app.service.IAppActivityFormExecutionService;
|
||||
import com.seer.teach.mp.entity.MpActivityFormExecutionEntity;
|
||||
import com.seer.teach.mp.entity.MpActivityFormFieldEntity;
|
||||
import com.seer.teach.mp.entity.MpActivityFormVariableEntity;
|
||||
import com.seer.teach.mp.service.IMpActivityFormExecutionService;
|
||||
import com.seer.teach.mp.service.IMpActivityFormFieldService;
|
||||
import com.seer.teach.mp.service.IMpActivityFormVariableService;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
* <p>
|
||||
* APP端活动表单执行器服务实现类
|
||||
* </p>
|
||||
*
|
||||
* @author Lingma
|
||||
* @since 2026-01-10
|
||||
*/
|
||||
@Slf4j
|
||||
@Service
|
||||
@RequiredArgsConstructor
|
||||
public class AppActivityFormExecutionServiceImpl implements IAppActivityFormExecutionService {
|
||||
|
||||
private final IMpActivityFormExecutionService activityFormExecutionService;
|
||||
private final IMpActivityFormVariableService activityFormVariableService;
|
||||
private final IMpActivityFormFieldService activityFormFieldService;
|
||||
|
||||
|
||||
@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); // 设置提交人ID为当前登录用户ID
|
||||
execution.setSubmitTime(LocalDateTime.now());
|
||||
execution.setStatus("submitted"); // 设置状态为已提交
|
||||
|
||||
// 保存表单执行实例
|
||||
activityFormExecutionService.save(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());
|
||||
activityFormVariableService.saveBatch(variables);
|
||||
}
|
||||
|
||||
log.info("表单提交成功,执行编号:{}", executionNo);
|
||||
return executionNo;
|
||||
}
|
||||
|
||||
@Override
|
||||
@Transactional
|
||||
public boolean updateForm(Integer executionId, ActivityFormSubmitReq req, Integer userId) {
|
||||
// 首先检查执行记录是否存在
|
||||
MpActivityFormExecutionEntity execution = activityFormExecutionService.getById(executionId);
|
||||
if (execution == null) {
|
||||
log.warn("执行记录不存在,ID:{}", executionId);
|
||||
return false;
|
||||
}
|
||||
|
||||
// 检查用户是否有权限更新此记录(可选的安全检查)
|
||||
if (!execution.getSubmitterId().equals(userId)) {
|
||||
log.warn("用户 {} 无权限更新执行记录 {}", userId, executionId);
|
||||
return false;
|
||||
}
|
||||
|
||||
// 更新执行记录的基本信息
|
||||
execution.setActivityId(req.getActivityId());
|
||||
execution.setAgentId(req.getAgentId());
|
||||
execution.setFormId(req.getFormId());
|
||||
execution.setSubmitterId(userId);
|
||||
execution.setSubmitTime(LocalDateTime.now());
|
||||
execution.setStatus("updated");
|
||||
|
||||
// 保存更新后的执行记录
|
||||
activityFormExecutionService.updateById(execution);
|
||||
|
||||
// 删除原有的变量数据
|
||||
boolean deletedFormVariablesResult = activityFormVariableService.deleteByExecutionId(executionId);
|
||||
log.info("删除表单变量结果:{}", deletedFormVariablesResult);
|
||||
|
||||
// 重新插入新的表单数据
|
||||
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());
|
||||
boolean savedBatch = activityFormVariableService.saveBatch(variables);
|
||||
log.info("保存表单变量结果:{}", savedBatch);
|
||||
return savedBatch;
|
||||
}
|
||||
log.info("表单更新成功,执行编号:{}", execution.getExecutionNo());
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public AppFormFieldWithValueResp getExecutionWithFormData(Integer activityId, Integer agentId, Integer userId) {
|
||||
MpActivityFormExecutionEntity execution = activityFormExecutionService.getOneByActivityIdAndAgentIdAndSubmitterId(activityId, agentId, userId);
|
||||
if (Objects.isNull(execution)) {
|
||||
return null;
|
||||
}
|
||||
// 获取表单字段定义
|
||||
List<MpActivityFormFieldEntity> fieldDefinitions = activityFormFieldService.getFieldListByFormId(execution.getFormId());
|
||||
|
||||
// 获取表单提交的数据
|
||||
List<MpActivityFormVariableEntity> variables = activityFormVariableService.getListByExecutionId(execution.getId());
|
||||
|
||||
Map<String, Object> formData = new HashMap<>();
|
||||
for (MpActivityFormVariableEntity variable : variables) {
|
||||
Object value = convertValueByType(variable.getVariableValue(), variable.getDataType());
|
||||
formData.put(variable.getVariableCode(), value);
|
||||
}
|
||||
|
||||
// 构建响应对象
|
||||
AppFormFieldWithValueResp response = new AppFormFieldWithValueResp();
|
||||
response.setExecutionId(execution.getId());
|
||||
response.setExecutionNo(execution.getExecutionNo());
|
||||
response.setActivityId(execution.getActivityId());
|
||||
response.setFormId(execution.getFormId());
|
||||
response.setSubmitterId(execution.getSubmitterId());
|
||||
// 组合字段定义和对应的值
|
||||
List<AppFormFieldWithValueResp.FieldWithValueItemResp> fields = fieldDefinitions.stream()
|
||||
.map(field -> {
|
||||
AppFormFieldWithValueResp.FieldWithValueItemResp item = new AppFormFieldWithValueResp.FieldWithValueItemResp();
|
||||
item.setFieldDefinition(AppActivityFormConvert.INSTANCE.convertToAppFieldResp(field));
|
||||
// 如果有对应的值则设置,否则为null
|
||||
item.setFieldValue(formData.get(field.getFieldCode()));
|
||||
return item;
|
||||
})
|
||||
.collect(Collectors.toList());
|
||||
|
||||
response.setFields(fields);
|
||||
|
||||
return response;
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据数据类型转换值
|
||||
*/
|
||||
private Object convertValueByType(String value, String dataType) {
|
||||
if (value == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
switch (dataType) {
|
||||
case "boolean":
|
||||
return Boolean.parseBoolean(value);
|
||||
case "number":
|
||||
try {
|
||||
// 尝试解析为整数或浮点数
|
||||
if (value.contains(".")) {
|
||||
return Double.parseDouble(value);
|
||||
} else {
|
||||
return Long.parseLong(value);
|
||||
}
|
||||
} catch (NumberFormatException e) {
|
||||
return value; // 如果无法解析为数字,则返回原始字符串
|
||||
}
|
||||
case "string":
|
||||
default:
|
||||
return value;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据值判断数据类型
|
||||
*/
|
||||
private String getDataType(Object value) {
|
||||
if (value instanceof Boolean) {
|
||||
return "boolean";
|
||||
} else if (value instanceof Number) {
|
||||
return "number";
|
||||
} else if (value instanceof String) {
|
||||
return "string";
|
||||
} else {
|
||||
return "string";
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -1,25 +1,14 @@
|
||||
package com.seer.teach.mp.app.service.impl;
|
||||
|
||||
import cn.hutool.core.collection.CollectionUtil;
|
||||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
||||
import com.seer.teach.common.PageListBean;
|
||||
import com.seer.teach.common.utils.OrderIdGenerator;
|
||||
import com.seer.teach.common.utils.PageConverterUtils;
|
||||
import com.seer.teach.mp.app.controller.req.ActivityFormSubmitReq;
|
||||
import com.seer.teach.mp.app.controller.req.AppActivityFormExecutionQueryReq;
|
||||
import com.seer.teach.mp.app.controller.resp.AppActivityFormExecutionResp;
|
||||
import com.seer.teach.mp.app.controller.resp.AppActivityFormFieldResp;
|
||||
import com.seer.teach.mp.app.controller.resp.AppActivityFormResp;
|
||||
import com.seer.teach.mp.app.controller.resp.FormFieldWithValueResp;
|
||||
import com.seer.teach.mp.app.convert.AppActivityFormConvert;
|
||||
import com.seer.teach.mp.app.service.IAppActivityFormService;
|
||||
import com.seer.teach.mp.entity.MpActivityEntity;
|
||||
import com.seer.teach.mp.entity.MpActivityFormEntity;
|
||||
import com.seer.teach.mp.entity.MpActivityFormExecutionEntity;
|
||||
import com.seer.teach.mp.entity.MpActivityFormFieldEntity;
|
||||
import com.seer.teach.mp.entity.MpActivityFormRelationEntity;
|
||||
import com.seer.teach.mp.entity.MpActivityFormVariableEntity;
|
||||
import com.seer.teach.mp.service.IMpActivityFormExecutionService;
|
||||
import com.seer.teach.mp.service.IMpActivityFormFieldService;
|
||||
import com.seer.teach.mp.service.IMpActivityFormRelationService;
|
||||
@ -29,12 +18,8 @@ import com.seer.teach.mp.service.IMpActivityService;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@ -53,8 +38,6 @@ public class AppActivityFormServiceImpl implements IAppActivityFormService {
|
||||
|
||||
private final IMpActivityFormService activityFormTemplateService;
|
||||
private final IMpActivityFormFieldService activityFormFieldService;
|
||||
private final IMpActivityFormExecutionService activityFormExecutionService;
|
||||
private final IMpActivityFormVariableService activityFormVariableService;
|
||||
private final IMpActivityService mpActivityService;
|
||||
private final IMpActivityFormRelationService activityFormTemplateRelationService;
|
||||
|
||||
@ -92,10 +75,15 @@ public class AppActivityFormServiceImpl implements IAppActivityFormService {
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<AppActivityFormFieldResp> getFieldListByTemplate(Integer templateId) {
|
||||
public List<AppActivityFormFieldResp> getFieldListByActivityId(Integer activityId) {
|
||||
MpActivityEntity activity = mpActivityService.getById(activityId);
|
||||
if (activity == null) {
|
||||
log.warn("活动不存在,ID:{}", activityId);
|
||||
return null;
|
||||
}
|
||||
List<MpActivityFormFieldEntity> fields = activityFormFieldService.list(
|
||||
new LambdaQueryWrapper<MpActivityFormFieldEntity>()
|
||||
.eq(MpActivityFormFieldEntity::getFormId, templateId)
|
||||
.eq(MpActivityFormFieldEntity::getFormId, activityId)
|
||||
.orderByAsc(MpActivityFormFieldEntity::getSortOrder)
|
||||
.orderByDesc(MpActivityFormFieldEntity::getId)
|
||||
);
|
||||
@ -104,176 +92,4 @@ public class AppActivityFormServiceImpl implements IAppActivityFormService {
|
||||
.map(AppActivityFormConvert.INSTANCE::convertToAppFieldResp)
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
|
||||
@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); // 设置提交人ID为当前登录用户ID
|
||||
execution.setSubmitTime(LocalDateTime.now());
|
||||
execution.setStatus("submitted"); // 设置状态为已提交
|
||||
|
||||
// 保存表单执行实例
|
||||
activityFormExecutionService.save(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());
|
||||
activityFormVariableService.saveBatch(variables);
|
||||
}
|
||||
|
||||
log.info("表单提交成功,执行编号:{}", executionNo);
|
||||
return executionNo;
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据执行ID获取完整表单数据
|
||||
*/
|
||||
public AppActivityFormExecutionResp getExecutionWithFormData(Integer executionId) {
|
||||
MpActivityFormExecutionEntity execution = activityFormExecutionService.getById(executionId);
|
||||
if (execution == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
AppActivityFormExecutionResp resp = AppActivityFormConvert.INSTANCE.convertToAppExecutionResp(execution);
|
||||
|
||||
// 获取表单变量数据
|
||||
List<MpActivityFormVariableEntity> variables = activityFormVariableService.list(
|
||||
new LambdaQueryWrapper<MpActivityFormVariableEntity>()
|
||||
.eq(MpActivityFormVariableEntity::getExecutionId, executionId)
|
||||
);
|
||||
|
||||
// 转换为Map形式的表单数据
|
||||
Map<String, Object> formData = new HashMap<>();
|
||||
for (MpActivityFormVariableEntity variable : variables) {
|
||||
// 根据数据类型转换值
|
||||
Object value = convertValueByType(variable.getVariableValue(), variable.getDataType());
|
||||
formData.put(variable.getVariableCode(), value);
|
||||
}
|
||||
|
||||
resp.setFormData(formData);
|
||||
return resp;
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据数据类型转换值
|
||||
*/
|
||||
private Object convertValueByType(String value, String dataType) {
|
||||
if (value == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
switch (dataType) {
|
||||
case "boolean":
|
||||
return Boolean.parseBoolean(value);
|
||||
case "number":
|
||||
try {
|
||||
// 尝试解析为整数或浮点数
|
||||
if (value.contains(".")) {
|
||||
return Double.parseDouble(value);
|
||||
} else {
|
||||
return Long.parseLong(value);
|
||||
}
|
||||
} catch (NumberFormatException e) {
|
||||
return value; // 如果无法解析为数字,则返回原始字符串
|
||||
}
|
||||
case "string":
|
||||
default:
|
||||
return value;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据值判断数据类型
|
||||
*/
|
||||
private String getDataType(Object value) {
|
||||
if (value instanceof Boolean) {
|
||||
return "boolean";
|
||||
} else if (value instanceof Number) {
|
||||
return "number";
|
||||
} else if (value instanceof String) {
|
||||
return "string";
|
||||
} else {
|
||||
return "string";
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public PageListBean<AppActivityFormExecutionResp> getExecutionList(AppActivityFormExecutionQueryReq query, Integer userId) {
|
||||
Page<MpActivityFormExecutionEntity> page = new Page<>(query.getPageNo(), query.getPageSize());
|
||||
|
||||
LambdaQueryWrapper<MpActivityFormExecutionEntity> wrapper = new LambdaQueryWrapper<>();
|
||||
wrapper.eq(MpActivityFormExecutionEntity::getSubmitterId, userId)
|
||||
.eq(query.getActivityId() != null, MpActivityFormExecutionEntity::getActivityId, query.getActivityId())
|
||||
.eq(query.getAgentId() != null, MpActivityFormExecutionEntity::getAgentId, query.getAgentId())
|
||||
.orderByDesc(MpActivityFormExecutionEntity::getId);
|
||||
|
||||
Page<MpActivityFormExecutionEntity> pageResult = activityFormExecutionService.page(page, wrapper);
|
||||
List<AppActivityFormExecutionResp> records = pageResult.getRecords().stream()
|
||||
.map(AppActivityFormConvert.INSTANCE::convertToAppExecutionResp)
|
||||
.collect(Collectors.toList());
|
||||
|
||||
return PageConverterUtils.convertPageListBean(pageResult, records);
|
||||
}
|
||||
|
||||
@Override
|
||||
public FormFieldWithValueResp getFormFieldsWithValues(Integer activityId, Integer agentId, Integer userId) {
|
||||
// 查找最近一次提交的表单执行记录
|
||||
MpActivityFormExecutionEntity execution = activityFormExecutionService.getOneByActivityIdAndAgentIdAndSubmitterId(activityId, agentId, userId);
|
||||
|
||||
if (execution == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
// 获取表单字段定义
|
||||
List<AppActivityFormFieldResp> fieldDefinitions = getFieldListByTemplate(execution.getFormId());
|
||||
|
||||
// 获取表单提交的数据
|
||||
List<MpActivityFormVariableEntity> variables = activityFormVariableService.getListByExecutionId(execution.getId());
|
||||
|
||||
Map<String, Object> formData = new HashMap<>();
|
||||
for (MpActivityFormVariableEntity variable : variables) {
|
||||
Object value = convertValueByType(variable.getVariableValue(), variable.getDataType());
|
||||
formData.put(variable.getVariableCode(), value);
|
||||
}
|
||||
|
||||
// 构建响应对象
|
||||
FormFieldWithValueResp response = new FormFieldWithValueResp();
|
||||
response.setExecutionId(execution.getId());
|
||||
response.setExecutionNo(execution.getExecutionNo());
|
||||
response.setActivityId(execution.getActivityId());
|
||||
response.setTemplateId(execution.getFormId());
|
||||
response.setExecutionId(execution.getSubmitterId());
|
||||
// 组合字段定义和对应的值
|
||||
List<FormFieldWithValueResp.FieldWithValueItemResp> fields = fieldDefinitions.stream()
|
||||
.map(field -> {
|
||||
FormFieldWithValueResp.FieldWithValueItemResp item = new FormFieldWithValueResp.FieldWithValueItemResp();
|
||||
item.setFieldDefinition(field);
|
||||
// 如果有对应的值则设置,否则为null
|
||||
item.setFieldValue(formData.get(field.getFieldCode()));
|
||||
return item;
|
||||
})
|
||||
.collect(Collectors.toList());
|
||||
|
||||
response.setFields(fields);
|
||||
|
||||
return response;
|
||||
}
|
||||
}
|
||||
@ -3,6 +3,8 @@ package com.seer.teach.mp.service;
|
||||
import com.baomidou.mybatisplus.extension.service.IService;
|
||||
import com.seer.teach.mp.entity.MpActivityFormFieldEntity;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* <p>
|
||||
* 活动表单字段定义表 服务类
|
||||
@ -21,4 +23,11 @@ public interface IMpActivityFormFieldService extends IService<MpActivityFormFiel
|
||||
*/
|
||||
boolean deleteByFormId(Integer formId);
|
||||
|
||||
/**
|
||||
* 根据表单ID获取表单字段定义
|
||||
*
|
||||
* @param formId
|
||||
* @return 表单字段定义列表
|
||||
*/
|
||||
List<MpActivityFormFieldEntity> getFieldListByFormId(Integer formId);
|
||||
}
|
||||
@ -22,4 +22,12 @@ public interface IMpActivityFormVariableService extends IService<MpActivityFormV
|
||||
* @return 变量列表
|
||||
*/
|
||||
List<MpActivityFormVariableEntity> getListByExecutionId(Integer executionId);
|
||||
|
||||
/**
|
||||
* 根据执行ID删除变量列表
|
||||
*
|
||||
* @param executionId 执行ID
|
||||
* @return 是否成功
|
||||
*/
|
||||
boolean deleteByExecutionId(Integer executionId);
|
||||
}
|
||||
@ -1,5 +1,6 @@
|
||||
package com.seer.teach.mp.service.impl;
|
||||
|
||||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||
import com.seer.teach.mp.entity.MpActivityFormFieldEntity;
|
||||
import com.seer.teach.mp.mapper.MpActivityFormFieldMapper;
|
||||
@ -8,6 +9,7 @@ import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
|
||||
/**
|
||||
@ -30,4 +32,16 @@ public class MpActivityFormFieldServiceImpl extends ServiceImpl<MpActivityFormFi
|
||||
}
|
||||
return super.getBaseMapper().deleteByFormId(formId) > 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<MpActivityFormFieldEntity> getFieldListByFormId(Integer formId) {
|
||||
if (Objects.isNull(formId) || formId == 0) {
|
||||
return List.of();
|
||||
}
|
||||
|
||||
return super.list(new LambdaQueryWrapper<MpActivityFormFieldEntity>()
|
||||
.eq(MpActivityFormFieldEntity::getFormId, formId)
|
||||
.orderByAsc(MpActivityFormFieldEntity::getSortOrder)
|
||||
.orderByDesc(MpActivityFormFieldEntity::getId));
|
||||
}
|
||||
}
|
||||
@ -1,5 +1,6 @@
|
||||
package com.seer.teach.mp.service.impl;
|
||||
|
||||
import cn.hutool.core.collection.CollectionUtil;
|
||||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||
import com.seer.teach.common.constants.CommonConstant;
|
||||
@ -10,6 +11,8 @@ import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.Comparator;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
/**
|
||||
@ -27,12 +30,22 @@ public class MpActivityFormRelationServiceImpl extends ServiceImpl<MpActivityFor
|
||||
|
||||
@Override
|
||||
public Integer getPrimaryFormIdByActivityId(Integer activityId) {
|
||||
MpActivityFormRelationEntity relation = this.getOne(
|
||||
List<MpActivityFormRelationEntity> relations = this.list(
|
||||
new LambdaQueryWrapper<MpActivityFormRelationEntity>()
|
||||
.eq(MpActivityFormRelationEntity::getActivityId, activityId)
|
||||
.eq(MpActivityFormRelationEntity::getIsPrimary, CommonConstant.ENABLE)
|
||||
);
|
||||
return relation != null ? relation.getFormId() : null;
|
||||
if(CollectionUtil.isEmpty(relations)){
|
||||
return 0;
|
||||
}
|
||||
Optional<MpActivityFormRelationEntity> first = relations.stream().filter(item -> item.getIsPrimary() == CommonConstant.ENABLE).findFirst();
|
||||
if(first.isPresent()){
|
||||
return first.get().getFormId();
|
||||
}
|
||||
// 按版本号排序,获取最新版本
|
||||
Optional<MpActivityFormRelationEntity> latest = relations.stream()
|
||||
.max(Comparator.comparing(this::parseVersionToNumber));
|
||||
|
||||
return latest.map(MpActivityFormRelationEntity::getFormId).orElse(0);
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -63,13 +76,45 @@ public class MpActivityFormRelationServiceImpl extends ServiceImpl<MpActivityFor
|
||||
|
||||
@Override
|
||||
public Optional<MpActivityFormRelationEntity> getLatestVersionByActivityId(Integer activityId) {
|
||||
MpActivityFormRelationEntity relation = this.getOne(
|
||||
List<MpActivityFormRelationEntity> relations = this.list(
|
||||
new LambdaQueryWrapper<MpActivityFormRelationEntity>()
|
||||
.eq(MpActivityFormRelationEntity::getActivityId, activityId)
|
||||
.orderByDesc(MpActivityFormRelationEntity::getId)
|
||||
.last("LIMIT 1")
|
||||
);
|
||||
log.info("getLatestVersionByActivityId: {}", relation);
|
||||
return Optional.ofNullable(relation);
|
||||
|
||||
if (CollectionUtil.isEmpty(relations)) {
|
||||
return Optional.empty();
|
||||
}
|
||||
|
||||
// 按版本号排序,获取最新版本
|
||||
Optional<MpActivityFormRelationEntity> latest = relations.stream()
|
||||
.max(Comparator.comparing(this::parseVersionToNumber));
|
||||
|
||||
log.info("getLatestVersionByActivityId: {}", latest.orElse(null));
|
||||
return latest;
|
||||
}
|
||||
|
||||
/**
|
||||
* 将版本号字符串转换为可比较的数字
|
||||
* 例如: "1.0.0" -> 1000000, "2.1.3" -> 2001003
|
||||
*/
|
||||
private long parseVersionToNumber(MpActivityFormRelationEntity entity) {
|
||||
if (entity.getVersion() == null || entity.getVersion().isEmpty()) {
|
||||
return 0L;
|
||||
}
|
||||
|
||||
String[] parts = entity.getVersion().split("\\.");
|
||||
long result = 0;
|
||||
|
||||
for (int i = 0; i < Math.min(parts.length, 3); i++) {
|
||||
try {
|
||||
int num = Integer.parseInt(parts[i]);
|
||||
result += (long) num * (long) Math.pow(1000, 2 - i);
|
||||
} catch (NumberFormatException e) {
|
||||
// 如果版本号格式不正确,返回0
|
||||
return 0L;
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
}
|
||||
@ -10,6 +10,7 @@ import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
|
||||
/**
|
||||
* <p>
|
||||
@ -28,4 +29,14 @@ public class MpActivityFormVariableServiceImpl extends ServiceImpl<MpActivityFor
|
||||
public List<MpActivityFormVariableEntity> getListByExecutionId(Integer executionId) {
|
||||
return super.list(new LambdaQueryWrapper<MpActivityFormVariableEntity>().eq(MpActivityFormVariableEntity::getExecutionId, executionId));
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean deleteByExecutionId(Integer executionId) {
|
||||
if(Objects.isNull(executionId) || executionId == 0){
|
||||
return false;
|
||||
}
|
||||
int deleteCount = this.getBaseMapper().deleteByExecutionId(executionId);
|
||||
log.info("删除表单变量数量:{}", deleteCount);
|
||||
return deleteCount > 0;
|
||||
}
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user