| | |
| | | import org.springblade.mdm.utils.ProgramFileNameParser; |
| | | import org.springblade.mdm.utils.ZipTextFileContentUtil; |
| | | import org.springblade.system.pojo.entity.DictBiz; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.stereotype.Service; |
| | | import org.springframework.transaction.annotation.Transactional; |
| | | import org.springframework.web.multipart.MultipartFile; |
| | | |
| | | import java.io.*; |
| | | import java.nio.charset.Charset; |
| | | import java.nio.file.Files; |
| | | import java.nio.file.Path; |
| | | import java.nio.file.StandardOpenOption; |
| | |
| | | import java.util.regex.Pattern; |
| | | import java.util.zip.ZipEntry; |
| | | import java.util.zip.ZipFile; |
| | | import java.util.zip.ZipInputStream; |
| | | |
| | | /** |
| | | * DNC回传文件处理服务 |
| | |
| | | */ |
| | | @Slf4j |
| | | @Service |
| | | @AllArgsConstructor |
| | | public class DNCSendBackService extends BizServiceImpl<NcProgramExchangeMapper, NcProgramExchange> { |
| | | private final CureFlowService cureFlowService; |
| | | private final QinzheFgbService qinzheFbgService; |
| | | private final NcNodeService ncNodeService; |
| | | private final OssTemplate ossTemplate; |
| | | private final BladeRedis bladeRedis; |
| | | private final FlowCommonService flowCommonService; |
| | | private final DncBackFileService dncBackFileService; |
| | | private final MachineService machineService; |
| | | private final ParamService paramService; |
| | | @Autowired |
| | | private CureFlowService cureFlowService; |
| | | @Autowired |
| | | private NcNodeService ncNodeService; |
| | | @Autowired |
| | | private OssTemplate ossTemplate; |
| | | @Autowired |
| | | private BladeRedis bladeRedis; |
| | | @Autowired |
| | | private FlowCommonService flowCommonService; |
| | | @Autowired |
| | | private DncBackFileService dncBackFileService; |
| | | @Autowired |
| | | private MachineService machineService; |
| | | |
| | | /** |
| | | * 偏离单文件末尾的模式:P+数字 |
| | | */ |
| | | private static final String P_NUMBER_PATTERN = "(?i)P\\d+"; |
| | | |
| | | static String[] encodings = {"UTF-8","GBK", "GB2312", "ISO-8859-1"}; |
| | | private String getFileKey(){ |
| | | return "dncimpfile-"+ AuthUtil.getUserId(); |
| | | } |
| | |
| | | /** |
| | | * 从压缩包 解析回传程序列表,这里解析目录即可,目录就是程序包名 |
| | | * @param inputStream 压缩包输入流 |
| | | * @return 文件操作异常 |
| | | */ |
| | | List<DncSendBackData> parseProgramListFromZip(InputStream inputStream) throws IOException { |
| | | List<DncSendBackData> result = null; |
| | | |
| | | ByteArrayInputStream byteInsStream = new ByteArrayInputStream(FileUtil.copyToByteArray(inputStream)); |
| | | for (String encoding : encodings) { |
| | | try { |
| | | result = parseProgramListByCharset(byteInsStream,Charset.forName(encoding)); |
| | | log.error("使用编码 {} 解析成功 ",encoding); |
| | | break; |
| | | } catch (Exception e) { |
| | | byteInsStream.reset(); |
| | | log.error("使用编码 {} 解析失败: ",encoding,e); |
| | | } |
| | | } |
| | | if(result != null) { |
| | | return result; |
| | | }else{ |
| | | return Collections.emptyList(); |
| | | } |
| | | } |
| | | /** |
| | | * 从压缩包 解析回传程序列表,这里解析目录即可,目录就是程序包名 |
| | | * @param inputStream 压缩包输入流 |
| | | * @return 回传程序列表 |
| | | * @throws IOException 文件操作异常 |
| | | */ |
| | | List<DncSendBackData> parseProgramListFromZip(InputStream inputStream) throws IOException { |
| | | List<DncSendBackData> parseProgramListByCharset(InputStream inputStream,Charset charset) throws IOException { |
| | | List<DncSendBackData> list = new ArrayList<>(); |
| | | |
| | | Path tempZipFile = createTempFile(inputStream); |
| | | |
| | | List<String> fileEntryNameList = new ArrayList<>(); |
| | | List<String> dirEntryNameList = new ArrayList<>();//程序包名+工序版次 作为文件夹名 |
| | | try (ZipFile zipFile = new ZipFile(tempZipFile.toFile())) { |
| | | |
| | | |
| | | ZipEntry entry; |
| | | try (ZipFile zipFile = new ZipFile(tempZipFile.toFile(),charset)) { |
| | | Enumeration<? extends ZipEntry> zipEntries = zipFile.entries(); |
| | | ZipEntry entry; |
| | | |
| | | //获取所有的entry名称 |
| | | while (zipEntries.hasMoreElements()) { |
| | | entry = zipEntries.nextElement(); |
| | |
| | | return list; |
| | | } |
| | | |
| | | /** |
| | | * 修复程序包名,fanuc不识别下划线,下发时转换为了-,这里需要确认。应该是从导出记录中查找修改后的包名,但是现场编制的没有咋办? |
| | | * @param packageNameInZip |
| | | * @return |
| | | */ |
| | | /* |
| | | private String fixProgramPackageName(String packageNameInZip) { |
| | | Matcher matcher = RegExpConstants.PROGRAM_PACKAGE_PATTERN.matcher(packageNameInZip); |
| | | |
| | | String drawingNo = null; |
| | | if(matcher.find()) { |
| | | drawingNo = matcher.group(1); |
| | | } |
| | | String processNo = null; |
| | | if(matcher.find()) { |
| | | processNo = matcher.group(2); |
| | | } |
| | | |
| | | String processEdition = null; |
| | | if(matcher.find()) { |
| | | processEdition = matcher.group(3); |
| | | } |
| | | if(drawingNo != null && processNo != null && processEdition != null) { |
| | | if(drawingNo.contains("_")) { |
| | | |
| | | } |
| | | } |
| | | return packageNameInZip; |
| | | } |
| | | */ |
| | | |
| | | /** |
| | | * 入库回传文件,并启动固化流程 |
| | |
| | | cureFlowService.startCureNew(pkgIdFileMap); |
| | | |
| | | } |
| | | |
| | | /** |
| | | * 处理回传文件 |
| | | * @param ossFileName |
| | |
| | | private Map<Long, List<FlowProgramFile>> dealWithBackFile(String ossFileName, List<Long> acceptIdList) throws IOException{ |
| | | Map<Long, List<FlowProgramFile>> pkgIdFileMap = new HashMap<>(); |
| | | |
| | | //ByteArrayInputStream byteInsStream = new ByteArrayInputStream(FileUtil.copyToByteArray(inputStream)); |
| | | for (String encoding : encodings) { |
| | | try { |
| | | pkgIdFileMap = dealWithBackFileWithCharset(ossFileName,acceptIdList,Charset.forName(encoding)); |
| | | log.error("使用编码 {} 解析成功 ",encoding); |
| | | break; |
| | | } catch (Exception e) { |
| | | //byteInsStream.reset(); |
| | | log.error("使用编码 {} 解析失败: ",encoding,e); |
| | | } |
| | | } |
| | | |
| | | return pkgIdFileMap; |
| | | } |
| | | |
| | | /** |
| | | * 处理回传文件 |
| | | * @param inputStream |
| | | * @param acceptIdList |
| | | * @return |
| | | * @throws IOException |
| | | */ |
| | | private Map<Long, List<FlowProgramFile>> dealWithBackFileWithCharset(String ossFileName, List<Long> acceptIdList,Charset charset) throws IOException{ |
| | | Map<Long, List<FlowProgramFile>> pkgIdFileMap = new HashMap<>(); |
| | | InputStream inputStream = this.ossTemplate.statFileStream(ossFileName); |
| | | Path tempZipFile = createTempFile(inputStream); |
| | | List<String> entryNameList = new ArrayList<>(); |
| | | |
| | | ZipEntry entry; |
| | | try (java.util.zip.ZipFile zipFile = new java.util.zip.ZipFile(tempZipFile.toFile())) { |
| | | try (java.util.zip.ZipFile zipFile = new java.util.zip.ZipFile(tempZipFile.toFile(),charset)) { |
| | | Enumeration<? extends ZipEntry> entries = zipFile.entries(); |
| | | while(entries.hasMoreElements()) { |
| | | entry = entries.nextElement(); |