CollectionRepo.java 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. package com.izouma.nineth.repo;
  2. import com.izouma.nineth.domain.Collection;
  3. import com.izouma.nineth.dto.RecommendCollection;
  4. import org.springframework.cache.annotation.CacheEvict;
  5. import org.springframework.cache.annotation.CachePut;
  6. import org.springframework.cache.annotation.Cacheable;
  7. import org.springframework.data.jpa.repository.JpaRepository;
  8. import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
  9. import org.springframework.data.jpa.repository.Modifying;
  10. import org.springframework.data.jpa.repository.Query;
  11. import javax.annotation.Nonnull;
  12. import javax.transaction.Transactional;
  13. import java.time.LocalDateTime;
  14. import java.util.List;
  15. import java.util.Optional;
  16. public interface CollectionRepo extends JpaRepository<Collection, Long>, JpaSpecificationExecutor<Collection> {
  17. @Query("update Collection t set t.del = true where t.id = ?1")
  18. @Modifying
  19. @Transactional
  20. @CacheEvict(value = {"collection", "recommend"}, allEntries = true)
  21. void softDelete(Long id);
  22. @Cacheable("collection")
  23. Optional<Collection> findById(Long id);
  24. Optional<Collection> findByIdAndDelFalse(Long id);
  25. @Query("update Collection t set t.likes = t.likes + ?2 where t.id = ?1")
  26. @Modifying
  27. @Transactional
  28. @CacheEvict(value = "collection", key = "#id")
  29. void addLike(Long id, int num);
  30. @Query(value = "select distinct c from Collection c join Like l on l.collectionId = c.id " +
  31. "where l.userId = ?1 and l.del = false and c.del = false")
  32. List<Collection> userLikes(Long userId);
  33. List<Collection> findByScheduleSaleTrueAndOnShelfFalseAndStartTimeBeforeAndDelFalse(LocalDateTime time);
  34. @Nonnull
  35. @CachePut(value = "collection", key = "#collection.id")
  36. Collection save(@Nonnull Collection collection);
  37. @Query("select new com.izouma.nineth.dto.RecommendCollection(c,r) from Collection c join Recommend r on c.id = r.collectionId " +
  38. "where c.del = false and c.onShelf = true and r.type = ?1 order by r.sort desc")
  39. List<RecommendCollection> recommend(String type);
  40. @Transactional
  41. @Modifying
  42. @Query("update Collection c set c.currentNumber = COALESCE(c.currentNumber, 0) + ?2 where c.id = ?1")
  43. @CacheEvict(value = "collection", key = "#id")
  44. void increaseNumber(Long id, int d);
  45. @Transactional
  46. @Modifying
  47. @Query("update Collection c set c.sale = COALESCE(c.sale, 0) + ?2 where c.id = ?1")
  48. @CacheEvict(value = "collection", key = "#id")
  49. void increaseSale(Long id, int d);
  50. @Transactional
  51. @Modifying
  52. @Query("update Collection c set c.stock = COALESCE(c.stock, 0) + ?2 where c.id = ?1")
  53. @CacheEvict(value = "collection", key = "#id")
  54. void increaseStock(Long id, int d);
  55. @Transactional
  56. @Modifying
  57. @Query("update Collection c set c.total = COALESCE(c.total, 0) + ?2 where c.id = ?1")
  58. @CacheEvict(value = "collection", key = "#id")
  59. void increaseTotal(Long id, int d);
  60. @Transactional
  61. @Modifying
  62. @Query("update Collection c set c.onShelf = ?2 where c.id = ?1")
  63. @CacheEvict(value = "collection", key = "#id")
  64. void setOnShelf(Long id, boolean onShelf);
  65. @Query("select c.currentNumber from Collection c where c.id = ?1")
  66. Optional<Integer> getCurrentNumber(Long id);
  67. }