From c4d687aacfb4e7b6ee5ce67df93cf2f8d8df80c1 Mon Sep 17 00:00:00 2001
From: xiejun <xj@2023>
Date: 星期日, 26 十一月 2023 14:05:48 +0800
Subject: [PATCH] Merge remote-tracking branch 'origin/master'

---
 Source/UBCS/ubcs-ops/ubcs-log/src/main/java/com/vci/ubcs/log/service/impl/LogLocalServiceImpl.java |  310 +++++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 310 insertions(+), 0 deletions(-)

diff --git a/Source/UBCS/ubcs-ops/ubcs-log/src/main/java/com/vci/ubcs/log/service/impl/LogLocalServiceImpl.java b/Source/UBCS/ubcs-ops/ubcs-log/src/main/java/com/vci/ubcs/log/service/impl/LogLocalServiceImpl.java
new file mode 100644
index 0000000..fb50d00
--- /dev/null
+++ b/Source/UBCS/ubcs-ops/ubcs-log/src/main/java/com/vci/ubcs/log/service/impl/LogLocalServiceImpl.java
@@ -0,0 +1,310 @@
+package com.vci.ubcs.log.service.impl;
+
+import com.vci.ubcs.log.vo.LocalLogVO;
+import com.vci.ubcs.log.service.ILogLocalService;
+import com.vci.ubcs.log.entity.LocalLog;
+import com.vci.ubcs.resource.utils.FileUtil;
+import com.vci.ubcs.resource.utils.ZipUtil;
+import com.vci.ubcs.starter.exception.VciBaseException;
+import lombok.extern.slf4j.Slf4j;
+import org.apache.commons.io.IOUtils;
+import org.springblade.core.log.exception.ServiceException;
+import com.vci.ubcs.omd.cache.EnumCache;
+import com.vci.ubcs.omd.enums.EnumEnum;
+import com.vci.ubcs.resource.bo.FileObjectBO;
+import org.springblade.core.tool.api.R;
+import org.springblade.core.tool.utils.Func;
+import org.springframework.beans.factory.annotation.Value;
+import org.springframework.stereotype.Service;
+
+import javax.annotation.Resource;
+import java.io.*;
+import java.nio.file.FileSystems;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.attribute.BasicFileAttributes;
+import java.rmi.ServerException;
+import java.text.SimpleDateFormat;
+import java.util.*;
+import java.util.stream.Collectors;
+
+/**
+ * 鏈湴绯荤粺鏃ュ織
+ * @author ludc
+ * @date 2023/10/31 15:39
+ */
+@Service
+@Slf4j
+public class LogLocalServiceImpl implements ILogLocalService{
+
+	/**
+	 * 鍚勪釜鏈嶅姟瀛樻斁鐨勭殑鐖惰矾寰�
+	 */
+	@Value("${local-log.parent-path:/data1/ubcs/ubcs-server}")
+	private String PARENTPATH;
+
+	/**
+	 * 鏃ュ織鏂囦欢鐨勫叿浣撲綅缃�
+	 */
+	@Value("${local-log.log-path:/target/log}")
+	private String LOGPATH;
+
+	/**
+	 * 褰撳墠鎿嶄綔绯荤粺锛屾槸鍚︿负windows绯荤粺
+	 */
+	private Boolean isWindows = true;
+
+	{
+		String os = System.getProperty("os.name").toLowerCase();
+		//榛樿灏遍厤缃负windows鐨勶紝濡傛灉涓嶆槸褰撳墠绯荤粺涓嶆槸windows灏遍渶瑕佸鍏惰浆鎹负linux鐨勬枃浠惰矾寰勬牸寮�
+		if (!os.contains("win")) {
+			this.PARENTPATH = this.PARENTPATH.substring(this.PARENTPATH.lastIndexOf(":") + 1).replace("\\", "/");
+			this.LOGPATH = this.LOGPATH.replace("\\", "/");
+			this.isWindows = false;
+		}
+	}
+
+	/**
+	 * 鍘嬬缉鏂囦欢鐨勫伐鍏风被
+	 */
+	@Resource
+	private ZipUtil zipUtil;
+
+	/**
+	 * 鑾峰彇鏈湴鏃ュ織鍒楄〃
+	 * @param logParentPath
+	 * @return
+	 */
+	//@Override
+	public List<LocalLog> getSystemLogList(String logParentPath) {
+		List<LocalLog> localLogs = new ArrayList<>();
+		// 涓嶄负绌鸿鏄庢槸鍔犺浇褰撳墠杩欎釜鏈嶅姟璺緞涓嬬殑鏃ュ織鏂囦欢
+		if(Func.isNotEmpty(logParentPath)){
+			File file = new File(logParentPath);
+			if (file.isDirectory()) {
+				File[] files = file.listFiles();
+				Arrays.stream(files).forEach(item->{
+					// 缁勫缓鏃ュ織鏂囦欢瀵硅薄
+					LocalLog localLog = new LocalLog();
+					localLog.setLogName(item.getName());
+					localLog.setLogType(getLogType(item.getName()));
+					localLog.setCreateTime(getLastModifiedOrCreatTime(false,logParentPath));
+					localLog.setLastModifier(getLastModifiedOrCreatTime(true,logParentPath));
+					localLog.setLogPath(logParentPath);
+					String serviceId = getServiceId(logParentPath);
+					localLog.setServiceId(serviceId);
+					localLog.setServiceName(getServiceName(serviceId));
+					localLog.setHasChildren(false);
+					localLogs.add(localLog);
+				});
+			}
+		}else {
+			File fileDir = new File(PARENTPATH);
+			File[] childDir = fileDir.listFiles();
+			Arrays.stream(childDir).forEach(dir->{
+				if(dir.getName().contains("ubcs_")){
+					String fullPath = dir.getPath() + LOGPATH;
+					File file = new File(fullPath);
+					LocalLog localLog = new LocalLog();
+					localLog.setLastModifier(getLastModifiedOrCreatTime(true,fullPath));
+					localLog.setCreateTime(getLastModifiedOrCreatTime(false,fullPath));
+					localLog.setLogPath(fullPath);
+					String serviceId = getServiceId(file.getPath());
+					localLog.setServiceId(serviceId);
+					String serviceName = getServiceName(serviceId);
+					localLog.setServiceName(serviceName);
+					localLog.setLogType(serviceName+"鏃ュ織鐖剁洰褰�");
+					localLog.setLogName(serviceName+"鏃ュ織鐖剁洰褰�");
+					localLog.setHasChildren(true);
+					localLogs.add(localLog);
+				}
+			});
+		}
+		return localLogs;
+	}
+
+	/**
+	 * 鑾峰彇鏂囦欢鏈�鍚庝慨鏀规垨鑰呭垱寤烘椂闂�
+	 * @param isModifier
+	 * @return
+	 */
+	private String getLastModifiedOrCreatTime(boolean isModifier,String pathStr) {
+		Path path = FileSystems.getDefault().getPath(pathStr);
+		String date = "";
+		try {
+			BasicFileAttributes attr = Files.readAttributes(path, BasicFileAttributes.class);
+			SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
+			// 鏄幏鍙栨渶鍚庝慨鏀规椂闂�
+			if(isModifier){
+				date = dateFormat.format(new Date(attr.lastModifiedTime().toMillis()));
+			}else {
+				date = dateFormat.format(new Date(attr.creationTime().toMillis()));
+			}
+		} catch (IOException e) {
+			throw new ServiceException("Error reading file date attributes: " + e.getMessage());
+		}
+		return date;
+	}
+
+	/**
+	 * 鑾峰彇鏃ュ織绫诲瀷
+	 * @param fileName
+	 * @return
+	 */
+	private String getLogType(String fileName){
+		//鍒ゆ柇鏃ュ織鐨勭殑绫诲瀷
+		if (fileName.contains("error")) {
+			return "Error";
+		} else if (fileName.contains("info")) {
+			return "Info";
+		} else if (fileName.contains("warning")) {
+			return "Warning";
+		} else {
+			return "Unknown";
+		}
+	}
+
+	/**
+	 * 鑾峰彇鏈嶅姟ID
+	 * @param servciePath
+	 * @return
+	 */
+	private String getServiceId(String servciePath){
+		// 鏍规嵁褰撳墠鎿嶄綔绯荤粺鏉ュ喅瀹氭槸閫氳繃浠�涔堝瓧绗︽潵鎴彇
+		String[] parts = servciePath.split(this.isWindows ? "\\\\":"/");
+		String extractedString = "";
+		if(parts.length > 3){
+			extractedString = parts[parts.length - 3];
+		}
+		return extractedString;
+	}
+
+	/**
+	 * 鑾峰彇鏈嶅姟鍚嶇О
+	 * @param serViceId
+	 * @return
+	 */
+	private String getServiceName(String serViceId){
+		return EnumCache.getValue(EnumEnum.SERCIVE_NAME_ROLE, serViceId);
+	}
+
+	/**
+	 * 鎴彇璺緞涓殑鏃ュ織鏂囦欢鍚嶇О
+	 * @param logFullPath
+	 * @return
+	 */
+	private String getLogFileName(String logFullPath){
+		// 鏍规嵁褰撳墠鎿嶄綔绯荤粺鏉ュ喅瀹氭槸閫氳繃浠�涔堝瓧绗︽潵鎴彇
+		String[] parts = logFullPath.split(this.isWindows ? "\\\\":"/");
+		String logFileName = "";
+		if(parts.length > 3){
+			logFileName = parts[parts.length - 1];
+		}
+		return logFileName;
+	}
+
+	/**
+	 * 涓嬭浇鏃ュ織鏂囦欢
+	 * @param localLogVO 涓嬭浇鏃ュ織瀵硅薄
+	 * @return
+	 * @throws ServerException
+	 */
+	@Override
+	public FileObjectBO downloadLogByServiceNameAndFileName(LocalLogVO localLogVO) throws ServerException {
+		if(Func.isEmpty(localLogVO) || Func.isEmpty(localLogVO.getLogPath())){
+			throw new ServerException("鏈幏鍙栧埌璇ユ棩蹇楄矾寰勶紒");
+		}
+		FileObjectBO fileObjectBO = new FileObjectBO();
+		String logFullPaths = localLogVO.getLogFullPaths();
+		// 鍒ゆ柇鏄惁鏄埗鐩綍
+		if(!localLogVO.getHasChildren()){
+			//鍙笅杞戒竴涓棩蹇楁枃浠�
+			File file = new File(logFullPaths);
+			if(!file.isFile() || !file.exists()){
+				throw new ServerException("鏈湴鏃ュ織鏂囦欢璺緞"+ logFullPaths +"涓湭鎵惧埌鏃ュ織");
+			}
+			try {
+				fileObjectBO.setName(file.getName());
+				fileObjectBO.setInputStream(new FileInputStream(file));
+				fileObjectBO.setFileLocalPath(logFullPaths);
+				fileObjectBO.setFileExtension(".log");
+			}catch (Throwable e){
+				throw new VciBaseException("鑾峰彇鏂囦欢鐨勬祦鏈夐棶棰�",new String[]{logFullPaths},e);
+			}
+		}else{
+			// 鏄埗鐩綍锛屾墍浠ラ渶瑕佽幏鍙栧埌涓嬮潰鐨勬墍鏈夊瓙鐩綍
+			// 鏈夊涓紝闇�瑕佷娇鐢▃ip杩涜鍘嬬缉
+			String tempFolder = FileUtil.getDefaultTempFolder();
+			File[] file1 = new File(logFullPaths).listFiles();
+			if(file1.length > 0){
+				Arrays.stream(file1).forEach(item->{
+					String fileName = tempFolder + File.separator + System.currentTimeMillis() + ".log";
+					File file = new File(fileName);
+					try {
+						if(!file.exists()) {
+							file.createNewFile();
+						}
+					}catch (Throwable e){
+						throw new VciBaseException("鍒涘缓鏂囦欢鍑洪敊,{0}",new String[]{fileName});
+					}
+					File logFile = new File(item.getPath());
+					if(!logFile.exists() || !logFile.isFile()){
+						throw new VciBaseException("鏈湴鏃ュ織鏂囦欢璺緞"+item.getPath()+"涓湭鎵惧埌鏃ュ織");
+					}
+
+					try(OutputStream os = new FileOutputStream(file);
+						InputStream ins = new FileInputStream(logFile);
+					){
+						IOUtils.copy(ins,os);
+					}catch (Throwable e){
+						throw new VciBaseException("涓嬭浇鏂囦欢鍒颁复鏃舵枃浠跺す閲屽嚭閿�,{0}",new String[]{fileName});
+					}
+				});
+				String zipName = new File(tempFolder).getPath() + File.separator + getLogFileName(logFullPaths) + "绛�"+file1.length + "涓枃浠�.zip";
+				zipUtil.folderToZipFile(tempFolder,zipName);
+				fileObjectBO.setFileLocalPath(zipName);
+				fileObjectBO.setFileExtension(".log");
+				if(log.isDebugEnabled()){
+					log.debug("涓嬭浇鏂囦欢鐨勪俊鎭�,",zipName);
+				}
+			}
+		}
+		return fileObjectBO;
+	}
+
+	/**
+	 * 鍒犻櫎鏃ュ織鏂囦欢
+	 * @param localLogVO 鏂囦欢鍏ㄨ矾寰勯泦鍚�
+	 * @throws ServerException
+	 */
+	@Override
+	public R deleteLogFile(LocalLogVO localLogVO) throws ServerException {
+		List<String> resMsgs = new ArrayList<>();
+		// 鍒ゆ柇鏄惁鏄棩蹇楅《灞傜洰褰�
+		if(localLogVO.getHasChildren()){
+			// 鏄《灞傜洰褰曪紝闇�瑕佸惊鐜幓鍒犻櫎鍖呭惈鐨勬墍鏈夋棩蹇楁枃浠�
+			File parentFile = new File(localLogVO.getLogFullPaths());
+			if(parentFile.isDirectory()){
+				Arrays.stream(parentFile.listFiles()).forEach(logFile->{
+					if (logFile.exists()) {
+						//鍒犻櫎澶辫触鐨勭洿鎺ヨ褰曚笅鏂囦欢鍚�
+						if (!logFile.delete()) {
+							resMsgs.add(getLogFileName(logFile.getPath()));
+						}
+					}
+				});
+			}
+		}else {
+			// 鍗曚釜鍒犻櫎
+			File file = new File(localLogVO.getLogFullPaths());
+			if (file.exists()) {
+				//鍒犻櫎澶辫触鐨勭洿鎺ヨ褰曚笅鏂囦欢鍚�
+				if (!file.delete()) {
+					resMsgs.add(getLogFileName(localLogVO.getLogFullPaths()));
+				}
+			}
+		}
+		return resMsgs.size()==0 ? R.success("鍒犻櫎鎴愬姛!"):R.fail("浠ヤ笅鏃ュ織鏂囦欢锛�"+resMsgs.stream().collect(Collectors.joining(","))+"鍒犻櫎澶辫触!");
+	}
+
+}

--
Gitblit v1.9.3