2024-01-03 09:13:22 +08:00
|
|
|
|
package com.realtime.protection.server.task;
|
|
|
|
|
|
|
2024-04-24 14:15:08 +08:00
|
|
|
|
import com.alibaba.excel.util.MapUtils;
|
2024-01-12 14:31:34 +08:00
|
|
|
|
import com.baomidou.dynamic.datasource.annotation.DS;
|
2024-05-28 02:21:58 +08:00
|
|
|
|
import com.realtime.protection.configuration.entity.defense.object.ProtectObject;
|
2024-04-24 14:15:08 +08:00
|
|
|
|
import com.realtime.protection.configuration.entity.rule.dynamicrule.DynamicRuleObject;
|
|
|
|
|
|
import com.realtime.protection.configuration.entity.rule.staticrule.StaticRuleObject;
|
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-29 15:33:09 +08:00
|
|
|
|
import com.realtime.protection.configuration.utils.Counter;
|
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-05-28 02:21:58 +08:00
|
|
|
|
import com.realtime.protection.configuration.utils.enums.TaskTypeEnum;
|
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-06-05 14:56:35 +08:00
|
|
|
|
import com.realtime.protection.server.command.CommandMapper;
|
2024-04-24 14:15:08 +08:00
|
|
|
|
import com.realtime.protection.server.rule.dynamicrule.DynamicRuleMapper;
|
|
|
|
|
|
import com.realtime.protection.server.rule.staticrule.StaticRuleMapper;
|
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-29 15:33:09 +08:00
|
|
|
|
import java.time.LocalDateTime;
|
|
|
|
|
|
import java.time.format.DateTimeFormatter;
|
|
|
|
|
|
import java.util.HashMap;
|
|
|
|
|
|
import java.util.List;
|
|
|
|
|
|
import java.util.Map;
|
|
|
|
|
|
import java.util.Objects;
|
2024-04-29 11:44:15 +08:00
|
|
|
|
import java.util.*;
|
2024-04-23 21:05:04 +08:00
|
|
|
|
import java.util.function.Function;
|
2024-04-24 14:15:08 +08:00
|
|
|
|
import java.util.stream.Collectors;
|
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-24 14:15:08 +08:00
|
|
|
|
private final StaticRuleMapper staticRuleMapper;
|
2024-04-23 21:05:04 +08:00
|
|
|
|
private final SqlSessionWrapper sqlSessionWrapper;
|
2024-04-24 14:15:08 +08:00
|
|
|
|
private static final int BATCH_SIZE = 100;
|
|
|
|
|
|
private final DynamicRuleMapper dynamicRuleMapper;
|
2024-04-29 15:33:09 +08:00
|
|
|
|
private final Counter counter;
|
2024-06-05 14:56:35 +08:00
|
|
|
|
private final CommandMapper commandMapper;
|
2024-01-03 09:13:22 +08:00
|
|
|
|
|
2024-06-05 14:56:35 +08:00
|
|
|
|
public TaskService(TaskMapper taskMapper, StaticRuleMapper staticRuleMapper, SqlSessionWrapper sqlSessionWrapper, DynamicRuleMapper dynamicRuleMapper, Counter counter, CommandMapper commandMapper) {
|
2024-01-03 22:53:02 +08:00
|
|
|
|
this.taskMapper = taskMapper;
|
2024-04-24 14:15:08 +08:00
|
|
|
|
this.staticRuleMapper = staticRuleMapper;
|
2024-04-23 21:05:04 +08:00
|
|
|
|
this.sqlSessionWrapper = sqlSessionWrapper;
|
2024-04-24 14:15:08 +08:00
|
|
|
|
this.dynamicRuleMapper = dynamicRuleMapper;
|
2024-04-29 15:33:09 +08:00
|
|
|
|
this.counter = counter;
|
2024-06-05 14:56:35 +08:00
|
|
|
|
this.commandMapper = commandMapper;
|
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-04-29 15:33:09 +08:00
|
|
|
|
// todo: 目前获取方式还不确定,以后再确定
|
2024-05-09 13:06:21 +08:00
|
|
|
|
// task.setTaskCreateUserId(1);
|
|
|
|
|
|
// task.setTaskCreateUsername("xxx");
|
|
|
|
|
|
// task.setTaskCreateDepart("xxx");
|
2024-01-15 20:40:55 +08:00
|
|
|
|
|
2024-05-28 02:21:58 +08:00
|
|
|
|
|
2024-04-29 15:33:09 +08:00
|
|
|
|
task.setTaskDisplayId(
|
|
|
|
|
|
"RW-"
|
|
|
|
|
|
+ LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMdd"))
|
|
|
|
|
|
+ "-"
|
|
|
|
|
|
+ String.format("%06d", counter.generateId("task")));
|
|
|
|
|
|
|
2024-01-08 20:01:20 +08:00
|
|
|
|
taskMapper.newTask(task);
|
2024-04-24 14:15:08 +08:00
|
|
|
|
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("部分静态规则已被其他任务使用");
|
|
|
|
|
|
}
|
|
|
|
|
|
});
|
2024-01-12 14:31:34 +08:00
|
|
|
|
taskMapper.newTaskStaticRuleConcat(task.getTaskId(), task.getStaticRuleIds());
|
2024-04-24 14:15:08 +08:00
|
|
|
|
}
|
2024-01-12 14:31:34 +08:00
|
|
|
|
|
2024-04-24 14:15:08 +08:00
|
|
|
|
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("部分动态规则已被其他任务使用");
|
|
|
|
|
|
}
|
|
|
|
|
|
});
|
2024-01-12 14:31:34 +08:00
|
|
|
|
taskMapper.newTaskDynamicRuleConcat(task.getTaskId(), task.getDynamicRuleIds());
|
2024-04-24 14:15:08 +08:00
|
|
|
|
}
|
2024-05-30 02:51:14 +08:00
|
|
|
|
if (task.getTaskType() != TaskTypeEnum.STATIC.getTaskType()) {
|
|
|
|
|
|
if (task.getProtectObjectIds() != null && !task.getProtectObjectIds().isEmpty()) {
|
|
|
|
|
|
//校验防护对象是否存在
|
|
|
|
|
|
boolean ProtectObjIdValid = task.getProtectObjectIds().stream()
|
|
|
|
|
|
.allMatch(dynamicRuleMapper::queryProtectObjectById);
|
|
|
|
|
|
if (!ProtectObjIdValid) {
|
|
|
|
|
|
throw new IllegalArgumentException("部分防护对象不存在");
|
|
|
|
|
|
}
|
|
|
|
|
|
//任务和防护对象多对多关联建立
|
|
|
|
|
|
taskMapper.newTaskProtectObjectConcat(task.getTaskId(), task.getProtectObjectIds());
|
|
|
|
|
|
|
2024-05-28 02:21:58 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2024-06-04 20:07:29 +08:00
|
|
|
|
insertTaskStatusLog(task.getTaskId());
|
2024-01-03 09:13:22 +08:00
|
|
|
|
return task.getTaskId();
|
|
|
|
|
|
}
|
2024-01-03 22:53:02 +08:00
|
|
|
|
|
2024-04-24 14:15:08 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* 更新任务关联的静态规则审批状态,用于任务新建/停止时候,修改审批状态为已使用/已审批,不能用于其他审批状态修改
|
2024-04-24 14:33:42 +08:00
|
|
|
|
* @param taskId 与静态规则关联的任务ID
|
2024-04-24 14:15:08 +08:00
|
|
|
|
* @param newAuditStatus 需要修改的审批状态
|
|
|
|
|
|
*/
|
2024-04-24 14:33:42 +08:00
|
|
|
|
public void updateStaticRuleAuditStatusInTask(Long taskId, AuditStatusEnum newAuditStatus) {
|
|
|
|
|
|
if (taskId == null) {
|
2024-04-24 14:15:08 +08:00
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 限制该函数仅能用于将规则修改为已审批/使用中
|
|
|
|
|
|
if (!List.of(AuditStatusEnum.AUDITED, AuditStatusEnum.USING).contains(newAuditStatus)) {
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-04-25 12:40:41 +08:00
|
|
|
|
List<Integer> staticRuleIds = taskMapper.queryStaticRuleIdsFromTaskId(taskId,
|
|
|
|
|
|
List.of(AuditStatusEnum.AUDITED.getNum(), AuditStatusEnum.USING.getNum()));
|
2024-04-25 12:09:23 +08:00
|
|
|
|
if (staticRuleIds == null || staticRuleIds.isEmpty()) {
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
List<StaticRuleObject> staticRuleObjects = staticRuleMapper.queryStaticRuleByIds(staticRuleIds);
|
2024-04-24 14:15:08 +08:00
|
|
|
|
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);
|
2024-06-05 03:29:32 +08:00
|
|
|
|
insertStaticRuleStatusLog(batchMap);
|
2024-04-24 14:15:08 +08:00
|
|
|
|
batchMap.clear();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
mapper.updateAuditStatusByIdBatch(batchMap);
|
2024-06-05 03:29:32 +08:00
|
|
|
|
insertStaticRuleStatusLog(batchMap);
|
2024-04-24 14:15:08 +08:00
|
|
|
|
batchMap.clear();
|
|
|
|
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
|
|
staticRuleAuditStatusBatch
|
|
|
|
|
|
);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 更新任务关联的动态规则审批状态,用于任务新建/停止时候,修改审批状态为已使用/已审批,不能用于其他审批状态修改
|
2024-04-24 14:33:42 +08:00
|
|
|
|
* @param taskId 与动态规则关联的任务ID
|
2024-04-24 14:15:08 +08:00
|
|
|
|
* @param newAuditStatus 需要修改的审批状态
|
|
|
|
|
|
*/
|
2024-04-24 14:33:42 +08:00
|
|
|
|
public void updateDynamicRuleAuditStatusInTask(Long taskId, AuditStatusEnum newAuditStatus) {
|
|
|
|
|
|
if (taskId == null) {
|
2024-04-24 14:15:08 +08:00
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 限制该函数仅能用于将规则修改为已审批/使用中
|
|
|
|
|
|
if (!List.of(AuditStatusEnum.AUDITED, AuditStatusEnum.USING).contains(newAuditStatus)) {
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-04-25 12:40:41 +08:00
|
|
|
|
List<Integer> dynamicRuleIds = taskMapper.queryDynamicRuleIdsFromTaskId(taskId,
|
|
|
|
|
|
List.of(AuditStatusEnum.AUDITED.getNum(), AuditStatusEnum.USING.getNum()));
|
2024-04-25 12:09:23 +08:00
|
|
|
|
if (dynamicRuleIds == null || dynamicRuleIds.isEmpty()) {
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
List<DynamicRuleObject> dynamicRuleObjects = dynamicRuleMapper.queryDynamicRuleByIds(dynamicRuleIds);
|
2024-04-24 14:15:08 +08:00
|
|
|
|
if (dynamicRuleObjects == null || dynamicRuleObjects.isEmpty()) {
|
2024-04-25 17:06:58 +08:00
|
|
|
|
throw new IllegalArgumentException("动态规则列表中的ID不存在,请检查动态规则是否真实存在");
|
2024-04-24 14:15:08 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 检查所有的动态规则列表的审批状态是否正确,如不正确则报错
|
|
|
|
|
|
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);
|
2024-06-05 03:29:32 +08:00
|
|
|
|
insertDynamicRuleStatusLog(batchMap);
|
2024-04-24 14:15:08 +08:00
|
|
|
|
batchMap.clear();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
mapper.updateAuditStatusByIdBatch(batchMap);
|
2024-06-05 03:29:32 +08:00
|
|
|
|
insertDynamicRuleStatusLog(batchMap);
|
2024-04-24 14:15:08 +08:00
|
|
|
|
batchMap.clear();
|
|
|
|
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
|
|
dynamicRuleAuditStatusBatch
|
|
|
|
|
|
);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
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-05-07 22:33:59 +08:00
|
|
|
|
String taskAct, String taskAuditor,
|
|
|
|
|
|
String taskSource, String ruleName,
|
2024-05-22 17:36:25 +08:00
|
|
|
|
String eventType,String createDateStr, String startDateStr,
|
2024-05-28 02:21:58 +08:00
|
|
|
|
Integer protectLevel,
|
2024-01-03 22:53:02 +08:00
|
|
|
|
Integer page, Integer pageSize) {
|
2024-05-07 22:33:59 +08:00
|
|
|
|
|
|
|
|
|
|
List<Task> tasks = taskMapper.queryTasks(taskStatus, taskType, taskName, taskCreator, auditStatus,
|
2024-05-28 02:21:58 +08:00
|
|
|
|
taskAct, taskAuditor, taskSource, ruleName,eventType, createDateStr, startDateStr,protectLevel, page, pageSize);
|
2024-01-15 20:40:55 +08:00
|
|
|
|
for (Task task : tasks) {
|
|
|
|
|
|
if (task == null) {
|
|
|
|
|
|
continue;
|
|
|
|
|
|
}
|
2024-06-05 03:29:32 +08:00
|
|
|
|
List<ProtectObject> protectObjects = taskMapper.queryProtectObjectsByTaskId(task.getTaskId());
|
|
|
|
|
|
task.setProtectObjects(protectObjects);
|
2024-05-09 21:58:05 +08:00
|
|
|
|
|
2024-04-25 12:40:41 +08:00
|
|
|
|
task.setStaticRuleIds(taskMapper.queryStaticRuleIdsFromTaskId(task.getTaskId(),
|
|
|
|
|
|
List.of(AuditStatusEnum.AUDITED.getNum(), AuditStatusEnum.USING.getNum())));
|
|
|
|
|
|
task.setDynamicRuleIds(taskMapper.queryDynamicRuleIdsFromTaskId(task.getTaskId(),
|
|
|
|
|
|
List.of(AuditStatusEnum.AUDITED.getNum(), AuditStatusEnum.USING.getNum())));
|
2024-01-15 20:40:55 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
}
|
2024-05-28 02:21:58 +08:00
|
|
|
|
List<ProtectObject> protectObjects = taskMapper.queryProtectObjectsByTaskId(id);
|
|
|
|
|
|
task.setProtectObjects(protectObjects);
|
2024-01-15 20:40:55 +08:00
|
|
|
|
|
2024-04-25 12:40:41 +08:00
|
|
|
|
task.setStaticRuleIds(taskMapper.queryStaticRuleIdsFromTaskId(task.getTaskId(),
|
|
|
|
|
|
List.of(AuditStatusEnum.AUDITED.getNum(), AuditStatusEnum.USING.getNum())));
|
|
|
|
|
|
task.setDynamicRuleIds(taskMapper.queryDynamicRuleIdsFromTaskId(task.getTaskId(),
|
|
|
|
|
|
List.of(AuditStatusEnum.AUDITED.getNum(), AuditStatusEnum.USING.getNum())));
|
2024-01-15 20:40:55 +08:00
|
|
|
|
|
|
|
|
|
|
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-05-28 02:21:58 +08:00
|
|
|
|
//校验防护对象是否存在
|
|
|
|
|
|
boolean ProtectObjIdValid = task.getProtectObjectIds().stream()
|
|
|
|
|
|
.allMatch(dynamicRuleMapper::queryProtectObjectById);
|
|
|
|
|
|
if (!ProtectObjIdValid) {
|
|
|
|
|
|
throw new IllegalArgumentException("部分防护对象不存在");
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//删除task关联的protectObjects
|
|
|
|
|
|
taskMapper.deleteTaskProtectObjectConcat(task.getTaskId());
|
|
|
|
|
|
//更新task
|
2024-01-05 09:32:19 +08:00
|
|
|
|
taskMapper.updateTask(task);
|
2024-05-28 02:21:58 +08:00
|
|
|
|
//重新关联task和protectObjects
|
|
|
|
|
|
|
|
|
|
|
|
taskMapper.newTaskProtectObjectConcat(task.getTaskId(), task.getProtectObjectIds());
|
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;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-05-21 09:42:57 +08:00
|
|
|
|
@Transactional
|
|
|
|
|
|
public Boolean changeTaskAuditStatus(Long taskId, Integer taskAuditStatus,
|
|
|
|
|
|
String auditUserName, String auditUserId, String auditUserDepart) {
|
|
|
|
|
|
Integer originalAuditStatus = taskMapper.queryTaskAuditStatus(taskId);
|
|
|
|
|
|
if (originalAuditStatus == null) {
|
|
|
|
|
|
throw new IllegalArgumentException("无法找到任务ID为" + taskId + "的任务,也许任务不存在?");
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (AuditStatusValidator.setOriginal(originalAuditStatus).checkValidate(taskAuditStatus))
|
|
|
|
|
|
taskMapper.changeTaskAuditStatusWithAudior(taskId, taskAuditStatus, auditUserName, auditUserId, auditUserDepart);
|
|
|
|
|
|
else return false;
|
2024-06-04 20:07:29 +08:00
|
|
|
|
insertTaskStatusLog(taskId);
|
2024-05-21 09:42:57 +08:00
|
|
|
|
return true;
|
|
|
|
|
|
}
|
2024-01-08 20:01:20 +08:00
|
|
|
|
@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;
|
2024-06-04 20:07:29 +08:00
|
|
|
|
insertTaskStatusLog(taskId);
|
2024-01-08 20:01:20 +08:00
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-01-11 19:49:07 +08:00
|
|
|
|
public Boolean deleteTask(Long taskId) {
|
2024-04-24 14:15:08 +08:00
|
|
|
|
Task task = taskMapper.queryTask(taskId);
|
|
|
|
|
|
if (task == null) {
|
|
|
|
|
|
return true;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-04-24 14:33:42 +08:00
|
|
|
|
updateStaticRuleAuditStatusInTask(taskId, AuditStatusEnum.AUDITED);
|
|
|
|
|
|
updateDynamicRuleAuditStatusInTask(taskId, AuditStatusEnum.AUDITED);
|
|
|
|
|
|
taskMapper.clearTaskConnectedStaticRule(task.getTaskId());
|
|
|
|
|
|
taskMapper.clearTaskConnectedDynamicRule(task.getTaskId());
|
2024-06-05 14:56:35 +08:00
|
|
|
|
|
|
|
|
|
|
commandMapper.removeCommandsByTaskId(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();
|
2024-05-09 13:06:21 +08:00
|
|
|
|
// taskCommandInfo.setMask();
|
2024-04-11 08:56:35 +08:00
|
|
|
|
});
|
|
|
|
|
|
|
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-05-07 22:33:59 +08:00
|
|
|
|
public Integer queryTaskTotalNum(Integer taskStatus, Integer taskType, String taskName, String taskCreator, Integer auditStatus
|
2024-05-22 17:36:25 +08:00
|
|
|
|
,String taskAct, String taskAuditor, String taskSource, String ruleName,
|
2024-05-28 02:21:58 +08:00
|
|
|
|
String eventType, String createDate, String startDate,Integer protectLevel) {
|
2024-05-07 22:33:59 +08:00
|
|
|
|
return taskMapper.queryTaskTotalNum(taskStatus, taskType, taskName, taskCreator, auditStatus,
|
2024-05-28 02:21:58 +08:00
|
|
|
|
taskAct, taskAuditor, taskSource, ruleName,null, eventType, createDate, startDate, protectLevel);
|
2024-01-22 15:40:03 +08:00
|
|
|
|
}
|
2024-04-23 21:05:04 +08:00
|
|
|
|
|
2024-05-09 13:06:21 +08:00
|
|
|
|
public Boolean updateAuditStatusBatch(Map<Integer, Integer> idsWithAuditStatusMap) {
|
2024-04-29 11:44:15 +08:00
|
|
|
|
//校验id和status是否合法
|
|
|
|
|
|
List<Integer> originalAuditStatusList = taskMapper.queryAuditStatusByIds(idsWithAuditStatusMap);
|
2024-05-09 13:06:21 +08:00
|
|
|
|
if (originalAuditStatusList == null || originalAuditStatusList.size() != idsWithAuditStatusMap.size()) {
|
|
|
|
|
|
throw new IllegalArgumentException("任务id部分不存在");
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-04-29 11:44:15 +08:00
|
|
|
|
int index = 0;
|
|
|
|
|
|
List<Integer> errorIds = new ArrayList<>();
|
|
|
|
|
|
for(Map.Entry<Integer, Integer> entry: idsWithAuditStatusMap.entrySet()) {
|
|
|
|
|
|
Integer id = entry.getKey();
|
|
|
|
|
|
Integer auditStatus = entry.getValue();
|
|
|
|
|
|
Integer originalAuditStatus = originalAuditStatusList.get(index);
|
|
|
|
|
|
index++;
|
2024-05-09 13:06:21 +08:00
|
|
|
|
|
2024-04-29 11:44:15 +08:00
|
|
|
|
if (!AuditStatusValidator.setOriginal(originalAuditStatus).checkValidate(auditStatus)) {
|
|
|
|
|
|
errorIds.add(id);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
if (!errorIds.isEmpty()){
|
2024-05-09 13:06:21 +08:00
|
|
|
|
throw new IllegalArgumentException("动态规则id无法修改为对应审核状态, errorIds: " + errorIds);
|
2024-04-29 11:44:15 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2024-04-23 21:05:04 +08:00
|
|
|
|
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);
|
2024-06-04 20:07:29 +08:00
|
|
|
|
//记录状态日志
|
|
|
|
|
|
insertTaskStatusLog(idWithAuditStatusBatch);
|
2024-04-23 21:05:04 +08:00
|
|
|
|
idWithAuditStatusBatch.clear();
|
|
|
|
|
|
}
|
|
|
|
|
|
if (!idWithAuditStatusBatch.isEmpty()) {
|
|
|
|
|
|
mapper.updateAuditStatusByIdBatch(idWithAuditStatusBatch);
|
2024-06-04 20:07:29 +08:00
|
|
|
|
insertTaskStatusLog(idWithAuditStatusBatch);
|
2024-04-23 21:05:04 +08:00
|
|
|
|
}
|
|
|
|
|
|
return true;
|
|
|
|
|
|
};
|
|
|
|
|
|
//实现事务操作
|
|
|
|
|
|
return sqlSessionWrapper.startBatchSession(TaskMapper.class, updateTaskAuditStatusFunction, idsWithAuditStatusMap);
|
|
|
|
|
|
|
|
|
|
|
|
|
2024-05-21 09:42:57 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public Boolean updateAuditStatusBatch(Map<Integer, Integer> idsWithAuditStatusMap,
|
|
|
|
|
|
String auditUserName, String auditUserId, String auditUserDepart) {
|
|
|
|
|
|
//校验id和status是否合法
|
|
|
|
|
|
List<Integer> originalAuditStatusList = taskMapper.queryAuditStatusByIds(idsWithAuditStatusMap);
|
|
|
|
|
|
if (originalAuditStatusList == null || originalAuditStatusList.size() != idsWithAuditStatusMap.size()) {
|
|
|
|
|
|
throw new IllegalArgumentException("任务id部分不存在");
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int index = 0;
|
|
|
|
|
|
List<Integer> errorIds = new ArrayList<>();
|
|
|
|
|
|
for(Map.Entry<Integer, Integer> entry: idsWithAuditStatusMap.entrySet()) {
|
|
|
|
|
|
Integer id = entry.getKey();
|
|
|
|
|
|
Integer auditStatus = entry.getValue();
|
|
|
|
|
|
Integer originalAuditStatus = originalAuditStatusList.get(index);
|
|
|
|
|
|
index++;
|
|
|
|
|
|
|
|
|
|
|
|
if (!AuditStatusValidator.setOriginal(originalAuditStatus).checkValidate(auditStatus)) {
|
|
|
|
|
|
errorIds.add(id);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
if (!errorIds.isEmpty()){
|
|
|
|
|
|
throw new IllegalArgumentException("动态规则id无法修改为对应审核状态, errorIds: " + errorIds);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
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.updateAuditStatusWithAuditorByIdBatch(idWithAuditStatusBatch, auditUserName, auditUserId, auditUserDepart);
|
|
|
|
|
|
idWithAuditStatusBatch.clear();
|
|
|
|
|
|
}
|
|
|
|
|
|
if (!idWithAuditStatusBatch.isEmpty()) {
|
|
|
|
|
|
mapper.updateAuditStatusWithAuditorByIdBatch(idWithAuditStatusBatch, auditUserName, auditUserId, auditUserDepart);
|
|
|
|
|
|
}
|
|
|
|
|
|
return true;
|
|
|
|
|
|
};
|
|
|
|
|
|
//实现事务操作
|
|
|
|
|
|
return sqlSessionWrapper.startBatchSession(TaskMapper.class, updateTaskAuditStatusFunction, idsWithAuditStatusMap);
|
|
|
|
|
|
|
|
|
|
|
|
|
2024-04-23 21:05:04 +08:00
|
|
|
|
}
|
2024-04-25 01:41:28 +08:00
|
|
|
|
|
|
|
|
|
|
public Integer queryAuditTaskTotalNum(Integer auditState) {
|
|
|
|
|
|
return taskMapper.queryAuditTaskTotalNum(auditState);
|
|
|
|
|
|
}
|
2024-05-07 22:33:59 +08:00
|
|
|
|
|
|
|
|
|
|
public List<Integer> queryAuditStatusBatch(Map<Integer, Integer> idsWithAuditStatusMap) {
|
|
|
|
|
|
//校验id和status是否合法
|
|
|
|
|
|
return taskMapper.queryAuditStatusByIds(idsWithAuditStatusMap);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-05-09 18:25:49 +08:00
|
|
|
|
public Boolean updateAuditInfo(List<Integer> ids, String auditInfo) {
|
|
|
|
|
|
return taskMapper.updateAuditInfo(ids, auditInfo);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public String queryAuditInfo(Integer id) {
|
|
|
|
|
|
return taskMapper.queryAuditInfo(id);
|
|
|
|
|
|
}
|
2024-06-04 20:07:29 +08:00
|
|
|
|
|
|
|
|
|
|
public void insertTaskStatusLog(Long taskId) {
|
|
|
|
|
|
taskMapper.updateTaskStatusLogExpireTime(taskId);
|
|
|
|
|
|
taskMapper.insertTaskStatusLog(taskId);
|
|
|
|
|
|
}
|
|
|
|
|
|
public void insertTaskStatusLog(Map<Integer, Integer> idWithAuditStatusBatch) {
|
|
|
|
|
|
Set<Integer> keys = idWithAuditStatusBatch.keySet();
|
|
|
|
|
|
ArrayList<Integer> taskIds = new ArrayList<>(keys);
|
|
|
|
|
|
|
|
|
|
|
|
taskMapper.updateTaskStatusLogExpireTimeBatch(taskIds);
|
|
|
|
|
|
taskMapper.insertTaskStatusLogBatch(taskIds);
|
|
|
|
|
|
}
|
2024-06-05 03:29:32 +08:00
|
|
|
|
|
|
|
|
|
|
public List<Task> queryHistory(Long id, Integer page, Integer pageSize) {
|
|
|
|
|
|
List<Task> tasks = taskMapper.queryHistory(id, page, pageSize);
|
|
|
|
|
|
|
|
|
|
|
|
for (Task task : tasks) {
|
|
|
|
|
|
if (task == null) {
|
|
|
|
|
|
continue;
|
|
|
|
|
|
}
|
|
|
|
|
|
List<ProtectObject> protectObjects = taskMapper.queryProtectObjectsByTaskId(task.getTaskId());
|
|
|
|
|
|
task.setProtectObjects(protectObjects);
|
|
|
|
|
|
|
|
|
|
|
|
task.setStaticRuleIds(taskMapper.queryStaticRuleIdsFromTaskId(task.getTaskId(),
|
|
|
|
|
|
List.of(AuditStatusEnum.AUDITED.getNum(), AuditStatusEnum.USING.getNum())));
|
|
|
|
|
|
task.setDynamicRuleIds(taskMapper.queryDynamicRuleIdsFromTaskId(task.getTaskId(),
|
|
|
|
|
|
List.of(AuditStatusEnum.AUDITED.getNum(), AuditStatusEnum.USING.getNum())));
|
|
|
|
|
|
}
|
|
|
|
|
|
return tasks;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public void removeDynamicRuleUsedTaskIdInTask(Long taskId) {
|
|
|
|
|
|
dynamicRuleMapper.removeUsedTaskId(taskId);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public void removeStaticRuleUsedTaskIdInTask(Long taskId) {
|
|
|
|
|
|
staticRuleMapper.removeUsedTaskId(taskId);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public void insertStaticRuleStatusLog(Map<Integer, Integer> idWithAuditStatusBatch) {
|
|
|
|
|
|
Set<Integer> keys = idWithAuditStatusBatch.keySet();
|
|
|
|
|
|
ArrayList<Integer> ids = new ArrayList<>(keys);
|
|
|
|
|
|
|
|
|
|
|
|
staticRuleMapper.updateStaticRuleStatusLogExpireTimeBatch(ids);
|
|
|
|
|
|
staticRuleMapper.insertStaticRuleStatusLogBatch(ids);
|
|
|
|
|
|
}
|
|
|
|
|
|
public void insertDynamicRuleStatusLog(Map<Integer, Integer> idWithAuditStatusBatch) {
|
|
|
|
|
|
Set<Integer> keys = idWithAuditStatusBatch.keySet();
|
|
|
|
|
|
ArrayList<Integer> ids = new ArrayList<>(keys);
|
|
|
|
|
|
|
|
|
|
|
|
dynamicRuleMapper.updateStatusLogExpireTimeBatch(ids);
|
|
|
|
|
|
dynamicRuleMapper.insertStatusLogBatch(ids);
|
|
|
|
|
|
|
|
|
|
|
|
}
|
2024-06-08 21:03:05 +08:00
|
|
|
|
|
|
|
|
|
|
public List<Long> getRunnableTasks() {
|
|
|
|
|
|
return taskMapper.queryRunnableTasks(StateEnum.PENDING.getStateNum(),AuditStatusEnum.AUDITED.getNum());
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public void updateTaskStartTime(Long taskId) {
|
|
|
|
|
|
taskMapper.updateTaskStartTime(taskId);
|
|
|
|
|
|
}
|
2024-01-03 09:13:22 +08:00
|
|
|
|
}
|