| | |
| | | |
| | | 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.utils.FileContentUtil; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.stereotype.Service; |
| | | import org.springframework.web.multipart.MultipartFile; |
| | |
| | | import java.io.IOException; |
| | | import java.io.InputStream; |
| | | import java.nio.charset.StandardCharsets; |
| | | import java.util.List; |
| | | |
| | | /** |
| | | * 流程程序处理,包括上传,查询展示 |
| | |
| | | @Autowired |
| | | private FlowCommonService flowCommonService; |
| | | |
| | | /** |
| | | * 上传程序徐文件(编制节点调用) |
| | | * @param uploadVO 上传对象 |
| | | */ |
| | | public void uploadFlowProgramFile(ProgramUploadVO uploadVO) { |
| | | |
| | | FlowProgramProperties progProps= flowCommonService.getProgramProperties(uploadVO.getProcessInstanceId()); |
| | | MultipartFile file = uploadVO.getFile(); |
| | | if(file.getSize() == 0){ |
| | | throw new ServiceException("程序文件不可为空文件"); |
| | | } |
| | | checkFilename(file.getOriginalFilename(),progProps); |
| | | |
| | | String programName = getProgramName(progProps); |
| | |
| | | progFile.setName(file.getOriginalFilename()); |
| | | progFile.setOssName(bfile.getName()); |
| | | progFile.setProcessInstanceId(uploadVO.getProcessInstanceId()); |
| | | |
| | | progFile.setIsCured(0); |
| | | progFile.setProgramName(programName); |
| | | |
| | | save(progFile); |
| | | |
| | | } |
| | | |
| | | /** |
| | | * 获取程序名称 |
| | | * @param progProps |
| | | * @return |
| | | */ |
| | | String getProgramName(FlowProgramProperties progProps){ |
| | | return progProps.getDrawingNo()+"-"+progProps.getProcessNo(); |
| | | } |
| | | |
| | | /** |
| | | * 检查文件名合法性 |
| | | * @param filename |
| | | * @param programProperties |
| | | * @param filename 文件名 |
| | | * @param programProperties 程序属性,发起时填写的 |
| | | */ |
| | | void checkFilename(String filename,FlowProgramProperties programProperties){ |
| | | //程序名称:零件号加工序号,文件名应该以此开头 |
| | |
| | | |
| | | /** |
| | | * 获取文件内容 |
| | | * @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; |
| | | } |
| | | |
| | | /** |
| | | * 验证程序文件的完整性,仅在编制节点使用 |
| | | * @param processInstanceId 流程实例id |
| | | */ |
| | | public void checkProgramFiles(String processInstanceId,boolean isPass) { |
| | | List<FlowProgramFile> flowPrograms = this.lambdaQuery().eq(FlowProgramFile::getProcessInstanceId, processInstanceId).orderByAsc(FlowProgramFile::getCreateTime).list(); |
| | | if(isPass && flowPrograms.isEmpty()){ |
| | | throw new ServiceException("请上传程序文件"); |
| | | } |
| | | int totalSeg = 0; |
| | | if(!flowPrograms.isEmpty()){ |
| | | FlowProgramFile progFile = flowPrograms.get(0); |
| | | totalSeg = 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); |
| | | } |
| | | } |