2026-03-06 17:28:52 +07:00
|
|
|
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;
|
2026-03-12 17:31:40 +07:00
|
|
|
import ru.soune.nocopy.service.file.FileEntityService;
|
2026-03-06 17:28:52 +07:00
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
2026-03-12 17:31:40 +07:00
|
|
|
private final FileEntityService fileEntityService;
|
|
|
|
|
|
2026-03-06 17:28:52 +07:00
|
|
|
@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 {
|
2026-03-12 17:31:40 +07:00
|
|
|
List<String> fileIds = request.getFileIds().isEmpty() ? fileEntityService.getAllUserFiles(userId)
|
|
|
|
|
.stream()
|
|
|
|
|
.map(FileEntity::getId)
|
|
|
|
|
.toList(): request.getFileIds();
|
|
|
|
|
|
|
|
|
|
return fileEntityRepository.findAllById(fileIds);
|
2026-03-06 17:28:52 +07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|