yangys
2025-08-16 8db9b68d0bf4b7ec2e80b580f5931a40788caac7
blade-service/blade-mdm/src/main/java/org/springblade/mdm/flow/service/ReplaceProgramFileService.java
@@ -10,10 +10,14 @@
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.entity.ReplaceProgramFile;
import org.springblade.mdm.flow.mapper.ReplaceProgramFileMapper;
import org.springblade.mdm.flow.vo.ProgramUploadVO;
import org.springblade.mdm.flow.vo.ReplaceUploadVO;
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;
@@ -36,185 +40,9 @@
   private RuntimeService runtimeService;
   @Autowired
   private FlowCommonService flowCommonService;
   @Autowired
   private FlowProgramFileService flowProgramFileService;
   /**
    * 上传程序徐文件(编制节点调用)
    * @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);
      BladeFile bfile = ossTemplate.putFile(file);
      ReplaceProgramFile progFile = new ReplaceProgramFile();
      progFile.setName(file.getOriginalFilename());
      progFile.setOssName(bfile.getName());
      progFile.setProcessInstanceId(uploadVO.getProcessInstanceId());
      progFile.setProgramName(programName);
      save(progFile);
   }
   /**
    * 获取程序名称
    * @param progProps
    * @return
    */
   String getProgramName(FlowProgramProperties progProps){
      return progProps.getDrawingNo()+"-"+progProps.getProcessNo();
   }
   /**
    * 检查文件名合法性
    * @param filename 文件名
    * @param programProperties 程序属性,发起时填写的
    */
   void checkFilename(String filename,FlowProgramProperties programProperties){
      //程序名称:零件号加工序号,文件名应该以此开头
      String expectedProgramName = programProperties.getDrawingNo()+"-"+programProperties.getProcessNo();
      if(!StringUtils.startsWith(filename,expectedProgramName)){
         IResultCode rc = new IResultCode() {
            @Override
            public String getMessage() {
               return "程序文件名不合法,应为:"+expectedProgramName+"-"+programProperties.getProcessEdition()+"-[段数]-[段号].[文件扩展名]";
            }
            @Override
            public int getCode() {
               return 1;
            }
         };
         throw new ServiceException(rc);
         //throw new ServiceException("程序文件名不合法,应为:"+expectedProgramName+"-"+programProperties.getCraftEdition()+"-[段数]-[段号].[文件扩展名]");
      }
      //截取后面的段数和第几段
      String endPart = StringUtils.removeStart(filename,expectedProgramName+"-"+programProperties.getProcessEdition()+"-");
      //去掉扩展名
      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.getProcessEdition()+"-[段数]-[段号].[文件扩展名]";
            }
            @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 文件id
    * @return 文件内容文本
    */
   public String getFileContent(Long id) {
      String result  ="";
      ReplaceProgramFile programFile = this.getById(id);
      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<ReplaceProgramFile> flowPrograms = this.lambdaQuery().eq(ReplaceProgramFile::getProcessInstanceId, processInstanceId).orderByAsc(ReplaceProgramFile::getCreateTime).list();
      if(isPass && flowPrograms.isEmpty()){
         throw new ServiceException("请上传程序文件");
      }
      int totalSeg = 0;
      if(!flowPrograms.isEmpty()){
         ReplaceProgramFile 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);
   }
   public void deleteByTempId(Long tempId) {
      this.baseMapper.deleteByTempId(tempId);