2026-03-06 17:28:52 +07:00
|
|
|
package ru.soune.nocopy.controller;
|
|
|
|
|
|
2026-05-29 18:17:04 +07:00
|
|
|
import jakarta.servlet.http.HttpServletRequest;
|
2026-03-06 17:28:52 +07:00
|
|
|
import lombok.RequiredArgsConstructor;
|
|
|
|
|
import lombok.extern.slf4j.Slf4j;
|
2026-04-28 15:41:57 +07:00
|
|
|
import org.springframework.data.domain.Page;
|
2026-03-06 17:28:52 +07:00
|
|
|
import org.springframework.http.ResponseEntity;
|
|
|
|
|
import org.springframework.web.bind.annotation.*;
|
|
|
|
|
import ru.soune.nocopy.dto.search.GlobalSearchStartRequest;
|
|
|
|
|
import ru.soune.nocopy.dto.search.GlobalSearchStartResponse;
|
|
|
|
|
import ru.soune.nocopy.dto.search.GlobalSearchStatusResponse;
|
2026-03-13 13:55:10 +07:00
|
|
|
import ru.soune.nocopy.dto.violation.FileViolationSummaryDTO;
|
2026-04-28 15:41:57 +07:00
|
|
|
import ru.soune.nocopy.dto.violation.ViolationRequest;
|
2026-03-06 17:28:52 +07:00
|
|
|
import ru.soune.nocopy.entity.file.FileEntity;
|
|
|
|
|
import ru.soune.nocopy.entity.search.GlobalSearchTask;
|
|
|
|
|
import ru.soune.nocopy.entity.search.SearchStatus;
|
|
|
|
|
import ru.soune.nocopy.repository.GlobalSearchTaskRepository;
|
|
|
|
|
import ru.soune.nocopy.service.search.GlobalSearchService;
|
2026-03-13 13:48:03 +07:00
|
|
|
import ru.soune.nocopy.service.violation.ViolationService;
|
2026-03-06 17:28:52 +07:00
|
|
|
|
|
|
|
|
import java.util.*;
|
|
|
|
|
|
|
|
|
|
@Slf4j
|
|
|
|
|
@RestController
|
|
|
|
|
@RequestMapping("/api/v1/global-search")
|
|
|
|
|
@RequiredArgsConstructor
|
|
|
|
|
public class GlobalSearchController {
|
|
|
|
|
|
|
|
|
|
private final GlobalSearchService globalSearchService;
|
|
|
|
|
|
|
|
|
|
private final GlobalSearchTaskRepository searchTaskRepository;
|
|
|
|
|
|
2026-03-13 13:48:03 +07:00
|
|
|
private final ViolationService violationService;
|
|
|
|
|
|
2026-05-29 18:17:04 +07:00
|
|
|
private final HttpServletRequest httpServletRequest;
|
|
|
|
|
|
2026-03-06 17:28:52 +07:00
|
|
|
@PostMapping("/start")
|
|
|
|
|
public ResponseEntity<?> startSearch(
|
2026-06-01 14:49:36 +07:00
|
|
|
@RequestBody GlobalSearchStartRequest request) {
|
2026-03-06 17:28:52 +07:00
|
|
|
GlobalSearchStartResponse response = new GlobalSearchStartResponse();
|
2026-03-12 17:27:22 +07:00
|
|
|
|
2026-06-01 14:49:36 +07:00
|
|
|
Long userId = (Long) httpServletRequest.getAttribute("userId");
|
2026-03-12 17:27:22 +07:00
|
|
|
|
2026-06-01 14:49:36 +07:00
|
|
|
List<FileEntity> filesToProcess = globalSearchService.getFilesToProcess(request, userId);
|
2026-03-12 17:27:22 +07:00
|
|
|
|
2026-06-01 14:49:36 +07:00
|
|
|
if (filesToProcess.isEmpty()) return ResponseEntity.ok().body("Files for search not found");
|
2026-03-12 17:27:22 +07:00
|
|
|
|
2026-06-01 14:49:36 +07:00
|
|
|
String taskId = globalSearchService.startSearch(request, userId, filesToProcess);
|
2026-03-12 17:27:22 +07:00
|
|
|
|
2026-06-01 14:49:36 +07:00
|
|
|
response.setTaskId(taskId);
|
|
|
|
|
response.setStatus(SearchStatus.ACCEPTED.name());
|
|
|
|
|
response.setTotalFiles(filesToProcess.size());
|
2026-03-06 17:28:52 +07:00
|
|
|
|
|
|
|
|
return ResponseEntity.ok(response);
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-12 15:46:37 +07:00
|
|
|
@GetMapping("/actual-task")
|
2026-06-01 14:49:36 +07:00
|
|
|
public ResponseEntity<?> getStatus() {
|
2026-03-12 17:27:22 +07:00
|
|
|
Optional<GlobalSearchTask> taskOptional;
|
2026-03-12 15:46:37 +07:00
|
|
|
|
2026-06-01 14:49:36 +07:00
|
|
|
Long userId = (Long) httpServletRequest.getAttribute("userId");
|
2026-03-12 15:46:37 +07:00
|
|
|
|
2026-06-01 14:49:36 +07:00
|
|
|
taskOptional = searchTaskRepository.findByUserIdAndStatus(userId,
|
|
|
|
|
"PROCESSING");
|
2026-03-12 17:27:22 +07:00
|
|
|
|
2026-06-01 14:49:36 +07:00
|
|
|
if (taskOptional.isEmpty()) return ResponseEntity.ok().body("Task not found");
|
2026-03-12 15:46:37 +07:00
|
|
|
|
|
|
|
|
return ResponseEntity.ok(taskOptional.get());
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-28 16:37:00 +07:00
|
|
|
@GetMapping("/violation-summary")
|
2026-06-01 14:49:36 +07:00
|
|
|
public ResponseEntity<?> getSummary() {
|
2026-04-28 16:37:00 +07:00
|
|
|
List<FileViolationSummaryDTO> fileViolationsSummary;
|
|
|
|
|
|
2026-06-01 14:49:36 +07:00
|
|
|
Long userId = (Long) httpServletRequest.getAttribute("userId");
|
2026-04-28 16:37:00 +07:00
|
|
|
|
2026-06-01 14:49:36 +07:00
|
|
|
fileViolationsSummary = violationService.getFileViolationsSummary(userId);
|
2026-04-28 16:37:00 +07:00
|
|
|
|
2026-06-01 14:49:36 +07:00
|
|
|
if (fileViolationsSummary.isEmpty()) return ResponseEntity.ok().body("Violations summary not found");
|
2026-04-28 16:37:00 +07:00
|
|
|
|
|
|
|
|
return ResponseEntity.ok(fileViolationsSummary);
|
|
|
|
|
}
|
2026-03-13 13:48:03 +07:00
|
|
|
|
2026-04-28 15:41:57 +07:00
|
|
|
|
2026-04-28 17:40:37 +07:00
|
|
|
@PostMapping("/violation-summary-with-files")
|
2026-04-28 15:41:57 +07:00
|
|
|
public ResponseEntity<?> getViolations(@RequestBody ViolationRequest request) {
|
2026-05-29 18:17:04 +07:00
|
|
|
Long userId = (Long) httpServletRequest.getAttribute("userId");
|
|
|
|
|
Page<FileViolationSummaryDTO> result = violationService.getFileViolationsSummary(
|
|
|
|
|
userId, request.getFileName(), request.getSortDirection(),
|
|
|
|
|
request.getPage(), request.getSize(), request.getStartDate(), request.getEndDate());
|
2026-03-13 13:48:03 +07:00
|
|
|
|
2026-05-29 18:17:04 +07:00
|
|
|
return ResponseEntity.ok(result);
|
2026-03-13 13:48:03 +07:00
|
|
|
}
|
|
|
|
|
|
2026-03-06 17:28:52 +07:00
|
|
|
@GetMapping("/status/{taskId}")
|
2026-06-01 14:49:36 +07:00
|
|
|
public ResponseEntity<?> getStatus(@PathVariable String taskId) {
|
2026-03-12 17:27:22 +07:00
|
|
|
GlobalSearchStatusResponse response;
|
2026-06-01 14:49:36 +07:00
|
|
|
Long userId = (Long) httpServletRequest.getAttribute("userId");
|
2026-03-06 17:28:52 +07:00
|
|
|
|
2026-06-01 14:49:36 +07:00
|
|
|
Optional<GlobalSearchTask> taskOptional = searchTaskRepository.findByTaskIdAndUserId(taskId, userId);
|
2026-03-06 17:28:52 +07:00
|
|
|
|
2026-06-01 14:49:36 +07:00
|
|
|
if (taskOptional.isEmpty()) return ResponseEntity.ok().body("Task not found");
|
2026-03-06 17:28:52 +07:00
|
|
|
|
2026-06-01 14:49:36 +07:00
|
|
|
GlobalSearchTask task = taskOptional.orElseThrow();
|
2026-03-06 17:28:52 +07:00
|
|
|
|
2026-06-01 14:49:36 +07:00
|
|
|
int progress = task.getTotalFiles() > 0 ? task.getProcessedFiles() * 100 / task.getTotalFiles() : 0;
|
2026-03-06 17:28:52 +07:00
|
|
|
|
2026-06-01 14:49:36 +07:00
|
|
|
response = new GlobalSearchStatusResponse();
|
|
|
|
|
response.setTaskId(taskId);
|
|
|
|
|
response.setStatus(task.getStatus());
|
|
|
|
|
response.setProgress(progress);
|
2026-03-06 17:28:52 +07:00
|
|
|
|
|
|
|
|
|
|
|
|
|
return ResponseEntity.ok(response);
|
|
|
|
|
}
|
|
|
|
|
}
|