2024-01-11 19:49:07 +08:00
|
|
|
package com.realtime.protection.server.command;
|
|
|
|
|
|
|
|
|
|
import com.alibaba.excel.util.ListUtils;
|
2024-01-12 14:31:34 +08:00
|
|
|
import com.baomidou.dynamic.datasource.annotation.DS;
|
2024-01-11 19:49:07 +08:00
|
|
|
import com.realtime.protection.configuration.entity.task.TaskCommandInfo;
|
|
|
|
|
import com.realtime.protection.configuration.utils.SqlSessionWrapper;
|
|
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
|
|
import org.springframework.stereotype.Service;
|
2024-01-19 15:09:23 +08:00
|
|
|
import org.springframework.transaction.annotation.Transactional;
|
2024-01-11 19:49:07 +08:00
|
|
|
|
|
|
|
|
import java.util.List;
|
2024-01-19 15:09:23 +08:00
|
|
|
import java.util.UUID;
|
2024-01-11 19:49:07 +08:00
|
|
|
import java.util.function.Function;
|
|
|
|
|
|
|
|
|
|
@Service
|
|
|
|
|
@Slf4j
|
2024-01-15 20:40:55 +08:00
|
|
|
@DS("doris")
|
2024-01-11 19:49:07 +08:00
|
|
|
public class CommandService {
|
|
|
|
|
|
|
|
|
|
private final CommandMapper commandMapper;
|
|
|
|
|
private final SqlSessionWrapper sqlSessionWrapper;
|
2024-01-12 14:31:34 +08:00
|
|
|
private static final int BatchSize = 100;
|
2024-01-11 19:49:07 +08:00
|
|
|
|
2024-01-15 20:40:55 +08:00
|
|
|
public CommandService(CommandMapper commandMapper, SqlSessionWrapper sqlSessionWrapper) {
|
2024-01-11 19:49:07 +08:00
|
|
|
this.commandMapper = commandMapper;
|
|
|
|
|
this.sqlSessionWrapper = sqlSessionWrapper;
|
2024-01-15 20:40:55 +08:00
|
|
|
}
|
2024-01-11 19:49:07 +08:00
|
|
|
|
2024-01-19 15:09:23 +08:00
|
|
|
@Transactional
|
|
|
|
|
public String createCommand(TaskCommandInfo commandInfo) {
|
|
|
|
|
commandInfo.setUUID(UUID.randomUUID().toString());
|
|
|
|
|
commandMapper.createCommand(commandInfo);
|
|
|
|
|
return commandInfo.getUUID();
|
|
|
|
|
|
2024-01-15 20:40:55 +08:00
|
|
|
}
|
2024-01-11 19:49:07 +08:00
|
|
|
|
2024-01-15 20:40:55 +08:00
|
|
|
public void createCommands(List<TaskCommandInfo> taskCommandInfos) {
|
|
|
|
|
Function<CommandMapper, Function<List<TaskCommandInfo>, Boolean>> function = mapper -> list -> {
|
|
|
|
|
List<TaskCommandInfo> taskCommandInfoBatch = ListUtils.newArrayListWithExpectedSize(BatchSize);
|
|
|
|
|
for (TaskCommandInfo info : list) {
|
2024-01-19 15:09:23 +08:00
|
|
|
info.setUUID(UUID.randomUUID().toString());
|
2024-01-15 20:40:55 +08:00
|
|
|
taskCommandInfoBatch.add(info);
|
|
|
|
|
if (taskCommandInfoBatch.size() < BatchSize) {
|
2024-01-11 19:49:07 +08:00
|
|
|
continue;
|
|
|
|
|
}
|
2024-01-15 20:40:55 +08:00
|
|
|
|
|
|
|
|
commandMapper.createCommands(taskCommandInfoBatch);
|
|
|
|
|
taskCommandInfoBatch.clear();
|
2024-01-11 19:49:07 +08:00
|
|
|
}
|
|
|
|
|
|
2024-01-15 20:40:55 +08:00
|
|
|
if (!taskCommandInfoBatch.isEmpty()) {
|
|
|
|
|
commandMapper.createCommands(taskCommandInfoBatch);
|
|
|
|
|
taskCommandInfoBatch.clear();
|
2024-01-11 19:49:07 +08:00
|
|
|
}
|
|
|
|
|
|
2024-01-15 20:40:55 +08:00
|
|
|
return true;
|
2024-01-11 19:49:07 +08:00
|
|
|
};
|
|
|
|
|
|
2024-01-15 20:40:55 +08:00
|
|
|
sqlSessionWrapper.startBatchSession(CommandMapper.class, function, taskCommandInfos);
|
|
|
|
|
}
|
2024-01-11 19:49:07 +08:00
|
|
|
|
2024-01-15 20:40:55 +08:00
|
|
|
public List<TaskCommandInfo> queryCommandInfoByTaskId(Long taskId) {
|
|
|
|
|
return commandMapper.queryCommandInfoByTaskId(taskId);
|
2024-01-11 19:49:07 +08:00
|
|
|
}
|
|
|
|
|
|
2024-01-19 15:09:23 +08:00
|
|
|
public TaskCommandInfo queryCommandInfoByUUID(String uuid) {
|
|
|
|
|
return commandMapper.queryCommandInfoByUUID(uuid);
|
|
|
|
|
}
|
|
|
|
|
|
2024-01-11 19:49:07 +08:00
|
|
|
public Boolean startCommandsByTaskId(Long taskId) {
|
|
|
|
|
return commandMapper.startCommandsByTaskId(taskId);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Boolean stopCommandsByTaskId(Long taskId) {
|
|
|
|
|
return commandMapper.stopCommandsByTaskId(taskId);
|
|
|
|
|
}
|
|
|
|
|
|
2024-01-19 15:45:06 +08:00
|
|
|
@DS("doris")
|
2024-01-11 19:49:07 +08:00
|
|
|
public Boolean removeCommandsByTaskId(Long taskId) {
|
|
|
|
|
return commandMapper.removeCommandsByTaskId(taskId);
|
|
|
|
|
}
|
2024-01-21 00:51:10 +08:00
|
|
|
|
|
|
|
|
@DS("doris")
|
|
|
|
|
public Object updateCommandVaid(String commandId, Integer isValid) {
|
|
|
|
|
if (isValid == 0) {
|
|
|
|
|
return commandMapper.setCommandInvalid(commandId);
|
|
|
|
|
}
|
|
|
|
|
if (isValid == 1) {
|
|
|
|
|
return commandMapper.setCommandValid(commandId);
|
|
|
|
|
}
|
|
|
|
|
return new IllegalArgumentException("isValid must be 0 or 1");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2024-01-11 19:49:07 +08:00
|
|
|
}
|