feat: tag statistic

main
itao 10 months ago
parent 85225111c1
commit dc917544d9

@ -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<ColumnTag, String>{
@Query(value = "select new com.luoo.tag.pojo.TagCountDTO(tagId, count(*)) from SongTag where tagId in :tagIds group by tagId")
List<TagCountDTO> countByTagIds(List<String> tagIds);
}

@ -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<SongTag, String>{
@Query(value = "select new com.luoo.tag.pojo.TagCountDTO(tagId, count(*)) from SongTag where tagId in :tagIds group by tagId")
List<TagCountDTO> countByTagIds(List<String> tagIds);
}

@ -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<Tag, String>, 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<TagStatistic> countByParentIds(List<String> parentIds);
@Query(value = "select new com.luoo.tag.pojo.TagCountDTO(parentId, count(*)) from Tag where parentId in :parentIds group by parentId")
List<TagCountDTO> countByParentIds(List<String> parentIds);
@Query(value = "select distinct creator_id from tb_tag order by create_time desc", nativeQuery = true)
List<String> queryCreator();

@ -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;
}

@ -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;
}

@ -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;
}
}

@ -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<String, TagStatistic> queryTagStatistic(List<String> tagIdList){
Map<String, TagStatistic> tagStatisticMap = tagIdList.stream()
.collect(toMap(Function.identity(), TagStatistic::new));
List<TagStatistic> childTagStatisticList = queryChildTagStatistic(tagIdList);
childTagStatisticList.forEach(childTagStatistic -> {
tagStatisticMap.get(childTagStatistic.getTagId()).setChildTagCount(childTagStatistic.getChildTagCount());
});
// todo 关联音乐或期刊
return tagStatisticMap;
}
private List<TagStatistic> queryChildTagStatistic(List<String> tagIdList){
if(CollectionUtils.isEmpty(tagIdList)){
return Collections.emptyList();
}
List<TagStatistic> childTagStatisticList = tagDao.countByParentIds(tagIdList);
Map<String, TagStatistic> 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;
return Collections.emptyMap();
}
List<TagCountDTO> childTagCountList = tagDao.countByParentIds(tagIdList);
Map<String, Long> childTagCountMap = childTagCountList.stream()
.collect(toMap(TagCountDTO::getTagId, TagCountDTO::getCount));
List<TagCountDTO> songTagCountList = songTagDao.countByTagIds(tagIdList);
Map<String, Long> songTagCountMap = songTagCountList.stream()
.collect(toMap(TagCountDTO::getTagId, TagCountDTO::getCount));
List<TagCountDTO> columnTagCountList = columnTagDao.countByTagIds(tagIdList);
Map<String, Long> columnTagCountMap = columnTagCountList.stream()
.collect(toMap(TagCountDTO::getTagId, TagCountDTO::getCount));
List<TagStatistic> 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()));
}
/**

Loading…
Cancel
Save