From dc2d4a83820f9ab2d58915133fc19ac83c36c43e Mon Sep 17 00:00:00 2001 From: whycxzp <perryhsu@163.com> Date: 星期六, 03 九月 2022 09:49:11 +0800 Subject: [PATCH] 物料提交更新 --- src/main/java/com/whyc/service/MaterialService.java | 143 +++++++++++++++++++++++++++++++++++++++++++++++ 1 files changed, 143 insertions(+), 0 deletions(-) diff --git a/src/main/java/com/whyc/service/MaterialService.java b/src/main/java/com/whyc/service/MaterialService.java index 37f4490..5979f31 100644 --- a/src/main/java/com/whyc/service/MaterialService.java +++ b/src/main/java/com/whyc/service/MaterialService.java @@ -6,17 +6,27 @@ import com.github.pagehelper.PageInfo; import com.whyc.dto.Response; import com.whyc.mapper.MaterialMapper; +import com.whyc.pojo.DocUser; import com.whyc.pojo.Material; 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.transaction.annotation.Transactional; import org.springframework.util.FileCopyUtils; +import org.springframework.web.multipart.MultipartFile; import java.io.File; +import java.io.FileOutputStream; import java.io.IOException; +import java.text.SimpleDateFormat; +import java.util.ArrayList; +import java.util.Date; import java.util.LinkedList; import java.util.List; import java.util.stream.Collectors; @@ -107,10 +117,12 @@ //鑾峰彇璺緞涓嬬殑鎵�鏈夋枃浠� List<String> fileUrlList = new LinkedList<>(); List<String> dwgUrlList = null; + List<String> picUrlList = null; List<String> materialUrlList = new LinkedList<>(); fileUrlList = FileUtil.getStaticFilePath(new File(fileUrl),fileUrlList); dwgUrlList = fileUrlList.stream().filter(url->url.contains(".dwg")).collect(Collectors.toList()); + picUrlList = fileUrlList.stream().filter(url->url.contains(".png") || url.contains(".jpeg")).collect(Collectors.toList()); //鍥剧焊鐨剈rl棰勮 dwgUrlList.forEach(dwgUrl-> { materialList.forEach(material -> { @@ -124,6 +136,19 @@ } }); }); + //鍥剧墖鐨剈rl棰勮 + //涓�瀹氭槸鏈夊搴旂墿鏂欑殑,浠巈xcel鍐呭墺绂荤殑 + picUrlList.forEach(picUrl->{ + for (Material material : materialList){ + String filename = picUrl.substring(picUrl.lastIndexOf(File.separator) + 1,picUrl.lastIndexOf(".")); + String fileFullName = picUrl.substring(picUrl.lastIndexOf(File.separator) + 1); + if(material.getSubModel().toUpperCase().equals(filename.toUpperCase())){ + material.setPictureUrl("doc_file" + File.separator + "material" + + File.separator + fileFullName); + break; + } + } + }); //鍥剧焊鏂囦欢杞Щ鍒版寮忔枃浠跺す String projectDir = CommonUtil.getProjectDir(); String materialDir = projectDir + File.separator + "doc_file" + File.separator + "material"; @@ -135,6 +160,14 @@ String dwgFullName = materialUrl.substring(materialUrl.lastIndexOf(File.separator) + 1); try { FileCopyUtils.copy(new File(materialUrl),new File(materialDir + File.separator + dwgFullName)); + } catch (IOException e) { + e.printStackTrace(); + } + }); + picUrlList.forEach(picUrl->{ + String picFullName = picUrl.substring(picUrl.lastIndexOf(File.separator) + 1); + try { + FileCopyUtils.copy(new File(picUrl),new File(materialDir+File.separator+picFullName)); } catch (IOException e) { e.printStackTrace(); } @@ -163,4 +196,114 @@ query.eq("sub_code",subCode).eq("sub_model",subModel).last(" limit 1"); return mapper.selectOne(query); } + + public Response zipParse(MultipartFile file) throws IOException, InvalidFormatException { + List<Material> list = new LinkedList<>(); + Response response = new Response(); + //妫�鏌ユ槸鍚︿负zip + boolean isZip = Zip4jUtil.checkZipFileParam(file); + if(!isZip){ + return response.set(1,false,"涓婁紶鐨勬枃浠舵牸寮忎笉鏄痾ip"); + } + //zip瀛樺偍璺緞:doc_file/material_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 + "material_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); + for (int i = 0; i < fileList.size(); i++) { + String fileTempUrl = fileList.get(i); + //鏌ヨ鍒皒ls鏁版嵁 + 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 = 8; l < lastRowNum-1; l++) { + Cell cellTemp = sheet.getRow(l).getCell(1); + cellTemp.setCellType(Cell.CELL_TYPE_STRING); + if(cellTemp.getStringCellValue().equals("")){ + break; + } + Material material = new Material(); + material.setCreateDate(new Date()); + + 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 || m==7){ + //cellValueDouble = cell.getNumericCellValue(); + //DecimalFormat decimalFormat = new DecimalFormat("0"); + //cellValue = decimalFormat.format(cellValueDouble); + cell.setCellType(Cell.CELL_TYPE_STRING); + } + cellValue = cell.getStringCellValue(); + + switch (m){ + case 2:{material.setCategory(cellValue);}break; + case 3:{material.setSubCode(cellValue);}break; + case 4:{material.setSubName(cellValue);}break; + case 5:{material.setSubModel(cellValue);}break; + case 6:{material.setMaterial(cellValue);}break; + case 7:{material.setThickness(cellValue);}break; + //case 8:{material.setProducer(cellValue);}break; + case 9:{material.setSurfaceDetail(cellValue);}break; + case 10:{material.setNotes(cellValue);}break; + case 11:{ + //鍥剧墖浠�1寮�濮�,0鏄痽c logo + PictureData pictureData = allPictures.get(l - 7); + String suffix = pictureData.suggestFileExtension(); + String picturePath = filePath + File.separator + material.getSubModel() + "." + suffix; + String picturePathFront = "doc_file" + File.separator + "material_submit" + File.separator + user.getName() + File.separator + dateFormat + File.separator + timeStamp + File.separator + material.getSubModel() + "." + suffix; + byte[] data = pictureData.getData(); + FileOutputStream fileOutputStream = null; + File pictureFile = new File(picturePath); + fileOutputStream = new FileOutputStream(pictureFile); + fileOutputStream.write(data); + + material.setPictureUrl(picturePathFront); + }break; + } + } + list.add(material); + } + } + } + for (Material material : list) { + if(material.getSubName()==null ||material.getSubName().equals("")){ + return response.set(1,false,"鎷掔粷瑙f瀽,excel鏂囦欢涓褰曞寘鍚悕绉颁负绌虹殑璁板綍"); + } + } + return response.setII(1,true,list,filePath); + } } -- Gitblit v1.9.1