!7 修改后端对json数据的接收方式,使用JacksonTypeHandler来做读取写入时的自动处理

Merge pull request !7 from new_new_new/feature/task_json_object
This commit is contained in:
new_new_new
2026-04-20 08:26:51 +00:00
committed by Gitee
5 changed files with 49 additions and 58 deletions
@@ -19,6 +19,7 @@ import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.modules.extbpm.process.service.impl.BpmBaseExtApiImpl;
import org.jeecg.modules.tasktask.constant.FlowConstants;
import org.jeecg.modules.tasktask.dto.RecurringTaskDTO;
import org.jeecg.modules.tasktask.entity.TaskTask;
import org.jeecg.modules.tasktask.mapper.TaskTaskMapper;
import org.jeecg.modules.tasktask.service.ITaskTaskService;
@@ -108,14 +109,14 @@ public class TaskTaskController extends JeecgController<TaskTask, ITaskTaskServi
/**
* 添加
*
* @param taskTask
* @param recurringTaskDTO
* @return
*/
@AutoLog(value = "事项任务计划表(发起周期性流程)-添加")
@Operation(summary = "事项任务计划表(发起周期性流程)-添加")
@RequiresPermissions("tasktask:task_task:add")
@PostMapping(value = "/flow-schedules")
public Result<?> addRecurringTask(@RequestBody TaskTask taskTask, Integer intervalType, Integer startCount) {
public Result<?> addRecurringTask(@RequestBody RecurringTaskDTO recurringTaskDTO) throws Exception {
// 获取用户信息
LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
String username = sysUser.getUsername();
@@ -126,7 +127,7 @@ public class TaskTaskController extends JeecgController<TaskTask, ITaskTaskServi
}
// 业务逻辑全部交给 Service
return taskTaskService.handleFlowStart(taskTask, username, intervalType, startCount);
return taskTaskService.handleFlowStart(recurringTaskDTO.getTaskTask(), username, recurringTaskDTO.getIntervalType(), recurringTaskDTO.getStartCount());
}
/**
@@ -0,0 +1,11 @@
package org.jeecg.modules.tasktask.dto;
import lombok.Data;
import org.jeecg.modules.tasktask.entity.TaskTask;
@Data
public class RecurringTaskDTO {
private TaskTask taskTask; // 或者直接展开 TaskTask 的属性
private Integer intervalType;
private Integer startCount;
}
@@ -4,10 +4,10 @@ import java.io.Serializable;
import java.io.UnsupportedEncodingException;
import java.util.Date;
import java.math.BigDecimal;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import com.baomidou.mybatisplus.annotation.TableLogic;
import java.util.Map;
import com.baomidou.mybatisplus.annotation.*;
import com.baomidou.mybatisplus.extension.handlers.JacksonTypeHandler;
import org.jeecg.common.constant.ProvinceCityArea;
import org.jeecg.common.util.SpringContextUtils;
import lombok.Data;
@@ -26,7 +26,7 @@ import lombok.experimental.Accessors;
* @Version: V1.0
*/
@Data
@TableName("task_task")
@TableName(value = "task_task", autoResultMap = true)
@Accessors(chain = true)
@EqualsAndHashCode(callSuper = false)
@Schema(description="事项任务计划表(发起流程)")
@@ -235,29 +235,7 @@ public class TaskTask implements Serializable {
@Schema(description = "1立即发起且非周期性发起,2不立即发起但周期性发起,3立即发起且周期性发起")
private java.lang.Integer triggerType;
private byte[] jsonData;
@TableField(typeHandler = JacksonTypeHandler.class)
private Map<String, Object> jsonData;
public byte[] getJsonData(){
if(jsonDataString==null){
return null;
}
try {
return jsonDataString.getBytes("UTF-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return null;
}
public String getJsonDataString(){
if(jsonData==null || jsonData.length==0){
return "";
}
try {
return new String(jsonData,"UTF-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return "";
}
}
@@ -13,5 +13,5 @@ import com.baomidou.mybatisplus.extension.service.IService;
public interface ITaskTaskService extends IService<TaskTask> {
boolean singleFlowStart(TaskTask taskTask,String userName) throws Exception;
boolean multiFlowStart(TaskTask taskTask,String userName,Integer intervalType, Integer startCount) throws Exception;
Result<?> handleFlowStart(TaskTask taskTask, String username, Integer intervalType, Integer startCount);
Result<?> handleFlowStart(TaskTask taskTask, String username, Integer intervalType, Integer startCount)throws Exception;
}
@@ -33,15 +33,6 @@ public class TaskTaskServiceImpl extends ServiceImpl<TaskTaskMapper, TaskTask> i
private BpmBaseExtApiImpl bpmBaseExtApiImpl;
@Transactional(rollbackFor = Exception.class)
public boolean singleFlowStart(TaskTask taskTask, String userName) throws Exception {
if (StringUtils.isBlank(taskTask.getFlowCode())) {
throw new IllegalArgumentException("flowCode不能为空");
}
if (StringUtils.isBlank(taskTask.getBusinessId())) {
throw new IllegalArgumentException("businessId不能为空");
}
if (StringUtils.isBlank(taskTask.getFormUrl())) {
throw new IllegalArgumentException("formUrl不能为空");
}
String jsonString = Optional.ofNullable(taskTask.getJsonData())
.map(JSON::toJSONString) // 使用 Fastjson 或 Jackson
.orElse("{}");
@@ -75,7 +66,7 @@ public class TaskTaskServiceImpl extends ServiceImpl<TaskTaskMapper, TaskTask> i
}
public Result<?> handleFlowStart(TaskTask taskTask, String username, Integer intervalType, Integer startCount) throws Exception{
// 1. 显式校验:代替 Assert
Result<?> checkResult = validateBusinessParams(taskTask, startCount);
Result<?> checkResult = validateBusinessParams(taskTask);
if (!checkResult.isSuccess()) {
return checkResult; // 校验不通过直接返回错误
}
@@ -89,20 +80,6 @@ public class TaskTaskServiceImpl extends ServiceImpl<TaskTaskMapper, TaskTask> i
}
if (FlowConstants.Strategy.RECURRING_ONLY.equals(triggerType)) {
boolean success = multiFlowStart(taskTask, username, intervalType, startCount);
return success ? Result.ok("周期性流程添加成功") : Result.error("周期性流程添加失败");
}
// 3. 保底处理
return Result.error("未知的触发类型: " + triggerType);
}
private Result<?> validateBusinessParams(TaskTask taskTask, Integer startCount) {
if (taskTask.getTriggerType() == null) {
return Result.error("触发类型不能为空");
}
// 如果是周期任务,校验次数和开始时间
if (FlowConstants.Strategy.RECURRING_ONLY.equals(taskTask.getTriggerType())) {
if (startCount == null || startCount <= 0) {
return Result.error("周期任务生成数量必须大于0");
}
@@ -112,7 +89,31 @@ public class TaskTaskServiceImpl extends ServiceImpl<TaskTaskMapper, TaskTask> i
if (taskTask.getStartTime() == null) {
return Result.error("周期性流程开始时间不能为空");
}
boolean success = multiFlowStart(taskTask, username, intervalType, startCount);
return success ? Result.ok("周期性流程添加成功") : Result.error("周期性流程添加失败");
}
// 3. 保底处理
return Result.error("未知的触发类型: " + triggerType);
}
private Result<?> validateBusinessParams(TaskTask taskTask) {
if(taskTask == null){
return Result.error("Task对象不能未空");
}
if (taskTask.getTriggerType() == null) {
return Result.error("触发类型不能为空");
}
//立即督办或周期性督办都需要该校验流程
if (StringUtils.isBlank(taskTask.getFlowCode())) {
return Result.error("flowCode不能为空");
}
if (StringUtils.isBlank(taskTask.getBusinessId())) {
return Result.error("businessId不能为空");
}
if (StringUtils.isBlank(taskTask.getFormUrl())) {
return Result.error("formUrl不能为空");
}
return Result.ok();
}
}