CollectionRepo.java 4.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. }