diff options
| author | PushM <[email protected]> | 2024-04-23 21:05:04 +0800 |
|---|---|---|
| committer | PushM <[email protected]> | 2024-04-23 21:05:04 +0800 |
| commit | b03eb6e993588395355cab1a3d853786588d8f59 (patch) | |
| tree | 831dac3d8311f3e4f8be213b807a7650093607a8 | |
| parent | 161bc45994cdf1d48dbe7a1181c8e3e6fd2fd591 (diff) | |
1、新增任务批量审核接口
5 files changed, 94 insertions, 2 deletions
diff --git a/src/main/java/com/realtime/protection/server/task/TaskController.java b/src/main/java/com/realtime/protection/server/task/TaskController.java index c289dce..cbbeff4 100644 --- a/src/main/java/com/realtime/protection/server/task/TaskController.java +++ b/src/main/java/com/realtime/protection/server/task/TaskController.java @@ -12,7 +12,9 @@ import jakarta.validation.constraints.Min; import jakarta.validation.constraints.NotNull; import org.springframework.web.bind.annotation.*; +import java.util.ArrayList; import java.util.List; +import java.util.Map; @RestController @RequestMapping("/task") @@ -155,4 +157,28 @@ public class TaskController implements TaskControllerApi { .setData("success", commandService.setCommandJudged(commandId, isJudged)) .setData("command_id", commandId); } + + + /** + * 批量修改审核状态 + */ + @PostMapping("/auditbatch") + public ResponseResult updateTaskAuditStatusBatch(@RequestBody Map<Integer, Integer> idsWithAuditStatusMap) { + List<Integer> errorIds = new ArrayList<>(); + for (Map.Entry<Integer, Integer> entry: idsWithAuditStatusMap.entrySet()) { + Integer id = entry.getKey(); + Integer auditStatus = entry.getValue(); + if (id <= 0 || auditStatus < 0 || auditStatus > 2) { + errorIds.add(id); + } + } + if (!errorIds.isEmpty()){ + return new ResponseResult(400, "id or status is invalid") + .setData("tasks_id", errorIds) + .setData("success", false); + } + + return ResponseResult.ok() + .setData("success", taskService.updateAuditStatusBatch(idsWithAuditStatusMap)); + } } diff --git a/src/main/java/com/realtime/protection/server/task/TaskMapper.java b/src/main/java/com/realtime/protection/server/task/TaskMapper.java index b8dca78..d70237f 100644 --- a/src/main/java/com/realtime/protection/server/task/TaskMapper.java +++ b/src/main/java/com/realtime/protection/server/task/TaskMapper.java @@ -8,6 +8,7 @@ import org.apache.ibatis.annotations.Param; import org.apache.ibatis.annotations.Select; import java.util.List; +import java.util.Map; @Mapper public interface TaskMapper { @@ -58,4 +59,6 @@ public interface TaskMapper { Integer queryTaskTotalNum(@Param("task_status") Integer taskStatus, @Param("task_type") Integer task_type, @Param("task_name") String taskName, @Param("task_creator") String taskCreator); + + void updateAuditStatusByIdBatch(@Param("idWithAuditStatusBatch") Map<Integer, Integer> idWithAuditStatusBatch); } diff --git a/src/main/java/com/realtime/protection/server/task/TaskService.java b/src/main/java/com/realtime/protection/server/task/TaskService.java index 79e1a5d..860a15e 100644 --- a/src/main/java/com/realtime/protection/server/task/TaskService.java +++ b/src/main/java/com/realtime/protection/server/task/TaskService.java @@ -4,6 +4,7 @@ import com.baomidou.dynamic.datasource.annotation.DS; import com.realtime.protection.configuration.entity.task.DynamicTaskInfo; import com.realtime.protection.configuration.entity.task.Task; import com.realtime.protection.configuration.entity.task.TaskCommandInfo; +import com.realtime.protection.configuration.utils.SqlSessionWrapper; import com.realtime.protection.configuration.utils.enums.StateEnum; import com.realtime.protection.configuration.utils.enums.audit.AuditStatusEnum; import com.realtime.protection.configuration.utils.enums.audit.AuditStatusValidator; @@ -11,17 +12,22 @@ import lombok.extern.slf4j.Slf4j; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; +import java.util.HashMap; import java.util.List; +import java.util.Map; import java.util.Objects; +import java.util.function.Function; @Service @Slf4j @DS("mysql") public class TaskService { private final TaskMapper taskMapper; + private final SqlSessionWrapper sqlSessionWrapper; - public TaskService(TaskMapper taskMapper) { + public TaskService(TaskMapper taskMapper,SqlSessionWrapper sqlSessionWrapper) { this.taskMapper = taskMapper; + this.sqlSessionWrapper = sqlSessionWrapper; } @Transactional @@ -153,4 +159,32 @@ public class TaskService { public Integer queryTaskTotalNum(Integer taskStatus, Integer taskType, String taskName, String taskCreator) { return taskMapper.queryTaskTotalNum(taskStatus, taskType, taskName, taskCreator); } + + public Object updateAuditStatusBatch(Map<Integer, Integer> idsWithAuditStatusMap) { + Function<TaskMapper, Function<Map<Integer, Integer>, Boolean>> updateTaskAuditStatusFunction = + mapper -> map -> { + if (map == null || map.isEmpty()) { + return false; + } + + Map<Integer, Integer> idWithAuditStatusBatch = new HashMap<>(); + for (Map.Entry<Integer, Integer> item : map.entrySet()) { + idWithAuditStatusBatch.put(item.getKey(), item.getValue()); + if (idWithAuditStatusBatch.size() < 100) { + continue; + } + //mapper指的就是外层函数输入的参数,也就是WhiteListMapper + mapper.updateAuditStatusByIdBatch(idWithAuditStatusBatch); + idWithAuditStatusBatch.clear(); + } + if (!idWithAuditStatusBatch.isEmpty()) { + mapper.updateAuditStatusByIdBatch(idWithAuditStatusBatch); + } + return true; + }; + //实现事务操作 + return sqlSessionWrapper.startBatchSession(TaskMapper.class, updateTaskAuditStatusFunction, idsWithAuditStatusMap); + + + } } diff --git a/src/main/resources/mappers/TaskMapper.xml b/src/main/resources/mappers/TaskMapper.xml index 350d03e..4c98aed 100644 --- a/src/main/resources/mappers/TaskMapper.xml +++ b/src/main/resources/mappers/TaskMapper.xml @@ -148,6 +148,21 @@ WHERE task_id = #{task_id} </update> + <!-- 批量审核 --> + <update id="updateAuditStatusByIdBatch"> + update t_task + set task_audit_status = CASE task_id + <foreach collection="idWithAuditStatusBatch" index="id" item="auditStatus" separator=" "> + WHEN #{id} THEN #{auditStatus} + </foreach> + ELSE task_audit_status + END + WHERE task_id IN + <foreach collection="idWithAuditStatusBatch" index="id" open="(" separator="," close=")"> + #{id} + </foreach> + </update> + <delete id="deleteTask"> DELETE FROM t_task diff --git a/src/test/java/com/realtime/protection/server/task/TaskServiceTest.java b/src/test/java/com/realtime/protection/server/task/TaskServiceTest.java index 5161482..5ce7305 100644 --- a/src/test/java/com/realtime/protection/server/task/TaskServiceTest.java +++ b/src/test/java/com/realtime/protection/server/task/TaskServiceTest.java @@ -16,7 +16,9 @@ import org.springframework.dao.DataIntegrityViolationException; import java.time.LocalDateTime; import java.util.ArrayList; +import java.util.HashMap; import java.util.List; +import java.util.Map; import static org.junit.jupiter.api.Assertions.*; @@ -54,7 +56,7 @@ class TaskServiceTest extends ProtectionApplicationTests { @Test void testNewTaskSuccess() { - for (int i = 1; i < 1000; i++) { + for (int i = 1; i < 10; i++) { List<StaticRuleObject> staticRuleObjects = staticRuleService.queryStaticRule( null, null, null, null, i, 2); List<Integer> staticRuleIds = new ArrayList<>(); @@ -175,4 +177,16 @@ class TaskServiceTest extends ProtectionApplicationTests { List<TaskCommandInfo> taskCommandInfos = taskService.getStaticCommandInfos(38L); assertNotNull(taskCommandInfos); } + + + @Test + void testUpdateTaskAuditStatusBatch(){ + Map<Integer, Integer> map = new HashMap<>(); + map.put(43830, 1); + map.put(43831, 1); + map.put(43832, 1); + + + System.out.println(taskService.updateAuditStatusBatch(map)); + } }
\ No newline at end of file |
