|
@@ -0,0 +1,124 @@
|
|
|
+package com.ruoyi.project.VRdemo.service.impl;
|
|
|
+
|
|
|
+import com.ruoyi.project.VRdemo.domain.Press;
|
|
|
+import com.ruoyi.project.VRdemo.domain.SearchData;
|
|
|
+import com.ruoyi.project.VRdemo.mapper.SearchDataMapper;
|
|
|
+import com.ruoyi.project.VRdemo.service.ISearchService;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+
|
|
|
+import java.util.*;
|
|
|
+import java.util.regex.Matcher;
|
|
|
+import java.util.regex.Pattern;
|
|
|
+
|
|
|
+@Service
|
|
|
+public class SearchServiceImpl implements ISearchService {
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private SearchDataMapper searchDataMapper;
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public String analyticParameter(List<SearchData> searchData) {
|
|
|
+ // 按照index排序
|
|
|
+ searchData.sort(Comparator.comparingInt(SearchData::getIndex));
|
|
|
+ StringBuilder sql = new StringBuilder("SELECT * FROM vr_press WHERE ");
|
|
|
+
|
|
|
+ int searchDataSize = searchData.size();
|
|
|
+ for (int i = 0; i < searchDataSize; i++) {
|
|
|
+ SearchData query = searchData.get(i);
|
|
|
+ // 添加条件连接符
|
|
|
+ if(i != 0){
|
|
|
+ sql.append(" ").append(query.getQuery_concat()).append(" ");
|
|
|
+ }
|
|
|
+ // 单项查询
|
|
|
+ if(searchDataSize == 1){
|
|
|
+ sql.append(parseLogicOperators(query.getField_name(),query.getText()));
|
|
|
+ return sql.toString();
|
|
|
+ }
|
|
|
+ // 添加查询条件
|
|
|
+ sql.append(handleQueryCondition(query));
|
|
|
+ }
|
|
|
+
|
|
|
+ return sql.toString();
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 处理单个查询条件并返回对应的SQL片段。
|
|
|
+ * @param query 查询数据对象
|
|
|
+ * @return SQL片段
|
|
|
+ */
|
|
|
+ private String handleQueryCondition(SearchData query) {
|
|
|
+ String text = query.getText();
|
|
|
+ switch (query.getQuery_style()) {
|
|
|
+ case "precise":
|
|
|
+ return query.getField_name() + " = '" + text + "'";
|
|
|
+ case "fuzzy":
|
|
|
+ return query.getField_name() + " LIKE '%" + text + "%'";
|
|
|
+ default:
|
|
|
+ throw new IllegalArgumentException("Unsupported query style: " + query.getQuery_style());
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 解析单项查询逻辑运算符并返回转换后的字符串。
|
|
|
+ * @param text 输入字符串
|
|
|
+ * @return 转换后的字符串
|
|
|
+ */
|
|
|
+ public StringBuilder parseLogicOperators(String fieldName, String text) {
|
|
|
+
|
|
|
+ List<String> protectedContents = new ArrayList<>(); // 用于存储双引号内的内容
|
|
|
+ String placeholder = "TEMP_PLUS_PLACEHOLDER"; // 占位符
|
|
|
+
|
|
|
+ // 使用Pattern和Matcher来查找并处理双引号内的内容
|
|
|
+ Pattern pattern = Pattern.compile("\"([^\"]*)\"");
|
|
|
+ Matcher matcher = pattern.matcher(text);
|
|
|
+ StringBuffer sb = new StringBuffer();
|
|
|
+ while (matcher.find()) {
|
|
|
+ String contentInsideQuotes = matcher.group(1); // 获取双引号内的内容
|
|
|
+ protectedContents.add(contentInsideQuotes); // 保存内容
|
|
|
+ matcher.appendReplacement(sb, placeholder); // 替换为占位符
|
|
|
+ }
|
|
|
+ matcher.appendTail(sb);
|
|
|
+ text = sb.toString();
|
|
|
+
|
|
|
+ // 替换逻辑运算符
|
|
|
+ text = text.replace("*", "AND")
|
|
|
+ .replace("+", "OR")
|
|
|
+ .replace("-", "NOT");
|
|
|
+
|
|
|
+ // 恢复双引号内的内容
|
|
|
+ for (String protectedContent : protectedContents) {
|
|
|
+ text = text.replaceFirst("TEMP_PLUS_PLACEHOLDER", protectedContent);
|
|
|
+ }
|
|
|
+
|
|
|
+ // 使用正则表达式拆分条件,并保留括号和逻辑运算符
|
|
|
+ List<String> conditions = new ArrayList<>();
|
|
|
+ Matcher conditionMatcher = Pattern.compile("\\(|\\)|\\bAND\\b|\\bOR\\b|\\bNOT\\b|[^\\s()]+").matcher(text);
|
|
|
+ while (conditionMatcher.find()) {
|
|
|
+ String match = conditionMatcher.group();
|
|
|
+ if (!match.trim().isEmpty()) {
|
|
|
+ conditions.add(match.trim());
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // 构建SQL条件
|
|
|
+ StringBuilder condition = new StringBuilder();
|
|
|
+ for (String item : conditions) {
|
|
|
+ boolean isLogicalOperatorOrBracket = "AND".equals(item) || "OR".equals(item) || "NOT".equals(item) || "(".equals(item) || ")".equals(item);
|
|
|
+ if (isLogicalOperatorOrBracket) {
|
|
|
+ condition.append(" ").append(item).append(" ");
|
|
|
+ } else {
|
|
|
+ condition.append(fieldName).append(" LIKE '%").append(item).append("%'");
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ return condition;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ // 执行查询
|
|
|
+ public List<Press> ExecuteSearchSql(String SqlString) {
|
|
|
+ return searchDataMapper.ExecuteSearchSql(SqlString);
|
|
|
+ }
|
|
|
+
|
|
|
+}
|