fix:87339

This commit is contained in:
梁琦涛
2024-07-24 15:02:03 +08:00
parent 53e698caec
commit e1171f7463
2 changed files with 64 additions and 6 deletions
@@ -40,6 +40,7 @@ import com.jero.modules.laws.standard.entity.LawsNewsFeed;
import com.jero.modules.laws.standard.service.ILawsDomesticStandardService;
import com.jero.modules.laws.standard.service.ILawsEnterpriseStandardService;
import com.jero.modules.laws.standard.service.ILawsNewsFeedService;
import com.jero.modules.laws.utils.HighlightUtil;
import com.jero.modules.oss.entity.OSSFile;
import com.jero.modules.oss.service.IOSSFileService;
import com.jero.modules.split.entity.SarFileSplitInfoEO;
@@ -230,7 +231,7 @@ public class LawsHomeSearchServiceImpl implements ILawsHomeSearchService {
}
select.append(") as sort_index");
QueryWrapper<LawsHomeNormalSearchDTO> queryWrapper = new QueryWrapper<>();
queryWrapper.select("id,standard_name,id,resource_type,standard_name," +
queryWrapper.select("id,standard_name,resource_type," +
"create_time,standard_type_order,standard_state_order,standard_class," +
"dynamic_type,applicable_vehicle," +
"standard_state,standard_code,grade_classification,implementation_date, " + select);
@@ -400,11 +401,7 @@ public class LawsHomeSearchServiceImpl implements ILawsHomeSearchService {
private String getString(String searchContent, String standardNumber, String standardName) {
String[] text = searchContent.split(BLANK_SPACE);
String content = standardNumber + " " + standardName;
for (int i = 0; i < text.length; i++) {
String searchContentSpan = SPAN_CLASS + text[i] + SPAN;
content = content.replace(text[i], searchContentSpan);
}
content = HighlightUtil.highlightKeywords(content, text, SPAN_CLASS,SPAN);
return content;
}
@@ -0,0 +1,61 @@
package com.jero.modules.laws.utils;
/**
* @author lqt
* @version 1.0
* @date 2024/7/24 14:10
*/
public class HighlightUtil {
/**
* 替换字符串中的关键词为高亮显示格式,确保每个关键词只被替换一次,同时避免高亮标签自身被替换。
* @param text 原始字符串
* @param keywords 需要高亮显示的关键词数组
* @param highlightStartTag 高亮开始标签,如 "<span class='highlight'>"
* @param highlightEndTag 高亮结束标签,如 "</span>"
* @return 替换后的高亮字符串
*/
public static String highlightKeywords(String text, String[] keywords, String highlightStartTag,
String highlightEndTag) {
if (text == null || keywords == null || highlightStartTag == null || highlightEndTag == null) {
throw new IllegalArgumentException("Input parameters cannot be null.");
}
// 临时替换高亮标签以避免它们被误识别为关键词
String tempStartTag = "";
String tempEndTag = "ב";
text = text.replace(highlightStartTag, tempStartTag).replace(highlightEndTag, tempEndTag);
StringBuilder highlightedText = new StringBuilder(text);
for (String keyword : keywords) {
if (!keyword.isEmpty()) {
int keywordLength = keyword.length();
int startIndex = 0;
while ((startIndex = highlightedText.indexOf(keyword, startIndex)) != -1) {
highlightedText.replace(startIndex, startIndex + keywordLength,
tempStartTag + keyword + tempEndTag);
startIndex += tempStartTag.length() + keywordLength + tempEndTag.length(); // 跳过替换后文本的长度
}
}
}
// 将临时标签替换回原来的高亮标签
return highlightedText.toString().replace(tempStartTag, highlightStartTag).replace(tempEndTag, highlightEndTag);
}
public static void main(String[] args) {
// String originalText = "测试标准解读一二三四五六七八九十1234567890qwertyuiop一二三四五六七八九十1234—2024";
// String[] keywords = {"1", "T","s"};
// String highlightTag = "<span class='highlight'>";
// String highlightTagEnd = "</span>";
// String highlightedText = highlightKeywords(originalText, keywords, highlightTag,highlightTagEnd);
// System.out.println(highlightedText);
String name = "Alice%s";
int age = 30;
double height = 1.75;
String formatted = String.format("Name:%sAge:%sHeight: %.2f meters.", name, name, height);
System.out.println(formatted);
}
}