| | |
| | | |
| | | 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.core.mp.base.BizServiceImpl; |
| | | import org.springblade.core.oss.OssTemplate; |
| | | import org.springblade.core.oss.model.BladeFile; |
| | | import org.springblade.core.tool.api.IResultCode; |
| | | import org.springblade.core.tool.utils.Func; |
| | | 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.entity.NcProgram; |
| | | import org.springblade.mdm.program.service.ProcessProgRefService; |
| | | 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.InputStream; |
| | | import java.nio.charset.StandardCharsets; |
| | | import java.util.List; |
| | | import java.util.Map; |
| | | |
| | | /** |
| | | * 流程程序处理,包括上传,查询展示 |
| | |
| | | */ |
| | | @Slf4j |
| | | @Service |
| | | @AllArgsConstructor |
| | | public class FlowProgramFileService extends BizServiceImpl<FlowProgramFileMapper, FlowProgramFile> { |
| | | private final OssTemplate ossTemplate; |
| | | private final RuntimeService runtimeService; |
| | | private final FlowCommonService flowCommonService; |
| | | @Autowired |
| | | private OssTemplate ossTemplate; |
| | | @Autowired |
| | | private RuntimeService runtimeService; |
| | | @Autowired |
| | | private FlowCommonService flowCommonService; |
| | | |
| | | /** |
| | | * 上传程序徐文件(编制节点调用) |
| | | * @param uploadVO 上传对象 |
| | | */ |
| | | public void uploadFlowProgramFile(ProgramUploadVO uploadVO) { |
| | | |
| | | //Map<String, Object> vars = runtimeService.getVariables(uploadVO.getProcessInstanceId()); |
| | | |
| | | FlowProgramProperties progProps= flowCommonService.getProgramProperties(uploadVO.getProcessInstanceId()); |
| | | checkFilenames(uploadVO.getFiles(),progProps); |
| | | MultipartFile file = uploadVO.getFile(); |
| | | if(file.getSize() == 0){ |
| | | throw new ServiceException("程序文件不可为空文件"); |
| | | } |
| | | checkFilename(file.getOriginalFilename(),progProps); |
| | | |
| | | String programName = getProgramName(progProps); |
| | | for(MultipartFile file : uploadVO.getFiles()) { |
| | | BladeFile bfile = ossTemplate.putFile(file); |
| | | |
| | | FlowProgramFile progFile = new FlowProgramFile(); |
| | | progFile.setName(file.getOriginalFilename()); |
| | | progFile.setOssName(bfile.getName()); |
| | | progFile.setProcessInstanceId(uploadVO.getProcessInstanceId()); |
| | | BladeFile bfile = ossTemplate.putFile(file); |
| | | |
| | | progFile.setProgramName(programName); |
| | | FlowProgramFile progFile = new FlowProgramFile(); |
| | | progFile.setName(file.getOriginalFilename()); |
| | | progFile.setOssName(bfile.getName()); |
| | | progFile.setProcessInstanceId(uploadVO.getProcessInstanceId()); |
| | | progFile.setIsCured(0); |
| | | progFile.setProgramName(programName); |
| | | |
| | | save(progFile); |
| | | } |
| | | // |
| | | //BladeFile bfile = ossTemplate.putFile(uploadVO.getFile()); |
| | | //String link = bfile.getLink(); |
| | | //保存profame |
| | | /* |
| | | NcProgram prog = new NcProgram(); |
| | | prog.setCode(generageCode()); |
| | | prog.setName(file.getOriginalFilename()); |
| | | prog.setNcNodeId(uploadVO.getNodeId()); |
| | | prog.setOssName(bfile.getName()); |
| | | prog.setDrawingNo(uploadVO.getDrawingNo()); |
| | | prog.setProcessEdition(uploadVO.getProcessEdition()); |
| | | prog.setIsLastEdition(1); |
| | | save(progFile); |
| | | |
| | | */ |
| | | } |
| | | |
| | | /** |
| | | * 获取程序名称 |
| | | * @param progProps |
| | | * @return |
| | | */ |
| | | String getProgramName(FlowProgramProperties progProps){ |
| | | return progProps.getDrawingNo()+"-"+progProps.getProcessNo(); |
| | | } |
| | | |
| | | public void checkFilenames(MultipartFile[] files,FlowProgramProperties programProperties){ |
| | | for (MultipartFile file : files){ |
| | | checkFilename(file.getOriginalFilename(),programProperties); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 检查文件名合法性 |
| | | * @param filename 文件名 |
| | | * @param programProperties 程序属性,发起时填写的 |
| | | */ |
| | | void checkFilename(String filename,FlowProgramProperties programProperties){ |
| | | //程序名称:零件号加工序号,文件名应该以此开头 |
| | | String expectedProgramName = programProperties.getDrawingNo()+"-"+programProperties.getProcessNo(); |
| | | if(!StringUtils.startsWith(filename,expectedProgramName)){ |
| | | throw new ServiceException("程序文件名不合法,应为:"+expectedProgramName+"-"+programProperties.getCraftEdition()+"-[段数]-[段号].[文件扩展名]"); |
| | | |
| | | IResultCode rc = new IResultCode() { |
| | | @Override |
| | | public String getMessage() { |
| | | return "程序文件名不合法,应为:"+expectedProgramName+"-"+programProperties.getCraftEdition()+"-[段数]-[段号].[文件扩展名]"; |
| | | } |
| | | |
| | | @Override |
| | | public int getCode() { |
| | | return 1; |
| | | } |
| | | }; |
| | | throw new ServiceException(rc); |
| | | //throw new ServiceException("程序文件名不合法,应为:"+expectedProgramName+"-"+programProperties.getCraftEdition()+"-[段数]-[段号].[文件扩展名]"); |
| | | } |
| | | |
| | | //截取后面的段数和第几段 |
| | | String endPart = StringUtils.removeStart(filename,expectedProgramName+"-"+programProperties.getCraftEdition()+"-"); |
| | | |
| | | //去掉扩展名 |
| | | if(endPart.contains(".")){ |
| | | endPart = endPart.substring(0,endPart.indexOf(".")); |
| | | } |
| | | |
| | | int sepCount = StringUtils.countMatches(endPart,"-"); |
| | | if(sepCount != 1){//- 号应该是1个 |
| | | IResultCode rc = new IResultCode() { |
| | | @Override |
| | | public String getMessage() { |
| | | return "程序文件名不合法,应为:"+expectedProgramName+"-"+programProperties.getCraftEdition()+"-[段数]-[段号].[文件扩展名]"; |
| | | } |
| | | |
| | | @Override |
| | | public int getCode() { |
| | | return 2; |
| | | } |
| | | }; |
| | | throw new ServiceException(rc); |
| | | //throw new ServiceException("程序文件名不合法,应为:"+expectedProgramName+"-"+programProperties.getCraftEdition()+"-[段数]-[段号].[文件扩展名]"); |
| | | } |
| | | |
| | | String[] arr = StringUtils.split(endPart,"-"); |
| | | if(!StringUtils.isNumeric(arr[0]) || Func.toInt(arr[0]) >99 || Func.toInt(arr[0]) < 1){ |
| | | IResultCode rc = new IResultCode() { |
| | | @Override |
| | | public String getMessage() { |
| | | return "程序段数不合法,应为两位以内整数"; |
| | | } |
| | | |
| | | @Override |
| | | public int getCode() { |
| | | return 3; |
| | | } |
| | | }; |
| | | throw new ServiceException(rc); |
| | | } |
| | | int segCount = Func.toInt(arr[0]); |
| | | |
| | | if(!StringUtils.isNumeric(arr[1]) || Func.toInt(arr[1]) < 1 || Func.toInt(arr[1]) > segCount){ |
| | | IResultCode rc = new IResultCode() { |
| | | @Override |
| | | public String getMessage() { |
| | | return "程序段号不合法,应为两位以内整数且小于等于段数。"; |
| | | } |
| | | |
| | | @Override |
| | | public int getCode() { |
| | | return 4; |
| | | } |
| | | }; |
| | | 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; |
| | | } |
| | | |
| | | /** |
| | | * 验证程序文件的完整性,仅在编制节点使用 |
| | | * @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); |
| | | } |
| | | } |