CollectionRepo.java 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. package com.izouma.nineth.repo;
  2. import com.izouma.nineth.domain.Collection;
  3. import com.izouma.nineth.dto.RecommendCollection;
  4. import org.springframework.cache.annotation.CacheEvict;
  5. import org.springframework.cache.annotation.CachePut;
  6. import org.springframework.cache.annotation.Cacheable;
  7. import org.springframework.data.jpa.repository.JpaRepository;
  8. import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
  9. import org.springframework.data.jpa.repository.Modifying;
  10. import org.springframework.data.jpa.repository.Query;
  11. import javax.annotation.Nonnull;
  12. import javax.transaction.Transactional;
  13. import java.time.LocalDateTime;
  14. import java.util.List;
  15. import java.util.Optional;
  16. public interface CollectionRepo extends JpaRepository<Collection, Long>, JpaSpecificationExecutor<Collection> {
  17. @Query("update Collection t set t.del = true where t.id = ?1")
  18. @Modifying
  19. @Transactional
  20. @CacheEvict(value = {"collection", "recommend"}, allEntries = true)
  21. void softDelete(Long id);
  22. @Transactional
  23. @Modifying
  24. @Query(value = "update collection_info c set c.on_shelf = ?2, c.salable = ?3, c.start_time = ?4, " +
  25. "c.schedule_sale = ?5, c.sort = ?6, c.detail = ?7, c.privileges = ?8, " +
  26. "c.properties = ?9, c.model3d = ?10 where c.id = ?1", nativeQuery = true)
  27. @CacheEvict(value = {"collection", "recommend"}, allEntries = true)
  28. void update(@Nonnull Long id, boolean onShelf, boolean salable, LocalDateTime startTime,
  29. boolean schedule, int sort, String detail, String privileges,
  30. String properties, String model3d);
  31. @Cacheable("collection")
  32. Optional<Collection> findById(Long id);
  33. Optional<Collection> findByIdAndDelFalse(Long id);
  34. @Query("update Collection t set t.likes = t.likes + ?2 where t.id = ?1")
  35. @Modifying
  36. @Transactional
  37. void addLike(Long id, int num);
  38. @Query(value = "select distinct c from Collection c join Like l on l.collectionId = c.id " +
  39. "where l.userId = ?1 and l.del = false and c.del = false")
  40. List<Collection> userLikes(Long userId);
  41. List<Collection> findByScheduleSaleTrueAndOnShelfFalseAndStartTimeBeforeAndDelFalse(LocalDateTime time);
  42. @Nonnull
  43. @CachePut(value = "collection", key = "#collection.id")
  44. Collection save(@Nonnull Collection collection);
  45. @Query("select new com.izouma.nineth.dto.RecommendCollection(c,r) from Collection c join Recommend r on c.id = r.collectionId " +
  46. "where c.del = false and c.onShelf = true and r.type = ?1 and c.projectId = ?2 order by r.sort desc")
  47. List<RecommendCollection> recommend(String type, int projectId);
  48. @Transactional
  49. @Modifying
  50. @Query("update Collection c set c.scheduleSale = false, c.startTime = null, c.onShelf = true, c.salable = true where c.id = ?1")
  51. @CacheEvict(value = "collection", key = "#id")
  52. void scheduleOnShelf(Long id);
  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.sale = COALESCE(c.sale, 0) + ?2 where c.id = ?1")
  61. @CacheEvict(value = "collection", key = "#id")
  62. void increaseSale(Long id, int d);
  63. @Transactional
  64. @Modifying
  65. @Query("update Collection c set c.stock = COALESCE(c.stock, 0) + ?2 where c.id = ?1")
  66. @CacheEvict(value = "collection", key = "#id")
  67. void increaseStock(Long id, int d);
  68. @Transactional
  69. @Modifying
  70. @Query("update Collection c set c.total = COALESCE(c.total, 0) + ?2 where c.id = ?1")
  71. @CacheEvict(value = "collection", key = "#id")
  72. void increaseTotal(Long id, int d);
  73. @Transactional
  74. @Modifying
  75. @Query("update Collection c set c.onShelf = ?2 where c.id = ?1")
  76. @CacheEvict(value = "collection", key = "#id")
  77. void setOnShelf(Long id, boolean onShelf);
  78. @Query("select c.currentNumber from Collection c where c.id = ?1")
  79. Optional<Integer> getCurrentNumber(Long id);
  80. @Query("select c.id from Collection c where c.assetId in ?1")
  81. List<Long> findAllByDelFalseAndAssetIdIn(java.util.Collection<Long> assetId);
  82. @Query("select c.id from Collection c where c.assetId = ?1 and c.source = 'TRANSFER'")
  83. List<Long> findAllByAssetId(Long assetId);
  84. @Query("update Collection t set t.del = true where t.id in ?1")
  85. @Modifying
  86. @Transactional
  87. void softDeleteByIdIn(java.util.Collection<Long> id);
  88. @Modifying
  89. @Transactional
  90. void deleteAllByIdIn(java.util.Collection<Long> id);
  91. @Query("select c.stock from Collection c where c.id = ?1")
  92. Integer getStock(Long id);
  93. @Query("select c.sale from Collection c where c.id = ?1")
  94. Integer getSale(Long id);
  95. @Query("update Collection c set c.stock = ?2 where c.id = ?1")
  96. @Transactional
  97. @Modifying
  98. int updateStock(Long id, int stock);
  99. @Query("update Collection c set c.sale = ?2 where c.id = ?1")
  100. @Transactional
  101. @Modifying
  102. int updateSale(Long id, int sale);
  103. @Transactional
  104. @Modifying
  105. int deleteByAssetId(Long assetId);
  106. @Query(value = "select c.id, c.pic, c.model3d, c.minter_avatar, c.owner_avatar, c.detail from collection_info c", nativeQuery = true)
  107. List<List<String>> selectResource();
  108. @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)
  109. List<List<String>> selectResource(Long id);
  110. @Modifying
  111. @Transactional
  112. @Query(value = "update collection_info c set c.pic = ?2, c.model3d = ?3, c.minter_avatar = ?4, " +
  113. "c.owner_avatar = ?5, c.detail = ?6 where c.id = ?1", nativeQuery = true)
  114. int updateCDN(Long id, String pic, String model3d, String minterAvatar,
  115. String ownerAvatar, String detail);
  116. }