yjwang 3 недель назад
Родитель
Сommit
e8425130f2

+ 103 - 12
fs-service/src/main/java/com/fs/hisStore/service/impl/FsStoreProductScrmServiceImpl.java

@@ -144,12 +144,10 @@ public class FsStoreProductScrmServiceImpl implements IFsStoreProductScrmService
     }
 
     private final static String CZT = "纯正堂";
+    private static final Pattern BRACKET_ALL_CONTENT_PATTERN = Pattern.compile("(?<=[((\\[[]).*?(?=[))\\]]])");
+    private static final Pattern SPECIAL_CHAR_PATTERN = Pattern.compile("[\\p{Punct}\\u3000\\u00A0&&[^\\[【]]");
 
-    private static final Pattern BRACKET_ALL_CONTENT_PATTERN = Pattern.compile("[((].*?[))]");
 
-    private static final Pattern SPECIAL_CHAR_PATTERN = Pattern.compile(
-            "[\\p{Punct}\\u0021-\\u002F\\u003A-\\u0040\\u005B-\\u0060\\u007B-\\u007E\\uFF01-\\uFF0F\\uFF1A-\\uFF20\\uFF3B-\\uFF40\\uFF5B-\\uFF65\\u3000\\u00A0]"
-    );
     private static final Pattern WHITESPACE_PATTERN = Pattern.compile("\\s+");
 
     private static final String BRACKET_NUM_EN = "(数字)";
@@ -178,7 +176,7 @@ public class FsStoreProductScrmServiceImpl implements IFsStoreProductScrmService
                 continue;
             }
             String processedKey = null;
-      //      if(vo.getField().equals("productName")){
+            if(vo.getField().equals("productName")){
                 if (originalKey != null) {
                     processedKey = originalKey.replace(BRACKET_NUM_EN, "").replace(BRACKET_NUM_CN, "");
                 }
@@ -188,8 +186,8 @@ public class FsStoreProductScrmServiceImpl implements IFsStoreProductScrmService
                     promptWordsMap.put(processedChange, medicine.getForbiddenKeywords());
                 }
 
-   //         }
-  //          if(vo.getField().equals("ingredient")){
+            }
+            if(vo.getField().equals("ingredient")){
                 String processedIngredient = null;
                 if (originalIngredient != null) {
                     processedIngredient = originalIngredient.replace(BRACKET_NUM_EN, "").replace(BRACKET_NUM_CN, "");
@@ -197,27 +195,36 @@ public class FsStoreProductScrmServiceImpl implements IFsStoreProductScrmService
                 if (processedIngredient != null && !Objects.equals(processedKey, processedIngredient)) {
                     String  medicineIngredientChange = cleanKeyword(processedIngredient);
                     forbiddenKeywords.add(medicineIngredientChange);
+
                     promptWordsMap.put(medicineIngredientChange, medicine.getMedicineIngredient());
                 }
             }
-   //     }
+        }
         String keyWords = vo.getKeywords();
-        DrugComponentAnalyzer.CheckResult checkResult = DrugComponentAnalyzer.checkForbiddenComponents(forbiddenKeywords, keyWords);
-        boolean forbidden = checkResult.isForbidden();
+//        DrugComponentAnalyzer.CheckResult checkResult = DrugComponentAnalyzer.checkForbiddenComponents(forbiddenKeywords, keyWords);
+        DrugComponentAnalyzer.CheckResult checkResult = null;
+        boolean forbidden = false;
         if(!forbidden){
             //二次-匹配到关键词
             StringBuilder msg = new StringBuilder();
             int lenght = keyWords.length();//原始长度
             keyWords = cleanKeyword(keyWords);
+            List<String> gatherList=new ArrayList<>();
             for (String  keyword: forbiddenKeywords){
                 if(!keyword.equals("")){
                     //验证是否包含数字
                     if(keyWords.contains(keyword) || (lenght > 2 && keyword.contains(keyWords))){
-                        msg.append(promptWordsMap.get(keyword)).append(",");
-                        break;
+                        //计入相关联的数据
+                        gatherList.add(keyword);
                     }
                 }
             }
+            if(!gatherList.isEmpty()){
+                //精确匹配字体长度
+                MatchResult matchResult = getHighPrecisionMatch(keyWords,gatherList);
+                msg.append(promptWordsMap.get(matchResult.getHighMatchWords().get(0)));
+                System.out.println(matchResult);
+            }
             if(msg.length() > 0){
                 result.setFlag(true);
                 result.setMsg(msg+"法律命令禁止销售包含该成分产品!");
@@ -1694,4 +1701,88 @@ private void addProductAttr(Long productId, List<ProductArrtDTO> items, List<FsS
         cleaned = WHITESPACE_PATTERN.matcher(cleaned).replaceAll("");
         return cleaned;
     }
+
+    /**
+     * 优先返回完全匹配项,再返回匹配计数最高的结果
+     * @param targetStr 目标字符串
+     * @param wordArray 相关词语数组
+     * @return 匹配结果封装
+     */
+    public static MatchResult getHighPrecisionMatch(String targetStr, List<String> wordArray) {
+        if (targetStr == null || targetStr.isEmpty() || wordArray == null || wordArray.isEmpty()) {
+            return new MatchResult(0, Collections.emptyList());
+        }
+        final int targetLength = targetStr.length();
+        final int maxCharCode = 65535;
+        BitSet targetCharBitSet = new BitSet(maxCharCode);
+
+        for (int i = 0; i < targetLength; i++) {
+            char c = targetStr.charAt(i);
+            targetCharBitSet.set(c);
+        }
+
+        Map<String, Integer> wordMatchCountMap = new HashMap<>(wordArray.size());
+        int maxMatchCount = 0;
+        for (String word : wordArray) {
+            if (word == null || word.isEmpty()) {
+                continue;
+            }
+
+            int currentMatchCount;
+            if (targetStr.equals(word)) {
+                currentMatchCount = targetLength + 1;
+            } else {
+                BitSet tempWordBitSet = new BitSet(maxCharCode);
+                currentMatchCount = 0;
+                int wordLength = word.length();
+                for (int i = 0; i < wordLength; i++) {
+                    char c = word.charAt(i);
+                    if (!tempWordBitSet.get(c) && targetCharBitSet.get(c)) {
+                        tempWordBitSet.set(c);
+                        currentMatchCount++;
+                    }
+                }
+            }
+            wordMatchCountMap.put(word, currentMatchCount);
+            if (currentMatchCount > maxMatchCount) {
+                maxMatchCount = currentMatchCount;
+            }
+        }
+        List<String> highMatchWords = new ArrayList<>(4);
+        for (Map.Entry<String, Integer> entry : wordMatchCountMap.entrySet()) {
+            if (entry.getValue() == maxMatchCount) {
+                highMatchWords.add(entry.getKey());
+            }
+        }
+
+        return new MatchResult(maxMatchCount, highMatchWords);
+    }
+
+    /**
+     * 匹配结果封装类:存储最高匹配计数和对应词语
+     */
+    public static class MatchResult {
+        private int maxMatchCount;
+        private List<String> highMatchWords;
+
+        public MatchResult(int maxMatchCount, List<String> highMatchWords) {
+            this.maxMatchCount = maxMatchCount;
+            this.highMatchWords = highMatchWords;
+        }
+
+        public int getMaxMatchCount() {
+            return maxMatchCount;
+        }
+
+        public List<String> getHighMatchWords() {
+            return highMatchWords;
+        }
+
+        @Override
+        public String toString() {
+            return "匹配结果:" +
+                    "\n最高匹配字符数:" + maxMatchCount +
+                    "\n匹配度最高的词语:" + highMatchWords;
+        }
+    }
 }