This commit is contained in:
@@ -87,6 +87,7 @@ public class HandlerConfig {
|
||||
StatisticIncomeHandler statisticIncomeHandler,
|
||||
MonitoringStatisticHandler monitoringStatisticHandler,
|
||||
FileInteranlInfoHandler fileInfo,
|
||||
TariffInformationHandler tariffInformationHandler,
|
||||
UserInfoHandler userInfoHandler
|
||||
)
|
||||
{
|
||||
@@ -103,6 +104,7 @@ public class HandlerConfig {
|
||||
map.put(30027, monitoringStatisticHandler);
|
||||
map.put(30029, statisticComplaintHandler);
|
||||
map.put(40001, fileInfo);
|
||||
map.put(40004, tariffInformationHandler);
|
||||
|
||||
return map;
|
||||
}
|
||||
@@ -111,6 +113,7 @@ public class HandlerConfig {
|
||||
public Map<Integer, RequestHandler> internalControlHandler(
|
||||
ControlComplaintHandler controlComplaintHandler,
|
||||
FileInternalControlHandler fileInternalControlHandler,
|
||||
TariffControlHandler tariffControlHandler,
|
||||
UserInfoHandler userInfoHandler
|
||||
)
|
||||
{
|
||||
@@ -118,6 +121,7 @@ public class HandlerConfig {
|
||||
map.put(30014, userInfoHandler);
|
||||
map.put(30030, controlComplaintHandler);
|
||||
map.put(40002, fileInternalControlHandler);
|
||||
map.put(40003, tariffControlHandler);
|
||||
|
||||
return map;
|
||||
}
|
||||
|
||||
@@ -16,12 +16,6 @@ public class FileListResponse {
|
||||
@JsonProperty("files")
|
||||
private List<FileEntityResponse> files;
|
||||
|
||||
@JsonProperty("total_count")
|
||||
private Integer totalCount;
|
||||
|
||||
@JsonProperty("total_size")
|
||||
private Long totalSize;
|
||||
|
||||
@JsonProperty("formatted_total_size")
|
||||
private String formattedTotalSize;
|
||||
|
||||
@@ -36,4 +30,10 @@ public class FileListResponse {
|
||||
|
||||
@JsonProperty("sort_order")
|
||||
private String sortOrder;
|
||||
|
||||
@JsonProperty("total_count")
|
||||
private Integer totalCount;
|
||||
|
||||
@JsonProperty("total_size")
|
||||
private Long totalSize;
|
||||
}
|
||||
|
||||
@@ -50,4 +50,16 @@ public class TariffRequest {
|
||||
|
||||
@JsonProperty("description")
|
||||
private String description;
|
||||
|
||||
@JsonProperty("page")
|
||||
private Integer page;
|
||||
|
||||
@JsonProperty("page_size")
|
||||
private Integer size;
|
||||
|
||||
@JsonProperty("sort_by")
|
||||
private String sortBy;
|
||||
|
||||
@JsonProperty("sort_direction")
|
||||
private String sortDirection = "desc";
|
||||
}
|
||||
@@ -1,12 +1,35 @@
|
||||
package ru.soune.nocopy.dto.tarriff;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||
import lombok.Builder;
|
||||
import lombok.Data;
|
||||
import ru.soune.nocopy.entity.tarif.Tariff;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@Data
|
||||
@Builder
|
||||
public class TariffResponse {
|
||||
private String tariffName;
|
||||
private String tariffType;
|
||||
|
||||
@JsonProperty("page")
|
||||
private Integer page;
|
||||
|
||||
@JsonProperty("page_size")
|
||||
private Integer pageSize;
|
||||
|
||||
@JsonProperty("sort_by")
|
||||
private String sortBy;
|
||||
|
||||
@JsonProperty("sort_direction")
|
||||
private String sortDirection;
|
||||
|
||||
@JsonProperty("total_pages")
|
||||
private Integer totalPages;
|
||||
|
||||
@JsonProperty("total_elements")
|
||||
private Long totalElements;
|
||||
|
||||
@JsonProperty("tariffs")
|
||||
private List<TariffDTO> tariffs;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,115 @@
|
||||
package ru.soune.nocopy.handler;
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
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.tarriff.TariffDTO;
|
||||
import ru.soune.nocopy.dto.tarriff.TariffRequest;
|
||||
import ru.soune.nocopy.dto.tarriff.TariffResponse;
|
||||
import ru.soune.nocopy.service.tariff.TariffService;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
@RequiredArgsConstructor
|
||||
@Component
|
||||
@Slf4j
|
||||
public class TariffControlHandler implements RequestHandler {
|
||||
private final ObjectMapper objectMapper;
|
||||
|
||||
private final TariffService tariffService;
|
||||
|
||||
@Override
|
||||
public BaseResponse handle(BaseRequest request) throws Exception {
|
||||
try {
|
||||
TariffRequest tariffRequest = objectMapper.convertValue(request.getMessageBody(), TariffRequest.class);
|
||||
String action = tariffRequest.getAction();
|
||||
|
||||
switch (action) {
|
||||
case "add":
|
||||
return handleAddTariff(request, tariffRequest);
|
||||
case "remove":
|
||||
return handleRemoveTariff(request, tariffRequest);
|
||||
case "update":
|
||||
return handleUpdateTariff(request, tariffRequest);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
log.error("Error add tariff", e);
|
||||
return new BaseResponse(
|
||||
request.getMsgId(),
|
||||
MessageCode.SUCCESS.getCode(),
|
||||
"Error processing request: " + e.getMessage(),
|
||||
null);
|
||||
}
|
||||
|
||||
return new BaseResponse(request.getMsgId(), MessageCode.SUCCESS.getCode(),
|
||||
MessageCode.SUCCESS.getDescription(), null);
|
||||
}
|
||||
|
||||
private BaseResponse handleAddTariff(BaseRequest request, TariffRequest tariffRequest) {
|
||||
tariffService.addTariff(tariffRequest.getName(), tariffRequest.getPrice(), tariffRequest.getType(),
|
||||
tariffRequest.getTokens(), tariffRequest.getDiskSize(), tariffRequest.getMaxUsers(),
|
||||
tariffRequest.getMaxFilesCount(), tariffRequest.getTariffTerm(), tariffRequest.getDescription());
|
||||
|
||||
TariffResponse tariffResponse = TariffResponse.builder()
|
||||
.tariffName(tariffRequest.getName())
|
||||
.tariffType(tariffRequest.getType())
|
||||
.build();
|
||||
|
||||
return new BaseResponse(request.getMsgId(), MessageCode.TARIFF_IS_ADD.getCode(),
|
||||
MessageCode.TARIFF_IS_ADD.getDescription(),
|
||||
tariffResponse);
|
||||
}
|
||||
|
||||
private BaseResponse handleRemoveTariff(BaseRequest request, TariffRequest tariffRequest) {
|
||||
TariffDTO tariff = tariffService.getTariffById(tariffRequest.getId());
|
||||
|
||||
if (tariff == null) {
|
||||
return new BaseResponse(request.getMsgId(), MessageCode.TARIFF_IS_NOT_FOUND.getCode(),
|
||||
MessageCode.TARIFF_IS_NOT_FOUND.getDescription(),
|
||||
Map.of("id", tariffRequest.getId()));
|
||||
}
|
||||
|
||||
TariffResponse tariffResponse = TariffResponse.builder()
|
||||
.tariffName(tariff.getName())
|
||||
.tariffType(tariff.getType())
|
||||
.build();
|
||||
|
||||
if (tariffService.existTariffInfoByTariffId(tariffRequest.getId())) {
|
||||
return new BaseResponse(request.getMsgId(), MessageCode.TARIFF_IS_NOT_DELETED.getCode(),
|
||||
MessageCode.TARIFF_IS_NOT_DELETED.getDescription(),
|
||||
tariffResponse);
|
||||
}
|
||||
|
||||
tariffService.deleteTariff(tariffRequest.getId());
|
||||
|
||||
return new BaseResponse(request.getMsgId(), MessageCode.TARIFF_IS_DELETED.getCode(),
|
||||
MessageCode.TARIFF_IS_DELETED.getDescription(),
|
||||
tariffResponse);
|
||||
}
|
||||
|
||||
private BaseResponse handleUpdateTariff(BaseRequest request, TariffRequest tariffRequest) {
|
||||
TariffDTO tariff = tariffService.getTariffById(tariffRequest.getId());
|
||||
|
||||
if (tariff == null) {
|
||||
return new BaseResponse(request.getMsgId(), MessageCode.TARIFF_IS_NOT_FOUND.getCode(),
|
||||
MessageCode.TARIFF_IS_NOT_FOUND.getDescription(),
|
||||
Map.of("id", tariffRequest.getId()));
|
||||
}
|
||||
|
||||
TariffDTO tariffDTO = tariffService.updateTariff(tariffRequest.getId(), tariff, tariffRequest);
|
||||
|
||||
TariffResponse tariffResponse = TariffResponse.builder()
|
||||
.tariffName(tariffDTO.getName())
|
||||
.tariffType(tariffDTO.getType())
|
||||
.build();
|
||||
|
||||
|
||||
return new BaseResponse(request.getMsgId(), MessageCode.TARIFF_IS_UPDATED.getCode(),
|
||||
MessageCode.TARIFF_IS_UPDATED.getDescription(),
|
||||
tariffResponse);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,130 @@
|
||||
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.data.domain.PageRequest;
|
||||
import org.springframework.data.domain.Pageable;
|
||||
import org.springframework.data.domain.Sort;
|
||||
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.tarriff.TariffDTO;
|
||||
import ru.soune.nocopy.dto.tarriff.TariffRequest;
|
||||
import ru.soune.nocopy.dto.tarriff.TariffResponse;
|
||||
import ru.soune.nocopy.entity.tarif.Tariff;
|
||||
import ru.soune.nocopy.entity.tarif.TariffTimeTerm;
|
||||
import ru.soune.nocopy.service.tariff.TariffService;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@RequiredArgsConstructor
|
||||
@Component
|
||||
@Slf4j
|
||||
public class TariffInformationHandler implements RequestHandler {
|
||||
|
||||
private final ObjectMapper objectMapper;
|
||||
|
||||
private final TariffService tariffService;
|
||||
|
||||
@Override
|
||||
public BaseResponse handle(BaseRequest request) throws Exception {
|
||||
try {
|
||||
TariffRequest tariffRequest = objectMapper.convertValue(request.getMessageBody(), TariffRequest.class);
|
||||
String action = tariffRequest.getAction();
|
||||
|
||||
switch (action) {
|
||||
case "get_all" :
|
||||
return handleGetTariffs(request, tariffRequest);
|
||||
case "tariffs_by_type" :
|
||||
return handleGetTariffsByType(request, tariffRequest);
|
||||
}
|
||||
|
||||
} catch (Exception e) {
|
||||
log.error("Error add tariff", e);
|
||||
return new BaseResponse(
|
||||
request.getMsgId(),
|
||||
MessageCode.SUCCESS.getCode(),
|
||||
"Error processing request: " + e.getMessage(),
|
||||
null);
|
||||
}
|
||||
|
||||
return new BaseResponse(request.getMsgId(), MessageCode.SUCCESS.getCode(),
|
||||
MessageCode.SUCCESS.getDescription(), null);
|
||||
}
|
||||
|
||||
|
||||
private BaseResponse handleGetTariffsByType(BaseRequest request, TariffRequest req) {
|
||||
int page = req.getPage() != null ? req.getPage() : 0;
|
||||
int pageSize = req.getSize() != null ? req.getSize() : 5;
|
||||
String sortDirection = req.getSortDirection() != null ? req.getSortDirection() : "desc";
|
||||
String sortBy = req.getSortBy() != null ? req.getSortBy() : "updatedAt";
|
||||
Pageable pageable = PageRequest.of(page, pageSize, Sort.by(Sort.Direction.fromString(sortDirection), sortBy));
|
||||
|
||||
Page<Tariff> allTariffs = tariffService.getTariffs(pageable);
|
||||
|
||||
List<TariffDTO> tariffDTOS = allTariffs.stream()
|
||||
.map(this::convertToDTO)
|
||||
.toList();
|
||||
|
||||
TariffResponse tariffResponse = TariffResponse.builder()
|
||||
.tariffs(tariffDTOS)
|
||||
.page(page)
|
||||
.pageSize(pageSize)
|
||||
.sortBy(sortBy)
|
||||
.sortDirection(sortDirection)
|
||||
.totalElements(allTariffs.getTotalElements())
|
||||
.totalPages(allTariffs.getTotalPages())
|
||||
.build();
|
||||
|
||||
return new BaseResponse(request.getMsgId(), MessageCode.SUCCESS.getCode(),
|
||||
MessageCode.SUCCESS.getDescription(), tariffResponse);
|
||||
}
|
||||
|
||||
private BaseResponse handleGetTariffs(BaseRequest request, TariffRequest req) {
|
||||
int page = req.getPage() != null ? req.getPage() : 0;
|
||||
int pageSize = req.getSize() != null ? req.getSize() : 5;
|
||||
String sortDirection = req.getSortDirection() != null ? req.getSortDirection() : "desc";
|
||||
String sortBy = req.getSortBy() != null ? req.getSortBy() : "updatedAt";
|
||||
|
||||
Pageable pageable = PageRequest.of(page, pageSize, Sort.by(Sort.Direction.fromString(sortDirection), sortBy));
|
||||
|
||||
Page<Tariff> allTariffs = tariffService.getTariffs(pageable);
|
||||
|
||||
List<TariffDTO> tariffDTOS = allTariffs.stream()
|
||||
.map(this::convertToDTO)
|
||||
.toList();
|
||||
|
||||
TariffResponse tariffResponse = TariffResponse.builder()
|
||||
.tariffs(tariffDTOS)
|
||||
.page(page)
|
||||
.pageSize(pageSize)
|
||||
.sortBy(sortBy)
|
||||
.sortDirection(sortDirection)
|
||||
.totalElements(allTariffs.getTotalElements())
|
||||
.totalPages(allTariffs.getTotalPages())
|
||||
.build();
|
||||
|
||||
return new BaseResponse(request.getMsgId(), MessageCode.SUCCESS.getCode(),
|
||||
MessageCode.SUCCESS.getDescription(), tariffResponse);
|
||||
}
|
||||
|
||||
private TariffDTO convertToDTO(Tariff tariff) {
|
||||
TariffTimeTerm tariffTerm = tariff.getTariffTerm();
|
||||
|
||||
return new TariffDTO(
|
||||
tariff.getId(),
|
||||
tariff.getType(),
|
||||
tariff.getName(),
|
||||
tariff.getPrice(),
|
||||
tariff.getTokens(),
|
||||
tariff.getMaxFilesCount(),
|
||||
tariff.getDiskSize(),
|
||||
tariff.getMaxUsers(),
|
||||
tariffTerm == null ? "" : tariffTerm.name(),
|
||||
tariff.getDescription() == null ? "" : tariff.getDescription()
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -1,10 +1,11 @@
|
||||
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.stereotype.Repository;
|
||||
import ru.soune.nocopy.entity.tarif.Tariff;
|
||||
import ru.soune.nocopy.entity.tarif.TariffTimeTerm;
|
||||
import ru.soune.nocopy.entity.tarif.TariffType;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
@@ -14,6 +15,7 @@ public interface TariffRepository extends JpaRepository<Tariff, Long> {
|
||||
Optional<Tariff> findByType(String type);
|
||||
List<Tariff> findAllByOrderByPriceAsc();
|
||||
List<Tariff> findByTariffAccountType(String typeAccount);
|
||||
Page<Tariff> findByTariffAccountType(String typeAccount, Pageable pageable);
|
||||
List<Tariff> findByTariffAccountTypeAndTariffTerm(String typeAccount, TariffTimeTerm tariffTerm);
|
||||
Tariff findByName(String name);
|
||||
}
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
package ru.soune.nocopy.service.tariff;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import org.springframework.data.domain.Page;
|
||||
import org.springframework.data.domain.Pageable;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
import ru.soune.nocopy.dto.tarriff.TariffDTO;
|
||||
@@ -62,6 +64,14 @@ public class TariffService {
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
|
||||
public Page<Tariff> getTariffs(Pageable pageable) {
|
||||
return tariffRepository.findAll(pageable);
|
||||
}
|
||||
|
||||
public Page<Tariff> getTariffByType(String type, Pageable pageable) {
|
||||
return tariffRepository.findByTariffAccountType(type, pageable);
|
||||
}
|
||||
|
||||
public List<TariffDTO> getTariffByType(String type) {
|
||||
return tariffRepository.findByTariffAccountType(type)
|
||||
.stream()
|
||||
|
||||
Reference in New Issue
Block a user