2026-03-10 22:47:43 +07:00
|
|
|
package ru.soune.nocopy.handler;
|
|
|
|
|
|
|
|
|
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
2026-05-29 18:17:04 +07:00
|
|
|
import jakarta.servlet.http.HttpServletRequest;
|
2026-03-10 22:47:43 +07:00
|
|
|
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-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 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-05-29 18:17:04 +07:00
|
|
|
private final HttpServletRequest httpServletRequest;
|
|
|
|
|
|
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-05-29 18:17:04 +07:00
|
|
|
Long userId = (Long) httpServletRequest.getAttribute("userId");
|
2026-03-18 17:07:44 +07:00
|
|
|
|
|
|
|
|
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()));
|
2026-05-13 07:44:24 +07:00
|
|
|
|
|
|
|
|
if (!Objects.equals(file.getUserId(), userId)) {
|
|
|
|
|
return BaseResponse.builder()
|
|
|
|
|
.messageCode(MessageCode.USER_NOT_HAD_PERMISSION.getCode())
|
2026-05-13 07:59:28 +07:00
|
|
|
.messageDesc(MessageCode.USER_NOT_HAD_PERMISSION.getDescription())
|
2026-05-13 07:44:24 +07:00
|
|
|
.msgId(request.getMsgId())
|
|
|
|
|
.messageBody(Map.of("user_id", userId))
|
|
|
|
|
.build();
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-18 17:07:44 +07:00
|
|
|
targetFiles = List.of(file);
|
|
|
|
|
} else {
|
2026-06-04 11:11:17 +07:00
|
|
|
targetFiles = fileEntityService.getAllUserFilesExcludingBlockedAndRemoved(userId);
|
2026-03-18 17:07:44 +07:00
|
|
|
}
|
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-05-18 15:13:16 +07:00
|
|
|
|
|
|
|
|
if (endDate != null && !violationRequest.getEndDate().contains(" ")) {
|
|
|
|
|
endDate = endDate.withHour(23).withMinute(59).withSecond(59);
|
|
|
|
|
}
|
2026-03-10 22:47:43 +07:00
|
|
|
}
|
|
|
|
|
|
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())
|
2026-05-14 11:44:08 +07:00
|
|
|
.messageCode(MessageCode.SUCCESS.getCode())
|
|
|
|
|
.messageDesc(MessageCode.SUCCESS.getDescription())
|
2026-03-26 14:34:34 +07:00
|
|
|
.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())
|
2026-05-14 11:44:08 +07:00
|
|
|
.messageCode(MessageCode.SUCCESS.getCode())
|
|
|
|
|
.messageDesc(MessageCode.SUCCESS.getDescription())
|
2026-03-10 22:47:43 +07:00
|
|
|
.messageBody(groupedData)
|
|
|
|
|
.build();
|
2026-03-11 16:30:26 +07:00
|
|
|
} else {
|
2026-05-19 12:11:14 +07:00
|
|
|
Page<Violation> violationPage = violationService.getViolationsByFilesAndFilters(
|
|
|
|
|
targetFiles,
|
|
|
|
|
startDate,
|
|
|
|
|
endDate,
|
|
|
|
|
violationRequest.getStatus(),
|
|
|
|
|
violationRequest.getPage(),
|
|
|
|
|
violationRequest.getSize(),
|
2026-05-21 13:15:11 +07:00
|
|
|
violationRequest.getSortDirection());
|
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-05-19 13:08:55 +07:00
|
|
|
String country;
|
|
|
|
|
String countryCode;
|
2026-04-22 12:16:37 +07:00
|
|
|
|
2026-04-15 09:34:32 +07:00
|
|
|
ViolationResponse.ViolationDto violationDto = ViolationResponse.ViolationDto.fromEntity(violation);
|
2026-05-19 13:08:55 +07:00
|
|
|
try {
|
|
|
|
|
country = geoCountryService.getCountryName(violation.getHost());
|
|
|
|
|
countryCode = geoCountryService.getCountryCode(violation.getHost());
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
country = geoCountryService.getCountryName(violation.getUrl());
|
|
|
|
|
countryCode = geoCountryService.getCountryCode(violation.getUrl());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
violationDto.setCountry(country);
|
|
|
|
|
violationDto.setCountryCode(countryCode);
|
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)
|
2026-05-13 07:44:24 +07:00
|
|
|
.messageCode(MessageCode.SUCCESS.getCode())
|
|
|
|
|
.messageDesc(MessageCode.SUCCESS.getDescription())
|
2026-03-10 22:47:43 +07:00
|
|
|
.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;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|