From 077902f4ab0060aac990ac82a01458a1693e8fb5 Mon Sep 17 00:00:00 2001 From: vladp Date: Mon, 6 Apr 2026 14:51:00 +0700 Subject: [PATCH] dev add lawcase logic --- .../nocopy/configuration/HandlerConfig.java | 4 +- .../java/ru/soune/nocopy/dto/MessageCode.java | 3 + .../nocopy/dto/complaint/BaseResponse.java | 35 +++ .../dto/complaint/LawCasePageResponse.java | 17 ++ .../nocopy/dto/complaint/LawCaseRequest.java | 30 +++ .../nocopy/dto/complaint/LawCaseResponse.java | 44 ++++ .../dto/complaint/PaginatedResponse.java | 17 ++ .../nocopy/entity/complaint/LawCase.java | 47 +++++ .../entity/complaint/LawCasePriority.java | 12 ++ .../nocopy/entity/complaint/LawCaseType.java | 15 ++ .../soune/nocopy/handler/LawCaseHandler.java | 199 ++++++++++++++++++ .../nocopy/repository/LawCaseRepository.java | 40 ++++ .../service/complaint/LawCaseService.java | 96 +++++++++ .../service/tariff/TariffConstants.java | 1 + 14 files changed, 559 insertions(+), 1 deletion(-) create mode 100644 src/main/java/ru/soune/nocopy/dto/complaint/BaseResponse.java create mode 100644 src/main/java/ru/soune/nocopy/dto/complaint/LawCasePageResponse.java create mode 100644 src/main/java/ru/soune/nocopy/dto/complaint/LawCaseRequest.java create mode 100644 src/main/java/ru/soune/nocopy/dto/complaint/LawCaseResponse.java create mode 100644 src/main/java/ru/soune/nocopy/dto/complaint/PaginatedResponse.java create mode 100644 src/main/java/ru/soune/nocopy/entity/complaint/LawCasePriority.java create mode 100644 src/main/java/ru/soune/nocopy/entity/complaint/LawCaseType.java create mode 100644 src/main/java/ru/soune/nocopy/handler/LawCaseHandler.java create mode 100644 src/main/java/ru/soune/nocopy/repository/LawCaseRepository.java create mode 100644 src/main/java/ru/soune/nocopy/service/complaint/LawCaseService.java diff --git a/src/main/java/ru/soune/nocopy/configuration/HandlerConfig.java b/src/main/java/ru/soune/nocopy/configuration/HandlerConfig.java index 41388e8..7a66c3f 100644 --- a/src/main/java/ru/soune/nocopy/configuration/HandlerConfig.java +++ b/src/main/java/ru/soune/nocopy/configuration/HandlerConfig.java @@ -36,7 +36,8 @@ public class HandlerConfig { ComplaintEntityHandler complaintEntityHandler, UserInfoHandler userInfoHandler, NotificationHandler notificationHandler, - UserVerificationHandler userVerificationHandler + UserVerificationHandler userVerificationHandler, + LawCaseHandler lawCaseHandler ) { Map map = new HashMap<>(); map.put(20001, login); @@ -64,6 +65,7 @@ public class HandlerConfig { map.put(30014, userInfoHandler); map.put(30015, notificationHandler); map.put(30016, userVerificationHandler); + map.put(30017, lawCaseHandler); return map; } diff --git a/src/main/java/ru/soune/nocopy/dto/MessageCode.java b/src/main/java/ru/soune/nocopy/dto/MessageCode.java index b3e24b1..b69e47b 100644 --- a/src/main/java/ru/soune/nocopy/dto/MessageCode.java +++ b/src/main/java/ru/soune/nocopy/dto/MessageCode.java @@ -19,6 +19,7 @@ public enum MessageCode { FILE_DELETE(2, "File was deleted"), USER_NOT_HAD_PERMISSION(2, "User not have permission for file"), USER_NOT_VERIFIED(2, "User not verified"), + LAW_CASE_NOT_FOUND(4, "Law case not found"), PERMISSION_NOT_FOUND(2, "Permission not found"), USER_NOT_FOUND(2, "User not found"), FILE_DOWNLOAD_ERROR_NOT_CORRECT_FIELD(2, "Not correct field"), @@ -26,8 +27,10 @@ public enum MessageCode { IMAGE_FOUND_ERROR(2, "Image found error"), INVALID_JSON_BODY(2, "Invalid fields in JSON object"), INCOMPLETE_UPLOAD(2, "Not load all chunks"), + USER_NOT_HAVE_TOKEN(2, "Not have tokens"), MSG_ID_NOT_FOUND(4, "Message id not found"), FILE_ENTITY_ERROR(2, "File entity error"), + NOT_VALID_FIELD(2, "Not valid field"), ACCESS_DENIED(2, "Access denied"), AUTH_EMAIL_NOT_FOUND(4, "Email not found"), AUTH_EMAIL_OR_TOKEN_NOT_FOUND(4, "Email or Token not found "), diff --git a/src/main/java/ru/soune/nocopy/dto/complaint/BaseResponse.java b/src/main/java/ru/soune/nocopy/dto/complaint/BaseResponse.java new file mode 100644 index 0000000..9506a69 --- /dev/null +++ b/src/main/java/ru/soune/nocopy/dto/complaint/BaseResponse.java @@ -0,0 +1,35 @@ +package ru.soune.nocopy.dto.complaint; +import com.fasterxml.jackson.annotation.JsonProperty; +import lombok.Builder; +import lombok.Data; + +@Data +@Builder +public class BaseResponse { + @JsonProperty("status") + private String status; + + @JsonProperty("message") + private String message; + + @JsonProperty("data") + private Object data; + + @JsonProperty("error_code") + private String errorCode; + + public static BaseResponse success(Object data) { + return BaseResponse.builder() + .status("SUCCESS") + .data(data) + .build(); + } + + public static BaseResponse error(String message, String errorCode) { + return BaseResponse.builder() + .status("ERROR") + .message(message) + .errorCode(errorCode) + .build(); + } +} diff --git a/src/main/java/ru/soune/nocopy/dto/complaint/LawCasePageResponse.java b/src/main/java/ru/soune/nocopy/dto/complaint/LawCasePageResponse.java new file mode 100644 index 0000000..5b4e847 --- /dev/null +++ b/src/main/java/ru/soune/nocopy/dto/complaint/LawCasePageResponse.java @@ -0,0 +1,17 @@ +package ru.soune.nocopy.dto.complaint; + +import lombok.Builder; +import lombok.Data; +import java.util.List; + +@Data +@Builder +public class LawCasePageResponse { + private List content; + private int pageNumber; + private int pageSize; + private long totalElements; + private int totalPages; + private boolean last; + private boolean first; +} diff --git a/src/main/java/ru/soune/nocopy/dto/complaint/LawCaseRequest.java b/src/main/java/ru/soune/nocopy/dto/complaint/LawCaseRequest.java new file mode 100644 index 0000000..c44416b --- /dev/null +++ b/src/main/java/ru/soune/nocopy/dto/complaint/LawCaseRequest.java @@ -0,0 +1,30 @@ +package ru.soune.nocopy.dto.complaint; + +import lombok.Data; +import ru.soune.nocopy.entity.complaint.LawCasePriority; +import ru.soune.nocopy.entity.complaint.LawCaseType; + +import java.math.BigDecimal; + +@Data +public class LawCaseRequest { + private Long id; + //required + private String name; + //required + private String description; + private BigDecimal amount; + private String priority = LawCasePriority.MIDDLE.toString(); + private String type; + private String lawyer; + private String token; + private Integer pageSize = 10; + private Integer pageNumber = 0; + private String sortBy = "createdAt"; + private String sortDir = "asc"; + private String action; + private LawCaseType filterType; + private String filterLawyer; + private LawCasePriority filterPriority; + +} diff --git a/src/main/java/ru/soune/nocopy/dto/complaint/LawCaseResponse.java b/src/main/java/ru/soune/nocopy/dto/complaint/LawCaseResponse.java new file mode 100644 index 0000000..761b864 --- /dev/null +++ b/src/main/java/ru/soune/nocopy/dto/complaint/LawCaseResponse.java @@ -0,0 +1,44 @@ +package ru.soune.nocopy.dto.complaint; + +import lombok.Builder; +import lombok.Data; +import ru.soune.nocopy.entity.complaint.LawCase; +import ru.soune.nocopy.entity.complaint.LawCasePriority; +import ru.soune.nocopy.entity.complaint.LawCaseType; + +import java.math.BigDecimal; +import java.time.LocalDateTime; +import java.util.List; + +@Data +@Builder +public class LawCaseResponse { + private Long id; + private String name; + private String description; + private BigDecimal amount; + private LawCasePriority priority; + private LawCaseType type; + private String lawyer; + private LocalDateTime createdAt; + private LocalDateTime updatedAt; + private int pageNumber; + private int pageSize; + private long totalElements; + private int totalPages; + private List content; + + public static LawCaseResponse fromEntity(LawCase lawCase) { + return LawCaseResponse.builder() + .id(lawCase.getId()) + .name(lawCase.getName()) + .description(lawCase.getDescription()) + .amount(lawCase.getAmount()) + .priority(lawCase.getPriority()) + .type(lawCase.getType()) + .lawyer(lawCase.getLawyer()) + .createdAt(lawCase.getCreatedAt()) + .updatedAt(lawCase.getUpdatedAt()) + .build(); + } +} diff --git a/src/main/java/ru/soune/nocopy/dto/complaint/PaginatedResponse.java b/src/main/java/ru/soune/nocopy/dto/complaint/PaginatedResponse.java new file mode 100644 index 0000000..1e3a6e6 --- /dev/null +++ b/src/main/java/ru/soune/nocopy/dto/complaint/PaginatedResponse.java @@ -0,0 +1,17 @@ +package ru.soune.nocopy.dto.complaint; + +import lombok.AllArgsConstructor; +import lombok.Data; +import java.util.List; + +@Data +@AllArgsConstructor +public class PaginatedResponse { + private List content; + private long totalElements; + private int totalPages; + private int pageNumber; + private int pageSize; + private boolean first; + private boolean last; +} diff --git a/src/main/java/ru/soune/nocopy/entity/complaint/LawCase.java b/src/main/java/ru/soune/nocopy/entity/complaint/LawCase.java index b9ca6e6..0a81265 100644 --- a/src/main/java/ru/soune/nocopy/entity/complaint/LawCase.java +++ b/src/main/java/ru/soune/nocopy/entity/complaint/LawCase.java @@ -2,6 +2,12 @@ package ru.soune.nocopy.entity.complaint; import jakarta.persistence.*; import lombok.*; +import org.springframework.data.annotation.CreatedDate; +import org.springframework.data.annotation.LastModifiedDate; +import ru.soune.nocopy.entity.user.User; + +import java.math.BigDecimal; +import java.time.LocalDateTime; @Entity @AllArgsConstructor @@ -14,4 +20,45 @@ public class LawCase { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; + + @ManyToOne(fetch = FetchType.LAZY) + @JoinColumn(name = "user_id", nullable = false) + @ToString.Exclude + private User user; + + @Column(name = "name") + private String name; + + @Column(length = 500, name = "description") + private String description; + + @Column(name = "amount") + private BigDecimal amount; + + @Column(name = "priority") + @Enumerated(EnumType.STRING) + private LawCasePriority priority; + + @Column(name = "type") + @Enumerated(EnumType.STRING) + private LawCaseType type = LawCaseType.ACTIVE; + + @CreatedDate + @Column(name = "created_at", updatable = false) + private LocalDateTime createdAt; + + @LastModifiedDate + @Column(name = "updated_at") + private LocalDateTime updatedAt; + + @Column(name = "lawyer") + private String lawyer; + + @PrePersist + public void prePersist() { + if (this.createdAt == null) { + this.createdAt = LocalDateTime.now(); + } + this.updatedAt = LocalDateTime.now(); + } } diff --git a/src/main/java/ru/soune/nocopy/entity/complaint/LawCasePriority.java b/src/main/java/ru/soune/nocopy/entity/complaint/LawCasePriority.java new file mode 100644 index 0000000..2b0bd3c --- /dev/null +++ b/src/main/java/ru/soune/nocopy/entity/complaint/LawCasePriority.java @@ -0,0 +1,12 @@ +package ru.soune.nocopy.entity.complaint; + +public enum LawCasePriority { + CRITICAL("critical"), HIGH("high"), + MIDDLE("middle"), LOW("low"); + + private String name; + + private LawCasePriority(String name) { + this.name = name; + } +} diff --git a/src/main/java/ru/soune/nocopy/entity/complaint/LawCaseType.java b/src/main/java/ru/soune/nocopy/entity/complaint/LawCaseType.java new file mode 100644 index 0000000..12a42a9 --- /dev/null +++ b/src/main/java/ru/soune/nocopy/entity/complaint/LawCaseType.java @@ -0,0 +1,15 @@ +package ru.soune.nocopy.entity.complaint; + +import lombok.Getter; + +@Getter +public enum LawCaseType { + ACTIVE("active"), CLOSED("closed"), + COMPLETED("completed"); + + private String name; + + private LawCaseType(String name) { + this.name = name; + } +} diff --git a/src/main/java/ru/soune/nocopy/handler/LawCaseHandler.java b/src/main/java/ru/soune/nocopy/handler/LawCaseHandler.java new file mode 100644 index 0000000..8742f45 --- /dev/null +++ b/src/main/java/ru/soune/nocopy/handler/LawCaseHandler.java @@ -0,0 +1,199 @@ +package ru.soune.nocopy.handler; + +import com.fasterxml.jackson.databind.ObjectMapper; +import jakarta.mail.MessagingException; +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.MessageCode; +import ru.soune.nocopy.dto.complaint.LawCaseRequest; +import ru.soune.nocopy.dto.complaint.LawCaseResponse; +import ru.soune.nocopy.dto.complaint.PaginatedResponse; +import ru.soune.nocopy.entity.complaint.LawCase; +import ru.soune.nocopy.entity.complaint.LawCasePriority; +import ru.soune.nocopy.entity.complaint.LawCaseType; +import ru.soune.nocopy.entity.user.User; +import ru.soune.nocopy.exception.NotValidFieldException; +import ru.soune.nocopy.exception.TariffNotFoundException; +import ru.soune.nocopy.exception.UserNotFoundException; +import ru.soune.nocopy.repository.UserRepository; +import ru.soune.nocopy.service.complaint.LawCaseService; +import ru.soune.nocopy.service.register.AuthService; +import ru.soune.nocopy.service.tariff.TariffConstants; +import ru.soune.nocopy.service.tariff.TariffInfoService; + +import java.io.IOException; +import java.util.List; +import java.util.Map; +import java.util.stream.Collectors; + +@Slf4j +@Component +@RequiredArgsConstructor +public class LawCaseHandler implements RequestHandler { + + private final LawCaseService lawCaseService; + + private final AuthService authService; + + private final ObjectMapper objectMapper; + + private final UserRepository userRepository; + + private final TariffInfoService tariffInfoService; + + @Override + public BaseResponse handle(BaseRequest request) throws Exception { + + LawCaseRequest lawCaseRequest = objectMapper.convertValue(request.getMessageBody(), + LawCaseRequest.class); + Long userId = authService.useUserAuthToken(lawCaseRequest.getToken()); + String action = lawCaseRequest.getAction(); + + return switch (action) { + case "get_all" -> getAllUserLawCases(userId, lawCaseRequest, request); + case "get_byId" -> getLawCaseById(lawCaseRequest.getId(), request); + case "create" -> createLawCase(userId, lawCaseRequest, request); + case "update_priority" -> updatePriority(lawCaseRequest, request); + case "update_type" -> updateType(lawCaseRequest, request); + case "update_amount" -> updateAmount(lawCaseRequest, request); + case "delete" -> deleteLawCase(lawCaseRequest.getId(), request); + default -> new BaseResponse( + request.getMsgId(), + MessageCode.INVALID_ACTION.getCode(), + MessageCode.INVALID_ACTION.getDescription(), + Map.of("actions", "get_all, get_byId, create, update_priority, update_type, " + + "update_amount, delete") + ); + }; + } + + private BaseResponse getAllUserLawCases(Long userId, LawCaseRequest lawCaseRequest, BaseRequest request) { + Page lawCases = lawCaseService.getAllUserLawCases( + userId, + lawCaseRequest.getPageSize(), + lawCaseRequest.getPageNumber(), + lawCaseRequest.getFilterType(), + lawCaseRequest.getFilterLawyer(), + lawCaseRequest.getFilterPriority(), + lawCaseRequest.getSortBy(), + lawCaseRequest.getSortDir()); + + List responses = lawCases.getContent().stream() + .map(LawCaseResponse::fromEntity) + .collect(Collectors.toList()); + + PaginatedResponse paginatedResponse = new PaginatedResponse<>( + responses, + lawCases.getTotalElements(), + lawCases.getTotalPages(), + lawCases.getNumber(), + lawCases.getSize(), + lawCases.isFirst(), + lawCases.isLast() + ); + + return new BaseResponse(request.getMsgId(), MessageCode.SUCCESS.getCode(), + MessageCode.SUCCESS.getDescription(), paginatedResponse); + } + + private BaseResponse getLawCaseById(Long id, BaseRequest request) { + LawCase lawCase = lawCaseService.getById(id); + + if (lawCase == null) { + return new BaseResponse(request.getMsgId(), MessageCode.LAW_CASE_NOT_FOUND.getCode(), + MessageCode.LAW_CASE_NOT_FOUND.getDescription(), Map.of("id", id)); + } + + return new BaseResponse(request.getMsgId(), MessageCode.SUCCESS.getCode(), + MessageCode.SUCCESS.getDescription(), LawCaseResponse.fromEntity(lawCase)); + } + + private BaseResponse createLawCase(Long userId, LawCaseRequest lawCaseRequest, BaseRequest request) { + try { + tariffInfoService.writeOffTokens(userId, TariffConstants.LEGAL_COST); + } catch (TariffNotFoundException e) { + return new BaseResponse(request.getMsgId(), MessageCode.USER_NOT_HAVE_TOKEN.getCode(), + MessageCode.USER_NOT_HAVE_TOKEN.getDescription(), + Map.of("error", "Tariff not found", "message", e.getMessage())); + } catch (MessagingException | IOException e) { + return new BaseResponse(request.getMsgId(), MessageCode.INVALID_FIELD.getCode(), + MessageCode.INVALID_FIELD.getDescription(), Map.of("id", lawCaseRequest.getId())); + } + + validateRequiredParameters(lawCaseRequest, request); + + User user = userRepository.findById(userId).orElseThrow(() -> new UserNotFoundException("User not found")); + + LawCase lawCase = lawCaseService.addLawCase( + lawCaseRequest.getAmount(), + lawCaseRequest.getDescription(), + lawCaseRequest.getName(), + LawCasePriority.valueOf(lawCaseRequest.getPriority())); + lawCase.setUser(user); + + return new BaseResponse(request.getMsgId(), MessageCode.SUCCESS.getCode(), + MessageCode.SUCCESS.getDescription(), LawCaseResponse.fromEntity(lawCase)); + } + + private BaseResponse updatePriority(LawCaseRequest lawCaseRequest, BaseRequest request) { + LawCase lawCase = lawCaseService.changePriority( + lawCaseRequest.getId(), + LawCasePriority.valueOf(lawCaseRequest.getPriority())); + + if (lawCase == null) { + return new BaseResponse(request.getMsgId(), MessageCode.LAW_CASE_NOT_FOUND.getCode(), + MessageCode.LAW_CASE_NOT_FOUND.getDescription(), Map.of("id", lawCaseRequest.getId())); + } + + return new BaseResponse(request.getMsgId(), MessageCode.SUCCESS.getCode(), + MessageCode.SUCCESS.getDescription(), LawCaseResponse.fromEntity(lawCase)); + } + + private BaseResponse updateType(LawCaseRequest lawCaseRequest, BaseRequest request) { + LawCase lawCase = lawCaseService.changeStatus( + lawCaseRequest.getId(), + LawCaseType.valueOf(lawCaseRequest.getType())); + + if (lawCase == null) { + return new BaseResponse(request.getMsgId(), MessageCode.LAW_CASE_NOT_FOUND.getCode(), + MessageCode.LAW_CASE_NOT_FOUND.getDescription(), Map.of("id", lawCaseRequest.getId())); + } + + return new BaseResponse(request.getMsgId(), MessageCode.SUCCESS.getCode(), + MessageCode.SUCCESS.getDescription(), LawCaseResponse.fromEntity(lawCase)); + } + + private BaseResponse updateAmount(LawCaseRequest lawCaseRequest, BaseRequest request) { + LawCase lawCase = lawCaseService.changeDamage( + lawCaseRequest.getId(), + lawCaseRequest.getAmount()); + + if (lawCase == null) { + return new BaseResponse(request.getMsgId(), MessageCode.LAW_CASE_NOT_FOUND.getCode(), + MessageCode.LAW_CASE_NOT_FOUND.getDescription(), Map.of("id", lawCaseRequest.getId())); + } + + return new BaseResponse(request.getMsgId(), MessageCode.SUCCESS.getCode(), + MessageCode.SUCCESS.getDescription(), LawCaseResponse.fromEntity(lawCase)); + } + + private BaseResponse deleteLawCase(Long id, BaseRequest request) { + lawCaseService.deleteById(id); + return new BaseResponse(request.getMsgId(), MessageCode.SUCCESS.getCode(), + MessageCode.SUCCESS.getDescription(), Map.of("id", id)); + } + + private void validateRequiredParameters(LawCaseRequest lawCaseRequest, BaseRequest request) { + if (lawCaseRequest.getName() == null) throw new NotValidFieldException("Name is required", + new BaseResponse(request.getMsgId(), MessageCode.NOT_VALID_FIELD.getCode(), + MessageCode.NOTION_NOT_FOUND.getDescription(), Map.of("fields", "name"))); + + if (lawCaseRequest.getDescription() == null) throw new NotValidFieldException("Description is required", + new BaseResponse(request.getMsgId(), MessageCode.NOT_VALID_FIELD.getCode(), + MessageCode.NOTION_NOT_FOUND.getDescription(), Map.of("fields", "description"))); + } +} diff --git a/src/main/java/ru/soune/nocopy/repository/LawCaseRepository.java b/src/main/java/ru/soune/nocopy/repository/LawCaseRepository.java new file mode 100644 index 0000000..fa2d6ad --- /dev/null +++ b/src/main/java/ru/soune/nocopy/repository/LawCaseRepository.java @@ -0,0 +1,40 @@ +package ru.soune.nocopy.repository; + +import org.springframework.data.domain.Page; +import org.springframework.data.domain.Pageable; +import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.data.jpa.repository.Query; +import org.springframework.data.repository.query.Param; +import org.springframework.stereotype.Repository; +import ru.soune.nocopy.entity.complaint.LawCase; +import ru.soune.nocopy.entity.complaint.LawCasePriority; +import ru.soune.nocopy.entity.complaint.LawCaseType; + +import java.util.List; + +@Repository +public interface LawCaseRepository extends JpaRepository { + + @Query("SELECT l FROM LawCase l WHERE l.user.Id = :userId") + List getUserLawCases(@Param("userId") Long userId, Pageable pageable); + +// @Query("SELECT l FROM LawCase l WHERE l.user.Id = :userId " + +// "AND (:type IS NULL OR l.type = :type) " + +// "AND (:lawyer IS NULL OR l.lawyer = :lawyer) " + +// "AND (:priority IS NULL OR l.priority = :priority)") +// List getUserLawCases(@Param("userId") Long userId, +// @Param("type") LawCaseType type, +// @Param("lawyer") String lawyer, +// @Param("priority") LawCasePriority priority, +// Pageable pageable); + + @Query("SELECT l FROM LawCase l WHERE l.user.Id = :userId " + + "AND (:type IS NULL OR l.type = :type) " + + "AND (:lawyer IS NULL OR l.lawyer = :lawyer) " + + "AND (:priority IS NULL OR l.priority = :priority)") + Page getUserLawCases(@Param("userId") Long userId, + @Param("type") LawCaseType type, + @Param("lawyer") String lawyer, + @Param("priority") LawCasePriority priority, + Pageable pageable); +} diff --git a/src/main/java/ru/soune/nocopy/service/complaint/LawCaseService.java b/src/main/java/ru/soune/nocopy/service/complaint/LawCaseService.java new file mode 100644 index 0000000..2308372 --- /dev/null +++ b/src/main/java/ru/soune/nocopy/service/complaint/LawCaseService.java @@ -0,0 +1,96 @@ +package ru.soune.nocopy.service.complaint; + +import lombok.RequiredArgsConstructor; +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.stereotype.Service; +import ru.soune.nocopy.entity.complaint.LawCase; +import ru.soune.nocopy.entity.complaint.LawCasePriority; +import ru.soune.nocopy.entity.complaint.LawCaseType; +import ru.soune.nocopy.repository.LawCaseRepository; + +import java.math.BigDecimal; +import java.util.List; + +@Service +@RequiredArgsConstructor +public class LawCaseService { + + private final LawCaseRepository lawCaseRepository; + + public LawCase addLawCase(BigDecimal amount, String description, String name, + LawCasePriority priority) { + LawCase lawCase = new LawCase(); + + BigDecimal damage = amount == null ? BigDecimal.ZERO : amount; + + lawCase.setAmount(damage); + lawCase.setName(name); + lawCase.setDescription(description); + lawCase.setPriority(priority); + + return lawCaseRepository.save(lawCase); + } + + public LawCase getById(Long id) { + return lawCaseRepository.findById(id).orElse(null); + } + + public LawCase changeStatus(Long id, LawCaseType type) { + LawCase lawCase = lawCaseRepository.findById(id).orElse(null); + if (lawCase == null) { + return null; + } + + lawCase.setType(type); + return lawCaseRepository.save(lawCase); + } + + public LawCase changePriority(Long id, LawCasePriority priority) { + LawCase lawCase = lawCaseRepository.findById(id).orElse(null); + if (lawCase == null) { + return null; + } + + lawCase.setPriority(priority); + return lawCaseRepository.save(lawCase); + } + + public LawCase changeDamage(Long id, BigDecimal damage) { + LawCase lawCase = lawCaseRepository.findById(id).orElse(null); + if (lawCase == null) { + return null; + } + lawCase.setAmount(damage); + return lawCaseRepository.save(lawCase); + } + + public List getAllUserLawCases(Long userId, int pageSize, int pageNumber, String propertySort, + String ascDesc) { + Sort.Direction direction = ascDesc.equalsIgnoreCase("desc") ? Sort.Direction.DESC: + Sort.Direction.ASC; + Sort sort = Sort.by(direction, propertySort); + Pageable pageable = PageRequest.of(pageNumber, pageSize, sort); + + return lawCaseRepository.getUserLawCases(userId, pageable); + } + + public Page getAllUserLawCases(Long userId, int pageSize, int pageNumber, + LawCaseType lawCaseType, String lawyer, + LawCasePriority lawCasePriority, + String propertySort, String ascDesc) { + Sort.Direction direction = ascDesc.equalsIgnoreCase("desc") ? Sort.Direction.DESC : Sort.Direction.ASC; + Sort sort = Sort.by(direction, propertySort); + Pageable pageable = PageRequest.of(pageNumber, pageSize, sort); + + return lawCaseRepository.getUserLawCases(userId, lawCaseType, lawyer, lawCasePriority, pageable); + } + + public void deleteById(Long id) { + lawCaseRepository.deleteById(id); + } + +} + diff --git a/src/main/java/ru/soune/nocopy/service/tariff/TariffConstants.java b/src/main/java/ru/soune/nocopy/service/tariff/TariffConstants.java index 14c08c4..6df1fdf 100644 --- a/src/main/java/ru/soune/nocopy/service/tariff/TariffConstants.java +++ b/src/main/java/ru/soune/nocopy/service/tariff/TariffConstants.java @@ -18,4 +18,5 @@ public class TariffConstants { public static final Integer AUDIO_TOKEN_VALUE = 8; public static final Integer PDF_TOKEN_VALUE = 6; public static final Integer TOKEN_VALUE_FOR_SEARCH = 5; + public static final Integer LEGAL_COST = 100; }