| | |
| | | |
| | | import lombok.AllArgsConstructor; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.apache.commons.io.FilenameUtils; |
| | | import org.apache.commons.io.IOUtils; |
| | | import org.apache.commons.lang3.StringUtils; |
| | | import org.flowable.engine.RuntimeService; |
| | |
| | | import org.springblade.mdm.flow.entity.FlowProgramFile; |
| | | import org.springblade.mdm.flow.mapper.FlowProgramFileMapper; |
| | | import org.springblade.mdm.flow.vo.ProgramUploadVO; |
| | | import org.springblade.mdm.program.service.NcNodeService; |
| | | import org.springblade.mdm.utils.FileContentUtil; |
| | | import org.springblade.mdm.utils.ProgramFileNameCheckUtil; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.stereotype.Service; |
| | | import org.springframework.web.multipart.MultipartFile; |
| | |
| | | @Autowired |
| | | private FlowCommonService flowCommonService; |
| | | |
| | | /** |
| | | * 上传程序徐文件(编制节点调用) |
| | | * @param uploadVO 上传对象 |
| | | */ |
| | | public void uploadFlowProgramFile(ProgramUploadVO uploadVO) { |
| | | |
| | | FlowProgramProperties progProps= flowCommonService.getProgramProperties(uploadVO.getProcessInstanceId()); |
| | | MultipartFile file = uploadVO.getFile(); |
| | | checkFilename(file.getOriginalFilename(),progProps); |
| | | if(file.getSize() == 0){ |
| | | throw new ServiceException("程序文件不可为空文件"); |
| | | } |
| | | ProgramFileNameCheckUtil.checkFilename(file.getOriginalFilename(),progProps); |
| | | |
| | | String programName = getProgramName(progProps); |
| | | String programName = NcNodeService.genProgramName(progProps.getDrawingNo(),progProps.getProcessNo()); |
| | | |
| | | BladeFile bfile = ossTemplate.putFile(file); |
| | | |
| | |
| | | progFile.setName(file.getOriginalFilename()); |
| | | progFile.setOssName(bfile.getName()); |
| | | progFile.setProcessInstanceId(uploadVO.getProcessInstanceId()); |
| | | |
| | | progFile.setProgramName(programName); |
| | | |
| | | progFile.setFileType(uploadVO.getFileType()); |
| | | save(progFile); |
| | | |
| | | } |
| | | |
| | | String getProgramName(FlowProgramProperties progProps){ |
| | | return progProps.getDrawingNo()+"-"+progProps.getProcessNo(); |
| | | } |
| | | |
| | | /** |
| | | * 检查文件名合法性 |
| | | * @param filename |
| | | * @param programProperties |
| | | * @param filename 文件名 |
| | | * @param programProperties 程序属性,发起时填写的 |
| | | */ |
| | | /* |
| | | void checkFilename(String filename,FlowProgramProperties programProperties){ |
| | | //程序名称:零件号加工序号,文件名应该以此开头 |
| | | String expectedProgramName = programProperties.getDrawingNo()+"-"+programProperties.getProcessNo(); |
| | | if(!StringUtils.startsWith(filename,expectedProgramName)){ |
| | | String programNamePrefix = NcNodeService.genProgramName(programProperties.getDrawingNo(),programProperties.getProcessNo()) + "-"+programProperties.getProcessEdition(); |
| | | if(!StringUtils.startsWith(filename,programNamePrefix)){ |
| | | |
| | | IResultCode rc = new IResultCode() { |
| | | @Override |
| | | public String getMessage() { |
| | | return "程序文件名不合法,应为:"+expectedProgramName+"-"+programProperties.getCraftEdition()+"-[段数]-[段号].[文件扩展名]"; |
| | | return "程序文件名不合法,应为:"+programNamePrefix+"-[段数]-[段号].[文件扩展名]"; |
| | | } |
| | | |
| | | @Override |
| | |
| | | } |
| | | }; |
| | | throw new ServiceException(rc); |
| | | //throw new ServiceException("程序文件名不合法,应为:"+expectedProgramName+"-"+programProperties.getCraftEdition()+"-[段数]-[段号].[文件扩展名]"); |
| | | } |
| | | |
| | | //截取后面的段数和第几段 |
| | | String endPart = StringUtils.removeStart(filename,expectedProgramName+"-"+programProperties.getCraftEdition()+"-"); |
| | | String endPart = StringUtils.removeStart(filename,programNamePrefix+"-"); |
| | | |
| | | //去掉扩展名 |
| | | if(endPart.contains(".")){ |
| | |
| | | IResultCode rc = new IResultCode() { |
| | | @Override |
| | | public String getMessage() { |
| | | return "程序文件名不合法,应为:"+expectedProgramName+"-"+programProperties.getCraftEdition()+"-[段数]-[段号].[文件扩展名]"; |
| | | return "程序文件名不合法,应为:"+programNamePrefix+"-[段数]-[段号].[文件扩展名]"; |
| | | } |
| | | |
| | | @Override |
| | |
| | | } |
| | | }; |
| | | throw new ServiceException(rc); |
| | | //throw new ServiceException("程序文件名不合法,应为:"+expectedProgramName+"-"+programProperties.getCraftEdition()+"-[段数]-[段号].[文件扩展名]"); |
| | | } |
| | | |
| | | String[] arr = StringUtils.split(endPart,"-"); |
| | |
| | | throw new ServiceException(rc); |
| | | } |
| | | } |
| | | |
| | | */ |
| | | /** |
| | | * 获取文件内容 |
| | | * @param id |
| | | * @return |
| | | * @param id 文件id |
| | | * @return 文件内容文本 |
| | | */ |
| | | public String getFileContent(Long id) { |
| | | String result =""; |
| | | |
| | | FlowProgramFile programFile = this.getById(id); |
| | | //if(prog.getIsTextFile()!=null && programFile.getIsTextFile()){ |
| | | /* |
| | | String fileName = programFile.getOssName(); |
| | | try (InputStream inputStream = ossTemplate.statFileStream(fileName)) { |
| | | result = IOUtils.toString(inputStream, StandardCharsets.UTF_8); |
| | | } catch (IOException e) { |
| | | throw new RuntimeException(e); |
| | | } |
| | | //}else{ |
| | | // result = "非文本格式文件"; |
| | | //} |
| | | */ |
| | | |
| | | String fileName = programFile.getOssName(); |
| | | try (InputStream inputStream = ossTemplate.statFileStream(fileName)) { |
| | | result = FileContentUtil.getContentFromStream(inputStream); |
| | | } catch (IOException e) { |
| | | throw new RuntimeException(e); |
| | | } |
| | | |
| | | return result; |
| | | } |
| | | |
| | |
| | | int totalSeg = 0; |
| | | if(!flowPrograms.isEmpty()){ |
| | | FlowProgramFile progFile = flowPrograms.get(0); |
| | | totalSeg = getProgramSegCount(progFile.getName()); |
| | | totalSeg = ProgramFileNameCheckUtil.getProgramSegCount(progFile.getName()); |
| | | } |
| | | |
| | | if(totalSeg != flowPrograms.size()){ |
| | | throw new ServiceException("应上传"+totalSeg+"段程序,实际上传"+flowPrograms.size()+"段"); |
| | | } |
| | | |
| | | |
| | | } |
| | | |
| | | /** |
| | | * 根据文件名获取程序总段树 |
| | | * @param filename 文件名 |
| | | * @return |
| | | */ |
| | | int getProgramSegCount(String filename){ |
| | | int idx = filename.lastIndexOf("."); |
| | | String tempstr = filename.substring(0,idx);//去掉扩展名 |
| | | //System.out.println(tempstr); |
| | | idx = tempstr.lastIndexOf("-"); |
| | | tempstr = tempstr.substring(0,idx); |
| | | //System.out.println(tempstr); |
| | | idx = tempstr.lastIndexOf("-"); |
| | | tempstr = tempstr.substring(idx+1); |
| | | //System.out.println(tempstr); |
| | | return Func.toInt(tempstr); |
| | | } |
| | | |
| | | } |