From 9810791a41d381a10451f3e9770cfcfedf98e886 Mon Sep 17 00:00:00 2001
From: yangys <y_ys79@sina.com>
Date: 星期五, 22 八月 2025 00:39:34 +0800
Subject: [PATCH] 增加首页搜索接口
---
blade-service/blade-mdm/src/main/java/org/springblade/mdm/program/service/MdmProgramImportService.java | 369 ++++++++++++++++++++++++++++++++++++++++------------
1 files changed, 280 insertions(+), 89 deletions(-)
diff --git a/blade-service/blade-mdm/src/main/java/org/springblade/mdm/program/service/MdmProgramImportService.java b/blade-service/blade-mdm/src/main/java/org/springblade/mdm/program/service/MdmProgramImportService.java
index 3133dde..9318958 100644
--- a/blade-service/blade-mdm/src/main/java/org/springblade/mdm/program/service/MdmProgramImportService.java
+++ b/blade-service/blade-mdm/src/main/java/org/springblade/mdm/program/service/MdmProgramImportService.java
@@ -1,25 +1,24 @@
package org.springblade.mdm.program.service;
-import com.alibaba.fastjson.JSONArray;
-import com.alibaba.fastjson.JSONObject;
-import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.codec.digest.DigestUtils;
-import org.apache.commons.compress.archivers.zip.ZipArchiveEntry;
-import org.apache.commons.compress.archivers.zip.ZipFile;
-import org.apache.commons.compress.utils.SeekableInMemoryByteChannel;
+import org.apache.commons.io.FileUtils;
+import org.apache.commons.lang3.StringUtils;
+import org.springblade.core.log.exception.ServiceException;
+import org.springblade.core.mp.base.BizEntity;
import org.springblade.core.mp.base.BizServiceImpl;
-import org.springblade.core.tool.utils.FileUtil;
+import org.springblade.core.redis.cache.BladeRedis;
+import org.springblade.core.secure.utils.AuthUtil;
import org.springblade.core.tool.utils.Func;
-import org.springblade.core.tool.utils.IoUtil;
-import org.springblade.mdm.flow.service.CureFlowService;
-import org.springblade.mdm.program.entity.NcProgram;
+import org.springblade.mdm.basesetting.machine.MachineService;
+import org.springblade.mdm.basesetting.machine.entity.Machine;
import org.springblade.mdm.program.entity.NcProgramExchange;
import org.springblade.mdm.program.mapper.NcProgramExchangeMapper;
-import org.springblade.mdm.program.vo.DncSendBackData;
import org.springblade.mdm.program.vo.MdmProgramImportVO;
-import org.springblade.mdm.utils.CustomBinaryReader;
+import org.springblade.mdm.utils.FileContentUtil;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.scheduling.support.SimpleTriggerContext;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
@@ -27,68 +26,140 @@
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
-import java.time.LocalDateTime;
+import java.time.Duration;
import java.util.*;
-
+import java.nio.file.*;
+import java.util.zip.ZipEntry;
+import java.util.zip.ZipInputStream;
/**
* MDM绋嬪簭瀵煎叆锛堝伐鎺х綉鍔熻兘锛�
- *
+ * 鐩墠宸ユ帶缃戝睍鐜板舰寮忔湭瀹氾紝鏆傛椂瀹炵幇鏆傚仠
* @author yangys
*/
@Slf4j
@Service
-@AllArgsConstructor
public class MdmProgramImportService extends BizServiceImpl<NcProgramExchangeMapper, NcProgramExchange> {
- private final CureFlowService cureFlowService;
- private final NcProgramService ncProgramService;
+
+ @Autowired
+ private BladeRedis bladeRedis;
+
+ @Autowired
+ private ProgramAnnotationService programAnnotationService;
+
+ @Autowired
+ private MachineService machineService;
+
+ private String getFileKey(){
+ return "mdmgkwimpfile-"+ AuthUtil.getUserId();
+ }
/**
- * MDM瀵煎叆鏂囦欢涓婁紶
+ * 宸ユ帶MDM瀵煎叆鏂囦欢涓婁紶
* @param file MDM娑夊瘑缃戝鍑烘枃浠�
* @return
*/
public List<MdmProgramImportVO> mdmImportUpload(MultipartFile file) {
- List<MdmProgramImportVO> list;
+ List<MdmProgramImportVO> list = new ArrayList<>();
try {
- //String fileName = file.getOriginalFilename();
- //InputStream zipFileInputStream = FileExchangeUtil.convertFileToZip(file.getInputStream());
+ if(file == null || file.isEmpty()){
+ throw new ServiceException("鏂囦欢涓虹┖");
+ }
+
+ if(!StringUtils.endsWith(file.getOriginalFilename(),".zip")){
+ throw new ServiceException("鏂囦欢蹇呴』涓簔ip鍖�");
+ }
+ String tempDir = System.getProperty("java.io.tmpdir");
+ Path tempPath = Paths.get(tempDir);
+
+ // 鍒涘缓瑙e帇鐩爣鐩綍锛堝湪涓存椂鐩綍涓嬪垱寤轰竴涓敮涓�瀛愮洰褰曪級
+ Path tempZipFile = Files.createTempFile("mdmimpfile-"+System.currentTimeMillis(), ".zip");
+ //Path extractDir = Files.createTempDirectory(tempPath, "unzip_");
+
+ file.transferTo(tempZipFile);
+
+ // 鍒涘缓瑙e帇鐩爣鐩綍锛堝湪涓存椂鐩綍涓嬪垱寤轰竴涓敮涓�瀛愮洰褰曪級
+ Path extractDir = Files.createTempDirectory(tempPath, "unzip_");
+
+ bladeRedis.setEx(getFileKey(),extractDir.toString(), Duration.ofHours(2));
+
+ extractZipToTempDir(tempZipFile,extractDir);
+
+ //璇诲彇鏂囦欢鐩綍
+ /*
+ BladeFile bfile = ossTemplate.putFile(file);//涓婁紶锛屼緵鍚庣画鍏ュ簱浣跨敤
+
+ //璁剧疆涓�涓紦瀛橈紝2灏忔椂杩囨湡
InputStream zipFileInputStream = file.getInputStream();//test
byte[] bytes = FileUtil.copyToByteArray(zipFileInputStream);
list = parseMdmZipFromByteArray(bytes);
-
-
+ */
+ list = readTempDir(extractDir);
} catch (IOException e) {
- log.error("涓婁紶dnc鍥炰紶鏂囦欢澶辫触",e);
- list = Collections.emptyList();
+ log.error("瀵煎叆娑夊瘑缃戞憜娓℃枃浠跺け璐�",e);
+ throw new ServiceException("瑙f瀽DNC鍥炰紶鏁版嵁澶辫触");
}
return list;
}
- InputStream convertFileToZip(InputStream inputStream) throws IOException {
+ public void extractZipToTempDir(Path zipFilePath,Path extractDir) throws IOException {
+ // 鑾峰彇绯荤粺涓存椂鐩綍
+ String tempDir = System.getProperty("java.io.tmpdir");
+ Path tempPath = Paths.get(tempDir);
- File tempFile = createTempFile();
- FileOutputStream fos = new FileOutputStream(tempFile);
- CustomBinaryReader.read(inputStream,fos);
+ // 鍒涘缓瑙e帇鐩爣鐩綍锛堝湪涓存椂鐩綍涓嬪垱寤轰竴涓敮涓�瀛愮洰褰曪級
+ //Path extractDir = Files.createTempDirectory(tempPath, "unzip_");
+ System.out.println("瑙e帇鐩綍: " + extractDir.toString());
+ //Files.newInputStream(Paths.get(zipFilePath));
+ try (InputStream fis = Files.newInputStream(zipFilePath);
+ ZipInputStream zis = new ZipInputStream(fis)) {
+ ZipEntry zipEntry = zis.getNextEntry();
+ while (zipEntry != null) {
+ Path newPath = zipSlipProtect(zipEntry, extractDir);
- FileInputStream dInstream = new FileInputStream(tempFile);
+ if (zipEntry.isDirectory()) {
+ Files.createDirectories(newPath);
+ } else {
+ // 纭繚鐖剁洰褰曞瓨鍦�
+ if (newPath.getParent() != null) {
+ Files.createDirectories(newPath.getParent());
+ }
- return dInstream;
+ // 鍐欏叆鏂囦欢
+ try (OutputStream fos = Files.newOutputStream(newPath)) {
+ byte[] buffer = new byte[1024];
+ int len;
+ while ((len = zis.read(buffer)) > 0) {
+ fos.write(buffer, 0, len);
+ }
+ }
+ }
+ zipEntry = zis.getNextEntry();
+ }
+ zis.closeEntry();
+ }
}
/**
- * 鍒涘缓涓�涓复鏃舵枃浠�
+ * 闃叉ZIP Slip鏀诲嚮
+ * @param zipEntry
+ * @param targetDir
* @return
* @throws IOException
*/
- File createTempFile() throws IOException {
- Path tempDir = Paths.get(System.getProperty("java.io.tmpdir"));
- // 鍦ㄤ复鏃剁洰褰曚腑鍒涘缓鏂囦欢
- String tfilename = "t"+System.currentTimeMillis();
- Path tempFile = Files.createTempFile(tempDir, tfilename, ".tmp");
- System.out.println("鍒涘缓鐨勪复鏃舵枃浠�: " + tempFile);
- return tempFile.toFile();
+ Path zipSlipProtect(ZipEntry zipEntry, Path targetDir) throws IOException {
+ Path targetDirResolved = targetDir.resolve(zipEntry.getName());
+
+ // 瑙勮寖鍖栬矾寰�
+ Path normalizePath = targetDirResolved.normalize();
+
+ if (!normalizePath.startsWith(targetDir)) {
+ throw new IOException("鎭舵剰ZIP鏉$洰: " + zipEntry.getName());
+ }
+
+ return normalizePath;
}
+ /*
public static List<MdmProgramImportVO> parseMdmZipFromByteArray(byte[] zipData) throws IOException {
List<MdmProgramImportVO> list = new ArrayList<>();
Map<String,String> fileMd5Map = new HashMap<>();
@@ -105,41 +176,7 @@
if (!entry.isDirectory()) {
//鐩存帴瑙f瀽绋嬪簭鐨刯son鏂囦欢
- if(entryName.equals(NcProgramExportDNCService.PROGRAM_JSON_FILE)){
- try (InputStream inputStream = zipFile.getInputStream(entry)) {
- String jsonStr = IoUtil.readToString(inputStream);
-
- JSONArray jsonArray = JSONArray.parseArray(jsonStr);
- for(int i=0;i<jsonArray.size();i++){
- JSONObject jsonObject = jsonArray.getJSONObject(i);
- MdmProgramImportVO d = new MdmProgramImportVO();
- d.setName(jsonObject.getString("name"));
- d.setId(jsonObject.getLong("id"));
- d.setCode(jsonObject.getString("code"));
- d.setDescription(jsonObject.getString("description"));
- d.setCategory(jsonObject.getString("category"));
- d.setBindNcNodeId(jsonObject.getLong("bindNcNodeId"));
- d.setIsLastEdition(jsonObject.getInteger("isLastEdition"));
- d.setIsLocked(jsonObject.getInteger("isLocked"));
- d.setIsTest(jsonObject.getInteger("isTest"));
- d.setMachineCode(jsonObject.getString("machineCode"));
- d.setNcNodeId(jsonObject.getLong("ncNodeId"));
- d.setPartNo(jsonObject.getString("partNo"));
- d.setProcessEdition(jsonObject.getString("processEdition"));
-
- fileDataMap.put(d.getName(),d);
-
- list.add(d);
- }
-
- }
- }else{
- try (InputStream inputStream = zipFile.getInputStream(entry)) {
- fileMd5Map.put(entryName,DigestUtils.md5Hex(inputStream));//鑾峰彇鏂囦欢MD5
- }
-
- }
}
@@ -154,32 +191,186 @@
});
return list;
}
+ */
+ public List<MdmProgramImportVO> readTempDir(Path extractDir) throws IOException {
+ List<MdmProgramImportVO> list = new ArrayList<>();
+
+ List<Machine> machines = machineService.lambdaQuery().eq(Machine::getStatus,Machine.STATUS_ENABLE).list();
+ //璇诲彇鎵�鏈夋枃浠跺す
+ //List<Path> dirs = Files.list(extractDir).filter(Files::isDirectory).toList();
+ try (DirectoryStream<Path> stream = Files.newDirectoryStream(extractDir)) {
+ for (Path path : stream) {
+ if (Files.isDirectory(path)) {
+
+
+
+ // 濡傛灉鏄瓙鐩綍锛岃鍙栧叾涓殑鏂囦欢
+ try (DirectoryStream<Path> subStream = Files.newDirectoryStream(path)) {
+ for (Path subPath : subStream) {
+ if (Files.isRegularFile(subPath)) {
+ System.out.println("鎵惧埌鏂囦欢: " + subPath);
+ //files.add(subPath.getFileName().toString());
+ // 杩欓噷鍙互娣诲姞瀵规枃浠剁殑澶勭悊閫昏緫
+ }
+ }
+ }
+ //vo.setFiles(files);
+
+ } else if (Files.isRegularFile(path)) {
+ System.out.println("鎵惧埌鏂囦欢2: " + path);
+ //杩欓噷 鎵惧埌鐨勬枃浠朵笉鏄�
+ MdmProgramImportVO vo = new MdmProgramImportVO();
+ vo.setFilename(path.getFileName().toString());
+ vo.setDrawingNo(parseDrawingNo(vo.getFilename()));
+
+ try (InputStream inputStream = Files.newInputStream(path)) {
+ // 浣跨敤杈撳叆娴佽鍙栨枃浠跺唴瀹�
+ byte[] buffer = new byte[2000];
+ inputStream.read(buffer);
+ vo.setMd5(DigestUtils.md5Hex(buffer));
+
+
+ } catch (IOException e) {
+ log.error("璇诲彇鏂囦欢md5澶辫触",e);
+ }
+
+
+ try (InputStream inputStream = Files.newInputStream(path, StandardOpenOption.READ)) {
+ // 浣跨敤杈撳叆娴佽鍙栨枃浠跺唴瀹�
+ ByteArrayInputStream bas = new ByteArrayInputStream(inputStream.readAllBytes());
+ String line1 = FileContentUtil.readLineAt(bas,1);
+ //bas.mark(0);
+ bas.reset();
+ String line2 = FileContentUtil.readLineAt(bas,2);
+ System.out.println("line1="+line1);
+ Machine matchedMachine = null;
+ for (Machine machine : machines) {
+ if(Func.isNotBlank(machine.getProgSendDir()) && line1.contains(machine.getProgSendDir())){
+ matchedMachine = machine;
+ break;
+ }
+
+ }
+ if (matchedMachine != null) {
+ vo.setName(parseProgramName(vo.getFilename()));
+ vo.setMachineCode(matchedMachine.getCode());
+ //String sendPath = programAnnotationService.removeAnnotation(matchedMachine.getMachineGroupCode(),line1,"(",")");
+ vo.setFullPath(path.toString());//鏂囦欢鍦板潃
+ vo.setSendPath(matchedMachine.getProgSendDir());
+ vo.setId(vo.getFullPath());
+ vo.setProgramStatus(programAnnotationService.removeAnnotation(matchedMachine.getMachineGroupCode(),line2,"(",")"));
+
+ list.add(vo);
+ }
+
+ } catch (IOException e) {
+ log.error("璇诲彇鏂囦欢md5澶辫触",e);
+ }
+
+
+ //List<String> files = new ArrayList<>();
+
+
+ // 杩欓噷鍙互娣诲姞瀵规枃浠剁殑澶勭悊閫昏緫
+ }
+ }
+ }
+
+
+ return list;
+ }
+
+
+ /**
+ * 瑙f瀽鍑洪浂缁勪欢濂�
+ * @param filename
+ * @return
+ */
+ String parseDrawingNo(String filename){
+ String drawingNo = "";
+ int idx = filename.lastIndexOf("-");
+ String temp;
+ if(idx != -1){
+ temp = filename.substring(0,idx);
+
+ idx = temp.lastIndexOf("-");
+ if(idx != -1){
+ temp = temp.substring(0,idx);
+
+ //鍘绘帀宸ュ簭鐗堟
+ idx = temp.lastIndexOf("-");
+ if(idx != -1){
+ temp = temp.substring(0,idx);
+
+ //鍘绘帀宸ュ簭鍙�
+ idx = temp.lastIndexOf("-");
+ if(idx != -1){
+ drawingNo = temp.substring(0,idx);
+ }
+ }
+ }
+ //浠ヤ笂鍘绘帀浜嗘渶鍚�2娈垫鏁板拰娈靛彿
+
+
+ }
+ return drawingNo;
+ }
+
+ String parseProgramName(String filename){
+ String programName = "";
+ int idx = filename.lastIndexOf("-");
+ String temp;
+ if(idx != -1){
+ temp = filename.substring(0,idx);
+
+ idx = temp.lastIndexOf("-");
+ if(idx != -1){
+ temp = temp.substring(0,idx);
+
+ //鍘绘帀宸ュ簭鐗堟
+ idx = temp.lastIndexOf("-");
+ if(idx != -1){
+ programName = temp.substring(0,idx);
+ }
+ }
+ //浠ヤ笂鍘绘帀浜嗘渶鍚�2娈垫鏁板拰娈靛彿
+
+
+ }
+ return programName;
+ }
/**
* 鍏ュ簱mdm娑夊瘑缃戞枃浠�
* @param ids id鍒楄〃閫楀彿鍒嗛殧
* @return
*/
- public void mdmFileAccept(String ids) {
- /*
- List<Long> idList = Func.toLongList(ids);
- List<NcProgram> progList = ncProgramService.listByIds(idList);
- NcProgramExchange exchange;
- //NcProgram program;
- //NcNode programNode;
+ public void mdmFileAccept(String ids) throws IOException {
- for(NcProgram prog:progList){
- exchange = new NcProgramExchange();
- exchange.setName(prog.getName());
- exchange.setExchangeType(2);//鍥炰紶
- exchange.setNcProgramId(prog.getId());
+ List<String> idList = Func.toStrList(ids);
- this.save(exchange);
+ String ditStr = bladeRedis.get(getFileKey());
+ Path extractDir = Paths.get(ditStr);
+ List<MdmProgramImportVO> list = readTempDir(extractDir);
+
+ String destFileFull;
+ for(String str : idList){
+ System.out.println(str);
+ for(MdmProgramImportVO vo : list){
+ if(StringUtils.equals(vo.getFullPath(),str)){
+ destFileFull = vo.getSendPath()+File.separator+vo.getFilename();
+ File destFile = new File(destFileFull);
+ FileUtils.forceMkdirParent(destFile);
+ FileUtils.copyFile(new File(str),destFile);
+ break;
+ }
+ }
}
- //鐩存帴鍏ュ簱锛屾棤娴佺▼
- */
+
}
+
+
}
--
Gitblit v1.9.3