CollectionRepo.java 4.8 KB

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