From dc917544d9cf854508a67b7660683b8e87cbc141 Mon Sep 17 00:00:00 2001 From: itao Date: Tue, 16 Jan 2024 01:08:32 +0800 Subject: [PATCH] feat: tag statistic --- .../java/com/luoo/tag/dao/ColumnTagDao.java | 17 +++++++ .../java/com/luoo/tag/dao/SongTagDao.java | 17 +++++++ .../main/java/com/luoo/tag/dao/TagDao.java | 6 +-- .../java/com/luoo/tag/pojo/ColumnTag.java | 33 ++++++++++++ .../main/java/com/luoo/tag/pojo/SongTag.java | 33 ++++++++++++ .../java/com/luoo/tag/pojo/TagCountDTO.java | 38 ++++++++++++++ .../java/com/luoo/tag/service/TagService.java | 51 ++++++++++--------- 7 files changed, 169 insertions(+), 26 deletions(-) create mode 100644 luoo_tag/src/main/java/com/luoo/tag/dao/ColumnTagDao.java create mode 100644 luoo_tag/src/main/java/com/luoo/tag/dao/SongTagDao.java create mode 100644 luoo_tag/src/main/java/com/luoo/tag/pojo/ColumnTag.java create mode 100644 luoo_tag/src/main/java/com/luoo/tag/pojo/SongTag.java create mode 100644 luoo_tag/src/main/java/com/luoo/tag/pojo/TagCountDTO.java diff --git a/luoo_tag/src/main/java/com/luoo/tag/dao/ColumnTagDao.java b/luoo_tag/src/main/java/com/luoo/tag/dao/ColumnTagDao.java new file mode 100644 index 0000000..9ed273d --- /dev/null +++ b/luoo_tag/src/main/java/com/luoo/tag/dao/ColumnTagDao.java @@ -0,0 +1,17 @@ +package com.luoo.tag.dao; + +import com.luoo.tag.pojo.ColumnTag; +import com.luoo.tag.pojo.TagCountDTO; +import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.data.jpa.repository.Query; + +import java.util.List; + +/** + * 期刊标签信息DAO + */ +public interface ColumnTagDao extends JpaRepository{ + + @Query(value = "select new com.luoo.tag.pojo.TagCountDTO(tagId, count(*)) from SongTag where tagId in :tagIds group by tagId") + List countByTagIds(List tagIds); +} diff --git a/luoo_tag/src/main/java/com/luoo/tag/dao/SongTagDao.java b/luoo_tag/src/main/java/com/luoo/tag/dao/SongTagDao.java new file mode 100644 index 0000000..619ee15 --- /dev/null +++ b/luoo_tag/src/main/java/com/luoo/tag/dao/SongTagDao.java @@ -0,0 +1,17 @@ +package com.luoo.tag.dao; + +import com.luoo.tag.pojo.SongTag; +import com.luoo.tag.pojo.TagCountDTO; +import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.data.jpa.repository.Query; + +import java.util.List; + +/** + * 歌曲标签信息DAO + */ +public interface SongTagDao extends JpaRepository{ + + @Query(value = "select new com.luoo.tag.pojo.TagCountDTO(tagId, count(*)) from SongTag where tagId in :tagIds group by tagId") + List countByTagIds(List tagIds); +} diff --git a/luoo_tag/src/main/java/com/luoo/tag/dao/TagDao.java b/luoo_tag/src/main/java/com/luoo/tag/dao/TagDao.java index e31d2ee..df9da92 100644 --- a/luoo_tag/src/main/java/com/luoo/tag/dao/TagDao.java +++ b/luoo_tag/src/main/java/com/luoo/tag/dao/TagDao.java @@ -1,7 +1,7 @@ package com.luoo.tag.dao; import com.luoo.tag.pojo.Tag; -import com.luoo.tag.pojo.TagStatistic; +import com.luoo.tag.pojo.TagCountDTO; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.JpaSpecificationExecutor; import org.springframework.data.jpa.repository.Modifying; @@ -29,8 +29,8 @@ public interface TagDao extends JpaRepository, JpaSpecificationExec "updaterId=:#{#updateTag.updaterId} where parentId=:#{#updateTag.id}") void updateStateByParentId(Tag updateTag); - @Query(value = "select new com.luoo.tag.pojo.TagStatistic(parentId, count(*)) from Tag where parentId in :parentIds group by parentId") - List countByParentIds(List parentIds); + @Query(value = "select new com.luoo.tag.pojo.TagCountDTO(parentId, count(*)) from Tag where parentId in :parentIds group by parentId") + List countByParentIds(List parentIds); @Query(value = "select distinct creator_id from tb_tag order by create_time desc", nativeQuery = true) List queryCreator(); diff --git a/luoo_tag/src/main/java/com/luoo/tag/pojo/ColumnTag.java b/luoo_tag/src/main/java/com/luoo/tag/pojo/ColumnTag.java new file mode 100644 index 0000000..c29fa7c --- /dev/null +++ b/luoo_tag/src/main/java/com/luoo/tag/pojo/ColumnTag.java @@ -0,0 +1,33 @@ +package com.luoo.tag.pojo; + +import lombok.Getter; +import lombok.RequiredArgsConstructor; +import lombok.Setter; +import lombok.ToString; + +import javax.persistence.Entity; +import javax.persistence.Id; +import javax.persistence.Table; +import java.io.Serializable; + +@Getter +@Setter +@ToString +@RequiredArgsConstructor +@Entity +@Table(name = "tb_column_tag") +public class ColumnTag implements Serializable { + private static final long serialVersionUID = -6632726210980182896L; + @Id + private String id; + + /** + * 期刊ID + */ + private String columnId; + + /** + * 标签ID + */ + private String tagId; +} diff --git a/luoo_tag/src/main/java/com/luoo/tag/pojo/SongTag.java b/luoo_tag/src/main/java/com/luoo/tag/pojo/SongTag.java new file mode 100644 index 0000000..97ee999 --- /dev/null +++ b/luoo_tag/src/main/java/com/luoo/tag/pojo/SongTag.java @@ -0,0 +1,33 @@ +package com.luoo.tag.pojo; + +import lombok.Getter; +import lombok.RequiredArgsConstructor; +import lombok.Setter; +import lombok.ToString; + +import javax.persistence.Entity; +import javax.persistence.Id; +import javax.persistence.Table; +import java.io.Serializable; + +@Getter +@Setter +@ToString +@RequiredArgsConstructor +@Entity +@Table(name = "tb_song_tag") +public class SongTag implements Serializable { + private static final long serialVersionUID = -292595950428042689L; + @Id + private String id; + + /** + * 歌曲ID + */ + private String songId; + + /** + * 标签ID + */ + private String tagId; +} diff --git a/luoo_tag/src/main/java/com/luoo/tag/pojo/TagCountDTO.java b/luoo_tag/src/main/java/com/luoo/tag/pojo/TagCountDTO.java new file mode 100644 index 0000000..a670795 --- /dev/null +++ b/luoo_tag/src/main/java/com/luoo/tag/pojo/TagCountDTO.java @@ -0,0 +1,38 @@ +package com.luoo.tag.pojo; + +import lombok.Builder; +import lombok.Data; +import lombok.NoArgsConstructor; + +import java.io.Serializable; + +/** + * 标签引用统计 + */ +@Data +@NoArgsConstructor +@Builder +public class TagCountDTO implements Serializable { + private static final long serialVersionUID = 7243999657362371575L; + + /** + * 标签ID + */ + private String tagId; + + /** + * 子标签数量 + */ + @Builder.Default + private Long count = 0L; + + public TagCountDTO(String tagId){ + this.tagId = tagId; + this.count = 0L; + } + + public TagCountDTO(String tagId, Long count){ + this.tagId = tagId; + this.count = count; + } +} diff --git a/luoo_tag/src/main/java/com/luoo/tag/service/TagService.java b/luoo_tag/src/main/java/com/luoo/tag/service/TagService.java index 14825fa..3024335 100644 --- a/luoo_tag/src/main/java/com/luoo/tag/service/TagService.java +++ b/luoo_tag/src/main/java/com/luoo/tag/service/TagService.java @@ -4,6 +4,8 @@ import api.PageResult; import com.google.common.collect.Lists; import com.luoo.tag.client.UserClient; import com.luoo.tag.config.RequestContext; +import com.luoo.tag.dao.ColumnTagDao; +import com.luoo.tag.dao.SongTagDao; import com.luoo.tag.dao.TagDao; import com.luoo.tag.enums.TagLevelEnum; import com.luoo.tag.enums.TagStateEnum; @@ -37,6 +39,9 @@ public class TagService { private final TagDao tagDao; private final IdWorker idWorker; private final UserClient userClient; + private final ColumnTagDao columnTagDao; + private final SongTagDao songTagDao; + /** * 标签信息分页查询 @@ -245,32 +250,32 @@ public class TagService { * @return 引用统计集合 */ private Map queryTagStatistic(List tagIdList){ - Map tagStatisticMap = tagIdList.stream() - .collect(toMap(Function.identity(), TagStatistic::new)); - List childTagStatisticList = queryChildTagStatistic(tagIdList); - childTagStatisticList.forEach(childTagStatistic -> { - tagStatisticMap.get(childTagStatistic.getTagId()).setChildTagCount(childTagStatistic.getChildTagCount()); - }); - // todo 关联音乐或期刊 - return tagStatisticMap; - } - - private List queryChildTagStatistic(List tagIdList){ if(CollectionUtils.isEmpty(tagIdList)){ - return Collections.emptyList(); + return Collections.emptyMap(); } - List childTagStatisticList = tagDao.countByParentIds(tagIdList); - Map childTagStatisticMap = childTagStatisticList.stream() - .collect(toMap(TagStatistic::getTagId, Function.identity())); - return tagIdList.stream().map(tagId -> { - TagStatistic tagStatistic = childTagStatisticMap.get(tagId); - if(Objects.isNull(tagStatistic)){ - tagStatistic = new TagStatistic(); - tagStatistic.setTagId(tagId); - tagStatistic.setChildTagCount(0L); - } - return tagStatistic; + List childTagCountList = tagDao.countByParentIds(tagIdList); + Map childTagCountMap = childTagCountList.stream() + .collect(toMap(TagCountDTO::getTagId, TagCountDTO::getCount)); + List songTagCountList = songTagDao.countByTagIds(tagIdList); + Map songTagCountMap = songTagCountList.stream() + .collect(toMap(TagCountDTO::getTagId, TagCountDTO::getCount)); + List columnTagCountList = columnTagDao.countByTagIds(tagIdList); + Map columnTagCountMap = columnTagCountList.stream() + .collect(toMap(TagCountDTO::getTagId, TagCountDTO::getCount)); + + List tagStatisticList = tagIdList.stream().map(tagId -> { + Long childTagCount = childTagCountMap.getOrDefault(tagId, 0L); + Long songTagCount = songTagCountMap.getOrDefault(tagId, 0L); + Long columnTagCount = columnTagCountMap.getOrDefault(tagId, 0L); + return TagStatistic.builder() + .tagId(tagId) + .childTagCount(childTagCount) + .songRefCount(songTagCount) + .columnRefCount(columnTagCount) + .build(); }).collect(toList()); + + return tagStatisticList.stream().collect(toMap(TagStatistic::getTagId, Function.identity())); } /**