CollectionRepo.java 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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, c.assignment = ?15 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,
  33. boolean noSoldOut, int assignment);
  34. @Cacheable("collection")
  35. Optional<Collection> findById(@Nonnull Long id);
  36. Optional<Collection> findByIdAndDelFalse(Long id);
  37. @Query("update Collection t set t.likes = t.likes + ?2 where t.id = ?1")
  38. @Modifying
  39. @Transactional
  40. @CacheEvict(value = "collection", key = "#id")
  41. void addLike(Long id, int num);
  42. @Query(value = "select distinct c from Collection c join Like l on l.collectionId = c.id " +
  43. "where l.userId = ?1 and l.del = false and c.del = false")
  44. List<Collection> userLikes(Long userId);
  45. List<Collection> findByScheduleSaleTrueAndOnShelfFalseAndStartTimeBeforeAndDelFalse(LocalDateTime time);
  46. @Nonnull
  47. @CachePut(value = "collection", key = "#collection.id")
  48. Collection save(@Nonnull Collection collection);
  49. @Query("select new com.izouma.nineth.dto.RecommendCollection(c,r) from Collection c join Recommend r on c.id = r.collectionId " +
  50. "where c.del = false and c.onShelf = true and r.type = ?1 order by r.sort desc")
  51. List<RecommendCollection> recommend(String type);
  52. @Transactional
  53. @Modifying
  54. @Query("update Collection c set c.currentNumber = COALESCE(c.currentNumber, 0) + ?2 where c.id = ?1")
  55. @CacheEvict(value = "collection", key = "#id")
  56. void increaseNumber(Long id, int d);
  57. @Transactional
  58. @Modifying
  59. @Query("update Collection c set c.total = COALESCE(c.total, 0) + ?2 where c.id = ?1")
  60. @CacheEvict(value = "collection", key = "#id")
  61. void increaseTotal(Long id, int d);
  62. @Transactional
  63. @Modifying
  64. @Query("update Collection c set c.onShelf = ?2 where c.id = ?1")
  65. @CacheEvict(value = "collection", key = "#id")
  66. void setOnShelf(Long id, boolean onShelf);
  67. @Transactional
  68. @Modifying
  69. @Query("update Collection c set c.scheduleSale = false, c.startTime = null, c.onShelf = ?2, c.salable = true where c.id = ?1")
  70. @CacheEvict(value = "collection", key = "#id")
  71. void scheduleOnShelf(Long id, boolean onShelf);
  72. @Query("select c.currentNumber from Collection c where c.id = ?1")
  73. Optional<Integer> getCurrentNumber(Long id);
  74. List<Collection> findByScheduleSaleTrue();
  75. List<Collection> findByNameLike(String name);
  76. List<Collection> findByStockGreaterThan(int stock);
  77. @Query("update Collection c set c.stock = ?2 where c.id = ?1")
  78. @Transactional
  79. @Modifying
  80. int updateStock(Long id, int stock);
  81. @Query("update Collection c set c.sale = ?2 where c.id = ?1")
  82. @Transactional
  83. @Modifying
  84. int updateSale(Long id, int sale);
  85. @Query("select c.stock from Collection c where c.id = ?1")
  86. Integer getStock(Long id);
  87. @Query("select c.sale from Collection c where c.id = ?1")
  88. Integer getSale(Long id);
  89. @Query("select new com.izouma.nineth.dto.CollectionStockAndSale(c.id, c.stock, c.sale) from Collection c where c.stock > 0")
  90. List<CollectionStockAndSale> getStockAndSale();
  91. }