CollectionRepo.java 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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. @Transactional
  23. @Modifying
  24. @Query(value = "update collection_info c set c.on_shelf = ?2, c.salable = ?3, c.start_time = ?4, " +
  25. "c.schedule_sale = ?5, c.sort = ?6, c.detail = ?7, c.privileges = ?8, " +
  26. "c.properties = ?9, c.model3d = ?10, c.max_count = ?11, c.count_id = ?12, c.scan_code = ?13 " +
  27. "where c.id = ?1", nativeQuery = true)
  28. @CacheEvict(value = {"collection", "recommend"}, allEntries = true)
  29. void update(@Nonnull Long id, boolean onShelf, boolean salable, LocalDateTime startTime,
  30. boolean schedule, int sort, String detail, String privileges,
  31. String properties, String model3d, int maxCount, String countId, boolean scanCode);
  32. @Cacheable("collection")
  33. Optional<Collection> findById(@Nonnull Long id);
  34. Optional<Collection> findByIdAndDelFalse(Long id);
  35. @Query("update Collection t set t.likes = t.likes + ?2 where t.id = ?1")
  36. @Modifying
  37. @Transactional
  38. @CacheEvict(value = "collection", key = "#id")
  39. void addLike(Long id, int num);
  40. @Query(value = "select distinct c from Collection c join Like l on l.collectionId = c.id " +
  41. "where l.userId = ?1 and l.del = false and c.del = false")
  42. List<Collection> userLikes(Long userId);
  43. List<Collection> findByScheduleSaleTrueAndOnShelfFalseAndStartTimeBeforeAndDelFalse(LocalDateTime time);
  44. @Nonnull
  45. @CachePut(value = "collection", key = "#collection.id")
  46. Collection save(@Nonnull Collection collection);
  47. @Query("select new com.izouma.nineth.dto.RecommendCollection(c,r) from Collection c join Recommend r on c.id = r.collectionId " +
  48. "where c.del = false and c.onShelf = true and r.type = ?1 order by r.sort desc")
  49. List<RecommendCollection> recommend(String type);
  50. @Transactional
  51. @Modifying
  52. @Query("update Collection c set c.currentNumber = COALESCE(c.currentNumber, 0) + ?2 where c.id = ?1")
  53. @CacheEvict(value = "collection", key = "#id")
  54. void increaseNumber(Long id, int d);
  55. @Transactional
  56. @Modifying
  57. @Query("update Collection c set c.sale = COALESCE(c.sale, 0) + ?2 where c.id = ?1")
  58. @CacheEvict(value = "collection", key = "#id")
  59. void increaseSale(Long id, int d);
  60. @Transactional
  61. @Modifying
  62. @Query("update Collection c set c.stock = COALESCE(c.stock, 0) + ?2 where c.id = ?1")
  63. @CacheEvict(value = "collection", key = "#id")
  64. void increaseStock(Long id, int d);
  65. @Transactional
  66. @Modifying
  67. @Query("update Collection c set c.total = COALESCE(c.total, 0) + ?2 where c.id = ?1")
  68. @CacheEvict(value = "collection", key = "#id")
  69. void increaseTotal(Long id, int d);
  70. @Transactional
  71. @Modifying
  72. @Query("update Collection c set c.onShelf = ?2 where c.id = ?1")
  73. @CacheEvict(value = "collection", key = "#id")
  74. void setOnShelf(Long id, boolean onShelf);
  75. @Transactional
  76. @Modifying
  77. @Query("update Collection c set c.scheduleSale = false, c.startTime = null, c.onShelf = ?2, c.salable = true where c.id = ?1")
  78. @CacheEvict(value = "collection", key = "#id")
  79. void scheduleOnShelf(Long id, boolean onShelf);
  80. @Query("select c.currentNumber from Collection c where c.id = ?1")
  81. Optional<Integer> getCurrentNumber(Long id);
  82. List<Collection> findByScheduleSaleTrue();
  83. List<Collection> findByNameLike(String name);
  84. }