From eec6adb6ea0f1c3ea222edfed296cd8b22da96c8 Mon Sep 17 00:00:00 2001
From: sinake <sinake1@qq.com>
Date: 星期三, 11 十一月 2026 09:41:15 +0800
Subject: [PATCH] 发票上传修改

---
 ruoyi-admin/src/main/java/com/ruoyi/web/controller/project/ServiceInvoiceController.java |  100 +++++++++++++++++++++++++++++++++++++-------------
 1 files changed, 74 insertions(+), 26 deletions(-)

diff --git a/ruoyi-admin/src/main/java/com/ruoyi/web/controller/project/ServiceInvoiceController.java b/ruoyi-admin/src/main/java/com/ruoyi/web/controller/project/ServiceInvoiceController.java
index f90d12f..619c2d3 100644
--- a/ruoyi-admin/src/main/java/com/ruoyi/web/controller/project/ServiceInvoiceController.java
+++ b/ruoyi-admin/src/main/java/com/ruoyi/web/controller/project/ServiceInvoiceController.java
@@ -20,6 +20,8 @@
 import com.ruoyi.web.controller.tool.HttpUtils;
 import io.swagger.annotations.Api;
 import io.swagger.annotations.ApiOperation;
+import org.apache.commons.lang3.ObjectUtils;
+import org.apache.commons.lang3.StringUtils;
 import org.apache.http.HttpResponse;
 import org.apache.http.util.EntityUtils;
 import org.springframework.beans.factory.annotation.Autowired;
@@ -126,7 +128,7 @@
         String base64= Base64.getEncoder().encodeToString( Files.readAllBytes(filePath));
 
         AjaxResult ajax = AjaxResult.success();
-        ajax.put("OCRList", aliOCR(base64,0,0));
+        ajax.put("OCRList", aliOCR(base64,"0","0"));
         return ajax;
     }
 
@@ -134,7 +136,7 @@
      * 閫氱敤涓婁紶璇锋眰
      */
     @PostMapping("/uploadOCR")
-    public AjaxResult uploadOCR(@RequestParam("file") MultipartFile file, @RequestParam("userId") long userId, @RequestParam("caseId") long caseId) throws Exception {
+    public AjaxResult uploadOCR(@RequestParam("file") MultipartFile file, @RequestParam("userId") String userId, @RequestParam("caseId") String caseId, @RequestParam(value = "voucher", required = false) String voucher) throws Exception {
 
         try {
             // 涓婁紶鏂囦欢璺緞
@@ -164,34 +166,78 @@
             String fileName = FileUploadUtils.upload(filePath, file);
             String url1 = serverConfig.getUrl();
             String url = url1 + fileName;
-            String base64= Base64.getEncoder().encodeToString(file.getBytes());
-            List<Map<String,Object>> lists=aliOCR(base64,userId,caseId);
-            for(Map<String,Object> row : lists){
-                ServiceInvoice invoice=new ServiceInvoice();
-                invoice.setUserId(userId);
-                invoice.setCaseId(caseId);
-                invoice.setInvoiceType(row.get("type")+"");
-                invoice.setInvoiceNo(row.get("鍙戠エ鍙风爜")+"");
-                invoice.setOccurDate(row.get("寮�绁ㄦ棩鏈�")+"");
-                invoice.setInvoiceAmount(row.get("鍙戠エ閲戦")+"");
-                invoice.setSellerName(row.get("鍙楃エ鏂瑰悕绉�")+"");
-                invoice.setInvoiceUrl(url);
-                invoice.setInvoiceName(newFilename);
-                invoice.setExtContent(JSON.toJSONString(row));
-                serviceInvoiceService.save(invoice);
-            }
+            // 鍕鹃�夆�滃嚟璇佲�濓細涓嶈皟鐢� OCR锛岀洿鎺ョ敓鎴愪竴鏉$被鍨嬩负鈥滃嚟璇佲�濈殑璁板綍锛岄噾棰�/鏃ユ湡绛夊埌绗簩姝ユ墜宸ヨˉ褰�
+            if ("1".equals(voucher) || "true".equalsIgnoreCase(voucher)) {
+                ServiceInvoice voucherInvoice = new ServiceInvoice();
+                if (ObjectUtils.isNotEmpty(userId)) {
+                    voucherInvoice.setUserId(Long.parseLong(userId));
+                }
+                if (ObjectUtils.isNotEmpty(caseId)) {
+                    voucherInvoice.setCaseId(Long.parseLong(caseId));
+                }
+                String voucherFileName = newFilename.isEmpty() ? filename : newFilename;
+                int dotIndex = voucherFileName.lastIndexOf(".");
+                voucherInvoice.setInvoiceName(dotIndex > 0 ? voucherFileName.substring(0, dotIndex) : voucherFileName);
+                voucherInvoice.setInvoiceType("鍑瘉");
+                voucherInvoice.setInvoiceAmount("0");
+                voucherInvoice.setInvoiceUrl(url);
+                voucherInvoice.setCreateBy(userId);
+                voucherInvoice.setCreateTime(DateUtils.getNowDate());
+                voucherInvoice.setUpdateBy(userId);
+                voucherInvoice.setUpdateTime(DateUtils.getNowDate());
+                serviceInvoiceService.save(voucherInvoice);
 
-            AjaxResult ajax = AjaxResult.success();
-            ajax.put("fileName", fileName);
-            ajax.put("url", url);
-            ajax.put("OCRList", lists);
-            return ajax;
+                List<ServiceInvoice> voucherList = new ArrayList<>();
+                voucherList.add(voucherInvoice);
+                AjaxResult ajax = AjaxResult.success();
+                ajax.put("fileName", fileName);
+                ajax.put("url", url);
+                ajax.put("OCRList", new ArrayList<>());
+                ajax.put("invoiceList", voucherList);
+                return ajax;
+            }else {
+                String base64 = Base64.getEncoder().encodeToString(file.getBytes());
+                List<Map<String, Object>> lists = aliOCR(base64, userId, caseId);
+                logger.info(JSON.toJSONString(lists));
+                List<ServiceInvoice> invoiceList = new ArrayList<>();
+                for (Map<String, Object> row : lists) {
+                    ServiceInvoice invoice = new ServiceInvoice();
+                    if (ObjectUtils.isNotEmpty(userId)) {
+                        invoice.setUserId(Long.parseLong(userId));
+                    }
+                    if (ObjectUtils.isNotEmpty(caseId)) {
+                        invoice.setCaseId(Long.parseLong(caseId));
+                    }
+                    //invoice.setInvoiceType(row.get("type")+"");
+                    invoice.setInvoiceNo(row.get("鍙戠エ鍙风爜") + "");
+                    invoice.setOccurDate(row.get("寮�绁ㄦ棩鏈�") + "");
+                    invoice.setInvoiceAmount(row.get("鍙戠エ閲戦") + "");
+                    invoice.setSellerName(row.get("鍙楃エ鏂瑰悕绉�") + "");
+                    invoice.setInvoiceUrl(url);
+                    String invoiceName = newFilename.isEmpty() ? filename : newFilename;
+                    invoice.setInvoiceName(invoiceName.substring(0, invoiceName.lastIndexOf(".")));
+                    invoice.setExtContent(JSON.toJSONString(row));
+                    invoice.setCreateBy(userId);
+                    invoice.setCreateTime(DateUtils.getNowDate());
+                    invoice.setUpdateBy(userId);
+                    invoice.setUpdateTime(DateUtils.getNowDate());
+                    serviceInvoiceService.save(invoice);
+                    invoiceList.add(invoice);
+                }
+
+                AjaxResult ajax = AjaxResult.success();
+                ajax.put("fileName", fileName);
+                ajax.put("url", url);
+                ajax.put("OCRList", lists);
+                ajax.put("invoiceList", invoiceList);
+                return ajax;
+            }
         } catch (Exception e) {
             return AjaxResult.error(e.getMessage());
         }
     }
 
-    public List<Map<String,Object>> aliOCR(String base64,long userId,long caseId) {
+    public List<Map<String,Object>> aliOCR(String base64,String userId,String caseId) {
         String host = "https://mixedmulti.market.alicloudapi.com";
         String path = "/ocrservice/mixedMultiInvoice";
         String method = "POST";
@@ -207,6 +253,7 @@
             HttpResponse response = HttpUtils.doPost(host, path, method, headers, querys, bodys);
             //鑾峰彇response鐨刡ody
             String content= EntityUtils.toString(response.getEntity());
+            logger.info(content);
             JSONObject jsonContent=JSONObject.parseObject(content);
             JSONArray subMsgs=jsonContent.getJSONArray("subMsgs");
 
@@ -214,7 +261,7 @@
 
                 Map<String,Object> tempList=new HashMap<>();
                 JSONObject rowObject=(JSONObject)row;
-                tempList.put("鍙戠エ鍒嗙被",rowObject.getString("type"));
+
                 JSONObject result=((JSONObject)rowObject.get("result")).getJSONObject("data");
 
                 for(Map.Entry<String, Object> entry : result.entrySet()){
@@ -227,12 +274,13 @@
                 if(rowObject.getString("type").equals("鐏溅绁�")){
                     tempList.put("鍙戠エ閲戦",result.getString("绁ㄤ环"));
                 }
-
+                tempList.put("type",rowObject.getString("type"));
                 lists.add(tempList);
             }
 
             return lists;
         } catch (Exception e) {
+            logger.error(e.getMessage());
             return lists;
         }
     }

--
Gitblit v1.9.3