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;
|
2026-04-27 12:21:26 +07:00
|
|
|
import ru.soune.nocopy.dto.MessageCode;
|
2026-03-10 22:47:43 +07:00
|
|
|
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-04-27 12:44:13 +07:00
|
|
|
import ru.soune.nocopy.service.complaint.ComplaintEntityService;
|
|
|
|
|
import ru.soune.nocopy.service.complaint.LawCaseService;
|
2026-03-18 17:07:44 +07:00
|
|
|
import ru.soune.nocopy.service.file.FileEntityService;
|
2026-04-15 09:34:32 +07:00
|
|
|
import ru.soune.nocopy.service.geo.GeoCountryService;
|
2026-03-18 17:07:44 +07:00
|
|
|
import ru.soune.nocopy.service.register.AuthService;
|
2026-03-10 22:47:43 +07:00
|
|
|
import ru.soune.nocopy.service.violation.ViolationService;
|
2026-03-26 14:34:34 +07:00
|
|
|
import ru.soune.nocopy.service.violation.ViolationStatus;
|
2026-03-10 22:47:43 +07:00
|
|
|
|
|
|
|
|
import java.io.FileNotFoundException;
|
|
|
|
|
import java.time.LocalDateTime;
|
|
|
|
|
import java.time.format.DateTimeFormatter;
|
|
|
|
|
import java.time.format.DateTimeParseException;
|
2026-04-15 09:34:32 +07:00
|
|
|
import java.util.ArrayList;
|
2026-03-10 22:47:43 +07:00
|
|
|
import java.util.List;
|
|
|
|
|
import java.util.Map;
|
2026-04-22 12:16:37 +07:00
|
|
|
import java.util.Objects;
|
2026-03-10 22:47:43 +07:00
|
|
|
|
|
|
|
|
@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-04-15 09:34:32 +07:00
|
|
|
private final GeoCountryService geoCountryService;
|
|
|
|
|
|
2026-04-27 12:44:13 +07:00
|
|
|
private final LawCaseService lawCaseService;
|
|
|
|
|
|
|
|
|
|
private final ComplaintEntityService complaintEntityService;
|
|
|
|
|
|
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());
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-26 14:34:34 +07:00
|
|
|
if ("updateStatus".equals(violationRequest.getAction())) {
|
|
|
|
|
String status = violationRequest.getStatus();
|
|
|
|
|
Long violationId = violationRequest.getViolationId();
|
|
|
|
|
|
2026-04-27 12:21:26 +07:00
|
|
|
if (status.equals(ViolationStatus.NOT_OWNER_FILE.name()) &&
|
2026-04-27 12:44:13 +07:00
|
|
|
lawCaseService.fetchByViolationId(violationId) != null &&
|
|
|
|
|
complaintEntityService.fetchComplaintByViolation(violationId) != null) {
|
2026-04-27 12:21:26 +07:00
|
|
|
|
|
|
|
|
return BaseResponse.builder()
|
|
|
|
|
.messageCode(MessageCode.COMPLAINT_OR_LAW_CASE_IN_WORK.getCode())
|
|
|
|
|
.messageCode(MessageCode.COMPLAINT_OR_LAW_CASE_IN_WORK.getCode())
|
|
|
|
|
.msgId(request.getMsgId())
|
|
|
|
|
.messageBody(Map.of("status", status))
|
|
|
|
|
.build();
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-26 14:34:34 +07:00
|
|
|
violationService.changeStatus(ViolationStatus.valueOf(status), violationId);
|
|
|
|
|
|
|
|
|
|
return BaseResponse.builder()
|
|
|
|
|
.msgId(request.getMsgId())
|
|
|
|
|
.messageBody(Map.of("status", status))
|
|
|
|
|
.build();
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-10 22:47:43 +07:00
|
|
|
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:55:14 +07:00
|
|
|
endDate,
|
|
|
|
|
violationRequest.getSortDirection());
|
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(
|
2026-04-24 12:54:48 +07:00
|
|
|
targetFiles, violationRequest.getPage(), violationRequest.getSize(),
|
|
|
|
|
violationRequest.getSortDirection());
|
2026-03-10 22:47:43 +07:00
|
|
|
}
|
2026-04-24 12:54:48 +07:00
|
|
|
|
2026-04-22 12:16:37 +07:00
|
|
|
ViolationResponse.ViolationDto firstShowViolation = null;
|
2026-04-15 09:34:32 +07:00
|
|
|
List<Violation> content = violationPage.getContent();
|
|
|
|
|
List<ViolationResponse.ViolationDto> violationDtos = new ArrayList<>();
|
|
|
|
|
|
2026-04-22 12:16:37 +07:00
|
|
|
if ("getByViolationId".equals(violationRequest.getAction())) {
|
|
|
|
|
Violation violation = violationService.getById(violationRequest.getViolationId());
|
|
|
|
|
if (violation != null) {
|
|
|
|
|
firstShowViolation = ViolationResponse.ViolationDto.fromEntity(violation);
|
|
|
|
|
violationDtos.add(firstShowViolation);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-15 09:34:32 +07:00
|
|
|
for (Violation violation : content) {
|
2026-04-24 13:11:49 +07:00
|
|
|
if (firstShowViolation != null && Objects.equals(firstShowViolation.getId(), violation.getId())) continue;
|
2026-04-22 12:16:37 +07:00
|
|
|
|
2026-04-15 09:34:32 +07:00
|
|
|
ViolationResponse.ViolationDto violationDto = ViolationResponse.ViolationDto.fromEntity(violation);
|
2026-04-15 10:25:25 +07:00
|
|
|
violationDto.setCountry(geoCountryService.getCountryName(violation.getPageUrl()));
|
|
|
|
|
violationDto.setCountryCode(geoCountryService.getCountryCode(violation.getPageUrl()));
|
2026-04-15 09:34:32 +07:00
|
|
|
|
|
|
|
|
violationDtos.add(violationDto);
|
|
|
|
|
}
|
2026-03-10 22:47:43 +07:00
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|