CollectionRepo.java 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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. @Query("update Collection t set t.likes = t.likes + ?2 where t.id = ?1")
  34. @Modifying
  35. @Transactional
  36. void addLike(Long id, int num);
  37. @Query(value = "select distinct c from Collection c join Like l on l.collectionId = c.id " +
  38. "where l.userId = ?1 and l.del = false and c.del = false")
  39. List<Collection> userLikes(Long userId);
  40. List<Collection> findByScheduleSaleTrueAndOnShelfFalseAndStartTimeBeforeAndDelFalse(LocalDateTime time);
  41. @Nonnull
  42. @CachePut(value = "collection", key = "#collection.id")
  43. Collection save(@Nonnull Collection collection);
  44. @Query("select new com.izouma.nineth.dto.RecommendCollection(c,r) from Collection c join Recommend r on c.id = r.collectionId " +
  45. "where c.del = false and c.onShelf = true and r.type = ?1 and c.projectId = ?2 order by r.sort desc")
  46. List<RecommendCollection> recommend(String type, int projectId);
  47. @Transactional
  48. @Modifying
  49. @Query("update Collection c set c.scheduleSale = false, c.startTime = null, c.onShelf = true, c.salable = true where c.id = ?1")
  50. @CacheEvict(value = "collection", key = "#id")
  51. void scheduleOnShelf(Long id);
  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.sale = COALESCE(c.sale, 0) + ?2 where c.id = ?1")
  60. @CacheEvict(value = "collection", key = "#id")
  61. void increaseSale(Long id, int d);
  62. @Transactional
  63. @Modifying
  64. @Query("update Collection c set c.stock = COALESCE(c.stock, 0) + ?2 where c.id = ?1")
  65. @CacheEvict(value = "collection", key = "#id")
  66. void increaseStock(Long id, int d);
  67. @Transactional
  68. @Modifying
  69. @Query("update Collection c set c.total = COALESCE(c.total, 0) + ?2 where c.id = ?1")
  70. @CacheEvict(value = "collection", key = "#id")
  71. void increaseTotal(Long id, int d);
  72. @Transactional
  73. @Modifying
  74. @Query("update Collection c set c.onShelf = ?2 where c.id = ?1")
  75. @CacheEvict(value = "collection", key = "#id")
  76. void setOnShelf(Long id, boolean onShelf);
  77. @Query("select c.currentNumber from Collection c where c.id = ?1")
  78. Optional<Integer> getCurrentNumber(Long id);
  79. @Query("select c.stock from Collection c where c.id = ?1")
  80. Integer getStock(Long id);
  81. List<Collection> findAllByIdIn(java.util.Collection<Long> ids);
  82. @Modifying
  83. @Transactional
  84. void deleteAllByIdIn(java.util.Collection<Long> id);
  85. @Query("select c.id from Collection c where c.assetId = ?1 and c.source = 'TRANSFER'")
  86. List<Long> findAllByAssetId(Long assetId);
  87. }