Files
no-copy/src/main/java/ru/soune/nocopy/service/tariff/TariffService.java
T

134 lines
4.7 KiB
Java
Raw Normal View History

2026-01-30 12:52:16 +07:00
package ru.soune.nocopy.service.tariff;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import ru.soune.nocopy.dto.tarriff.TariffDTO;
import ru.soune.nocopy.entity.tarif.Tariff;
2026-03-25 12:46:33 +07:00
import ru.soune.nocopy.entity.tarif.TariffTimeTerm;
2026-01-30 12:52:16 +07:00
import ru.soune.nocopy.entity.tarif.TariffType;
2026-02-09 17:16:08 +07:00
import ru.soune.nocopy.entity.user.User;
2026-01-30 12:52:16 +07:00
import ru.soune.nocopy.exception.TariffNotFoundException;
import ru.soune.nocopy.repository.TariffRepository;
import java.util.List;
import java.util.stream.Collectors;
@Service
@AllArgsConstructor
public class TariffService {
private final TariffRepository tariffRepository;
2026-02-02 15:00:18 +07:00
public void addTariff(String tariffName, double price, String type, int tokens, long diskSize, long maxUsers,
2026-05-15 14:44:54 +07:00
int maxFilesCount, String tariffTerm, String description) {
2026-01-30 12:52:16 +07:00
Tariff tariff = new Tariff();
tariff.setName(tariffName);
tariff.setPrice(price);
tariff.setType(type);
tariff.setTokens(tokens);
tariff.setDiskSize(diskSize);
tariff.setMaxUsers(maxUsers);
tariff.setMaxFilesCount(maxFilesCount);
2026-03-25 12:46:33 +07:00
tariff.setTariffTerm(TariffTimeTerm.valueOf(tariffTerm.toUpperCase()));
2026-05-15 14:44:54 +07:00
tariff.setDescription(description);
2026-01-30 12:52:16 +07:00
2026-02-02 15:00:18 +07:00
tariffRepository.save(tariff);
2026-01-30 12:52:16 +07:00
}
2026-02-09 17:16:08 +07:00
public List<TariffDTO> getTariffByAccountType(User user) {
String typeAccount = user.canManageCompanySettings() ? "b2b": "b2c";
return tariffRepository.findByTariffAccountType(typeAccount)
.stream()
.map(this::convertToDTO)
.collect(Collectors.toList());
}
2026-03-25 12:46:33 +07:00
public List<TariffDTO> getTariffByAccountTypeAndTariffTerm(User user, TariffTimeTerm tariffTerm) {
String typeAccount = user.canManageCompanySettings() ? "b2b": "b2c";
return tariffRepository.findByTariffAccountTypeAndTariffTerm(typeAccount, tariffTerm)
.stream()
.map(this::convertToDTO)
.collect(Collectors.toList());
}
2026-02-19 14:15:59 +07:00
public List<TariffDTO> getTariffByType(String type) {
return tariffRepository.findByTariffAccountType(type)
.stream()
.map(this::convertToDTO)
.collect(Collectors.toList());
}
2026-01-30 12:52:16 +07:00
public List<TariffDTO> getAllTariffs() {
return tariffRepository.findAllByOrderByPriceAsc()
.stream()
.map(this::convertToDTO)
.collect(Collectors.toList());
}
public TariffDTO getTariffById(Long id) {
return tariffRepository.findById(id)
.map(this::convertToDTO)
.orElseThrow(() -> new TariffNotFoundException("Tariff not found"));
}
public TariffDTO getTariffByType(TariffType type) {
2026-01-30 22:36:20 +07:00
return tariffRepository.findByType(type.toString())
2026-01-30 12:52:16 +07:00
.map(this::convertToDTO)
.orElseThrow(() -> new TariffNotFoundException("Tariff not found"));
}
@Transactional
public TariffDTO createTariff(TariffDTO tariffDTO) {
Tariff tariff = new Tariff();
updateTariffFromDTO(tariff, tariffDTO);
return convertToDTO(tariffRepository.save(tariff));
}
@Transactional
public TariffDTO updateTariff(Long id, TariffDTO tariffDTO) {
Tariff tariff = tariffRepository.findById(id)
.orElseThrow(() -> new TariffNotFoundException("Tariff not found"));
updateTariffFromDTO(tariff, tariffDTO);
return convertToDTO(tariffRepository.save(tariff));
}
@Transactional
public void deleteTariff(Long id) {
tariffRepository.deleteById(id);
}
private TariffDTO convertToDTO(Tariff tariff) {
2026-03-25 13:08:10 +07:00
TariffTimeTerm tariffTerm = tariff.getTariffTerm();
2026-01-30 12:52:16 +07:00
return new TariffDTO(
tariff.getId(),
tariff.getType(),
tariff.getName(),
tariff.getPrice(),
tariff.getTokens(),
tariff.getMaxFilesCount(),
tariff.getDiskSize(),
2026-03-25 12:46:33 +07:00
tariff.getMaxUsers(),
2026-05-15 14:44:54 +07:00
tariffTerm == null ? "" : tariffTerm.name(),
tariff.getDescription() == null ? "" : tariff.getDescription()
2026-01-30 12:52:16 +07:00
);
}
private void updateTariffFromDTO(Tariff tariff, TariffDTO dto) {
tariff.setType(dto.getType());
tariff.setName(dto.getName());
tariff.setPrice(dto.getPrice());
tariff.setTokens(dto.getTokens());
tariff.setMaxFilesCount(dto.getMaxFilesCount());
tariff.setDiskSize(dto.getDiskSize());
tariff.setMaxUsers(dto.getMaxUsers());
2026-05-15 14:44:54 +07:00
tariff.setDescription(dto.getDescription());
2026-03-25 12:46:33 +07:00
tariff.setTariffTerm(TariffTimeTerm.valueOf(dto.getTariffTerm().toUpperCase()));
2026-01-30 12:52:16 +07:00
}
}