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 idsWithAuditStatusMap) { + List errorIds = new ArrayList<>(); + for (Map.Entry 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 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 idsWithAuditStatusMap) { + Function, Boolean>> updateTaskAuditStatusFunction = + mapper -> map -> { + if (map == null || map.isEmpty()) { + return false; + } + + Map idWithAuditStatusBatch = new HashMap<>(); + for (Map.Entry 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 t_task + set task_audit_status = CASE task_id + + WHEN #{id} THEN #{auditStatus} + + ELSE task_audit_status + END + WHERE task_id IN + + #{id} + + + 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 staticRuleObjects = staticRuleService.queryStaticRule( null, null, null, null, i, 2); List staticRuleIds = new ArrayList<>(); @@ -175,4 +177,16 @@ class TaskServiceTest extends ProtectionApplicationTests { List taskCommandInfos = taskService.getStaticCommandInfos(38L); assertNotNull(taskCommandInfos); } + + + @Test + void testUpdateTaskAuditStatusBatch(){ + Map 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