2026-03-10 22:47:43 +07:00
|
|
|
package ru.soune.nocopy.handler;
|
|
|
|
|
|
|
|
|
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
|
|
|
|
import lombok.RequiredArgsConstructor;
|
|
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
|
|
import org.springframework.data.domain.Page;
|
|
|
|
|
import org.springframework.stereotype.Component;
|
|
|
|
|
import ru.soune.nocopy.dto.BaseRequest;
|
|
|
|
|
import ru.soune.nocopy.dto.BaseResponse;
|
|
|
|
|
import ru.soune.nocopy.dto.violation.ViolationRequest;
|
|
|
|
|
import ru.soune.nocopy.dto.violation.ViolationResponse;
|
|
|
|
|
import ru.soune.nocopy.entity.file.FileEntity;
|
|
|
|
|
import ru.soune.nocopy.entity.violation.Violation;
|
|
|
|
|
import ru.soune.nocopy.repository.FileEntityRepository;
|
2026-03-18 17:07:44 +07:00
|
|
|
import ru.soune.nocopy.service.file.FileEntityService;
|
|
|
|
|
import ru.soune.nocopy.service.register.AuthService;
|
2026-03-10 22:47:43 +07:00
|
|
|
import ru.soune.nocopy.service.violation.ViolationService;
|
|
|
|
|
|
|
|
|
|
import java.io.FileNotFoundException;
|
|
|
|
|
import java.time.LocalDateTime;
|
|
|
|
|
import java.time.format.DateTimeFormatter;
|
|
|
|
|
import java.time.format.DateTimeParseException;
|
|
|
|
|
import java.util.List;
|
|
|
|
|
import java.util.Map;
|
|
|
|
|
|
|
|
|
|
@Component
|
|
|
|
|
@Slf4j
|
|
|
|
|
@RequiredArgsConstructor
|
|
|
|
|
public class ViolationHandler implements RequestHandler {
|
|
|
|
|
private final ObjectMapper objectMapper;
|
|
|
|
|
|
|
|
|
|
private final ViolationService violationService;
|
|
|
|
|
|
|
|
|
|
private final FileEntityRepository fileRepository;
|
|
|
|
|
|
2026-03-18 17:07:44 +07:00
|
|
|
private final AuthService authService;
|
|
|
|
|
|
|
|
|
|
private final FileEntityService fileEntityService;
|
|
|
|
|
|
2026-03-10 22:47:43 +07:00
|
|
|
private static final DateTimeFormatter DATE_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public BaseResponse handle(BaseRequest request) throws Exception {
|
|
|
|
|
ViolationRequest violationRequest = objectMapper.convertValue(request.getMessageBody(), ViolationRequest.class);
|
|
|
|
|
|
|
|
|
|
validateRequest(violationRequest);
|
2026-03-18 17:07:44 +07:00
|
|
|
|
|
|
|
|
String token = violationRequest.getToken();
|
|
|
|
|
Long userId = authService.useUserAuthToken(token);
|
|
|
|
|
|
|
|
|
|
List<FileEntity> targetFiles;
|
|
|
|
|
|
|
|
|
|
if (violationRequest.getFileId() != null && !violationRequest.getFileId().isEmpty()) {
|
|
|
|
|
FileEntity file = fileRepository.findById(violationRequest.getFileId())
|
|
|
|
|
.orElseThrow(() -> new FileNotFoundException("File not found with id: " + violationRequest.getFileId()));
|
|
|
|
|
targetFiles = List.of(file);
|
|
|
|
|
} else {
|
|
|
|
|
targetFiles = fileEntityService.getAllUserFiles(userId);
|
|
|
|
|
}
|
2026-03-10 22:47:43 +07:00
|
|
|
|
|
|
|
|
LocalDateTime startDate = null;
|
|
|
|
|
LocalDateTime endDate = null;
|
|
|
|
|
|
|
|
|
|
if (violationRequest.getStartDate() != null && violationRequest.getEndDate() != null) {
|
|
|
|
|
startDate = parseDate(violationRequest.getStartDate());
|
|
|
|
|
endDate = parseDate(violationRequest.getEndDate());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ("group".equalsIgnoreCase(violationRequest.getAction())) {
|
|
|
|
|
Map<String, Long> groupedData = violationService.getGroupedViolations(
|
2026-03-18 17:07:44 +07:00
|
|
|
targetFiles,
|
2026-03-10 22:47:43 +07:00
|
|
|
violationRequest.getGroupBy(),
|
|
|
|
|
violationRequest.getStatus(),
|
|
|
|
|
startDate,
|
2026-03-18 17:07:44 +07:00
|
|
|
endDate);
|
2026-03-10 22:47:43 +07:00
|
|
|
|
|
|
|
|
return BaseResponse.builder()
|
|
|
|
|
.msgId(request.getMsgId())
|
|
|
|
|
.messageBody(groupedData)
|
|
|
|
|
.build();
|
2026-03-11 16:30:26 +07:00
|
|
|
} else {
|
2026-03-10 22:47:43 +07:00
|
|
|
Page<Violation> violationPage;
|
|
|
|
|
|
|
|
|
|
if (startDate != null && endDate != null) {
|
2026-03-18 17:07:44 +07:00
|
|
|
violationPage = violationService.getViolationsByFilesAndDateRange(
|
|
|
|
|
targetFiles, startDate, endDate,
|
2026-03-10 22:47:43 +07:00
|
|
|
violationRequest.getPage(), violationRequest.getSize(), violationRequest.getSortDirection()
|
|
|
|
|
);
|
|
|
|
|
} else if (violationRequest.getStatus() != null && !violationRequest.getStatus().isEmpty()) {
|
2026-03-18 17:07:44 +07:00
|
|
|
violationPage = violationService.getViolationsByFilesAndStatus(
|
|
|
|
|
targetFiles, violationRequest.getStatus(),
|
2026-03-10 22:47:43 +07:00
|
|
|
violationRequest.getPage(), violationRequest.getSize(), violationRequest.getSortDirection()
|
|
|
|
|
);
|
|
|
|
|
} else {
|
2026-03-18 17:07:44 +07:00
|
|
|
violationPage = violationService.getViolationsByFiles(
|
|
|
|
|
targetFiles,
|
2026-03-10 22:47:43 +07:00
|
|
|
violationRequest.getPage(), violationRequest.getSize(), violationRequest.getSortDirection()
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
List<ViolationResponse.ViolationDto> violationDtos = violationPage.getContent()
|
|
|
|
|
.stream()
|
|
|
|
|
.map(ViolationResponse.ViolationDto::fromEntity)
|
|
|
|
|
.toList();
|
|
|
|
|
|
|
|
|
|
ViolationResponse response = ViolationResponse.builder()
|
|
|
|
|
.violations(violationDtos)
|
|
|
|
|
.totalElements(violationPage.getTotalElements())
|
|
|
|
|
.totalPages(violationPage.getTotalPages())
|
|
|
|
|
.currentPage(violationPage.getNumber())
|
|
|
|
|
.pageSize(violationPage.getSize())
|
|
|
|
|
.hasNext(violationPage.hasNext())
|
|
|
|
|
.hasPrevious(violationPage.hasPrevious())
|
|
|
|
|
.build();
|
|
|
|
|
|
|
|
|
|
return BaseResponse.builder()
|
|
|
|
|
.msgId(request.getMsgId())
|
|
|
|
|
.messageBody(response)
|
|
|
|
|
.build();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void validateRequest(ViolationRequest request) {
|
|
|
|
|
if (request.getPage() < 0) {
|
|
|
|
|
request.setPage(0);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (request.getSize() <= 0 || request.getSize() > 100) {
|
|
|
|
|
request.setSize(10);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!request.getSortDirection().equalsIgnoreCase("asc") &&
|
|
|
|
|
!request.getSortDirection().equalsIgnoreCase("desc")) {
|
|
|
|
|
request.setSortDirection("desc");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private LocalDateTime parseDate(String dateStr) {
|
|
|
|
|
try {
|
|
|
|
|
return LocalDateTime.parse(dateStr, DATE_FORMATTER);
|
|
|
|
|
} catch (DateTimeParseException e) {
|
|
|
|
|
log.error(e.getMessage());
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|