Browse Source

Merge remote-tracking branch 'origin/feat_file_compare_manger' into feat_20250514_wangshuo

wangshuo 1 month ago
parent
commit
fd92461538

+ 18 - 14
ruoyi-admin/src/main/java/com/ruoyi/web/controller/system/SysRepoController.java

@@ -9,9 +9,11 @@ import com.ruoyi.common.constant.Constants;
 import com.ruoyi.common.utils.StringUtils;
 import com.ruoyi.common.utils.file.FileUploadUtils;
 import com.ruoyi.system.domain.SysRepoHistory;
+import com.ruoyi.common.utils.VersionUtils;
 import com.ruoyi.common.utils.OfficeCompareUtils;
 import com.ruoyi.system.service.ISysRepoHistoryService;
 import com.ruoyi.common.utils.file.FileUtils;
+import org.apache.commons.io.FilenameUtils;
 import org.springframework.http.MediaType;
 import org.springframework.security.access.prepost.PreAuthorize;
 import org.springframework.beans.factory.annotation.Autowired;
@@ -76,8 +78,10 @@ public class SysRepoController extends BaseController
     {
         sysRepo.setCreateBy(getUsername());
         sysRepo.setUpdateBy(getUsername());
-        String filename = StringUtils.substringAfterLast(sysRepo.getUrl(), "/");
+        String filename = FileUtils.getNameNotExtract(sysRepo.getUrl());
         sysRepo.setFileName(filename);
+        sysRepo.setRepoType(FileUtils.getFileExtension(filename));
+        sysRepo.setVersion(VersionUtils.incrementVersion(sysRepo.getVersion()));
         return toAjax(sysRepoService.insertSysRepo(sysRepo));
     }
 
@@ -94,9 +98,10 @@ public class SysRepoController extends BaseController
         }else{
             SysRepo  sysRepo_old = sysRepoService.selectSysRepoByRepoId(sysRepo.getRepoId());
             sysRepo.setUpdateBy(getUsername());
-            String filename = StringUtils.substringAfterLast(sysRepo.getUrl(), "/");
+            String filename = FileUtils.getNameNotExtract(sysRepo.getUrl());
             sysRepo.setFileName(filename);
-            sysRepo.setVersion("v1.1.1");
+            sysRepo.setRepoType(FileUtils.getFileExtension(filename));
+            sysRepo.setVersion(VersionUtils.incrementVersion(sysRepo_old.getVersion()));
             SysRepoHistory sysRepoHistory = new SysRepoHistory();
             sysRepoHistory.setRepoId(sysRepo_old.getRepoId());
             sysRepoHistory.setUrl(sysRepo_old.getUrl());
@@ -110,8 +115,12 @@ public class SysRepoController extends BaseController
             if(sysRepoService.updateSysRepo(sysRepo)<1){
                 return AjaxResult.error("文件插入失败");
             }
-            Set<String> set =  OfficeCompareUtils.compare(sysRepoHistory.getUrl(),sysRepo.getUrl(),"spire");
-            return AjaxResult.success(set);
+            if(sysRepo.getRepoType().equals(".docx")){
+                Set<String> set =  OfficeCompareUtils.compare(sysRepoHistory.getUrl(),sysRepo.getUrl(),"spire");
+                return AjaxResult.success(set);
+            }
+            return success();
+
         }
     }
     /**
@@ -143,19 +152,14 @@ public class SysRepoController extends BaseController
         sysRepoHistory_repo.setFileName(sysRepo.getFileName());
         sysRepoHistory_repo.setVersion(sysRepo.getVersion());
         sysRepoHistory_repo.setRemark(sysRepo.getRemark());
-        if(sysRepoHistoryService.insertSysRepoHistory(sysRepoHistory_repo)<1){
-            return AjaxResult.error("文件插入失败");
-        }
-//        SysRepoHistory sysRepoHistory_back = sysRepoHistoryService.selectSysRepoHistory(sysRepoHistory);
-//        sysRepoHistory_back.sethId(null);
-//        if(sysRepoHistoryService.insertSysRepoHistory(sysRepoHistory_back)<1){
-//            return AjaxResult.error("文件插入失败");
-//        }
         sysRepo.setUpdateBy(getUsername());
         sysRepo.setUrl(sysRepoHistory.getUrl());
-        sysRepo.setVersion(sysRepoHistory.getVersion());
+        sysRepo.setVersion(VersionUtils.incrementVersion(sysRepo.getVersion()));
         sysRepo.setFileName(sysRepoHistory.getFileName());
         sysRepo.setRemark("回滚版本"+sysRepoHistory.getVersion());
+        if(sysRepoHistoryService.insertSysRepoHistory(sysRepoHistory_repo)<1){
+            return AjaxResult.error("文件插入失败");
+        }
         if(sysRepoService.updateSysRepo(sysRepo)<1){
             return AjaxResult.error("文件插入失败");
         }

+ 25 - 0
ruoyi-common/src/main/java/com/ruoyi/common/utils/VersionUtils.java

@@ -0,0 +1,25 @@
+package com.ruoyi.common.utils;
+
+public class VersionUtils {
+    /**
+     * 将版本号加1
+     * @param version 当前版本号,格式为"V"后跟一个数字,例如"V1"
+     * @return 新版本号,例如传入"V1"返回"V2"
+     * @throws IllegalArgumentException 如果版本号格式不正确
+     */
+    public static String incrementVersion(String version) {
+        if (version == null || version.isEmpty()) {
+            return "V0";
+        }
+        if (!version.startsWith("V")) {
+            throw new IllegalArgumentException("版本号必须以'V'开头");
+        }
+        String numberPart = version.substring(1);
+        if (!numberPart.matches("\\d+")) {
+            throw new IllegalArgumentException("版本号格式不正确,应为'V'后跟一个数字");
+        }
+        int number = Integer.parseInt(numberPart);
+        int incrementedNumber = number + 1;
+        return "V" + incrementedNumber;
+    }
+}

+ 41 - 0
ruoyi-common/src/main/java/com/ruoyi/common/utils/file/FileUtils.java

@@ -288,4 +288,45 @@ public class FileUtils
         String baseName = FilenameUtils.getBaseName(fileName);
         return baseName;
     }
+
+    /**
+     * 获取文件名称 /profile/upload/2025/05/20/test_20250520101754A001.png -- test.png
+     *
+     * @param fileName 路径名称
+     * @return 没有文件路径以及扩展的上传文件名称
+     */
+    public static String getNameNotExtract(String fileName)
+    {
+        if (fileName == null)
+        {
+            return null;
+        }
+        int lastUnixPos = fileName.lastIndexOf('/');
+        int lastWindowsPos = fileName.lastIndexOf('\\');
+        int index = Math.max(lastUnixPos, lastWindowsPos);
+        String  baseName = fileName.substring(index + 1);
+        int lastUnderscoreIndex = baseName.lastIndexOf('_');
+        int lastDotIndex = baseName.lastIndexOf('.');
+        if (lastDotIndex == -1 || lastDotIndex < lastUnderscoreIndex) {
+            return baseName.substring(0, lastUnderscoreIndex);
+        }
+        return baseName.substring(0, lastUnderscoreIndex) + baseName.substring(lastDotIndex);
+    }
+
+    /**
+     * 获取文件名称
+     *
+     * @param fileName 路径名称
+     * @return 文件类型后缀
+     */
+    public static String getFileExtension(String fileName) {
+        if (fileName == null || fileName.isEmpty()) {
+            return null;
+        }
+        int lastDotIndex = fileName.lastIndexOf('.');
+        if (lastDotIndex == -1 || lastDotIndex == 0) {
+            return "";
+        }
+        return fileName.substring(lastDotIndex + 1);
+    }
 }