CollectionRepo.java 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. package com.izouma.nineth.repo;
  2. import com.izouma.nineth.domain.Collection;
  3. import com.izouma.nineth.dto.CollectionInfoDTO;
  4. import com.izouma.nineth.dto.CollectionStockAndSale;
  5. import com.izouma.nineth.dto.RecommendCollection;
  6. import com.izouma.nineth.enums.CollectionSource;
  7. import org.springframework.cache.annotation.CacheEvict;
  8. import org.springframework.cache.annotation.CachePut;
  9. import org.springframework.cache.annotation.Cacheable;
  10. import org.springframework.data.domain.Page;
  11. import org.springframework.data.domain.Pageable;
  12. import org.springframework.data.jpa.repository.JpaRepository;
  13. import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
  14. import org.springframework.data.jpa.repository.Modifying;
  15. import org.springframework.data.jpa.repository.Query;
  16. import javax.annotation.Nonnull;
  17. import javax.transaction.Transactional;
  18. import java.math.BigDecimal;
  19. import java.time.LocalDateTime;
  20. import java.util.List;
  21. import java.util.Optional;
  22. import java.util.Set;
  23. public interface CollectionRepo extends JpaRepository<Collection, Long>, JpaSpecificationExecutor<Collection> {
  24. @Query("update Collection t set t.del = true where t.id = ?1")
  25. @Modifying
  26. @Transactional
  27. @CacheEvict(value = {"collection", "recommend"}, allEntries = true)
  28. void softDelete(Long id);
  29. @Transactional
  30. @Modifying
  31. @Query(value = "update collection_info c set c.on_shelf = ?2, c.salable = ?3, c.start_time = ?4, " +
  32. "c.schedule_sale = ?5, c.sort = ?6, c.detail = ?7, c.privileges = ?8, " +
  33. "c.properties = ?9, c.model3d = ?10, c.max_count = ?11, c.count_id = ?12, c.scan_code = ?13, " +
  34. "c.no_sold_out = ?14, c.assignment = ?15, c.coupon_payment = ?16, c.share_bg = ?17," +
  35. "c.register_bg = ?18, c.vip_quota = ?19, c.time_delay = ?20, c.sale_time = ?21, c.hold_days = ?22, " +
  36. "c.open_quota = ?23, c.total_quota = ?24, c.minimum_charge = ?25 " +
  37. "where c.id = ?1", nativeQuery = true)
  38. @CacheEvict(value = {"collection", "recommend"}, allEntries = true)
  39. void update(@Nonnull Long id, boolean onShelf, boolean salable, LocalDateTime startTime,
  40. boolean schedule, int sort, String detail, String privileges,
  41. String properties, String model3d, int maxCount, String countId, boolean scanCode,
  42. boolean noSoldOut, int assignment, boolean couponPayment, String shareBg, String registerBg,
  43. Integer vipQuota, Boolean timeDelay, LocalDateTime saleTime, Integer holdDays, Boolean openQuota,
  44. Integer totalQuota, BigDecimal minimumCharge);
  45. @Cacheable("collection")
  46. Optional<Collection> findById(@Nonnull Long id);
  47. @Query("select c from Collection c where c.del = false and c.id = ?1")
  48. Optional<Collection> findDetailById(@Nonnull Long id);
  49. @Cacheable("collectionInfo")
  50. @Query("select new com.izouma.nineth.dto.CollectionInfoDTO(c,cp) from Collection c left join CollectionPrivilege cp " +
  51. "on c.id = cp.collectionId where c.id = ?1 ")
  52. Optional<CollectionInfoDTO> findInfoById(@Nonnull Long id);
  53. Optional<Collection> findByIdAndDelFalse(Long id);
  54. @Query("update Collection t set t.likes = t.likes + ?2 where t.id = ?1")
  55. @Modifying
  56. @Transactional
  57. @CacheEvict(value = "collection", key = "#id")
  58. void addLike(Long id, int num);
  59. @Query(value = "select distinct c from Collection c join Like l on l.collectionId = c.id " +
  60. "where l.userId = ?1 and l.del = false and c.del = false")
  61. List<Collection> userLikes(Long userId);
  62. List<Collection> findByScheduleSaleTrueAndOnShelfFalseAndStartTimeBeforeAndDelFalse(LocalDateTime time);
  63. @Nonnull
  64. @CachePut(value = "collection", key = "#collection.id")
  65. Collection save(@Nonnull Collection collection);
  66. @Query("select new com.izouma.nineth.dto.RecommendCollection(c,r) from Collection c join Recommend r on c.id = r.collectionId " +
  67. "where c.del = false and c.onShelf = true and r.type = ?1 and r.category = 'COLLECTION' order by r.sort desc")
  68. List<RecommendCollection> recommend(String type);
  69. @Transactional
  70. @Modifying
  71. @Query("update Collection c set c.currentNumber = COALESCE(c.currentNumber, 0) + ?2 where c.id = ?1")
  72. @CacheEvict(value = "collection", key = "#id")
  73. void increaseNumber(Long id, int d);
  74. @Transactional
  75. @Modifying
  76. @Query("update Collection c set c.total = COALESCE(c.total, 0) + ?2 where c.id = ?1")
  77. @CacheEvict(value = "collection", key = "#id")
  78. void increaseTotal(Long id, int d);
  79. @Transactional
  80. @Modifying
  81. @Query("update Collection c set c.onShelf = ?2 where c.id = ?1")
  82. @CacheEvict(value = "collection", key = "#id")
  83. void setOnShelf(Long id, boolean onShelf);
  84. @Transactional
  85. @Modifying
  86. @Query("update Collection c set c.scheduleSale = false, c.startTime = null, c.onShelf = ?2, c.salable = true where c.id = ?1")
  87. @CacheEvict(value = "collection", key = "#id")
  88. void scheduleOnShelf(Long id, boolean onShelf);
  89. @Query("select c.currentNumber from Collection c where c.id = ?1")
  90. Optional<Integer> getCurrentNumber(Long id);
  91. List<Collection> findByScheduleSaleTrue();
  92. List<Collection> findByNameLike(String name);
  93. List<Collection> findByStockGreaterThan(int stock);
  94. @Query("update Collection c set c.stock = ?2 where c.id = ?1")
  95. @Transactional
  96. @Modifying
  97. int updateStock(Long id, int stock);
  98. @Query("update Collection c set c.sale = ?2 where c.id = ?1")
  99. @Transactional
  100. @Modifying
  101. int updateSale(Long id, int sale);
  102. @Query("select c.stock from Collection c where c.id = ?1")
  103. Integer getStock(Long id);
  104. @Query("select c.sale from Collection c where c.id = ?1")
  105. Integer getSale(Long id);
  106. @Query("select new com.izouma.nineth.dto.CollectionStockAndSale(c.id, c.stock, c.sale) from Collection c where c.stock > 0")
  107. List<CollectionStockAndSale> getStockAndSale();
  108. List<Collection> findAllByIdIn(java.util.Collection<Long> ids);
  109. @Query(value = "select c.id, c.pic, c.model3d, c.minter_avatar, c.owner_avatar, c.detail from collection_info c", nativeQuery = true)
  110. List<List<String>> selectResource();
  111. @Query(value = "select c.id, c.pic, c.model3d, c.minter_avatar, c.owner_avatar, c.detail from collection_info c where c.id = ?1", nativeQuery = true)
  112. List<List<String>> selectResource(Long id);
  113. @Modifying
  114. @Transactional
  115. @Query(value = "update collection_info c set c.pic = ?2, c.model3d = ?3, c.minter_avatar = ?4, " +
  116. "c.owner_avatar = ?5, c.detail = ?6 where c.id = ?1", nativeQuery = true)
  117. int updateCDN(Long id, String pic, String model3d, String minterAvatar,
  118. String ownerAvatar, String detail);
  119. @Query("update Collection c set c.vipQuota = ?2 where c.id = ?1")
  120. @Transactional
  121. @Modifying
  122. int updateVipQuota(Long id, int vipQuota);
  123. @Query("select c.vipQuota from Collection c where c.id = ?1")
  124. Integer getVipQuota(Long id);
  125. @Query("select c.assetId from Collection c where c.price >= ?1 and c.source = ?2 and c.createdAt <= ?3 and c.salable = ?4")
  126. Set<Long> findResaleCollectionPriceOver20K(BigDecimal price, CollectionSource source, LocalDateTime startTime, boolean salable);
  127. List<Collection> findAllByNameLike(String name);
  128. @Query(value = "select avg(t.price) from (select c.price from collection_info c where c.name like ?1 " +
  129. "and c.source = 'TRANSFER' " +
  130. "and c.salable = true " +
  131. "and c.stock > 0 order by c.price limit 5) t", nativeQuery = true)
  132. String lowestPrice(String search);
  133. @Query("select c from Collection c join c.tags t on t.id = ?1 ")
  134. Page<Collection> byTag(Long tagId, Pageable pageable);
  135. @Query("select count(id) from Collection where source = 'TRANSFER' and name like ?1")
  136. int countAllByNameLike(String name);
  137. Collection findFirstByOnShelfAndAssetId(boolean onShelf, Long assetId);
  138. List<Collection> findAllByOasisIdInAndSourceInAndStockGreaterThan(List<Long> oasisIds, List<CollectionSource> sources, int sale);
  139. }