From b7af95d487f49490de16f9636147e5a40911e157 Mon Sep 17 00:00:00 2001
From: yangys <y_ys79@sina.com>
Date: 星期五, 29 八月 2025 23:36:48 +0800
Subject: [PATCH] 确认表增加程序编号

---
 blade-service/blade-mdm/src/main/java/org/springblade/mdm/flow/service/FlowProgramFileService.java |  171 ++++++++++++++++----------------------------------------
 1 files changed, 50 insertions(+), 121 deletions(-)

diff --git a/blade-service/blade-mdm/src/main/java/org/springblade/mdm/flow/service/FlowProgramFileService.java b/blade-service/blade-mdm/src/main/java/org/springblade/mdm/flow/service/FlowProgramFileService.java
index 1b8062a..aa2ce55 100644
--- a/blade-service/blade-mdm/src/main/java/org/springblade/mdm/flow/service/FlowProgramFileService.java
+++ b/blade-service/blade-mdm/src/main/java/org/springblade/mdm/flow/service/FlowProgramFileService.java
@@ -1,6 +1,8 @@
 
 package org.springblade.mdm.flow.service;
 
+import jakarta.servlet.ServletOutputStream;
+import jakarta.servlet.http.HttpServletResponse;
 import lombok.AllArgsConstructor;
 import lombok.extern.slf4j.Slf4j;
 import org.apache.commons.io.IOUtils;
@@ -15,11 +17,14 @@
 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;
 
+import java.io.ByteArrayInputStream;
 import java.io.IOException;
 import java.io.InputStream;
 import java.nio.charset.StandardCharsets;
@@ -51,9 +56,13 @@
 		if(file.getSize() == 0){
 			throw new ServiceException("绋嬪簭鏂囦欢涓嶅彲涓虹┖鏂囦欢");
 		}
-		checkFilename(file.getOriginalFilename(),progProps);
+		if(exists(file.getOriginalFilename(),uploadVO.getProcessInstanceId())){
+			throw new ServiceException("鍚屽悕鏂囦欢宸插瓨鍦細"+file.getOriginalFilename());
+		}
 
-		String programName = getProgramName(progProps);
+		if(FlowProgramFile.TYPE_PROGRAM.equals(uploadVO.getFileType())) {
+			ProgramFileNameCheckUtil.checkFilename(file.getOriginalFilename(), progProps);
+		}
 
 		BladeFile bfile = ossTemplate.putFile(file);
 
@@ -61,105 +70,21 @@
 		progFile.setName(file.getOriginalFilename());
 		progFile.setOssName(bfile.getName());
 		progFile.setProcessInstanceId(uploadVO.getProcessInstanceId());
-		progFile.setIsCured(0);
-		progFile.setProgramName(programName);
-
+		progFile.setProgramName(NcNodeService.genProgramName(progProps.getDrawingNo(),progProps.getProcessNo()));
+		progFile.setFileType(uploadVO.getFileType());
 		save(progFile);
 
 	}
 
 	/**
-	 * 鑾峰彇绋嬪簭鍚嶇О
-	 * @param progProps
-	 * @return
-	 */
-	String getProgramName(FlowProgramProperties progProps){
-		return progProps.getDrawingNo()+"-"+progProps.getProcessNo();
-	}
-
-	/**
-	 * 妫�鏌ユ枃浠跺悕鍚堟硶鎬�
+	 * 鍒ゆ柇閭f祦绋嬩笅鏄惁鏈夊悓鍚嶉棶棰�
 	 * @param filename 鏂囦欢鍚�
-	 * @param programProperties 绋嬪簭灞炴�э紝鍙戣捣鏃跺~鍐欑殑
+	 * @param processInstanceId 娴佺▼瀹炰緥id
+	 * @return 鏄惁
 	 */
-	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.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);
-		}
+	boolean exists(String filename,String processInstanceId){
+		return lambdaQuery().eq(FlowProgramFile::getName, filename).eq(FlowProgramFile::getProcessInstanceId, processInstanceId).count()>0;
 	}
-
 	/**
 	 * 鑾峰彇鏂囦欢鍐呭
 	 * @param id 鏂囦欢id
@@ -169,18 +94,20 @@
 		String result  ="";
 
 		FlowProgramFile programFile = this.getById(id);
-		/*
-			String fileName = programFile.getOssName();
-			try (InputStream inputStream = ossTemplate.statFileStream(fileName)) {
-				result = IOUtils.toString(inputStream, StandardCharsets.UTF_8);
-			} catch (IOException e) {
-				throw new RuntimeException(e);
-			}
-		*/
 
 		String fileName = programFile.getOssName();
 		try (InputStream inputStream = ossTemplate.statFileStream(fileName)) {
-			result = FileContentUtil.getContentFromStream(inputStream);
+			ByteArrayInputStream bos = new ByteArrayInputStream(inputStream.readAllBytes());
+			boolean isText = StringUtils.endsWithIgnoreCase(fileName,".txt") || StringUtils.endsWithIgnoreCase(fileName,".nc")|| StringUtils.endsWithIgnoreCase(fileName,".xml");
+			if(!isText){
+				isText= FileContentUtil.isTextFile(bos);
+			}
+			if(isText){
+				bos.reset();
+				result = FileContentUtil.getContentFromStream(bos);
+			}else{
+				result = "<闈炴枃鏈枃浠�>";
+			}
 		} catch (IOException e) {
 			throw new RuntimeException(e);
 		}
@@ -194,37 +121,39 @@
 	 */
 	public void checkProgramFiles(String processInstanceId,boolean isPass) {
 		List<FlowProgramFile> flowPrograms = this.lambdaQuery().eq(FlowProgramFile::getProcessInstanceId, processInstanceId).orderByAsc(FlowProgramFile::getCreateTime).list();
-		if(isPass && flowPrograms.isEmpty()){
+		List<FlowProgramFile> purePrograms = flowPrograms.stream().filter(FlowProgramFile::isProgram).toList();
+		if(isPass && purePrograms.isEmpty()){
 			throw new ServiceException("璇蜂笂浼犵▼搴忔枃浠�");
 		}
 		int totalSeg = 0;
-		if(!flowPrograms.isEmpty()){
-			FlowProgramFile progFile = flowPrograms.get(0);
-			totalSeg = getProgramSegCount(progFile.getName());
+
+		if(!purePrograms.isEmpty()){
+			FlowProgramFile progFile = purePrograms.get(0);
+			totalSeg = ProgramFileNameCheckUtil.getProgramSegCount(progFile.getName());
 		}
 
-		if(totalSeg != flowPrograms.size()){
+		if(totalSeg != purePrograms.size()){
 			throw new ServiceException("搴斾笂浼�"+totalSeg+"娈电▼搴忥紝瀹為檯涓婁紶"+flowPrograms.size()+"娈�");
 		}
 
+	}
 
+	public void download(Long id, HttpServletResponse response) throws IOException {
+		FlowProgramFile flowFile = this.getById(id);
+		response.setHeader("Content-Disposition", "attachment; filename="+flowFile.getName());
+		response.setContentType("application/octet-stream");
+		try(InputStream ins = ossTemplate.statFileStream(flowFile.getOssName());){
+			IOUtils.copy(ins,response.getOutputStream());
+		}
 	}
 
 	/**
-	 * 鏍规嵁鏂囦欢鍚嶈幏鍙栫▼搴忔�绘鏍�
-	 * @param filename 鏂囦欢鍚�
-	 * @return
+	 * 鑾峰彇鍏充簬娴佺▼鐨勬墍鏈夋枃浠�
+	 * @param processInstanceId 娴佺▼瀹炰緥id
+	 * @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 List<FlowProgramFile> listByProcessInstanceId(String processInstanceId){
+		return lambdaQuery()
+		.eq(FlowProgramFile::getProcessInstanceId, processInstanceId).list();
 	}
 }

--
Gitblit v1.9.3