PurchaseLevelService.java 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. package com.izouma.nineth.service;
  2. import com.izouma.nineth.domain.PurchaseLevel;
  3. import com.izouma.nineth.dto.PageQuery;
  4. import com.izouma.nineth.dto.PageWrapper;
  5. import com.izouma.nineth.repo.PurchaseLevelRepo;
  6. import com.izouma.nineth.utils.JpaUtils;
  7. import lombok.AllArgsConstructor;
  8. import org.springframework.cache.annotation.Cacheable;
  9. import org.springframework.data.domain.Page;
  10. import org.springframework.stereotype.Service;
  11. @Service
  12. @AllArgsConstructor
  13. public class PurchaseLevelService {
  14. private PurchaseLevelRepo purchaseLevelRepo;
  15. @Cacheable(value = "purchaseLevelList", key = "#pageQuery.hashCode()")
  16. public PageWrapper<PurchaseLevel> all(PageQuery pageQuery) {
  17. Page<PurchaseLevel> page = purchaseLevelRepo.findAll(JpaUtils.toSpecification(pageQuery, PurchaseLevel.class), JpaUtils.toPageRequest(pageQuery));
  18. return new PageWrapper<>(page.getContent(), page.getPageable().getPageNumber(),
  19. page.getPageable().getPageSize(), page.getTotalElements());
  20. }
  21. public PurchaseLevel findPurchaseLevelByLevel(int level) {
  22. int maxStartLevel = purchaseLevelRepo.findMaxStartLevel();
  23. PurchaseLevel purchaseLevel;
  24. if (level >= maxStartLevel) {
  25. purchaseLevel = purchaseLevelRepo.findByStartLevel(maxStartLevel);
  26. } else {
  27. purchaseLevel = purchaseLevelRepo.findByLevel(level);
  28. }
  29. return purchaseLevel;
  30. }
  31. }