From 3ee1e956019390abd433dc53bc26176fff0cf87c Mon Sep 17 00:00:00 2001
From: whycxzp <perryhsu@163.com>
Date: 星期三, 31 八月 2022 15:57:29 +0800
Subject: [PATCH] 产品bom新增,zip解析

---
 src/main/java/com/whyc/service/ProductService.java |  188 +++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 188 insertions(+), 0 deletions(-)

diff --git a/src/main/java/com/whyc/service/ProductService.java b/src/main/java/com/whyc/service/ProductService.java
index 1c92412..3ae376e 100644
--- a/src/main/java/com/whyc/service/ProductService.java
+++ b/src/main/java/com/whyc/service/ProductService.java
@@ -5,12 +5,27 @@
 import com.baomidou.mybatisplus.core.toolkit.Wrappers;
 import com.github.pagehelper.PageHelper;
 import com.github.pagehelper.PageInfo;
+import com.whyc.dto.FileUrlDTO;
 import com.whyc.dto.Response;
 import com.whyc.mapper.ProductMapper;
+import com.whyc.pojo.DocUser;
 import com.whyc.pojo.Product;
+import com.whyc.pojo.ProductBom;
+import com.whyc.util.ActionUtil;
+import com.whyc.util.CommonUtil;
+import com.whyc.util.FileUtil;
+import com.whyc.util.Zip4jUtil;
+import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
+import org.apache.poi.ss.usermodel.*;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
+import org.springframework.web.multipart.MultipartFile;
 
+import java.io.*;
+import java.text.SimpleDateFormat;
+import java.util.ArrayList;
+import java.util.Date;
+import java.util.LinkedList;
 import java.util.List;
 
 @Service
@@ -75,4 +90,177 @@
     public void insert(Product product) {
         mapper.insert(product);
     }
+
+    public Response zipParse(MultipartFile file) throws IOException, InvalidFormatException {
+        List<ProductBom> list = new LinkedList<>();
+
+        Product product = new Product();
+        List<ProductBom> bomList = new LinkedList<>();
+        Response response = new Response();
+        //妫�鏌ユ槸鍚︿负zip
+        boolean isZip = Zip4jUtil.checkZipFileParam(file);
+        if (!isZip) {
+            return response.set(1, false, "涓婁紶鐨勬枃浠舵牸寮忎笉鏄痾ip");
+        }
+        //zip瀛樺偍璺緞:doc_file/product_submit/${username}/{dateFormat}/${timeStamp}
+        String rootFile = CommonUtil.getRootFile();
+        DocUser user = ActionUtil.getUser();
+        String dateFormat = new SimpleDateFormat("YYYY-MM").format(new Date());
+        long timeStamp = System.currentTimeMillis();
+        String filePath = rootFile + File.separator + "product_submit" + File.separator + user.getName() + File.separator + dateFormat + File.separator + timeStamp;
+        File parentFile = new File(filePath);
+        String originalFilename = file.getOriginalFilename();
+        File zipFile = new File(filePath + File.separator + originalFilename);
+        if (!zipFile.exists()) {
+            zipFile.mkdirs();
+        }
+        file.transferTo(zipFile);
+        //瑙e帇鏂囦欢澶�
+        Zip4jUtil.unPackZip(zipFile, null, filePath);
+        //閬嶅巻瑙e帇鍚庣殑鏂囦欢澶硅矾寰�,瑙f瀽excel
+        System.out.println(filePath);
+        List<String> fileList = new ArrayList<>();
+        //鍥剧焊绛涢��
+        fileList = FileUtil.getStaticFilePath(parentFile, fileList);
+        List<FileUrlDTO> dwgExistsList = getDwgList(fileList);
+        for (int i = 0; i < fileList.size(); i++) {
+            String fileTempUrl = fileList.get(i);
+            //鏌ヨ闇�涓婁紶鐨刡om鏁版嵁
+            if (fileTempUrl.contains(".xls") && fileTempUrl.contains("(BOM)")) {
+                Workbook workbook = null;
+                InputStream inputStream = new FileInputStream(new File(fileTempUrl));
+                workbook = WorkbookFactory.create(inputStream);
+                inputStream.close();
+                List<? extends PictureData> allPictures = workbook.getAllPictures();
+                //鍙栫涓�涓猻heet琛�
+                Sheet sheet = workbook.getSheetAt(0);
+                int lastRowNum = sheet.getLastRowNum();
+                //鍙栫涓夎,骞朵互绗笁琛屽紑濮�
+                Row row2 = sheet.getRow(1);
+                short lastCellNum = row2.getLastCellNum();
+                for (int k = 2; k < lastRowNum + 1; k++) {
+                    if(k==2){
+                        product.setParentCode(sheet.getRow(2).getCell(1).getStringCellValue());
+                        product.setParentName(sheet.getRow(2).getCell(2).getStringCellValue());
+                        product.setParentModel(sheet.getRow(2).getCell(3).getStringCellValue());
+                    }
+                    ProductBom bom = new ProductBom();
+                    for (int j = 0; j < lastCellNum; j++) {
+                        Row row = sheet.getRow(k);
+                        Cell cell = row.getCell(j);
+                        String cellValue = null;
+                        Double cellValueInt = 0d;
+                        if (j == 9 || j == 0) {
+                            cellValueInt = cell.getNumericCellValue();
+                        } else if (j != 15) {
+                            cellValue = cell.getStringCellValue();
+                        }
+                        switch (j) {
+                            case 0: {
+                                bom.setId(cellValueInt.intValue());
+                            }
+                            break;
+                            case 4: {
+                                bom.setCategory(cellValue);
+                            }
+                            break;
+                            case 5: {
+                                bom.setSubCode(cellValue);
+                            }
+                            break;
+                            case 6: {
+                                bom.setSubName(cellValue);
+                            }
+                            break;
+                            case 7: {
+                                bom.setSubModel(cellValue);
+                                //鍒ゆ柇鍥剧焊鏌ョ湅鏄惁瀛樺湪
+                                bom.setDwgExist(0);
+                                String dwgFileName = cellValue + ".dwg";
+                                for (int n = 0; n < dwgExistsList.size(); n++) {
+                                    FileUrlDTO fileUrlDTO = dwgExistsList.get(n);
+                                    if (dwgFileName.toUpperCase().equals(fileUrlDTO.getFileName().toUpperCase())) {
+                                        //瀛樺湪
+                                        bom.setDwgExist(1);
+                                        bom.setDwgUrl(fileUrlDTO.getHttpFileUrl());
+                                        break;
+                                    }
+                                }
+                            }
+                            break;
+                            case 8: {
+                                bom.setUnit(cellValue);
+                            }
+                            break;
+                            case 9: {
+                                bom.setQuantity(cellValueInt.intValue());
+                            }
+                            break;
+                            case 10: {
+                                bom.setProducer(cellValue);
+                            }
+                            break;
+                            case 11: {
+                                bom.setMaterial(cellValue);
+                            }
+                            break;
+                            case 12: {
+                                bom.setThickness(cellValue);
+                            }
+                            break;
+                            case 13: {
+                                bom.setSurfaceDetail(cellValue);
+                            }
+                            break;
+                            case 14: {
+                                bom.setNotes(cellValue);
+                            }
+                            break;
+                            case 15: {
+                                //鍥剧墖,浠�0寮�濮�,鍒板浘鐗噑ize涓烘
+                                int m = k - 2;
+                                if (m < allPictures.size()) {
+                                    PictureData pictureData = allPictures.get(m);
+                                    //鍥剧墖瀛樺偍 product_submit/username/2022-07/
+                                    String approvingPath = rootFile + File.separator + "product_submit" + File.separator + user.getName() + File.separator + dateFormat + File.separator + timeStamp;
+                                    File provingFile = new File(approvingPath);
+                                    if (!provingFile.exists()) {
+                                        provingFile.mkdirs();
+                                    }
+                                    String suffix = pictureData.suggestFileExtension();
+                                    String picturePath = approvingPath + File.separator + bom.getSubModel() + "." + suffix;
+                                    String picturePathFront = "doc_file" + File.separator + "product_submit" + File.separator + user.getName() + File.separator + dateFormat + File.separator + timeStamp + File.separator + bom.getSubModel() + "." + suffix;
+                                    byte[] data = pictureData.getData();
+                                    FileOutputStream fileOutputStream = null;
+                                    File pictureFile = new File(picturePath);
+                                    fileOutputStream = new FileOutputStream(pictureFile);
+                                    fileOutputStream.write(data);
+
+                                    bom.setPictureUrl(picturePathFront);
+                                }
+                            }
+                            break;
+                        }
+                    }
+                    bomList.add(bom);
+                }
+                product.setBomList(bomList);
+            }
+        }
+        return response.setII(1, true, product, filePath);
+
+    }
+
+    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;
+    }
 }

--
Gitblit v1.9.1