|
|
|
@@ -2,19 +2,19 @@ package ru.soune.nocopy.service.violation;
|
|
|
|
|
|
|
|
|
|
import lombok.AllArgsConstructor;
|
|
|
|
|
import org.apache.commons.codec.digest.DigestUtils;
|
|
|
|
|
import org.springframework.data.domain.Page;
|
|
|
|
|
import org.springframework.data.domain.PageRequest;
|
|
|
|
|
import org.springframework.data.domain.Pageable;
|
|
|
|
|
import org.springframework.data.domain.Sort;
|
|
|
|
|
import org.springframework.data.domain.*;
|
|
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
import org.springframework.transaction.annotation.Transactional;
|
|
|
|
|
import ru.soune.nocopy.dto.file.YandexSearchResponse;
|
|
|
|
|
import ru.soune.nocopy.dto.violation.FileViolationSummaryDTO;
|
|
|
|
|
import ru.soune.nocopy.dto.violation.ViolationStatisticsResponse;
|
|
|
|
|
import ru.soune.nocopy.entity.file.FileEntity;
|
|
|
|
|
import ru.soune.nocopy.entity.file.FileStatus;
|
|
|
|
|
import ru.soune.nocopy.entity.search.GlobalSearchResult;
|
|
|
|
|
import ru.soune.nocopy.entity.violation.Violation;
|
|
|
|
|
import ru.soune.nocopy.repository.ComplaintEntityRepository;
|
|
|
|
|
import ru.soune.nocopy.repository.FileEntityRepository;
|
|
|
|
|
import ru.soune.nocopy.repository.LawCaseRepository;
|
|
|
|
|
import ru.soune.nocopy.repository.ViolationRepository;
|
|
|
|
|
import ru.soune.nocopy.service.file.FileEntityService;
|
|
|
|
|
|
|
|
|
@@ -32,6 +32,10 @@ public class ViolationService {
|
|
|
|
|
|
|
|
|
|
private final FileEntityService fileEntityService;
|
|
|
|
|
|
|
|
|
|
private final ComplaintEntityRepository complaintRepository;
|
|
|
|
|
|
|
|
|
|
private final LawCaseRepository lawCaseRepository;
|
|
|
|
|
|
|
|
|
|
public void processViolation(YandexSearchResponse.ImageResult imageResult, FileEntity file,
|
|
|
|
|
GlobalSearchResult result) {
|
|
|
|
|
String url = imageResult.getUrl();
|
|
|
|
@@ -61,22 +65,26 @@ public class ViolationService {
|
|
|
|
|
return violationRepository.findById(violationId).orElse(null);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public List<FileViolationSummaryDTO> getFileViolationsSummary(Long userId) {
|
|
|
|
|
List<FileEntity> userFiles = fileEntityService.getAllUserFiles(userId);
|
|
|
|
|
List<Violation> violations = violationRepository.findByFileEntityInAndStatusNot(userFiles,
|
|
|
|
|
ViolationStatus.NOT_OWNER_FILE.name());
|
|
|
|
|
public Page<FileViolationSummaryDTO> getFileViolationsSummary(Long userId, String fileName, String sortDirection, Integer page,
|
|
|
|
|
Integer size, LocalDateTime start, LocalDateTime end) {
|
|
|
|
|
List<FileEntity> userFiles = fileName == null || fileName.isEmpty() ? fileEntityService.getAllUserFiles(userId)
|
|
|
|
|
: fileEntityService.getAllUserFilesWithFileName(userId, fileName,
|
|
|
|
|
List.of(FileStatus.ACTIVE, FileStatus.BLOCKED, FileStatus.MODERATION));
|
|
|
|
|
|
|
|
|
|
Map<String, List<Violation>> violationsByFileId = violations.stream()
|
|
|
|
|
Sort sort = Sort.by(sortDirection.equalsIgnoreCase("desc") ? Sort.Direction.DESC : Sort.Direction.ASC,
|
|
|
|
|
"createdDate");
|
|
|
|
|
Pageable pageable = PageRequest.of(page, size, sort);
|
|
|
|
|
|
|
|
|
|
Page<Violation> violations = violationRepository.findByFileEntityInAndStatusNotAndCreatedDateBetween(
|
|
|
|
|
userFiles, ViolationStatus.NOT_OWNER_FILE.name(), start, end, pageable);
|
|
|
|
|
|
|
|
|
|
Map<String, List<Violation>> violationsByFileId = violations.getContent().stream()
|
|
|
|
|
.collect(Collectors.groupingBy(v -> v.getFileEntity().getId()));
|
|
|
|
|
|
|
|
|
|
return userFiles.stream()
|
|
|
|
|
.map(file -> {
|
|
|
|
|
List<Violation> fileViolations = violationsByFileId.getOrDefault(file.getId(),
|
|
|
|
|
Collections.emptyList());
|
|
|
|
|
|
|
|
|
|
if (fileViolations.isEmpty()) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
List<FileViolationSummaryDTO> dtoList = violationsByFileId.entrySet().stream()
|
|
|
|
|
.map(entry -> {
|
|
|
|
|
List<Violation> fileViolations = entry.getValue();
|
|
|
|
|
FileEntity file = fileViolations.get(0).getFileEntity();
|
|
|
|
|
|
|
|
|
|
LocalDateTime latestDate = fileViolations.stream()
|
|
|
|
|
.map(Violation::getCreatedDate)
|
|
|
|
@@ -95,8 +103,9 @@ public class ViolationService {
|
|
|
|
|
.latestViolationDate(latestDate)
|
|
|
|
|
.build();
|
|
|
|
|
})
|
|
|
|
|
.filter(Objects::nonNull)
|
|
|
|
|
.collect(Collectors.toList());
|
|
|
|
|
|
|
|
|
|
return new PageImpl<>(dtoList, pageable, violations.getTotalElements());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Transactional
|
|
|
|
@@ -226,11 +235,19 @@ public class ViolationService {
|
|
|
|
|
.filter(v -> v.getStatus().equals(ViolationStatus.AUTHORIZED_USE.name()))
|
|
|
|
|
.count();
|
|
|
|
|
|
|
|
|
|
List<Long> violationIds = violations.stream().map(Violation::getId).toList();
|
|
|
|
|
|
|
|
|
|
int totalComplaints = complaintRepository.countComplaintByViolations(violationIds);
|
|
|
|
|
|
|
|
|
|
int totalLawCases = lawCaseRepository.countLawCaseByViolationIds(violationIds);
|
|
|
|
|
|
|
|
|
|
return ViolationStatisticsResponse.builder()
|
|
|
|
|
.totalViolations(violations.size())
|
|
|
|
|
.newViolations(newViolations)
|
|
|
|
|
.inProgressViolations(inProgressViolations)
|
|
|
|
|
.resolvedViolations(resolvedViolations)
|
|
|
|
|
.totalComplaints(totalComplaints)
|
|
|
|
|
.totalLawCases(totalLawCases)
|
|
|
|
|
.build();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|