diff --git a/store/src/main/java/com/ktds/hi/store/biz/service/TagService.java b/store/src/main/java/com/ktds/hi/store/biz/service/TagService.java index 9fcf1a9..38dd36a 100644 --- a/store/src/main/java/com/ktds/hi/store/biz/service/TagService.java +++ b/store/src/main/java/com/ktds/hi/store/biz/service/TagService.java @@ -28,6 +28,8 @@ public class TagService implements TagUseCase { private final TagRepositoryPort tagRepositoryPort; + + @Override public List getTopClickedTags() { log.info("가장 많이 클릭된 상위 5개 태그 조회 시작"); diff --git a/store/src/main/java/com/ktds/hi/store/infra/gateway/entity/StoreTagClickEntity.java b/store/src/main/java/com/ktds/hi/store/infra/gateway/entity/StoreTagClickEntity.java new file mode 100644 index 0000000..055d364 --- /dev/null +++ b/store/src/main/java/com/ktds/hi/store/infra/gateway/entity/StoreTagClickEntity.java @@ -0,0 +1,52 @@ +package com.ktds.hi.store.infra.gateway.entity; + +import jakarta.persistence.*; +import lombok.*; +import java.time.LocalDateTime; + +/** + * 매장별 태그 클릭 통계 엔티티 + * 어떤 매장에서 어떤 태그가 얼마나 클릭되었는지 저장 + */ +@Entity +@Table(name = "store_tag_clicks") +@Getter +@Builder +@NoArgsConstructor +@AllArgsConstructor +public class StoreTagClickEntity { + + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + private Long id; + + @Column(name = "store_id", nullable = false) + private Long storeId; + + @Column(name = "tag_id", nullable = false) + private Long tagId; + + @Column(name = "click_count") + @Builder.Default + private Long clickCount = 0L; + + @Column(name = "created_at") + private LocalDateTime createdAt; + + @Column(name = "updated_at") + private LocalDateTime updatedAt; + + /** + * 클릭 수 증가 + */ + public void incrementClickCount() { + this.clickCount = this.clickCount != null ? this.clickCount + 1 : 1L; + this.updatedAt = LocalDateTime.now(); + } + + @PrePersist + protected void onCreate() { + this.createdAt = LocalDateTime.now(); + this.updatedAt = LocalDateTime.now(); + } +} \ No newline at end of file diff --git a/store/src/main/java/com/ktds/hi/store/infra/gateway/repository/StoreTagClickJpaRepository.java b/store/src/main/java/com/ktds/hi/store/infra/gateway/repository/StoreTagClickJpaRepository.java new file mode 100644 index 0000000..6d99390 --- /dev/null +++ b/store/src/main/java/com/ktds/hi/store/infra/gateway/repository/StoreTagClickJpaRepository.java @@ -0,0 +1,43 @@ +package com.ktds.hi.store.infra.gateway.repository; + + +import com.ktds.hi.store.infra.gateway.entity.StoreTagClickEntity; +import org.springframework.data.domain.PageRequest; +import org.springframework.data.jpa.repository.JpaRepository; +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; + +@Repository +public interface StoreTagClickJpaRepository extends JpaRepository { + + /** + * 특정 매장의 가장 많이 클릭된 태그 5개 조회 + */ + @Query("SELECT stc FROM StoreTagClickEntity stc WHERE stc.storeId = :storeId " + + "ORDER BY stc.clickCount DESC") + List findTop5TagsByStoreIdOrderByClickCountDesc( + @Param("storeId") Long storeId, PageRequest pageRequest); + + /** + * 전체 매장에서 가장 많이 클릭된 태그 5개 조회 + */ + @Query("SELECT stc.tagId, SUM(stc.clickCount) as totalClicks " + + "FROM StoreTagClickEntity stc " + + "GROUP BY stc.tagId " + + "ORDER BY totalClicks DESC") + List findTop5TagsGloballyOrderByClickCount(PageRequest pageRequest); + + /** + * 매장별 태그별 클릭 통계 조회 + */ + Optional findByStoreIdAndTagId(Long storeId, Long tagId); + + /** + * 특정 매장의 모든 태그 클릭 통계 조회 + */ + List findByStoreIdOrderByClickCountDesc(Long storeId); +} \ No newline at end of file