63 lines
2.3 KiB
Java
63 lines
2.3 KiB
Java
package ru.soune.nocopy.service.search;
|
|||
|
|
|
||
|
|
import lombok.RequiredArgsConstructor;
|
||
|
|
import lombok.extern.slf4j.Slf4j;
|
||
|
|
import org.springframework.stereotype.Service;
|
||
|
|
import org.springframework.transaction.annotation.Transactional;
|
||
|
|
import ru.soune.nocopy.dto.search.GlobalSearchStartRequest;
|
||
|
|
import ru.soune.nocopy.entity.file.FileEntity;
|
||
|
|
import ru.soune.nocopy.entity.monitoring.FileMonitoringEntity;
|
||
|
|
import ru.soune.nocopy.entity.search.GlobalSearchTask;
|
||
|
|
import ru.soune.nocopy.entity.search.SearchStatus;
|
||
|
|
import ru.soune.nocopy.repository.FileEntityRepository;
|
||
|
|
import ru.soune.nocopy.repository.FileMonitoringRepository;
|
||
|
|
import ru.soune.nocopy.repository.GlobalSearchTaskRepository;
|
||
|
|
|
||
|
|
import java.time.LocalDateTime;
|
||
|
|
import java.util.ArrayList;
|
||
|
|
import java.util.List;
|
||
|
|
|
||
|
|
@Slf4j
|
||
|
|
@Service
|
||
|
|
@RequiredArgsConstructor
|
||
|
|
public class GlobalSearchService {
|
||
|
|
|
||
|
|
private final FileEntityRepository fileEntityRepository;
|
||
|
|
|
||
|
|
private final FileMonitoringRepository fileMonitoringRepository;
|
||
|
|
|
||
|
|
private final GlobalSearchTaskRepository globalSearchTaskRepository;
|
||
|
|
|
||
|
|
private final GlobalSearchAsyncProcessor asyncProcessor;
|
||
|
|
|
||
|
|
@Transactional
|
||
|
|
public String startSearch(GlobalSearchStartRequest request, Long userId, List<FileEntity> filesToProcess) {
|
||
|
|
GlobalSearchTask task = new GlobalSearchTask();
|
||
|
|
task.setUserId(userId);
|
||
|
|
task.setSearchType(request.getSearchType());
|
||
|
|
task.setStatus(SearchStatus.PROCESSING.name());
|
||
|
|
task.setTotalFiles(filesToProcess.size());
|
||
|
|
task.setProcessedFiles(0);
|
||
|
|
task.setCreatedAt(LocalDateTime.now());
|
||
|
|
|
||
|
|
globalSearchTaskRepository.save(task);
|
||
|
|
|
||
|
|
asyncProcessor.processFilesAsync(task.getTaskId(), filesToProcess, userId);
|
||
|
|
|
||
|
|
return task.getTaskId();
|
||
|
|
}
|
||
|
|
|
||
|
|
public List<FileEntity> getFilesToProcess(GlobalSearchStartRequest request, Long userId) {
|
||
|
|
if ("monitoring".equals(request.getSearchType())) {
|
||
|
|
List<FileMonitoringEntity> monitoringFiles =
|
||
|
|
fileMonitoringRepository.findByUserIdAndIsActiveTrue(userId);
|
||
|
|
|
||
|
|
return monitoringFiles.isEmpty() ? new ArrayList<>(): monitoringFiles.stream()
|
||
|
|
.map(FileMonitoringEntity::getFile)
|
||
|
|
.toList();
|
||
|
|
} else {
|
||
|
|
return fileEntityRepository.findAllById(request.getFileIds());
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|