1. 在静态和动态实体类中添加验证审批状态方法,用于批量验证审批状态中

2. 在任务状态修改函数中,添加更新动静态规则审批状态方法,用以更新规则审批状态为使用中/已审批
3. 在部分Mapper的update函数中修改modify_time为NOW()
4. 修复部分NullPointerException bug
5. 在新建任务时立刻检查所有规则的审批状态,必须为已审批才可以添加到任务中
6. 在taskService中添加更新动静态规则审批状态函数。该函数不能用于除已审批/使用中的其他审批状态更新
This commit is contained in:
EnderByEndera
2024-04-24 14:15:08 +08:00
parent 10d95b1417
commit 0526a1322b
17 changed files with 242 additions and 48 deletions

View File

@@ -1,6 +1,9 @@
package com.realtime.protection.server.task;
import com.alibaba.excel.util.MapUtils;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.realtime.protection.configuration.entity.rule.dynamicrule.DynamicRuleObject;
import com.realtime.protection.configuration.entity.rule.staticrule.StaticRuleObject;
import com.realtime.protection.configuration.entity.task.DynamicTaskInfo;
import com.realtime.protection.configuration.entity.task.Task;
import com.realtime.protection.configuration.entity.task.TaskCommandInfo;
@@ -8,6 +11,8 @@ 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;
import com.realtime.protection.server.rule.dynamicrule.DynamicRuleMapper;
import com.realtime.protection.server.rule.staticrule.StaticRuleMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
@@ -17,17 +22,23 @@ import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.function.Function;
import java.util.stream.Collectors;
@Service
@Slf4j
@DS("mysql")
public class TaskService {
private final TaskMapper taskMapper;
private final StaticRuleMapper staticRuleMapper;
private final SqlSessionWrapper sqlSessionWrapper;
private static final int BATCH_SIZE = 100;
private final DynamicRuleMapper dynamicRuleMapper;
public TaskService(TaskMapper taskMapper,SqlSessionWrapper sqlSessionWrapper) {
public TaskService(TaskMapper taskMapper, StaticRuleMapper staticRuleMapper, SqlSessionWrapper sqlSessionWrapper, DynamicRuleMapper dynamicRuleMapper) {
this.taskMapper = taskMapper;
this.staticRuleMapper = staticRuleMapper;
this.sqlSessionWrapper = sqlSessionWrapper;
this.dynamicRuleMapper = dynamicRuleMapper;
}
@Transactional
@@ -36,19 +47,146 @@ public class TaskService {
task.setTaskCreateUsername("xxx");
task.setTaskCreateDepart("xxx");
// todo: 添加新建任务时,将动态/静态规则从“已审核”修改为“使用中”
taskMapper.newTask(task);
if (task.getStaticRuleIds() != null && !task.getStaticRuleIds().isEmpty())
if (task.getStaticRuleIds() != null && !task.getStaticRuleIds().isEmpty()) {
staticRuleMapper.queryStaticRuleByIds(task.getStaticRuleIds()).forEach(staticRuleObject -> {
if (!staticRuleObject.getAuditStatus().equals(AuditStatusEnum.AUDITED.getNum())) {
throw new IllegalArgumentException("部分规则审批状态错误");
}
if (staticRuleObject.getStaticRuleUsedTaskId() != null) {
throw new IllegalArgumentException("部分静态规则已被其他任务使用");
}
});
taskMapper.newTaskStaticRuleConcat(task.getTaskId(), task.getStaticRuleIds());
}
if (task.getDynamicRuleIds() != null && !task.getDynamicRuleIds().isEmpty())
if (task.getDynamicRuleIds() != null && !task.getDynamicRuleIds().isEmpty()) {
dynamicRuleMapper.queryDynamicRuleByIds(task.getDynamicRuleIds()).forEach(dynamicRuleObject -> {
if (!dynamicRuleObject.getAuditStatus().equals(AuditStatusEnum.AUDITED.getNum())) {
throw new IllegalArgumentException("部分规则审批状态错误");
}
if (dynamicRuleObject.getDynamicRuleUsedTaskId() != null) {
throw new IllegalArgumentException("部分动态规则已被其他任务使用");
}
});
taskMapper.newTaskDynamicRuleConcat(task.getTaskId(), task.getDynamicRuleIds());
}
return task.getTaskId();
}
/**
* 更新任务关联的静态规则审批状态,用于任务新建/停止时候,修改审批状态为已使用/已审批,不能用于其他审批状态修改
* @param task 与静态规则关联的任务
* @param newAuditStatus 需要修改的审批状态
*/
public void updateStaticRuleAuditStatusInTask(Task task, AuditStatusEnum newAuditStatus) {
if (task == null) {
return;
}
// 限制该函数仅能用于将规则修改为已审批/使用中
if (!List.of(AuditStatusEnum.AUDITED, AuditStatusEnum.USING).contains(newAuditStatus)) {
return;
}
List<StaticRuleObject> staticRuleObjects = staticRuleMapper.queryStaticRuleByIds(task.getStaticRuleIds());
if (staticRuleObjects == null || staticRuleObjects.isEmpty()) {
throw new IllegalArgumentException("静态规则列表中的ID不存在请检查静态规则是否真实存在");
}
// 检查所有的静态规则审批状态是否正确,如果不正确则报错
staticRuleObjects.forEach(staticRuleObject -> staticRuleObject.checkAuditStatusValidate(newAuditStatus));
Map<Integer, Integer> staticRuleAuditStatusBatch = staticRuleObjects
.stream()
.collect(Collectors.toMap(
StaticRuleObject::getStaticRuleId,
k -> newAuditStatus.getNum(), // 将审核状态全部修改为使用中状态
(existing, replacement) -> existing)); // 如果有重复字段,默认使用先前值
sqlSessionWrapper.startBatchSession(
StaticRuleMapper.class,
(Function<StaticRuleMapper, Function<Map<Integer, Integer>, Void>>) mapper -> staticRuleBatch -> {
Map<Integer, Integer> batchMap = MapUtils.newHashMapWithExpectedSize(BATCH_SIZE);
for (Map.Entry<Integer, Integer> auditStatusEntry : staticRuleBatch.entrySet()) {
batchMap.put(auditStatusEntry.getKey(), auditStatusEntry.getValue());
if (batchMap.size() < BATCH_SIZE) {
continue;
}
mapper.updateAuditStatusByIdBatch(batchMap);
batchMap.clear();
}
mapper.updateAuditStatusByIdBatch(batchMap);
batchMap.clear();
return null;
},
staticRuleAuditStatusBatch
);
}
/**
* 更新任务关联的动态规则审批状态,用于任务新建/停止时候,修改审批状态为已使用/已审批,不能用于其他审批状态修改
* @param task 与动态规则关联的任务
* @param newAuditStatus 需要修改的审批状态
*/
public void updateDynamicRuleAuditStatusInTask(Task task, AuditStatusEnum newAuditStatus) {
if (task == null) {
return;
}
// 限制该函数仅能用于将规则修改为已审批/使用中
if (!List.of(AuditStatusEnum.AUDITED, AuditStatusEnum.USING).contains(newAuditStatus)) {
return;
}
List<DynamicRuleObject> dynamicRuleObjects = dynamicRuleMapper.queryDynamicRuleByIds(task.getDynamicRuleIds());
if (dynamicRuleObjects == null || dynamicRuleObjects.isEmpty()) {
throw new IllegalArgumentException("静态规则列表中的ID不存在请检查静态规则是否真实存在");
}
// 检查所有的动态规则列表的审批状态是否正确,如不正确则报错
dynamicRuleObjects.forEach(dynamicRuleObject -> dynamicRuleObject.checkAuditStatusValidate(newAuditStatus));
Map<Integer, Integer> dynamicRuleAuditStatusBatch = dynamicRuleObjects
.stream()
.collect(Collectors.toMap(
DynamicRuleObject::getDynamicRuleId,
k -> newAuditStatus.getNum(),
(existing, replacement) -> existing));
sqlSessionWrapper.startBatchSession(
DynamicRuleMapper.class,
(Function<DynamicRuleMapper, Function<Map<Integer, Integer>, Void>>) mapper -> batch -> {
Map<Integer, Integer> batchMap = MapUtils.newHashMapWithExpectedSize(BATCH_SIZE);
for (Map.Entry<Integer, Integer> auditStatusEntry : batch.entrySet()) {
batchMap.put(auditStatusEntry.getKey(), auditStatusEntry.getValue());
if (batchMap.size() < BATCH_SIZE) {
continue;
}
mapper.updateAuditStatusByIdBatch(batchMap);
batchMap.clear();
}
mapper.updateAuditStatusByIdBatch(batchMap);
batchMap.clear();
return null;
},
dynamicRuleAuditStatusBatch
);
}
@Transactional
public List<Task> queryTasks(Integer taskStatus,
Integer taskType, String taskName, String taskCreator,
@@ -116,6 +254,12 @@ public class TaskService {
}
public Boolean deleteTask(Long taskId) {
Task task = taskMapper.queryTask(taskId);
if (task == null) {
return true;
}
updateStaticRuleAuditStatusInTask(task, AuditStatusEnum.AUDITED);
return taskMapper.deleteTask(taskId);
}