2024-01-03 09:13:22 +08:00
|
|
|
package com.realtime.protection.server.task;
|
|
|
|
|
|
|
|
|
|
import com.realtime.protection.configuration.entity.task.Task;
|
2024-01-08 20:01:20 +08:00
|
|
|
import com.realtime.protection.configuration.utils.status.AuditStatusValidator;
|
2024-01-09 09:20:13 +08:00
|
|
|
import com.realtime.protection.server.task.state.PauseState;
|
|
|
|
|
import com.realtime.protection.server.task.state.RunningState;
|
|
|
|
|
import com.realtime.protection.server.task.state.State;
|
|
|
|
|
import com.realtime.protection.server.task.state.StopState;
|
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-01-03 22:53:02 +08:00
|
|
|
import java.util.List;
|
2024-01-03 09:13:22 +08:00
|
|
|
|
|
|
|
|
@Service
|
|
|
|
|
public class TaskService {
|
2024-01-03 22:53:02 +08:00
|
|
|
private final TaskMapper taskMapper;
|
2024-01-03 09:13:22 +08:00
|
|
|
|
2024-01-08 20:01:20 +08:00
|
|
|
public TaskService(TaskMapper taskMapper) {
|
2024-01-03 22:53:02 +08:00
|
|
|
this.taskMapper = taskMapper;
|
2024-01-03 09:13:22 +08:00
|
|
|
}
|
|
|
|
|
|
2024-01-08 20:01:20 +08:00
|
|
|
@Transactional
|
2024-01-03 09:13:22 +08:00
|
|
|
public Integer newTask(Task task) {
|
2024-01-08 20:01:20 +08:00
|
|
|
taskMapper.newTask(task);
|
|
|
|
|
|
|
|
|
|
taskMapper.newTaskStaticRuleConcat(task.getTaskId(), task.getStaticRuleIds());
|
|
|
|
|
taskMapper.newTaskDynamicRuleConcat(task.getTaskId(), task.getDynamicRuleIds());
|
|
|
|
|
|
2024-01-03 09:13:22 +08:00
|
|
|
return task.getTaskId();
|
|
|
|
|
}
|
2024-01-03 22:53:02 +08:00
|
|
|
|
|
|
|
|
public List<Task> queryTasks(Integer taskStatus,
|
|
|
|
|
String taskType, String taskName, String taskCreator,
|
|
|
|
|
Integer page, Integer pageSize) {
|
|
|
|
|
return taskMapper.queryTasks(taskStatus, taskType, taskName, taskCreator, page, pageSize);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Task queryTask(Integer id) {
|
2024-01-08 20:01:20 +08:00
|
|
|
return taskMapper.queryTask(id);
|
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-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-05 09:32:19 +08:00
|
|
|
taskMapper.newTaskStaticRuleConcat(task.getTaskId(), task.getStaticRuleIds());
|
2024-01-08 20:01:20 +08:00
|
|
|
taskMapper.newTaskDynamicRuleConcat(task.getTaskId(), task.getDynamicRuleIds());
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Transactional
|
|
|
|
|
public Boolean changeTaskAuditStatus(Integer taskId, Integer taskAuditStatus) {
|
|
|
|
|
if (AuditStatusValidator.setOriginal(taskMapper.queryTask(taskId).getTaskAuditStatus()).checkValidate(taskAuditStatus))
|
|
|
|
|
taskMapper.changeTaskAuditStatus(taskId, taskAuditStatus);
|
|
|
|
|
else return false;
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Boolean deleteTask(Integer taskId) {
|
|
|
|
|
return taskMapper.deleteTask(taskId);
|
2024-01-03 22:53:02 +08:00
|
|
|
}
|
2024-01-09 09:20:13 +08:00
|
|
|
|
|
|
|
|
@Transactional
|
|
|
|
|
public Boolean changeTaskStatus(Integer taskId, Integer stateNum) {
|
|
|
|
|
State originalState = switch (taskMapper.queryTask(taskId).getTaskStatus()) {
|
|
|
|
|
// 运行中
|
|
|
|
|
case 1 -> new RunningState();
|
|
|
|
|
// 暂停中
|
|
|
|
|
case 2 -> new PauseState();
|
|
|
|
|
// 停止中
|
|
|
|
|
case 3 -> new StopState();
|
|
|
|
|
default -> throw new IllegalArgumentException();
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
State newState = switch (stateNum) {
|
|
|
|
|
// 运行中
|
|
|
|
|
case 1 -> new RunningState();
|
|
|
|
|
// 暂停中
|
|
|
|
|
case 2 -> new PauseState();
|
|
|
|
|
// 停止中
|
|
|
|
|
case 3 -> new StopState();
|
|
|
|
|
default -> throw new IllegalArgumentException();
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return StatusChanger.setOriginal(originalState).changeState(newState);
|
|
|
|
|
}
|
2024-01-03 09:13:22 +08:00
|
|
|
}
|