CollectionRepo.java 9.6 KB

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