yangys
2025-08-04 1130aee058b7f47f2df56ebd0e2934392102c354
blade-service/blade-mdm/src/main/java/org/springblade/mdm/program/service/MdmProgramImportService.java
@@ -9,6 +9,8 @@
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.springblade.core.log.exception.ServiceException;
import org.springblade.core.mp.base.BizEntity;
import org.springblade.core.mp.base.BizServiceImpl;
import org.springblade.core.oss.OssTemplate;
import org.springblade.core.oss.model.BladeFile;
@@ -17,12 +19,13 @@
import org.springblade.core.tool.utils.FileUtil;
import org.springblade.core.tool.utils.Func;
import org.springblade.core.tool.utils.IoUtil;
import org.springblade.mdm.flow.entity.ApproveRecord;
import org.springblade.mdm.flow.service.ApproveRecordService;
import org.springblade.mdm.flow.service.CureFlowService;
import org.springblade.mdm.program.entity.NcNode;
import org.springblade.mdm.program.entity.NcProgram;
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.springframework.stereotype.Service;
@@ -33,12 +36,11 @@
import java.nio.file.Path;
import java.nio.file.Paths;
import java.time.Duration;
import java.time.LocalDateTime;
import java.util.*;
/**
 * MDM程序导入(工控网功能)
 *
 * 目前工控网展现形式未定,暂时实现暂停
 * @author yangys
 */
@Slf4j
@@ -48,6 +50,7 @@
   private final CureFlowService cureFlowService;
   private final NcProgramService ncProgramService;
   private final NcNodeService ncNodeService;
   private final ApproveRecordService approveRecordService;
   private final BladeRedis bladeRedis;
   private final OssTemplate ossTemplate;
@@ -66,8 +69,7 @@
         //设置一个缓存,2小时过期
         bladeRedis.setEx(getFileKey(),bfile.getName(), Duration.ofHours(2));
         //String fileName = file.getOriginalFilename();
         //InputStream zipFileInputStream = FileExchangeUtil.convertFileToZip(file.getInputStream());
         String pkgFileName = bladeRedis.get(getFileKey());
         InputStream zipFileInputStream = file.getInputStream();//test
         byte[] bytes = FileUtil.copyToByteArray(zipFileInputStream);
@@ -75,37 +77,12 @@
      } catch (IOException e) {
         log.error("上传dnc回传文件失败",e);
         list = Collections.emptyList();
         log.error("导入涉密网摆渡文件失败",e);
         throw new ServiceException("解析DNC回传数据失败");
      }
      return list;
   }
   InputStream convertFileToZip(InputStream inputStream) throws IOException {
      File tempFile = createTempFile();
      FileOutputStream fos = new FileOutputStream(tempFile);
      CustomBinaryReader.read(inputStream,fos);
      FileInputStream dInstream = new FileInputStream(tempFile);
      return dInstream;
   }
   /**
    * 创建一个临时文件
    * @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();
   }
   public static List<MdmProgramImportVO> parseMdmZipFromByteArray(byte[] zipData) throws IOException {
      List<MdmProgramImportVO> list = new ArrayList<>();
      Map<String,String> fileMd5Map = new HashMap<>();
@@ -122,41 +99,7 @@
            if (!entry.isDirectory()) {
               //直接解析程序的json文件
               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
                  }
               }
            }
@@ -186,21 +129,6 @@
      //
      updateProgramFile(pkgFileName,idList);
      List<NcProgram> progList = ncProgramService.listByIds(idList);
      NcProgramExchange exchange;
      for(NcProgram prog:progList){
         exchange = new NcProgramExchange();
         exchange.setName(prog.getName());
         exchange.setExchangeType(2);//回传
         exchange.setNcProgramId(prog.getId());
         this.save(exchange);
      }
   }
   /**
@@ -214,73 +142,7 @@
      List<NcNode> nodeList = new ArrayList<>();
      List<NcProgram> progList = new ArrayList<>();
      try (SeekableInMemoryByteChannel channel = new SeekableInMemoryByteChannel(bytes);
          ZipFile zipFile = new ZipFile(channel)) {
         ZipArchiveEntry entry;
         Enumeration<ZipArchiveEntry> entries = zipFile.getEntries();
         while (entries.hasMoreElements()) {
            entry = entries.nextElement();
            String entryName = entry.getName();
            if (!entry.isDirectory()) {
               if(entryName.equals(NcProgramExportDNCService.PROGRAM_JSON_FILE)){
                  try (InputStream insJson = zipFile.getInputStream(entry)) {
                     String jsonStr = IoUtil.readToString(insJson);
                     JSONArray jsonArray = JSONArray.parseArray(jsonStr);
                     for(int i=0;i<jsonArray.size();i++){
                        JSONObject jsonObject = jsonArray.getJSONObject(i);
                        NcProgram d = new NcProgram();
                        d.setId(jsonObject.getLong("id"));
                        d.setName(jsonObject.getString("name"));
                        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"));
                        progList.add(d);
                     }
                  }
               }else if(entryName.equals(NcProgramExportDNCService.NODE_JSON_FILE)){
                  try (InputStream insJson = zipFile.getInputStream(entry)) {
                     String jsonStr = IoUtil.readToString(insJson);
                     JSONArray jsonArray = JSONArray.parseArray(jsonStr);
                     for (int i = 0; i < jsonArray.size(); i++) {
                        JSONObject jsonObject = jsonArray.getJSONObject(i);
                        NcNode node = new NcNode();
                        node.setId(jsonObject.getLong("id"));
                        node.setName(jsonObject.getString("name"));
                        node.setDescription(jsonObject.getString("description"));
                        node.setNodeType(jsonObject.getString("nodeType"));
                        node.setProcessName(jsonObject.getString("processName"));
                        node.setPartNo(jsonObject.getString("partNo"));
                        node.setMachineCode(jsonObject.getString("machineCode"));
                        node.setParentId(jsonObject.getLong("parentId"));
                        node.setParentIds(jsonObject.getString("parentIds"));
                        node.setIsCured(jsonObject.getInteger("isCured"));
                        nodeList.add(node);
                     }
                  }
               }
            }
         }
      }
      List<ApproveRecord> recordList = new ArrayList<>();
      try (SeekableInMemoryByteChannel channel = new SeekableInMemoryByteChannel(bytes);
          ZipFile zipFile = new ZipFile(channel)) {
@@ -292,22 +154,11 @@
            String entryName = entry.getName();
            if (!entry.isDirectory() && !entryName.equals(NcProgramExportDNCService.PROGRAM_JSON_FILE)) {
               for(NcProgram prog:progList){
                  if(prog.getName().equals(entryName)){
                     try (InputStream ncFileStream = zipFile.getInputStream(entry)) {
                        BladeFile bfile = this.ossTemplate.putFile(prog.getName(),ncFileStream);
                        prog.setOssName(bfile.getName());
                        prog.setUrl(bfile.getLink());
                     }
                     break;
                  }
               }
            }
         }
      }
      for(NcNode node:nodeList){
         NcNode nodeTemp = this.ncNodeService.getById(node.getId());
@@ -327,5 +178,22 @@
         }
      }
      for(ApproveRecord record:recordList) {
         ApproveRecord recordTemp = this.approveRecordService.getById(record.getId());
         if (recordTemp == null) {
            approveRecordService.save(record);
         } else {
            approveRecordService.updateById(record);
         }
      }
   }
   void setBaseProperties(BizEntity entity,JSONObject jsonObject){
      entity.setCreateTime(jsonObject.getDate("createTime"));
      entity.setUpdateTime(jsonObject.getDate("updateTime"));
      entity.setStatus(jsonObject.getInteger("status"));
      entity.setCreateUser(jsonObject.getLong("createUser"));
      entity.setUpdateUser(jsonObject.getLong("updateUser"));
   }
}