@@ -8,10 +8,12 @@ import org.springframework.data.domain.Pageable;
|
||||
import org.springframework.data.domain.Sort;
|
||||
import org.springframework.stereotype.Service;
|
||||
import ru.soune.nocopy.dto.file.YandexSearchResponse;
|
||||
import ru.soune.nocopy.dto.violation.ViolationStatisticsResponse;
|
||||
import ru.soune.nocopy.entity.file.FileEntity;
|
||||
import ru.soune.nocopy.entity.search.GlobalSearchResult;
|
||||
import ru.soune.nocopy.entity.violation.Violation;
|
||||
import ru.soune.nocopy.exception.ViolationNotFoundException;
|
||||
import ru.soune.nocopy.repository.FileEntityRepository;
|
||||
import ru.soune.nocopy.repository.ViolationRepository;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
@@ -25,6 +27,8 @@ public class ViolationService {
|
||||
|
||||
private final ViolationRepository violationRepository;
|
||||
|
||||
private final FileEntityRepository fileEntityRepository;
|
||||
|
||||
public void processViolation(YandexSearchResponse.ImageResult imageResult, FileEntity file,
|
||||
GlobalSearchResult result) {
|
||||
String url = imageResult.getUrl();
|
||||
@@ -113,4 +117,65 @@ public class ViolationService {
|
||||
return "unknown";
|
||||
}
|
||||
}
|
||||
|
||||
public ViolationStatisticsResponse getViolationStatistics(Long userId, String fileId,
|
||||
LocalDateTime startDate, LocalDateTime endDate) {
|
||||
|
||||
List<FileEntity> userFiles;
|
||||
|
||||
if (fileId != null && !fileId.isEmpty()) {
|
||||
FileEntity file = fileEntityRepository.findById(fileId)
|
||||
.orElseThrow(() -> new IllegalArgumentException("File not found with id: " + fileId));
|
||||
|
||||
if (!file.getUserId().equals(userId)) {
|
||||
throw new IllegalArgumentException("File does not belong to user");
|
||||
}
|
||||
|
||||
userFiles = List.of(file);
|
||||
} else {
|
||||
userFiles = fileEntityRepository.findByUserId(userId);
|
||||
}
|
||||
|
||||
if (userFiles.isEmpty()) {
|
||||
return ViolationStatisticsResponse.builder()
|
||||
.totalViolations(0)
|
||||
.newViolations(0)
|
||||
.inProgressViolations(0)
|
||||
.resolvedViolations(0)
|
||||
.build();
|
||||
}
|
||||
|
||||
List<Violation> violations;
|
||||
|
||||
if (startDate != null && endDate != null) {
|
||||
violations = violationRepository.findByFileEntityInAndCreatedDateBetween(userFiles, startDate, endDate);
|
||||
} else {
|
||||
violations = violationRepository.findByFileEntityIn(userFiles);
|
||||
}
|
||||
|
||||
long newViolations = violations.stream()
|
||||
.filter(v -> "CREATED".equalsIgnoreCase(v.getStatus()) ||
|
||||
"NEW".equalsIgnoreCase(v.getStatus()))
|
||||
.count();
|
||||
|
||||
long inProgressViolations = violations.stream()
|
||||
.filter(v -> "IN_PROGRESS".equalsIgnoreCase(v.getStatus()) ||
|
||||
"PROCESSING".equalsIgnoreCase(v.getStatus()) ||
|
||||
"IN_WORK".equalsIgnoreCase(v.getStatus()))
|
||||
.count();
|
||||
|
||||
long resolvedViolations = violations.stream()
|
||||
.filter(v -> "RESOLVED".equalsIgnoreCase(v.getStatus()) ||
|
||||
"COMPLETED".equalsIgnoreCase(v.getStatus()) ||
|
||||
"SOLVED".equalsIgnoreCase(v.getStatus()) ||
|
||||
"DONE".equalsIgnoreCase(v.getStatus()))
|
||||
.count();
|
||||
|
||||
return ViolationStatisticsResponse.builder()
|
||||
.totalViolations(violations.size())
|
||||
.newViolations(newViolations)
|
||||
.inProgressViolations(inProgressViolations)
|
||||
.resolvedViolations(resolvedViolations)
|
||||
.build();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user