From f7c2a186cf7b8a498aa5528934be3079547df2df Mon Sep 17 00:00:00 2001 From: vladp Date: Wed, 18 Mar 2026 18:21:33 +0700 Subject: [PATCH] dev add violation notion --- .../service/violation/ViolationService.java | 50 ++++++++++++++++--- 1 file changed, 43 insertions(+), 7 deletions(-) diff --git a/src/main/java/ru/soune/nocopy/service/violation/ViolationService.java b/src/main/java/ru/soune/nocopy/service/violation/ViolationService.java index c90b5c1..8133dcf 100644 --- a/src/main/java/ru/soune/nocopy/service/violation/ViolationService.java +++ b/src/main/java/ru/soune/nocopy/service/violation/ViolationService.java @@ -185,27 +185,63 @@ public class ViolationService { Collectors.counting())); } +// public Map getGroupedViolations(List files, String groupBy, String status, +// LocalDateTime startDate, LocalDateTime endDate, +// String sortDirection) { +// List violations; +// +// Sort sort = Sort.by(sortDirection.equalsIgnoreCase("desc") ? Sort.Direction.DESC : Sort.Direction.ASC, +// "createdDate"); +// +// if (status != null && !status.isEmpty() && startDate != null && endDate != null) { +// violations = violationRepository.findByFileEntityInAndStatusAndCreatedDateBetween(files, status, startDate, endDate, sort); +// } else if (status != null && !status.isEmpty()) { +// violations = violationRepository.findByFileEntityInAndStatus(files, status, sort); +// } else if (startDate != null && endDate != null) { +// violations = violationRepository.findByFileEntityInAndCreatedDateBetween(files, startDate, endDate, sort); +// } else { +// violations = violationRepository.findByFileEntityIn(files, sort); +// } +// +// return violations.stream() +// .collect(Collectors.groupingBy( +// v -> extractGroupKey(v.getPageUrl(), groupBy), +// Collectors.counting())); +// } + public Map getGroupedViolations(List files, String groupBy, String status, LocalDateTime startDate, LocalDateTime endDate, String sortDirection) { List violations; - Sort sort = Sort.by(sortDirection.equalsIgnoreCase("desc") ? Sort.Direction.DESC : Sort.Direction.ASC, "createdDate"); - if (status != null && !status.isEmpty() && startDate != null && endDate != null) { - violations = violationRepository.findByFileEntityInAndStatusAndCreatedDateBetween(files, status, startDate, endDate, sort); + violations = violationRepository.findByFileEntityInAndStatusAndCreatedDateBetween(files, status, startDate, endDate); } else if (status != null && !status.isEmpty()) { - violations = violationRepository.findByFileEntityInAndStatus(files, status, sort); + violations = violationRepository.findByFileEntityInAndStatus(files, status); } else if (startDate != null && endDate != null) { - violations = violationRepository.findByFileEntityInAndCreatedDateBetween(files, startDate, endDate, sort); + violations = violationRepository.findByFileEntityInAndCreatedDateBetween(files, startDate, endDate); } else { - violations = violationRepository.findByFileEntityIn(files, sort); + violations = violationRepository.findByFileEntityIn(files); } - return violations.stream() + Map grouped = violations.stream() .collect(Collectors.groupingBy( v -> extractGroupKey(v.getPageUrl(), groupBy), Collectors.counting())); + + return grouped.entrySet().stream() + .sorted((e1, e2) -> { + if (sortDirection.equalsIgnoreCase("desc")) { + return e2.getValue().compareTo(e1.getValue()); + } else { + return e1.getValue().compareTo(e2.getValue()); + } + }) + .collect(Collectors.toMap( + Map.Entry::getKey, + Map.Entry::getValue, + (e1, e2) -> e1, + LinkedHashMap::new)); } public ViolationStatisticsResponse getViolationStatistics(Long userId, String fileId,