From 4c7296d45efe849dc70a3b2e2240c905481a91c9 Mon Sep 17 00:00:00 2001
From: yangys <y_ys79@sina.com>
Date: 星期一, 29 九月 2025 17:44:46 +0800
Subject: [PATCH] 工控网涉密网联调
---
blade-service/blade-mdm/src/main/java/org/springblade/mdm/program/service/MdmProgramImportService.java | 197 ++++++++++++++++++++++++++++--------------------
1 files changed, 115 insertions(+), 82 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 f5ac1a9..6f91428 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
@@ -4,7 +4,6 @@
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.io.FileUtils;
-import org.apache.commons.io.FilenameUtils;
import org.apache.commons.lang3.StringUtils;
import org.springblade.core.log.exception.ServiceException;
import org.springblade.core.mp.base.BizServiceImpl;
@@ -15,28 +14,32 @@
import org.springblade.core.tool.utils.Func;
import org.springblade.mdm.basesetting.machine.service.MachineService;
import org.springblade.mdm.basesetting.machine.entity.Machine;
-import org.springblade.mdm.commons.contants.RegExpConstants;
+import org.springblade.mdm.commons.contants.ZipConstants;
import org.springblade.mdm.gkw.programnode.vo.ProgramNameVO;
+import org.springblade.mdm.gkw.task.entity.MachineBackTask;
+import org.springblade.mdm.gkw.task.service.MachineBackTaskService;
import org.springblade.mdm.machinefile.entity.FileSendRecord;
import org.springblade.mdm.machinefile.service.FileSendRecordService;
import org.springblade.mdm.program.entity.NcProgramExchange;
import org.springblade.mdm.program.mapper.NcProgramExchangeMapper;
-import org.springblade.mdm.program.service.programannotation.AnnotationProperties;
+import org.springblade.mdm.program.service.programannotation.*;
import org.springblade.mdm.program.vo.MdmProgramImportVO;
import org.springblade.mdm.utils.FileContentUtil;
import org.springblade.mdm.utils.ProgramFileNameParser;
+import org.springblade.system.pojo.entity.DictBiz;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import java.io.*;
+import java.nio.charset.Charset;
+import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.time.Duration;
import java.util.*;
import java.nio.file.*;
-import java.util.regex.Matcher;
import java.util.stream.Stream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
@@ -58,6 +61,11 @@
private FileSendRecordService fileSendRecordService;
@Autowired
private OssTemplate ossTemplate;
+ @Autowired
+ private AnnotationProcessorHelper annotationProcessorHelper;
+ @Autowired
+ private MachineBackTaskService machineBackTaskService;
+
private String getFileKey(){
return "mdmgkwimpfile-"+ AuthUtil.getUserId();
}
@@ -85,14 +93,8 @@
Path tempZipFile = Files.createTempFile("mdmimpfile-"+System.currentTimeMillis(), ".zip");
file.transferTo(tempZipFile);
extractZipToTempDir(tempZipFile,extractDir);
-
-
}else{
//鏅�氭枃浠讹紝鐩存帴鏀惧叆extract鏂囦欢澶�
- String basename = FilenameUtils.getBaseName(file.getOriginalFilename());
- String ext = FilenameUtils.getExtension(file.getOriginalFilename());
- //Path filepath = Files.createTempFile(extractDir,basename,StringUtils.isEmpty(ext) ? "": "."+ext);
- //Path filepath = Files.createFile();
file.transferTo(Paths.get(extractDir.toString()+File.separator+file.getOriginalFilename()));
}
@@ -111,13 +113,24 @@
* @param extractDir 鐩爣鐩綍
* @throws IOException 鏂囦欢鎿嶄綔寮傚父
*/
- public void extractZipToTempDir(Path zipFilePath,Path extractDir) throws IOException {
+ public void extractZipToTempDir(Path zipFilePath,Path extractDir) throws IOException{
+ for (String encoding : ZipConstants.TRY_ENCODINGS) {
+ try {
+ extractZipToTempDirWithCharset(zipFilePath,extractDir,Charset.forName(encoding));
+ log.error("浣跨敤缂栫爜 {} 瑙f瀽鎴愬姛 ",encoding);
+ break;
+ } catch (Exception e) {
+ log.error("浣跨敤缂栫爜 {} 瑙f瀽澶辫触: ",encoding,e);
+ }
+ }
+ }
+ public void extractZipToTempDirWithCharset(Path zipFilePath, Path extractDir, Charset charset) throws IOException {
// 鑾峰彇绯荤粺涓存椂鐩綍
String tempDir = System.getProperty("java.io.tmpdir");
// 鍒涘缓瑙e帇鐩爣鐩綍锛堝湪涓存椂鐩綍涓嬪垱寤轰竴涓敮涓�瀛愮洰褰曪級
try (InputStream fis = Files.newInputStream(zipFilePath);
- ZipInputStream zis = new ZipInputStream(fis)) {
+ ZipInputStream zis = new ZipInputStream(fis,charset)) {
ZipEntry zipEntry = zis.getNextEntry();
while (zipEntry != null) {
@@ -177,8 +190,7 @@
try (Stream<Path> paths = Files.walk(extractDir)) {
List<Path> filePathList = paths
.filter(Files::isRegularFile).toList(); // 鍙繚鐣欐櫘閫氭枃浠讹紝鎺掗櫎鐩綍
- //.collect(Collectors.toList());
- //System.out.println("鎵�鏈夋枃浠�"+filePathList);
+
for(Path path : filePathList){
list.add(readFileToVO(path));
}
@@ -205,19 +217,47 @@
}*/
return list;
}
+ public static byte[] getUTF8BytesFromGBKString(String gbkStr) {
+ int n = gbkStr.length();
+ byte[] utfBytes = new byte[3 * n];
+ int k = 0;
+ for (int i = 0; i < n; i++) {
+ int m = gbkStr.charAt(i);
+ if (m < 128 && m >= 0) {
+ utfBytes[k++] = (byte) m;
+ continue;
+ }
+ utfBytes[k++] = (byte) (0xe0 | (m >> 12));
+ utfBytes[k++] = (byte) (0x80 | ((m >> 6) & 0x3f));
+ utfBytes[k++] = (byte) (0x80 | (m & 0x3f));
+ }
+ if (k < utfBytes.length) {
+ byte[] tmp = new byte[k];
+ System.arraycopy(utfBytes, 0, tmp, 0, k);
+ return tmp;
+ }
+ return utfBytes;
+ }
+ ProgramNameVO tryParseProgramName(String fiilename){
+ ProgramNameVO pnmameVO = ProgramFileNameParser.parseProgramName(fiilename);//鏍囧噯utf8缂栫爜
+ if(pnmameVO.getDrawingNo() == null) {
+ //浣跨敤GBK缂栫爜瑙f瀽
+ pnmameVO = ProgramFileNameParser.parseProgramName(new String(getUTF8BytesFromGBKString(fiilename), StandardCharsets.UTF_8));
+ }
+ return pnmameVO;
+ }
/**
* 灏嗘枃浠剁粍缁囨垚VO
* @param path 鏂囦欢path
* @return vo
*/
- MdmProgramImportVO readFileToVO(Path path){
+ MdmProgramImportVO readFileToVO(Path path) throws UnsupportedEncodingException {
MdmProgramImportVO vo = new MdmProgramImportVO();
vo.setFilename(path.getFileName().toString());
- ProgramNameVO pnmameVO = ProgramFileNameParser.parseProgramName(vo.getFilename());
- vo.setDrawingNo(pnmameVO.getDrawingNo());
- //vo.setDrawingNo(parseDrawingNo(vo.getFilename()));
+ ProgramNameVO progNmameVO = tryParseProgramName(vo.getFilename());
+ vo.setDrawingNo(progNmameVO.getDrawingNo());
try (InputStream inputStream = Files.newInputStream(path)) {
// 浣跨敤杈撳叆娴佽鍙栨枃浠跺唴瀹�
@@ -234,7 +274,7 @@
AnnotationProperties defAnnoProperties = AnnotationProperties.getDefault();
String sendPathLine = FileContentUtil.readLineAt(bas,defAnnoProperties.getSendPathLineIndex());
- //bas.mark(0);
+
bas.reset();
String statusLine = FileContentUtil.readLineAt(bas,defAnnoProperties.getStatusLineIndex());
log.info("sendPathLine={}", sendPathLine);
@@ -242,8 +282,7 @@
Machine matchedMachine = machineService.getMachineBySendPathAnnotation(sendPathLine);
if (matchedMachine != null) {
- //vo.setName(parseProgramName(vo.getFilename()));
- vo.setName(pnmameVO.logicProgramName());
+ vo.setName(progNmameVO.logicProgramName());
vo.setMachineCode(matchedMachine.getCode());
vo.setFullPath(path.toString());//鏂囦欢鍦板潃
@@ -257,74 +296,14 @@
}
return vo;
}
- /**
- * 瑙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鍒楄〃閫楀彿鍒嗛殧
*/
public void mdmFileAccept(String ids) throws IOException {
+ List<FileSendRecord> importedRecords = new ArrayList<>();
List<String> idList = Func.toStrList(ids);
String dictStr = bladeRedis.get(getFileKey());
@@ -334,6 +313,7 @@
Path extractDir = Paths.get(dictStr);
List<MdmProgramImportVO> list = readTempDir(extractDir);
+ //List<DictBiz> annoDictList = programAnnotationService.getAnnotionDictList();
String destFileFull;
for(String str : idList){
Optional<MdmProgramImportVO> optVO = list.stream().filter(vo -> vo.getId().equals(str)).findFirst();
@@ -346,6 +326,7 @@
destFileFull = vo.getSendPath()+File.separator+vo.getFilename();
File destFile = new File(destFileFull);
+
FileUtils.forceMkdirParent(destFile);
FileUtils.copyFile(new File(str),destFile);
@@ -359,8 +340,60 @@
BladeFile bFile = ossTemplate.putFile(record.getName(), inputStream);
record.setOssName(bFile.getName());
}
+
+ try(InputStream inputStream = ossTemplate.statFileStream(record.getOssName())){
+ //璇诲彇绋嬪簭鐘舵��
+ Machine machine = this.machineService.getByCode(record.getMachineCode());
+ AnnotationProcessor processor = annotationProcessorHelper.getProcessor(machine.getControlSystem());
+ AnnotationData annotationData = processor.readAnnotationData(inputStream);
+
+ record.setDeviation(annotationData.getDeviation());
+ ProgramNameVO nameVO = ProgramFileNameParser.parseProgramName(record.getName());
+ record.setDeviationSerial(nameVO.lgSerial());
+
+ record.setProgramStatus(annotationData.getProgramStatus());
+ }
fileSendRecordService.save(record);
+
+ importedRecords.add(record);
}
+ machineBackTaskService.saveBatch(parseMachineBackTask(importedRecords));
+ }
+
+ /**
+ * 鍒涘缓鏈哄簥鍥炰紶浠诲姟
+ * @param importedRecords 瀵煎叆璁板綍鍒楄〃
+ */
+ List<MachineBackTask> parseMachineBackTask(List<FileSendRecord> importedRecords){
+ List<FileSendRecord> recList = importedRecords.stream().filter(r -> AnnotationUtil.SQ.equals(r.getProgramStatus())
+ || AnnotationUtil.LG.equals(r.getProgramStatus())).toList();
+
+ Map<String,FileSendRecord> map = new HashMap<>();
+ for(FileSendRecord record : recList){
+ ProgramNameVO nameVO = ProgramFileNameParser.parseProgramName(record.getName());
+ String key = record.getMachineCode()+","+nameVO.logicProgramName();
+
+ map.put(key,record);
+ }
+
+ List<MachineBackTask> backTasks = new ArrayList<>();
+ for(String key : map.keySet()){
+ MachineBackTask task = new MachineBackTask();
+ task.setTaskType(MachineBackTask.TASK_TYPE_PROGRAM);
+
+ FileSendRecord record = map.get(key);
+ task.setMachineCode(record.getMachineCode());
+
+ ProgramNameVO nameVO = ProgramFileNameParser.parseProgramName(record.getName());
+ task.setDeviation(record.getDeviation());
+ task.setDeviationSerial(record.getDeviationSerial());
+
+ task.setProgramName(nameVO.logicProgramName());
+ task.setSegCount(nameVO.getSegmentCount());
+ backTasks.add(task);
+ }
+ //鎻愬彇绋嬪簭鍖呭悕锛屾満搴婄紪鐮侀泦鍚�
+ return backTasks;
}
}
--
Gitblit v1.9.3