From 306cd49b58a925591b8a19e0a62649bf69742e4e Mon Sep 17 00:00:00 2001 From: wangfukang <15630117759@163.com> Date: Fri, 17 May 2024 12:22:42 +0800 Subject: [PATCH] =?UTF-8?q?ai=E8=AF=86=E5=88=AB=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/cc/hiver/mall/utils/AliOcrUtil.java | 32 ++++++++++++++----- 1 file changed, 24 insertions(+), 8 deletions(-) diff --git a/hiver-modules/hiver-mall/src/main/java/cc/hiver/mall/utils/AliOcrUtil.java b/hiver-modules/hiver-mall/src/main/java/cc/hiver/mall/utils/AliOcrUtil.java index a1abf995..11ccfa72 100644 --- a/hiver-modules/hiver-mall/src/main/java/cc/hiver/mall/utils/AliOcrUtil.java +++ b/hiver-modules/hiver-mall/src/main/java/cc/hiver/mall/utils/AliOcrUtil.java @@ -40,6 +40,7 @@ import java.util.regex.Pattern; @Component public class AliOcrUtil { + private static final Pattern COMPILED = Pattern.compile("..."); @Autowired private static AliOcrConfig aliOcrConfig; @@ -127,7 +128,7 @@ public class AliOcrUtil { // attributeList 有时候解析的不是json格式,特殊处理一下 String jsonStr = '[' + text.substring(startIndex, endIndex + 1) + ']'; // 有时候返回的是productCounts,改为productCount - jsonStr = fixProductCounts(jsonStr); + jsonStr = fixJsonStr(jsonStr); JSONArray endJson = new JSONArray(); try{ endJson = JSON.parseArray(jsonStr); @@ -192,7 +193,7 @@ public class AliOcrUtil { // attributeList 有时候解析的不是json格式,特殊处理一下 String errorJsonJsonStr = '[' + text.substring(secondStartIndex, secondEndIndex + 1) + ']'; // 有时候返回的是productCounts,改为productCount - errorJsonJsonStr = fixProductCounts(errorJsonJsonStr); + errorJsonJsonStr = fixJsonStr(errorJsonJsonStr); try{ // @@ -267,7 +268,7 @@ public class AliOcrUtil { }else{ String secondJsonStr = '[' + secondText.substring(secondStartIndex, secondEndIndex + 1) + ']'; // 有时候返回的是productCounts,改为productCount - secondJsonStr = fixProductCounts(secondJsonStr); + secondJsonStr = fixJsonStr(secondJsonStr); try{ final JSONArray secondJson = JSON.parseArray(secondJsonStr); final int secondResultCount = secondJson.size(); @@ -344,7 +345,7 @@ public class AliOcrUtil { // attributeList 有时候解析的不是json格式,特殊处理一下 String errorJsonJsonStr = '[' + secondText.substring(errorSsecondStartIndex, errorSecondEndIndex + 1) + ']'; // 有时候返回的是productCounts,改为productCount - errorJsonJsonStr = fixProductCounts(errorJsonJsonStr); + errorJsonJsonStr = fixJsonStr(errorJsonJsonStr); try{ // // endJson = JSON.parseArray(errorJsonJsonStr); @@ -450,14 +451,28 @@ public class AliOcrUtil { } /** - * 有时候返回的是productCounts,改为productCount + * 1.有时候返回的是productCounts,改为productCount + * 2. 有的时候中间会有...,需要特殊处理一下 * * @param str * @return String * @author 王富康 * @date 2024/5/12 */ - public static String fixProductCounts(String str) { + public static String fixJsonStr(String str) { + // 如果包含"...",那么就截取掉,在商品中不会出现,并且在json应该只会出现一次,如果会出现多次的话,那这里需要再次优化 + if(str.contains("...")){ + final String[] newStr = COMPILED.split(str); + final int newStrLength = newStr.length; + if(newStrLength ==2){ + // 第一个字符串从开头截取到第一个} + newStr[0] = newStr[0].substring(0, newStr[0].lastIndexOf('}')); + // 第二个字符串从第一个{截取到最后 + newStr[1] = newStr[1].substring(newStr[1].indexOf('{')); + // 重新拼接,并且中间加上逗号 + str = newStr[0]+ ',' +newStr[1]; + } + } str = str.replace("productCounts", "productCount"); return str; } @@ -690,11 +705,11 @@ public class AliOcrUtil { // orderId final Map purchaseDetailMap = new ConcurrentHashMap<>(); for (PurchaseOcrVo purchaseOcrVo : purchaseOcrVos) { - String productSn = purchaseOcrVo.getProductSn(); + final String productSn = purchaseOcrVo.getProductSn(); final String productName = purchaseOcrVo.getProductName(); // 根据商品的货号或者 - Product product = productSnMap.get(productSn); + final Product product = productSnMap.get(productSn); // 如果货号存在,就以货号为准查询库存商品能查到就是有,没有查到就是没有,如果有货号,不再根据名称查询 /*if (product == null && StringUtils.isEmpty(productSn)) { product = productNameMap.get(productName); @@ -771,6 +786,7 @@ public class AliOcrUtil { purchaseDetail.setCategoryId(product.getCategoryId()); purchaseDetail.setPrice(product.getPrice()); purchaseDetail.setWholesalePrice(product.getWholesalePrice()); + purchaseDetail.setProductName(product.getProductName()); stockLog.setProductId(product.getId()); } stockLogList.add(stockLog);