From af979636b20c788fff58785be75a8cd216078389 Mon Sep 17 00:00:00 2001 From: JSoi Date: Sat, 30 Jul 2022 05:56:03 +0900 Subject: [PATCH] =?UTF-8?q?#44=20[Update]=20=EC=8A=A4=EC=BC=80=EC=A5=B4?= =?UTF-8?q?=EB=9F=AC=EB=A5=BC=20=ED=86=B5=ED=95=B4=20Store=EC=9D=98=20?= =?UTF-8?q?=EB=B6=81=EB=A7=88=ED=81=AC,=20=EB=B3=84=EC=A0=90=20=ED=8F=89?= =?UTF-8?q?=EA=B7=A0=20=EC=97=85=EB=8D=B0=EC=9D=B4=ED=8A=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../mpnp/baechelin/BaechelinApplication.java | 2 ++ .../store/repository/StoreRepository.java | 16 ++++++++++++++ .../baechelin/store/service/StoreService.java | 21 +++++++++++++++++++ 3 files changed, 39 insertions(+) diff --git a/src/main/java/com/mpnp/baechelin/BaechelinApplication.java b/src/main/java/com/mpnp/baechelin/BaechelinApplication.java index 57c44d5..4c559f9 100644 --- a/src/main/java/com/mpnp/baechelin/BaechelinApplication.java +++ b/src/main/java/com/mpnp/baechelin/BaechelinApplication.java @@ -8,6 +8,7 @@ import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.context.annotation.*; import org.springframework.data.jpa.repository.config.EnableJpaAuditing; +import org.springframework.scheduling.annotation.EnableScheduling; @EnableJpaAuditing @@ -15,6 +16,7 @@ CorsProperties.class, AppProperties.class }) +@EnableScheduling @SpringBootApplication(exclude = {SecurityAutoConfiguration.class}) @PropertySources({@PropertySource("classpath:application-key.properties")}) public class BaechelinApplication { diff --git a/src/main/java/com/mpnp/baechelin/store/repository/StoreRepository.java b/src/main/java/com/mpnp/baechelin/store/repository/StoreRepository.java index 9e59a3e..426abef 100644 --- a/src/main/java/com/mpnp/baechelin/store/repository/StoreRepository.java +++ b/src/main/java/com/mpnp/baechelin/store/repository/StoreRepository.java @@ -3,10 +3,26 @@ import com.mpnp.baechelin.store.domain.Store; import com.mpnp.baechelin.store.domain.StoreImage; import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.data.jpa.repository.Modifying; +import org.springframework.data.jpa.repository.Query; +import org.springframework.data.repository.query.Param; import org.springframework.stereotype.Repository; import java.util.List; import java.util.Optional; public interface StoreRepository extends JpaRepository { + @Modifying + @Query("UPDATE Store s set s.pointAvg = :avg where s.id = :storeId") + void updateAvg(@Param("avg") double avg, @Param("storeId") Long storeId); + + @Query("SELECT AVG(p.point) FROM Store s join s.reviewList p where s.id = :id") + double getAvg(@Param("id") Long id); + + @Modifying + @Query("UPDATE Store s set s.bookMarkCount = :count where s.id = :storeId") + void updateBookmarkCnt(@Param("count") int count, @Param("storeId") Long storeId); + + @Query("SELECT COUNT(b) FROM Store s join s.bookmarkList b where s.id = :id") + int getBookmarkCnt(@Param("id") Long id); } \ No newline at end of file diff --git a/src/main/java/com/mpnp/baechelin/store/service/StoreService.java b/src/main/java/com/mpnp/baechelin/store/service/StoreService.java index ecf2791..0586abc 100644 --- a/src/main/java/com/mpnp/baechelin/store/service/StoreService.java +++ b/src/main/java/com/mpnp/baechelin/store/service/StoreService.java @@ -3,6 +3,8 @@ import com.mpnp.baechelin.bookmark.domain.Bookmark; import com.mpnp.baechelin.bookmark.repository.BookmarkRepository; import com.mpnp.baechelin.common.QuerydslLocation; +import com.mpnp.baechelin.exception.CustomException; +import com.mpnp.baechelin.exception.ErrorCode; import com.mpnp.baechelin.review.domain.Review; import com.mpnp.baechelin.review.domain.ReviewImage; import com.mpnp.baechelin.store.domain.Store; @@ -18,6 +20,8 @@ import lombok.extern.slf4j.Slf4j; import org.springframework.data.domain.Page; import org.springframework.data.domain.Pageable; +import org.springframework.scheduling.annotation.EnableScheduling; +import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Service; import javax.transaction.Transactional; @@ -31,6 +35,8 @@ @Service @Transactional @RequiredArgsConstructor +@EnableScheduling +@Slf4j public class StoreService { private final StoreRepository storeRepository; @@ -246,4 +252,19 @@ public List searchStores(String sido, String sigungu, Stri } return result; } + + @Scheduled(cron = "0 0 0-23 * * *") + public void updateSchedule() { + List storeList = storeRepository.findAll(); + for (Store store : storeList) { + if (!store.getReviewList().isEmpty()) { + double storeAvg = Double.parseDouble(String.format("%.1f", storeRepository.getAvg(store.getId()))); + storeRepository.updateAvg(storeAvg, store.getId()); + } + if (!store.getBookmarkList().isEmpty()) { + int bookmarkCnt = storeRepository.getBookmarkCnt(store.getId()); + storeRepository.updateBookmarkCnt(bookmarkCnt,store.getId()); + } + } + } } \ No newline at end of file