CollectionRepo.java 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. package com.izouma.nineth.repo;
  2. import com.izouma.nineth.domain.Collection;
  3. import com.izouma.nineth.dto.CollectionStockAndSale;
  4. import com.izouma.nineth.dto.RecommendCollection;
  5. import org.springframework.cache.annotation.CacheEvict;
  6. import org.springframework.cache.annotation.CachePut;
  7. import org.springframework.cache.annotation.Cacheable;
  8. import org.springframework.data.jpa.repository.JpaRepository;
  9. import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
  10. import org.springframework.data.jpa.repository.Modifying;
  11. import org.springframework.data.jpa.repository.Query;
  12. import javax.annotation.Nonnull;
  13. import javax.transaction.Transactional;
  14. import java.time.LocalDateTime;
  15. import java.util.List;
  16. import java.util.Optional;
  17. public interface CollectionRepo extends JpaRepository<Collection, Long>, JpaSpecificationExecutor<Collection> {
  18. @Query("update Collection t set t.del = true where t.id = ?1")
  19. @Modifying
  20. @Transactional
  21. @CacheEvict(value = {"collection", "recommend"}, allEntries = true)
  22. void softDelete(Long id);
  23. @Transactional
  24. @Modifying
  25. @Query(value = "update collection_info c set c.on_shelf = ?2, c.salable = ?3, c.start_time = ?4, " +
  26. "c.schedule_sale = ?5, c.sort = ?6, c.detail = ?7, c.privileges = ?8, " +
  27. "c.properties = ?9, c.model3d = ?10, c.max_count = ?11, c.count_id = ?12, c.scan_code = ?13, " +
  28. "c.no_sold_out = ?14 where c.id = ?1", nativeQuery = true)
  29. @CacheEvict(value = {"collection", "recommend"}, allEntries = true)
  30. void update(@Nonnull Long id, boolean onShelf, boolean salable, LocalDateTime startTime,
  31. boolean schedule, int sort, String detail, String privileges,
  32. String properties, String model3d, int maxCount, String countId, boolean scanCode, boolean noSoldOut);
  33. @Cacheable("collection")
  34. Optional<Collection> findById(@Nonnull Long id);
  35. Optional<Collection> findByIdAndDelFalse(Long id);
  36. @Query("update Collection t set t.likes = t.likes + ?2 where t.id = ?1")
  37. @Modifying
  38. @Transactional
  39. @CacheEvict(value = "collection", key = "#id")
  40. void addLike(Long id, int num);
  41. @Query(value = "select distinct c from Collection c join Like l on l.collectionId = c.id " +
  42. "where l.userId = ?1 and l.del = false and c.del = false")
  43. List<Collection> userLikes(Long userId);
  44. List<Collection> findByScheduleSaleTrueAndOnShelfFalseAndStartTimeBeforeAndDelFalse(LocalDateTime time);
  45. @Nonnull
  46. @CachePut(value = "collection", key = "#collection.id")
  47. Collection save(@Nonnull Collection collection);
  48. @Query("select new com.izouma.nineth.dto.RecommendCollection(c,r) from Collection c join Recommend r on c.id = r.collectionId " +
  49. "where c.del = false and c.onShelf = true and r.type = ?1 order by r.sort desc")
  50. List<RecommendCollection> recommend(String type);
  51. @Transactional
  52. @Modifying
  53. @Query("update Collection c set c.currentNumber = COALESCE(c.currentNumber, 0) + ?2 where c.id = ?1")
  54. @CacheEvict(value = "collection", key = "#id")
  55. void increaseNumber(Long id, int d);
  56. @Transactional
  57. @Modifying
  58. @Query("update Collection c set c.total = COALESCE(c.total, 0) + ?2 where c.id = ?1")
  59. @CacheEvict(value = "collection", key = "#id")
  60. void increaseTotal(Long id, int d);
  61. @Transactional
  62. @Modifying
  63. @Query("update Collection c set c.onShelf = ?2 where c.id = ?1")
  64. @CacheEvict(value = "collection", key = "#id")
  65. void setOnShelf(Long id, boolean onShelf);
  66. @Transactional
  67. @Modifying
  68. @Query("update Collection c set c.scheduleSale = false, c.startTime = null, c.onShelf = ?2, c.salable = true where c.id = ?1")
  69. @CacheEvict(value = "collection", key = "#id")
  70. void scheduleOnShelf(Long id, boolean onShelf);
  71. @Query("select c.currentNumber from Collection c where c.id = ?1")
  72. Optional<Integer> getCurrentNumber(Long id);
  73. List<Collection> findByScheduleSaleTrue();
  74. List<Collection> findByNameLike(String name);
  75. List<Collection> findByStockGreaterThan(int stock);
  76. @Query("update Collection c set c.stock = ?2 where c.id = ?1")
  77. @Transactional
  78. @Modifying
  79. int updateStock(Long id, int stock);
  80. @Query("update Collection c set c.sale = ?2 where c.id = ?1")
  81. @Transactional
  82. @Modifying
  83. int updateSale(Long id, int sale);
  84. @Query("select c.stock from Collection c where c.id = ?1")
  85. Integer getStock(Long id);
  86. @Query("select c.sale from Collection c where c.id = ?1")
  87. Integer getSale(Long id);
  88. @Query("select new com.izouma.nineth.dto.CollectionStockAndSale(c.id, c.stock, c.sale) from Collection c where c.stock > 0")
  89. List<CollectionStockAndSale> getStockAndSale();
  90. }