From 432198337fb3d8a99fadb4b7825771d68bb10b9e Mon Sep 17 00:00:00 2001
From: yangys <y_ys79@sina.com>
Date: 星期三, 20 八月 2025 19:58:22 +0800
Subject: [PATCH] 去掉mail依赖

---
 blade-service/blade-mdm/src/main/java/org/springblade/mdm/gkw/programnode/service/MachineFileService.java |  135 ++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 133 insertions(+), 2 deletions(-)

diff --git a/blade-service/blade-mdm/src/main/java/org/springblade/mdm/gkw/programnode/service/MachineFileService.java b/blade-service/blade-mdm/src/main/java/org/springblade/mdm/gkw/programnode/service/MachineFileService.java
index faf428c..33915b3 100644
--- a/blade-service/blade-mdm/src/main/java/org/springblade/mdm/gkw/programnode/service/MachineFileService.java
+++ b/blade-service/blade-mdm/src/main/java/org/springblade/mdm/gkw/programnode/service/MachineFileService.java
@@ -1,15 +1,31 @@
 
 package org.springblade.mdm.gkw.programnode.service;
 
+import com.baomidou.mybatisplus.core.metadata.IPage;
 import lombok.AllArgsConstructor;
 import lombok.extern.slf4j.Slf4j;
+import org.apache.commons.io.FileUtils;
+import org.apache.commons.lang3.StringUtils;
+import org.springblade.core.log.exception.ServiceException;
 import org.springblade.core.mp.base.BizServiceImpl;
-import org.springblade.core.tool.utils.Func;
+import org.springblade.core.mp.support.Condition;
+import org.springblade.mdm.basesetting.machine.MachineService;
+import org.springblade.mdm.basesetting.machine.entity.Machine;
 import org.springblade.mdm.gkw.programnode.entity.MachineFile;
 import org.springblade.mdm.gkw.programnode.mapper.MachineFileMapper;
+import org.springblade.mdm.machineback.vo.MachineBackFileHandleQueryVO;
+import org.springblade.mdm.machineback.vo.MachineBackFileQueryVO;
+import org.springblade.mdm.machineback.vo.MachineBackFileVO;
+import org.springblade.mdm.utils.FileContentUtil;
 import org.springframework.stereotype.Service;
+import org.springframework.transaction.annotation.Transactional;
 
-import java.util.List;
+import java.io.File;
+import java.io.IOException;
+import java.io.InputStream;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.Paths;
 
 /**
  * 鏈哄簥鏂囦欢
@@ -20,6 +36,121 @@
 @Service
 @AllArgsConstructor
 public class MachineFileService extends BizServiceImpl<MachineFileMapper, MachineFile> {
+	private final MachineService machineService;
+	/**
+	 * 妫�娴嬫枃浠舵槸鍚﹀瓨鍦�
+	 * @param name 鏂囦欢鍚�
+	 * @param dirType 鏂囦欢澶圭被鍨�
+	 * @param machineCode 鎵�灞炴満搴�
+	 * @return 鏄惁瀛樺湪浜庡簱鍐�
+	 */
+	public boolean fileExists(String name, String dirType,String machineCode) {
+		return this.lambdaQuery().eq(MachineFile::getName, name)
+			.eq(MachineFile::getDirType, dirType)
+			.eq(MachineFile::getMachineCode, machineCode).count()>0;
+	}
 
+	@Transactional(readOnly = true)
+	public String getMachineFileContent(Long id) {
+		MachineFile machineFile = getById(id);
+		Machine machine = machineService.getByCode(machineFile.getMachineCode());
 
+		String filePathStr = getBasePath(machine,machineFile.getDirType())+ File.separator+machineFile.getName();
+
+		String content;
+
+		Path filePath = Paths.get(filePathStr);
+		if(!filePath.toFile().exists()){
+			return StringUtils.EMPTY;
+		}
+		try (InputStream inputStream = Files.newInputStream(filePath)) {
+			// 浣跨敤杈撳叆娴佽鍙栨枃浠跺唴瀹�
+			content = FileContentUtil.getContentFromStream(inputStream);
+		} catch (IOException e) {
+			log.error("璇诲彇鏂囦欢md5澶辫触",e);
+			return StringUtils.EMPTY;
+		}
+
+		return content;
+	}
+
+	/**
+	 * 鑾峰彇鍩烘湰璺緞
+	 * @param machine
+	 * @param dirType
+	 * @return
+	 */
+	public static String getBasePath(Machine machine,String dirType){
+		String dirPath;
+		switch (dirType) {
+			case MachineFile.DIR_TYPE_REC:
+				dirPath = machine.getProgReceiveDir();
+				break;
+			case MachineFile.DIR_TYPE_SEND:
+				dirPath = machine.getProgSendDir();
+				break;
+			case MachineFile.DIR_TYPE_TEMP:
+				dirPath = machine.getProgTempDir();
+				break;
+			default:
+				log.warn("鐩綍绫诲瀷涓嶅尮閰�:{}",dirType);
+				return null;//涓嶇鍚堜换浣曠被鍨嬶紝鐩存帴閫�鍑�
+		}
+		return dirPath;
+	}
+
+	@Transactional
+	public void saveFileContent(Long id, String content) throws IOException {
+		MachineFile machineFile = getById(id);
+		Machine machine = machineService.getByCode(machineFile.getMachineCode());
+
+		String filePathStr = getBasePath(machine,machineFile.getDirType())+ File.separator+machineFile.getName();
+		Path filePath = Paths.get(filePathStr);
+		String charsetName = "UTF-8";
+		try (InputStream inputStream = Files.newInputStream(filePath)) {
+			// 浣跨敤杈撳叆娴佽鍙栨枃浠跺唴瀹�
+			charsetName = FileContentUtil.detectFromInputStream(inputStream);
+		} catch (IOException e) {
+			log.error("璇诲彇鏂囦欢缂栫爜澶辫触",e);
+			throw new ServiceException("鑾峰彇鏂囦欢缂栫爜澶辫触");
+		}
+		FileUtils.writeStringToFile(filePath.toFile(),content,charsetName);
+	}
+
+	/**
+	 * 鑾峰彇鏂囦欢鐨勮緭鍏ユ祦
+	 * @param machineFile
+	 * @return
+	 * @throws IOException
+	 */
+	public InputStream getInputStream(MachineFile machineFile) throws IOException {
+
+		Machine machine = machineService.getByCode(machineFile.getMachineCode());
+
+		String filePathStr = getBasePath(machine,machineFile.getDirType())+ File.separator+machineFile.getName();
+		Path filePath = Paths.get(filePathStr);
+		return Files.newInputStream(filePath);
+
+	}
+
+	@Transactional(readOnly = true)
+    public IPage<MachineBackFileVO> machineBackFilePageQuery(MachineBackFileQueryVO query) {
+		return this.baseMapper.machineBackFilePageQuery(Condition.getPage(query),query);
+    }
+
+	public MachineFile getExistsFile(String name, String dirType, String machineCode) {
+		return this.lambdaQuery().eq(MachineFile::getName, name)
+			.eq(MachineFile::getDirType, dirType)
+			.eq(MachineFile::getMachineCode, machineCode).list().stream().findFirst().orElse(null);
+	}
+
+	/**
+	 * 鍥炰紶绋嬪簭澶勭悊 鍒嗛〉鏌ヨ
+	 * @param query
+	 * @return
+	 */
+	public IPage<MachineBackFileVO> handlePageQuery(MachineBackFileHandleQueryVO query) {
+		IPage<MachineBackFileVO> page = this.getBaseMapper().handlePageQuery(Condition.getPage(query),query);
+		return page;
+	}
 }

--
Gitblit v1.9.3