From 1dcd4d1a3a54cd8f92a1d8f19d2ca2908ddc7dbf Mon Sep 17 00:00:00 2001
From: whycxzp <perryhsu@163.com>
Date: 星期四, 21 七月 2022 17:25:48 +0800
Subject: [PATCH] 更新zip解析

---
 src/main/java/com/whyc/service/ProductBomApprovingService.java |  146 ++++++++++++++++++++++++++++++++++++++++++------
 1 files changed, 128 insertions(+), 18 deletions(-)

diff --git a/src/main/java/com/whyc/service/ProductBomApprovingService.java b/src/main/java/com/whyc/service/ProductBomApprovingService.java
index f6f5c96..4843e39 100644
--- a/src/main/java/com/whyc/service/ProductBomApprovingService.java
+++ b/src/main/java/com/whyc/service/ProductBomApprovingService.java
@@ -1,5 +1,6 @@
 package com.whyc.service;
 
+import com.whyc.dto.FileUrlDTO;
 import com.whyc.dto.Response;
 import com.whyc.mapper.ProductBomApprovingMapper;
 import com.whyc.pojo.DocUser;
@@ -7,7 +8,9 @@
 import com.whyc.pojo.WorksheetMain;
 import com.whyc.util.ActionUtil;
 import com.whyc.util.CommonUtil;
+import com.whyc.util.FileUtil;
 import com.whyc.util.Zip4jUtil;
+import io.swagger.models.auth.In;
 import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
 import org.apache.poi.ss.usermodel.*;
 import org.springframework.stereotype.Component;
@@ -20,7 +23,9 @@
 import java.io.FileOutputStream;
 import java.io.IOException;
 import java.io.InputStream;
+import java.text.DecimalFormat;
 import java.text.SimpleDateFormat;
+import java.util.ArrayList;
 import java.util.Date;
 import java.util.LinkedList;
 import java.util.List;
@@ -108,7 +113,8 @@
         mapper.insertBatchSomeColumn(bomList);
     }
 
-    public Response zipParse(MultipartFile file) throws IOException {
+    public Response zipParse(MultipartFile file) throws IOException, InvalidFormatException {
+        List<ProductBomApproving> list = new LinkedList<>();
         Response response = new Response();
         //妫�鏌ユ槸鍚︿负zip
         boolean isZip = Zip4jUtil.checkZipFileParam(file);
@@ -131,29 +137,133 @@
         Zip4jUtil.unPackZip(zipFile,null,filePath);
         //閬嶅巻瑙e帇鍚庣殑鏂囦欢澶硅矾寰�,瑙f瀽excel
         System.out.println(filePath);
-        File[] bomFileDirArr = parentFile.listFiles();
-        for (File bomFileDir : bomFileDirArr){
-            String bomFileDirName = bomFileDir.getName();
-            System.out.println(bomFileDirName);
-            File[] bomFileArr = bomFileDir.listFiles();
-            for (File bomFile : bomFileArr){
-                String bomFileName = bomFile.getName();
-                //瑙f瀽excel
-                if (bomFileName.contains(".xls")){
-                    System.out.println("寰呰В鏋愮殑excel鏂囦欢涓�:"+bomFileName);
+        List<String> fileList = new ArrayList<>();
+        //鍥剧焊绛涢��
+        List<FileUrlDTO> dwgExistsList = getDwgList(fileList);
+        fileList = FileUtil.getStaticFilePath(parentFile,fileList);
+        for (int i = 0; i < fileList.size(); i++) {
+            String fileTempUrl = fileList.get(i);
+            if(fileTempUrl.contains(".xls")){
+                File fileTemp = new File(fileTempUrl);
+                Workbook workbook = null;
+                workbook = WorkbookFactory.create(fileTemp);
+                List<? extends PictureData> allPictures = workbook.getAllPictures();
+                //鍙栫涓�涓猻heet琛�
+                Sheet sheet = workbook.getSheetAt(0);
+                int lastRowNum = sheet.getLastRowNum();
+                //鍙栫涓夎,骞朵互绗笁琛屽紑濮�
+                Row rowTemp = sheet.getRow(1);
+                short lastCellNum = rowTemp.getLastCellNum();
+                //鍏堝崟鐙幏鍙栦骇鍝佸瀷鍙峰拰鐗堟湰鍙�/鍒嗙被
+                Row row = sheet.getRow(1);
+                String type = row.getCell(1).getStringCellValue();
+
+                Row row2 = sheet.getRow(2);
+                String productModel = row2.getCell(5).getStringCellValue();
+                Row row3 = sheet.getRow(3);
+                String productVersion = row3.getCell(5).getStringCellValue();
+                for (int l = 9; l < lastRowNum-1; l++) {
+                    ProductBomApproving bomApproving = new ProductBomApproving();
+                    bomApproving.setParentModel(productModel);
+                    bomApproving.setParentVersion(productVersion);
+                    bomApproving.setType(type);
+
+                    for (int m = 2; m < lastCellNum; m++) {
+                        row = sheet.getRow(l);
+                        Cell cell = row.getCell(m);
+                        String cellValue = null;
+                        int cellValueInt = 0;
+                        Double cellValueDouble = null;
+                        if(m == 3){
+                            cellValueDouble = cell.getNumericCellValue();
+                            DecimalFormat decimalFormat = new DecimalFormat("0");
+                            cellValue = decimalFormat.format(cellValueDouble);
+                        }
+                        else if(m ==7){
+                            cellValueDouble = cell.getNumericCellValue();
+                            if(cellValueDouble.equals(0d)){
+                                cellValue = "";
+                            }else{
+                                cellValue = cellValueDouble.toString();
+                            }
+                        }
+                        else if(m==8){
+                            cellValueDouble = cell.getNumericCellValue();
+                        }
+                        else {
+                            cellValue = cell.getStringCellValue();
+                        }
+
+                        switch (m){
+                            case 2:{bomApproving.setCategory(cellValue);}break;
+                            case 3:{bomApproving.setSubCode(cellValue);}break;
+                            case 4:{bomApproving.setSubName(cellValue);}break;
+                            case 5:{
+                                bomApproving.setSubModel(cellValue);
+                                //鍒ゆ柇鍥剧焊鏌ョ湅鏄惁瀛樺湪
+                                bomApproving.setDwgExist(0);
+                                String dwgFileName = cellValue+".dwg";
+                                for (int n = 0; n < dwgExistsList.size(); n++) {
+                                    FileUrlDTO fileUrlDTO = dwgExistsList.get(n);
+                                    if(dwgFileName.equals(fileUrlDTO.getFileName())){
+                                        //瀛樺湪
+                                        bomApproving.setDwgExist(1);
+                                        bomApproving.setDwgUrl(fileUrlDTO.getHttpFileUrl());
+                                        break;
+                                    }
+                                }
+                            }break;
+                            case 6:{bomApproving.setMaterial(cellValue);}break;
+                            case 7:{bomApproving.setThickness(cellValue);}break;
+                            case 8:{bomApproving.setQuantity(cellValueDouble.intValue());}break;
+                            case 9:{bomApproving.setSurfaceDetail(cellValue);}break;
+                            case 10:{bomApproving.setNotes(cellValue);}break;
+                            case 11:{
+                                //鍥剧墖,浠�0寮�濮�,鍒板浘鐗噑ize涓烘
+                                int k = l-9;
+                                if(k<allPictures.size()){
+                                    PictureData pictureData = allPictures.get(k);
+                                    //鍥剧墖瀛樺偍 doc_file/product_approving/${username}/{dateFormat}/${timeStamp}
+                                    String suffix = pictureData.suggestFileExtension();
+                                    String picturePath = filePath + File.separator + bomApproving.getSubModel() + "." + suffix;
+                                    String picturePathFront = "doc_file" + File.separator + "product_approving" + File.separator + user.getName() + File.separator + dateFormat + File.separator + timeStamp  + File.separator + bomApproving.getSubModel() + "." + suffix;
+                                    byte[] data = pictureData.getData();
+                                    FileOutputStream fileOutputStream = null;
+                                    File pictureFile = new File(picturePath);
+                                    fileOutputStream = new FileOutputStream(pictureFile);
+                                    fileOutputStream.write(data);
+
+                                    bomApproving.setPictureUrl(picturePathFront);
+                                }
+                            }break;
+                        }
+                    }
+                    list.add(bomApproving);
                 }
             }
         }
         return null;
     }
 
+    private List<FileUrlDTO> getDwgList(List<String> fileList) {
+        List<FileUrlDTO> list = new LinkedList<>();
+        fileList.forEach(fileUrl->{
+            if(fileUrl.substring(fileUrl.lastIndexOf(".")+1).equals("dwg")) {
+                FileUrlDTO dto = new FileUrlDTO();
+                dto.setFileName(fileUrl.substring(fileUrl.lastIndexOf(File.separator) + 1));
+                dto.setHttpFileUrl(fileUrl.substring(fileUrl.lastIndexOf("doc_file" + File.separator + "product_approving")));
+                list.add(dto);
+            }
+        });
+        return list;
+    }
+
     public static void main(String[] args) throws IOException, InvalidFormatException {
-        File file = new File("C:\\Users\\29550\\Desktop\\褰撳墠椤圭洰\\202207鍥剧焊绠$悊\\0940000072(BOM).xls");
-        boolean exists = file.exists();
-        System.out.println(exists);
-        Workbook workbook = WorkbookFactory.create(file);
-        Sheet sheet = workbook.getSheetAt(0);
-        int lastRowNum = sheet.getLastRowNum();
-        System.out.println(lastRowNum);
+        String a = "C:\\Users\\29550\\Desktop\\褰撳墠椤圭洰\\202207娉板窞骞冲彴\\49娉板窞鏁版嵁搴撳浠�-鏈嶅姟鍣ㄤ粠windows鍒囨崲鍒癠buntu鏃剁殑鏁版嵁搴撳浠絓\db_alarm.sql";
+        String substring = a.substring(a.lastIndexOf(File.separator)+1);
+       String s2 = (a.substring(a.lastIndexOf("褰撳墠椤圭洰"+File.separator+"202207娉板窞骞冲彴")));
+
+        System.out.println(substring);
+        System.out.println(s2);
     }
 }

--
Gitblit v1.9.1