概念标签返回数据调整

This commit is contained in:
sunflower2014 2025-06-17 18:34:46 +08:00
parent 8ec45e21fe
commit fed8358c80
11 changed files with 184 additions and 72 deletions

View File

@ -34,7 +34,8 @@ redis:
password: Xgf_redis password: Xgf_redis
elasticsearch: elasticsearch:
scheme: http scheme: http
# 111.13.176.3 # 111.13.176.3 部署地址
# 10.127.2.194 本地测试地址
host: 10.127.2.194 host: 10.127.2.194
port: 9200 port: 9200
enable: true enable: true

View File

@ -1 +1,5 @@
alter table tag add level tinyint after name; alter table tag add level tinyint after name;
alter table tag add column parent_name varchar(200);
update tag t inner join tag x on t.parent_id = x.id
set t.parent_name = x.name
where t.level = 2;

View File

@ -114,8 +114,11 @@ public interface NewsMapper {
"</if>" + "</if>" +
" left join news_tags on news.newsinfo_id = news_tags.newsinfo_id " + " left join news_tags on news.newsinfo_id = news_tags.newsinfo_id " +
"<where>" + "<where>" +
"<if test=\"tags != null and tags.isEmpty()\">" +
" and 1 = 2 " +
"</if>" +
"<if test=\"keyword != null and !keyword.isEmpty()\">" + "<if test=\"keyword != null and !keyword.isEmpty()\">" +
"news.title like concat('%', #{keyword}, '%') " + " and news.title like concat('%', #{keyword}, '%') " +
"</if>" + "</if>" +
"<if test=\"column != null and !column.isEmpty()\">" + "<if test=\"column != null and !column.isEmpty()\">" +
"and news_column_rel.column_id in (${column}) " + "and news_column_rel.column_id in (${column}) " +
@ -171,8 +174,11 @@ public interface NewsMapper {
"left join news_tags on news.newsinfo_id = news_tags.newsinfo_id " + "left join news_tags on news.newsinfo_id = news_tags.newsinfo_id " +
"</if>" + "</if>" +
"<where>" + "<where>" +
"<if test=\"tags != null and tags.isEmpty()\">" +
" and 1 = 2 " +
"</if>" +
"<if test=\"keyword != null and !keyword.isEmpty()\">" + "<if test=\"keyword != null and !keyword.isEmpty()\">" +
"news.title like concat('%', #{keyword}, '%') " + " and news.title like concat('%', #{keyword}, '%') " +
"</if>" + "</if>" +
"<if test=\"column != null and !column.isEmpty()\">" + "<if test=\"column != null and !column.isEmpty()\">" +
"and news_column_rel.column_id in (${column}) " + "and news_column_rel.column_id in (${column}) " +

View File

@ -19,6 +19,7 @@ public interface TagMapper {
@Results({ @Results({
@Result(column = "id", property = "id", id = true), @Result(column = "id", property = "id", id = true),
@Result(column = "parent_id", property = "parentId"), @Result(column = "parent_id", property = "parentId"),
@Result(column = "parent_name", property = "parentName"),
@Result(column = "name", property = "name"), @Result(column = "name", property = "name"),
@Result(column = "create_time", property = "createTime", javaType = Date.class, jdbcType = JdbcType.TIMESTAMP), @Result(column = "create_time", property = "createTime", javaType = Date.class, jdbcType = JdbcType.TIMESTAMP),
@Result(column = "update_time", property = "updateTime", javaType = Date.class, jdbcType = JdbcType.TIMESTAMP) @Result(column = "update_time", property = "updateTime", javaType = Date.class, jdbcType = JdbcType.TIMESTAMP)

View File

@ -88,10 +88,17 @@ public class Industry {
this.updateTime = updateTime; this.updateTime = updateTime;
} }
public String getDisplayName() { public String getName() {
if (ObjectUtils.isEmpty(this.getSecondaryName())) { if (ObjectUtils.isEmpty(this.getSecondaryName())) {
return this.getPrimaryName(); return this.getPrimaryName();
} }
return this.getSecondaryName(); return this.getSecondaryName();
} }
public String getDisplayName() {
if (ObjectUtils.isEmpty(this.getSecondaryName())) {
return this.getPrimaryName();
}
return this.getPrimaryName()+ "-" +this.getSecondaryName();
}
} }

View File

@ -2,6 +2,8 @@ package com.jinrui.reference.core.model.entity;
import java.util.Date; import java.util.Date;
import org.springframework.util.ObjectUtils;
/** /**
* 标签 * 标签
*/ */
@ -17,6 +19,8 @@ public class Tag {
* 上级标签ID * 上级标签ID
*/ */
private Long parentId; private Long parentId;
private String parentName;
/** /**
* 标签名称 * 标签名称
@ -72,4 +76,20 @@ public class Tag {
public void setUpdateTime(Date updateTime) { public void setUpdateTime(Date updateTime) {
this.updateTime = updateTime; this.updateTime = updateTime;
} }
public String getParentName() {
return parentName;
}
public void setParentName(String parentName) {
this.parentName = parentName;
}
public String getDisplayName() {
if (ObjectUtils.isEmpty(this.getParentName())) {
return this.getName();
}
return this.getParentName()+ "-" +this.getName();
}
} }

View File

@ -47,7 +47,7 @@ public class IndustryHierarchyVO {
public IndustryHierarchyVO(Industry industry) { public IndustryHierarchyVO(Industry industry) {
this.id = industry.getId(); this.id = industry.getId();
this.name = industry.getDisplayName(); this.name = industry.getName();
} }
public Long getId() { public Long getId() {

View File

@ -6,7 +6,7 @@ public class NewsDetailTagItem {
private Long id; private Long id;
private String name; private String name;
public Long getId() { public Long getId() {
return id; return id;
} }
@ -22,4 +22,14 @@ public class NewsDetailTagItem {
public void setName(String name) { public void setName(String name) {
this.name = name; this.name = name;
} }
public String getParentName() {
return parentName;
}
public void setParentName(String parentName) {
this.parentName = parentName;
}
private String parentName;
} }

View File

@ -42,7 +42,7 @@ public class NewsInfoDetailVO {
/** /**
* 数据来源名称或标识 * 数据来源名称或标识
*/ */
private NewsDetailTag sourcename; private NewsDetailTag tag;
/** /**
* H5富文本 * H5富文本
@ -172,12 +172,12 @@ public class NewsInfoDetailVO {
this.updateTime = updateTime; this.updateTime = updateTime;
} }
public NewsDetailTag getSourcename() { public NewsDetailTag getTag() {
return sourcename; return tag;
} }
public void setSourcename(NewsDetailTag sourcename) { public void setTag(NewsDetailTag tag) {
this.sourcename = sourcename; this.tag = tag;
} }
public Long getNewsId() { public Long getNewsId() {

View File

@ -2,7 +2,6 @@ package com.jinrui.reference.core.service;
import java.io.IOException; import java.io.IOException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Objects; import java.util.Objects;
@ -19,6 +18,7 @@ import org.springframework.util.StringUtils;
import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.ObjectMapper;
import com.jinrui.reference.core.mapper.IndustryMapper; import com.jinrui.reference.core.mapper.IndustryMapper;
import com.jinrui.reference.core.mapper.NewsMapper; import com.jinrui.reference.core.mapper.NewsMapper;
import com.jinrui.reference.core.mapper.NewsTagsMapper;
import com.jinrui.reference.core.mapper.TagMapper; import com.jinrui.reference.core.mapper.TagMapper;
import com.jinrui.reference.core.model.dto.news.SaveDraftTag; import com.jinrui.reference.core.model.dto.news.SaveDraftTag;
import com.jinrui.reference.core.model.entity.Industry; import com.jinrui.reference.core.model.entity.Industry;
@ -26,6 +26,7 @@ import com.jinrui.reference.core.model.entity.News;
import com.jinrui.reference.core.model.entity.NewsIndustryRel; import com.jinrui.reference.core.model.entity.NewsIndustryRel;
import com.jinrui.reference.core.model.entity.NewsInfo; import com.jinrui.reference.core.model.entity.NewsInfo;
import com.jinrui.reference.core.model.entity.NewsTagRel; import com.jinrui.reference.core.model.entity.NewsTagRel;
import com.jinrui.reference.core.model.entity.NewsTags;
import com.jinrui.reference.core.model.entity.Tag; import com.jinrui.reference.core.model.entity.Tag;
import com.jinrui.reference.core.model.vo.PageObject; import com.jinrui.reference.core.model.vo.PageObject;
import com.jinrui.reference.core.model.vo.ResultObject; import com.jinrui.reference.core.model.vo.ResultObject;
@ -62,17 +63,28 @@ public class NewsInfoService {
private final TagMapper tagMapper; private final TagMapper tagMapper;
private final IndustryMapper industryMapper; private final IndustryMapper industryMapper;
private final NewsMapper newsMapper; private final NewsMapper newsMapper;
private final NewsTagsMapper newsTagsMapper;
public NewsInfoService(ElasticsearchClient elasticsearchClient, public NewsInfoService(ElasticsearchClient elasticsearchClient,
ObjectMapper objectMapper, ObjectMapper objectMapper,
TagMapper tagMapper, TagMapper tagMapper,
IndustryMapper industryMapper, IndustryMapper industryMapper,
NewsMapper newsMapper) { NewsMapper newsMapper,
NewsTagsMapper newsTagsMapper) {
this.elasticsearchClient = elasticsearchClient; this.elasticsearchClient = elasticsearchClient;
this.objectMapper = objectMapper; this.objectMapper = objectMapper;
this.tagMapper = tagMapper; this.tagMapper = tagMapper;
this.industryMapper = industryMapper; this.industryMapper = industryMapper;
this.newsMapper = newsMapper; this.newsMapper = newsMapper;
this.newsTagsMapper = newsTagsMapper;
NewsTags newsTags = newsTagsMapper.getNewsTagsByNewsId("102694660");
for (String conceptLabel: newsTags.getConceptLabel()) {
System.out.println("+++++++++++++++++++++conceptLabel++++++++++++++++++++++" + conceptLabel);
}
for (String industryLabel: newsTags.getIndustryLabel()) {
System.out.println("+++++++++++++++++++++industryLabel++++++++++++++++++++++" + industryLabel);
}
} }
public ResultObject<Void> publish(String id, long editorId) { public ResultObject<Void> publish(String id, long editorId) {
@ -98,33 +110,7 @@ public class NewsInfoService {
relNews.setStatus(1); relNews.setStatus(1);
relNews.setEditorId(editorId); relNews.setEditorId(editorId);
newsMapper.saveNews(relNews); newsMapper.saveNews(relNews);
saveNewsRel(id, relNews.getId());
String industry = newsInfo.getIndustry();
if (!ObjectUtils.isEmpty(industry)) {
List<Industry> industries = industryMapper.queryAll();
for (Industry industryItem: industries) {
if (industry.equals(industryItem.getPrimaryName()) || industryItem.equals(industryItem.getSecondaryName())) {
industryMapper.saveNewsIndustryRel(relNews.getId(), industryItem.getId());
break;
}
}
}
String sourcename = newsInfo.getSourcename();
if (!ObjectUtils.isEmpty(sourcename)) {
boolean isFound = false;
List<Tag> matchedTags = tagMapper.queryTag(1L, sourcename, null, "create_time", null);
for (Tag tag: matchedTags) {
if (tag.getName().equals(sourcename)) {
tagMapper.saveNewsTagRel(relNews.getId(), tag.getId());
isFound = true;
break;
}
}
if (!isFound) {
tagMapper.saveTag(1L, sourcename);
}
}
} }
NewsInfo publishedNewsInfo = new NewsInfo(); NewsInfo publishedNewsInfo = new NewsInfo();
@ -139,6 +125,54 @@ public class NewsInfoService {
return ResultObject.success(); return ResultObject.success();
} }
private void saveNewsRel(String newsInfoId, Long newsId) {
NewsTags newsTags = newsTagsMapper.getNewsTagsByNewsId(newsInfoId);
if (newsTags == null) {
return;
}
if (!ObjectUtils.isEmpty(newsTags.getIndustryLabel())) {
List<Industry> industries = industryMapper.queryAll();
for (String industryLabel: newsTags.getIndustryLabel()) {
for (Industry industry: industries) {
if (industryLabel.equals(industry.getDisplayName())) {
industryMapper.saveNewsIndustryRel(newsId, industry.getId());
break;
}
}
}
}
String sourcename = newsTags.getSource();
if (!ObjectUtils.isEmpty(sourcename)) {
boolean isFound = false;
List<Tag> matchedTags = tagMapper.queryTag(1L, sourcename, null, "create_time", null);
for (Tag tag: matchedTags) {
if (tag.getName().equals(sourcename)) {
tagMapper.saveNewsTagRel(newsId, tag.getId());
isFound = true;
break;
}
}
if (!isFound) {
tagMapper.saveTag(1L, sourcename);
}
}
List<String> conceptLabels = newsTags.getConceptLabel();
if (!ObjectUtils.isEmpty(conceptLabels)) {
List<Tag> tags = tagMapper.queryAll().stream().filter(e -> !ObjectUtils.isEmpty(e.getParentName())).collect(Collectors.toList());
for (String conceptLabel: conceptLabels) {
for (Tag tag:tags) {
if (conceptLabel.equals(tag.getDisplayName())) {
tagMapper.saveNewsTagRel(newsId, tag.getId());
break;
}
}
}
}
}
public ResultObject<NewsInfoDetailVO> detail(String id) { public ResultObject<NewsInfoDetailVO> detail(String id) {
@ -158,34 +192,62 @@ public class NewsInfoService {
if (newsInfo.getStatus() == null ||newsInfo.getStatus() == 1) { if (newsInfo.getStatus() == null ||newsInfo.getStatus() == 1) {
for (Tag tag: allTags) { NewsTags newsTags = newsTagsMapper.getNewsTagsByNewsId(id);
if (tag.getName().equals(newsInfo.getSourcename())) { if (newsTags != null) {
NewsDetailTag newsDetailTag = new NewsDetailTag(); NewsDetailTag newsDetailTag = new NewsDetailTag();
for (Tag tag: allTags) {
NewsDetailTagItem sourceTagItem = new NewsDetailTagItem(); if (tag.getName().equals(newsTags.getSource())) {
newsDetailTag.setSource(sourceTagItem); NewsDetailTagItem sourceTagItem = new NewsDetailTagItem();
sourceTagItem.setId(tag.getId()); sourceTagItem.setId(tag.getId());
sourceTagItem.setName(tag.getName()); sourceTagItem.setName(tag.getName());
newsInfoDetailVO.setSourcename(newsDetailTag); newsDetailTag.setSource(sourceTagItem);
}
} }
} List<String> conceptLabels = newsTags.getConceptLabel();
String industryStr = newsInfo.getIndustry(); if (!ObjectUtils.isEmpty(conceptLabels)) {
for (Industry industry: allIndustrys) { List<Tag> levelTwoTags = allTags.stream().filter(e -> !ObjectUtils.isEmpty(e.getParentName())).collect(Collectors.toList());
if (Objects.equals(industry.getDisplayName(), industryStr)) { List<NewsDetailTagItem> fieldArr = new ArrayList<>();
NewsDetailIndustry newsDetailIndustry = new NewsDetailIndustry(); for (String conceptLabel: conceptLabels) {
newsDetailIndustry.setId(newsDetailIndustry.getId()); for (Tag tag: levelTwoTags) {
newsDetailIndustry.setPrimaryName(newsDetailIndustry.getPrimaryName()); if (Objects.equals(conceptLabel, tag.getDisplayName())) {
newsDetailIndustry.setSecondaryName(newsDetailIndustry.getSecondaryName()); NewsDetailTagItem conceptTagItem = new NewsDetailTagItem();
newsInfoDetailVO.setIndustry(Arrays.asList(newsDetailIndustry)); conceptTagItem.setId(tag.getId());
conceptTagItem.setName(tag.getName());
conceptTagItem.setParentName(tag.getParentName());
fieldArr.add(conceptTagItem);
break;
}
}
}
newsDetailTag.setFieldArr(fieldArr);
} }
newsInfoDetailVO.setTag(newsDetailTag);
List<String> industryLabels = newsTags.getIndustryLabel();
if (!ObjectUtils.isEmpty(industryLabels)) {
List<NewsDetailIndustry> industries = new ArrayList<>();
for (String industryLabel: industryLabels) {
for (Industry industry: allIndustrys) {
if (Objects.equals(industry.getDisplayName(), industryLabel)) {
NewsDetailIndustry newsDetailIndustry = new NewsDetailIndustry();
newsDetailIndustry.setId(newsDetailIndustry.getId());
newsDetailIndustry.setPrimaryName(newsDetailIndustry.getPrimaryName());
newsDetailIndustry.setSecondaryName(newsDetailIndustry.getSecondaryName());
industries.add(newsDetailIndustry);
break;
}
}
}
newsInfoDetailVO.setIndustry(industries);
}
} }
} else { } else {
Map<Long, Industry> industryMap = allIndustrys.stream().collect(Collectors.toMap(Industry::getId, Function.identity())); Map<Long, Industry> industryMap = allIndustrys.stream().collect(Collectors.toMap(Industry::getId, Function.identity()));
List<NewsTagRel> tagRelList = tagMapper.getNewsTagRelList(newsInfo.getNewsId()); List<NewsTagRel> tagRelList = tagMapper.getNewsTagRelList(newsInfo.getNewsId());
NewsDetailTag newsDetailTag = new NewsDetailTag(); NewsDetailTag newsDetailTag = new NewsDetailTag();
newsInfoDetailVO.setSourcename(newsDetailTag); newsInfoDetailVO.setTag(newsDetailTag);
if (!CollectionUtils.isEmpty(tagRelList)) { if (!CollectionUtils.isEmpty(tagRelList)) {
List<NewsDetailTagItem> arr = new ArrayList<>(); List<NewsDetailTagItem> arr = new ArrayList<>();
@ -229,7 +291,7 @@ public class NewsInfoService {
} }
} }
public ResultObject<Void> save(SaveNewsInfoDTO saveNewsDTO) { public ResultObject<Void> save(SaveNewsInfoDTO saveNewsDTO) {
String id = saveNewsDTO.getId(); String id = saveNewsDTO.getId();
if (ObjectUtils.isEmpty(id)) { if (ObjectUtils.isEmpty(id)) {
return saveNewsInfo(saveNewsDTO); return saveNewsInfo(saveNewsDTO);
@ -313,7 +375,7 @@ public class NewsInfoService {
if (!ObjectUtils.isEmpty(industries)) { if (!ObjectUtils.isEmpty(industries)) {
List<Industry> allIndustry = industryMapper.queryAll(); List<Industry> allIndustry = industryMapper.queryAll();
Map<Long, Industry> industryMap = allIndustry.stream().collect(Collectors.toMap(Industry::getId, Function.identity())); Map<Long, Industry> industryMap = allIndustry.stream().collect(Collectors.toMap(Industry::getId, Function.identity()));
String industry = industries.stream().map(e -> industryMap.get(e)).map(Industry::getDisplayName).collect(Collectors.joining(";")); String industry = industries.stream().map(e -> industryMap.get(e)).map(Industry::getName).collect(Collectors.joining(";"));
newsInfo.setIndustry(industry); newsInfo.setIndustry(industry);
} }

View File

@ -4,7 +4,6 @@ import java.io.IOException;
import java.io.UnsupportedEncodingException; import java.io.UnsupportedEncodingException;
import java.net.URLDecoder; import java.net.URLDecoder;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections;
import java.util.Date; import java.util.Date;
import java.util.HashMap; import java.util.HashMap;
import java.util.HashSet; import java.util.HashSet;
@ -24,7 +23,6 @@ import org.springframework.util.StringUtils;
import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference; import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.ObjectMapper;
import com.jinrui.reference.core.mapper.ColumnMapper; import com.jinrui.reference.core.mapper.ColumnMapper;
import com.jinrui.reference.core.mapper.IndustryMapper; import com.jinrui.reference.core.mapper.IndustryMapper;
@ -211,11 +209,13 @@ public class NewsService {
for (DraftIndustryRel rel: industryRelList) { for (DraftIndustryRel rel: industryRelList) {
Long industryId = rel.getIndustryId(); Long industryId = rel.getIndustryId();
Industry industry = industryMap.get(industryId); Industry industry = industryMap.get(industryId);
NewsDetailIndustry newsDetailIndustry = new NewsDetailIndustry(); if (industry != null) {
newsDetailIndustry.setId(industryId); NewsDetailIndustry newsDetailIndustry = new NewsDetailIndustry();
newsDetailIndustry.setPrimaryName(industry.getPrimaryName()); newsDetailIndustry.setId(industryId);
newsDetailIndustry.setSecondaryName(industry.getSecondaryName()); newsDetailIndustry.setPrimaryName(industry.getPrimaryName());
newsIndustryList.add(newsDetailIndustry); newsDetailIndustry.setSecondaryName(industry.getSecondaryName());
newsIndustryList.add(newsDetailIndustry);
}
} }
} }
@ -304,6 +304,7 @@ public class NewsService {
NewsDetailTagItem tagItem = new NewsDetailTagItem(); NewsDetailTagItem tagItem = new NewsDetailTagItem();
tagItem.setId(tagId); tagItem.setId(tagId);
tagItem.setName(tag.getName()); tagItem.setName(tag.getName());
tagItem.setParentName(tag.getParentName());
if (parentId != null && parentId == 1) { if (parentId != null && parentId == 1) {
newsDetailTag.setSource(tagItem); newsDetailTag.setSource(tagItem);
} else { } else {
@ -463,7 +464,7 @@ public class NewsService {
if (!ObjectUtils.isEmpty(industries)) { if (!ObjectUtils.isEmpty(industries)) {
List<Industry> allIndustry = industryMapper.queryAll(); List<Industry> allIndustry = industryMapper.queryAll();
Map<Long, Industry> industryMap = allIndustry.stream().collect(Collectors.toMap(Industry::getId, Function.identity())); Map<Long, Industry> industryMap = allIndustry.stream().collect(Collectors.toMap(Industry::getId, Function.identity()));
String industry = industries.stream().map(e -> industryMap.get(e)).map(Industry::getDisplayName).collect(Collectors.joining(";")); String industry = industries.stream().map(e -> industryMap.get(e)).map(Industry::getName).collect(Collectors.joining(";"));
newsInfo.setIndustry(industry); newsInfo.setIndustry(industry);
} }
@ -913,7 +914,7 @@ public class NewsService {
private List<Long> getMatchTags(String tagParam) { private List<Long> getMatchTags(String tagParam) {
if (ObjectUtils.isEmpty(tagParam)) { if (ObjectUtils.isEmpty(tagParam)) {
return Collections.emptyList(); return null;
} }
String[] tags = StringUtils.trimWhitespace(tagParam).split("\\s+"); String[] tags = StringUtils.trimWhitespace(tagParam).split("\\s+");
List<Tag> levelOneTags = tagMapper.queryTag(6L, null, null, "create_time", null); List<Tag> levelOneTags = tagMapper.queryTag(6L, null, null, "create_time", null);