2 Commits 42edf6c4e1 ... 85a0872adf

Author SHA1 Message Date
  刘嘉伟 85a0872adf Merge remote-tracking branch 'origin/master' 4 years ago
  zhanghai a7e449b3d0 档案管理 4 years ago

+ 15 - 7
src/main/java/com/gz/service/statistics/impl/SelectStatisticsServiceImpl.java

@@ -27,6 +27,7 @@ import com.gz.service.statistics.SelectStatisticsService;
 import com.gz.service.system.ArchivesTreeService;
 import com.gz.service.system.impl.AuthServiceImpl;
 import com.gz.utils.JwtUtils;
+import com.gz.utils.UnitUtils;
 import com.gz.vo.statistics.StatisticsVO;
 import lombok.AllArgsConstructor;
 import lombok.Data;
@@ -95,7 +96,7 @@ public class SelectStatisticsServiceImpl implements SelectStatisticsService {
             archivesTreeDTOS.forEach(dto -> {
                 if (dto.getParentId() != -1) {
                     for (ArchivesTreeDTO dt : archivesTreeDTOS) {
-                        if (dto.getParentId() == dt.getId()) {
+                        if (dto.getParentId().equals(dt.getId())) {
                             list.add(dt.getTitle());
                         }
                     }
@@ -119,12 +120,12 @@ public class SelectStatisticsServiceImpl implements SelectStatisticsService {
                                             //二级分类
                                             for (File category2 : category.listFiles()) {
                                                 if (ObjectUtil.isNotEmpty(category2)) {
-                                                    String s1 = this.getLinuxDirectorySize(category);
+                                                    String s1 = UnitUtils.storageUnitConvertStr(this.getLinuxDirectorySize(category));
                                                     map.put(category.getName(), s1);
                                                 }
                                             }
                                         }
-                                        String s2 = this.getLinuxDirectorySize(category);
+                                        String s2 = UnitUtils.storageUnitConvertStr(this.getLinuxDirectorySize(category));
                                         map.put(category.getName(), s2);
                                     }
                                 }
@@ -185,7 +186,7 @@ public class SelectStatisticsServiceImpl implements SelectStatisticsService {
                         // 年份
                         for (File year : qzh.listFiles()) {
                             if (ObjectUtil.isNotEmpty(year)) {
-                                String linuxDirectorySize = this.getLinuxDirectorySize(year);
+                                String linuxDirectorySize = UnitUtils.storageUnitConvertStr(this.getLinuxDirectorySize(year));
                                 archiveYearStatisticsRVOS.forEach(statisticsRVO -> {
                                     if (statisticsRVO.getGdnd().equals(year.getName())) {
                                         statisticsRVO.setSpace(linuxDirectorySize);
@@ -202,11 +203,12 @@ public class SelectStatisticsServiceImpl implements SelectStatisticsService {
     }
 
     /**
+     * @return 文件夹占用空间大小 单位:字节(K) 1GB = 1024MB = 1024 * 1024 KB = 1024 * 1024 * 1024 B
      * @description 获取Linux文件夹大小
      * @author ZhangHai
      * @since 2021/9/15 11:33
      */
-    private String getLinuxDirectorySize(File file) {
+    private Long getLinuxDirectorySize(File file) {
         if (!file.exists()) {
             throw new BusinessException(500, "文件见不存在");
         }
@@ -214,13 +216,19 @@ public class SelectStatisticsServiceImpl implements SelectStatisticsService {
             throw new BusinessException(500, "参数非文件见");
         }
         // 执行命令Format
-        final String commandFomat = "du -sh {}";
+        final String commandFomat = "du -s {}";
         // 需要执行的命令
         final String command = StrUtil.format(commandFomat, file.getAbsoluteFile());
         log.info("文件夹大小扫描命令执行:{}", command);
         // 执行命令的结果
         String commandResult = RuntimeUtil.execForStr(command);
-        return commandResult.substring(0, commandResult.indexOf(" "));
+        // 占用空间大小 String类型
+        String sizeStr = commandResult.substring(0, commandResult.indexOf("\t"));
+        Long size = 0L;
+        if (StrUtil.isNotEmpty(sizeStr)) {
+            size = Long.parseLong(sizeStr);
+        }
+        return size;
     }
 
     @Override

+ 53 - 0
src/main/java/com/gz/utils/UnitUtils.java

@@ -0,0 +1,53 @@
+package com.gz.utils;
+
+import cn.hutool.core.util.NumberUtil;
+
+import java.math.BigDecimal;
+
+/**
+ * 单位Utils
+ *
+ * @author LiuChangLan
+ * @since 2021/9/5 0:20
+ */
+public class UnitUtils {
+
+    /**
+     * 1KB
+     */
+    public static final Long KB = 1L * 1024;
+    /**
+     * 1MB
+     */
+    public static final Long MB = KB * 1024;
+    /**
+     * 1GB
+     */
+    public static final Long GB = MB * 1024;
+
+
+    /**
+     * 单位转换
+     *
+     * @author LiuChangLan
+     * @since 2021/9/5 0:28
+     */
+    public static String storageUnitConvertStr(Long byteValue) {
+        String value = "";
+        if (byteValue >= KB && byteValue < MB) {
+            value += NumberUtil.round(byteValue / 1.0 / KB, 2).toString();
+            value += "KB";
+        } else if (byteValue >= MB && byteValue < GB) {
+            value += NumberUtil.round(byteValue / 1.0 / MB, 2).toString();
+            value += "MB";
+        } else if (byteValue >= GB) {
+            value += NumberUtil.round(byteValue / 1.0 / GB, 2).toString();
+            value += "GB";
+        }
+        return value;
+    }
+
+    public static BigDecimal storageUnitConvertMB(Long byteValue) {
+        return NumberUtil.round(byteValue / 1.0 / MB, 2);
+    }
+}