2024-01-03 09:13:22 +08:00
|
|
|
|
package com.realtime.protection.server.task;
|
|
|
|
|
|
|
2024-01-12 14:31:34 +08:00
|
|
|
|
import com.baomidou.dynamic.datasource.annotation.DS;
|
2024-01-17 19:07:04 +08:00
|
|
|
|
import com.realtime.protection.configuration.entity.task.DynamicTaskInfo;
|
2024-01-03 09:13:22 +08:00
|
|
|
|
import com.realtime.protection.configuration.entity.task.Task;
|
2024-01-11 19:49:07 +08:00
|
|
|
|
import com.realtime.protection.configuration.entity.task.TaskCommandInfo;
|
2024-04-23 21:05:04 +08:00
|
|
|
|
import com.realtime.protection.configuration.utils.SqlSessionWrapper;
|
2024-01-17 09:44:29 +08:00
|
|
|
|
import com.realtime.protection.configuration.utils.enums.StateEnum;
|
2024-04-22 15:07:49 +08:00
|
|
|
|
import com.realtime.protection.configuration.utils.enums.audit.AuditStatusEnum;
|
2024-04-17 14:01:46 +08:00
|
|
|
|
import com.realtime.protection.configuration.utils.enums.audit.AuditStatusValidator;
|
2024-01-17 09:44:29 +08:00
|
|
|
|
import lombok.extern.slf4j.Slf4j;
|
2024-01-03 09:13:22 +08:00
|
|
|
|
import org.springframework.stereotype.Service;
|
2024-01-05 09:32:19 +08:00
|
|
|
|
import org.springframework.transaction.annotation.Transactional;
|
2024-01-03 09:13:22 +08:00
|
|
|
|
|
2024-04-23 21:05:04 +08:00
|
|
|
|
import java.util.HashMap;
|
2024-01-03 22:53:02 +08:00
|
|
|
|
import java.util.List;
|
2024-04-23 21:05:04 +08:00
|
|
|
|
import java.util.Map;
|
2024-04-22 15:07:49 +08:00
|
|
|
|
import java.util.Objects;
|
2024-04-23 21:05:04 +08:00
|
|
|
|
import java.util.function.Function;
|
2024-01-03 09:13:22 +08:00
|
|
|
|
|
|
|
|
|
|
@Service
|
2024-01-17 09:44:29 +08:00
|
|
|
|
@Slf4j
|
2024-01-15 20:40:55 +08:00
|
|
|
|
@DS("mysql")
|
2024-01-03 09:13:22 +08:00
|
|
|
|
public class TaskService {
|
2024-01-03 22:53:02 +08:00
|
|
|
|
private final TaskMapper taskMapper;
|
2024-04-23 21:05:04 +08:00
|
|
|
|
private final SqlSessionWrapper sqlSessionWrapper;
|
2024-01-03 09:13:22 +08:00
|
|
|
|
|
2024-04-23 21:05:04 +08:00
|
|
|
|
public TaskService(TaskMapper taskMapper,SqlSessionWrapper sqlSessionWrapper) {
|
2024-01-03 22:53:02 +08:00
|
|
|
|
this.taskMapper = taskMapper;
|
2024-04-23 21:05:04 +08:00
|
|
|
|
this.sqlSessionWrapper = sqlSessionWrapper;
|
2024-01-03 09:13:22 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2024-01-08 20:01:20 +08:00
|
|
|
|
@Transactional
|
2024-01-11 19:49:07 +08:00
|
|
|
|
public Long newTask(Task task) {
|
2024-01-15 20:40:55 +08:00
|
|
|
|
task.setTaskCreateUserId(1);
|
|
|
|
|
|
task.setTaskCreateUsername("xxx");
|
|
|
|
|
|
task.setTaskCreateDepart("xxx");
|
|
|
|
|
|
|
2024-04-23 12:15:07 +08:00
|
|
|
|
// todo: 添加新建任务时,将动态/静态规则从“已审核”修改为“使用中”
|
|
|
|
|
|
|
2024-01-08 20:01:20 +08:00
|
|
|
|
taskMapper.newTask(task);
|
|
|
|
|
|
|
2024-01-12 14:31:34 +08:00
|
|
|
|
if (task.getStaticRuleIds() != null && !task.getStaticRuleIds().isEmpty())
|
|
|
|
|
|
taskMapper.newTaskStaticRuleConcat(task.getTaskId(), task.getStaticRuleIds());
|
|
|
|
|
|
|
|
|
|
|
|
if (task.getDynamicRuleIds() != null && !task.getDynamicRuleIds().isEmpty())
|
|
|
|
|
|
taskMapper.newTaskDynamicRuleConcat(task.getTaskId(), task.getDynamicRuleIds());
|
2024-01-08 20:01:20 +08:00
|
|
|
|
|
2024-01-03 09:13:22 +08:00
|
|
|
|
return task.getTaskId();
|
|
|
|
|
|
}
|
2024-01-03 22:53:02 +08:00
|
|
|
|
|
2024-01-15 20:40:55 +08:00
|
|
|
|
@Transactional
|
2024-01-03 22:53:02 +08:00
|
|
|
|
public List<Task> queryTasks(Integer taskStatus,
|
2024-01-23 12:17:10 +08:00
|
|
|
|
Integer taskType, String taskName, String taskCreator,
|
2024-04-23 12:15:07 +08:00
|
|
|
|
Integer auditStatus,
|
2024-01-03 22:53:02 +08:00
|
|
|
|
Integer page, Integer pageSize) {
|
2024-04-22 15:07:49 +08:00
|
|
|
|
List<Task> tasks = taskMapper.queryTasks(taskStatus, taskType, taskName, taskCreator, auditStatus, page, pageSize);
|
2024-01-15 20:40:55 +08:00
|
|
|
|
for (Task task : tasks) {
|
|
|
|
|
|
if (task == null) {
|
|
|
|
|
|
continue;
|
|
|
|
|
|
}
|
|
|
|
|
|
task.setStaticRuleIds(taskMapper.queryStaticRuleIdsFromTaskId(task.getTaskId()));
|
|
|
|
|
|
task.setDynamicRuleIds(taskMapper.queryDynamicRuleIdsFromTaskId(task.getTaskId()));
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return tasks;
|
2024-01-03 22:53:02 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2024-01-15 20:40:55 +08:00
|
|
|
|
@Transactional
|
2024-01-11 19:49:07 +08:00
|
|
|
|
public Task queryTask(Long id) {
|
2024-01-15 20:40:55 +08:00
|
|
|
|
Task task = taskMapper.queryTask(id);
|
|
|
|
|
|
if (task == null) {
|
|
|
|
|
|
return null;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
task.setStaticRuleIds(taskMapper.queryStaticRuleIdsFromTaskId(task.getTaskId()));
|
|
|
|
|
|
task.setDynamicRuleIds(taskMapper.queryDynamicRuleIdsFromTaskId(task.getTaskId()));
|
|
|
|
|
|
|
|
|
|
|
|
return task;
|
2024-01-03 22:53:02 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2024-01-05 09:32:19 +08:00
|
|
|
|
@Transactional
|
2024-01-08 20:01:20 +08:00
|
|
|
|
public Boolean updateTask(Task task) {
|
2024-04-22 15:07:49 +08:00
|
|
|
|
if (!Objects.equals(taskMapper.queryTaskAuditStatus(task.getTaskId()), AuditStatusEnum.AUDITED.getNum())) {
|
|
|
|
|
|
return false;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
task.setTaskAuditStatus(AuditStatusEnum.PENDING.getNum());
|
|
|
|
|
|
|
2024-01-05 09:32:19 +08:00
|
|
|
|
taskMapper.updateTask(task);
|
2024-01-03 22:53:02 +08:00
|
|
|
|
|
2024-01-05 09:32:19 +08:00
|
|
|
|
taskMapper.clearTaskConnectedStaticRule(task.getTaskId());
|
2024-01-08 20:01:20 +08:00
|
|
|
|
taskMapper.clearTaskConnectedDynamicRule(task.getTaskId());
|
2024-01-03 22:53:02 +08:00
|
|
|
|
|
2024-01-11 19:49:07 +08:00
|
|
|
|
if (task.getStaticRuleIds() != null && !task.getStaticRuleIds().isEmpty())
|
|
|
|
|
|
taskMapper.newTaskStaticRuleConcat(task.getTaskId(), task.getStaticRuleIds());
|
|
|
|
|
|
|
|
|
|
|
|
if (task.getDynamicRuleIds() != null && !task.getDynamicRuleIds().isEmpty())
|
|
|
|
|
|
taskMapper.newTaskDynamicRuleConcat(task.getTaskId(), task.getDynamicRuleIds());
|
2024-01-08 20:01:20 +08:00
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Transactional
|
2024-01-11 19:49:07 +08:00
|
|
|
|
public Boolean changeTaskAuditStatus(Long taskId, Integer taskAuditStatus) {
|
|
|
|
|
|
Integer originalAuditStatus = taskMapper.queryTaskAuditStatus(taskId);
|
|
|
|
|
|
if (originalAuditStatus == null) {
|
2024-01-13 10:23:48 +08:00
|
|
|
|
throw new IllegalArgumentException("无法找到任务ID为" + taskId + "的任务,也许任务不存在?");
|
2024-01-11 19:49:07 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (AuditStatusValidator.setOriginal(originalAuditStatus).checkValidate(taskAuditStatus))
|
2024-01-08 20:01:20 +08:00
|
|
|
|
taskMapper.changeTaskAuditStatus(taskId, taskAuditStatus);
|
|
|
|
|
|
else return false;
|
|
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-01-11 19:49:07 +08:00
|
|
|
|
public Boolean deleteTask(Long taskId) {
|
2024-01-08 20:01:20 +08:00
|
|
|
|
return taskMapper.deleteTask(taskId);
|
2024-01-03 22:53:02 +08:00
|
|
|
|
}
|
2024-01-09 09:20:13 +08:00
|
|
|
|
|
2024-01-15 20:40:55 +08:00
|
|
|
|
|
2024-01-11 19:49:07 +08:00
|
|
|
|
public Boolean changeTaskStatus(Long taskId, Integer stateNum) {
|
|
|
|
|
|
return taskMapper.changeTaskStatus(taskId, stateNum);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public List<TaskCommandInfo> getStaticCommandInfos(Long taskId) {
|
2024-04-10 15:45:09 +08:00
|
|
|
|
List<TaskCommandInfo> staticCommandInfos = taskMapper.getStaticCommandInfos(taskId);
|
2024-04-11 08:56:35 +08:00
|
|
|
|
|
|
|
|
|
|
staticCommandInfos.forEach(taskCommandInfo -> {
|
|
|
|
|
|
taskCommandInfo.setProtocolNum();
|
|
|
|
|
|
taskCommandInfo.setMask();
|
|
|
|
|
|
});
|
|
|
|
|
|
|
2024-04-10 15:45:09 +08:00
|
|
|
|
return staticCommandInfos;
|
2024-01-11 19:49:07 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2024-01-17 19:07:04 +08:00
|
|
|
|
public List<DynamicTaskInfo> getDynamicTaskInfos(Long taskId) {
|
|
|
|
|
|
return taskMapper.getDynamicTaskInfos(taskId);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-01-11 19:49:07 +08:00
|
|
|
|
public Integer queryTaskAuditStatus(Long taskId) {
|
|
|
|
|
|
return taskMapper.queryTaskAuditStatus(taskId);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public Integer queryTaskStatus(Long taskId) {
|
|
|
|
|
|
return taskMapper.queryTaskStatus(taskId);
|
2024-01-09 09:20:13 +08:00
|
|
|
|
}
|
2024-01-15 20:40:55 +08:00
|
|
|
|
|
|
|
|
|
|
public Long newTaskUsingCommandInfo(TaskCommandInfo taskCommandInfo) {
|
|
|
|
|
|
taskMapper.newTaskUsingCommandInfo(taskCommandInfo);
|
|
|
|
|
|
return taskCommandInfo.getTaskId();
|
|
|
|
|
|
}
|
2024-01-17 09:44:29 +08:00
|
|
|
|
|
|
|
|
|
|
public List<Long> getFinishedTasks() {
|
|
|
|
|
|
return taskMapper.queryTasksByStatus(StateEnum.FINISHED.getStateNum());
|
|
|
|
|
|
}
|
2024-01-22 15:40:03 +08:00
|
|
|
|
|
2024-04-22 15:07:49 +08:00
|
|
|
|
public Integer queryTaskTotalNum(Integer taskStatus, Integer taskType, String taskName, String taskCreator) {
|
2024-01-23 12:17:10 +08:00
|
|
|
|
return taskMapper.queryTaskTotalNum(taskStatus, taskType, taskName, taskCreator);
|
2024-01-22 15:40:03 +08:00
|
|
|
|
}
|
2024-04-23 21:05:04 +08:00
|
|
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
2024-01-03 09:13:22 +08:00
|
|
|
|
}
|