Kaynağa Gözat

fix:
文件导入数量统计不准问题修复

18339543638 2 yıl önce
ebeveyn
işleme
f2142a2beb

+ 0 - 1
tr-modules/tr-module-export/src/main/java/cn/tr/module/excel/core/service/ExcelService.java

@@ -89,7 +89,6 @@ public class ExcelService {
         SysExportSheetDTO sheet = new SysExportSheetDTO();
         initSheet(sheet,filename,fileId,aClass);
         String sheetId = exportSheetService.insertSysExportSheetReturnId(sheet);
-        sheetManage.init(sheetId);
         try {
             EasyExcel
                     .read(IoUtil.toStream(content),aClass,new CustomerReadListener<T>(aClass,eventBus,sheetId))

+ 64 - 47
tr-modules/tr-module-export/src/main/java/cn/tr/module/excel/core/service/ExcelSheetManage.java

@@ -1,17 +1,23 @@
 package cn.tr.module.excel.core.service;
 
+import cn.hutool.core.util.NumberUtil;
 import cn.tr.module.excel.core.constant.ExcelConstant;
-import cn.tr.module.excel.mapper.SysExportSheetMapper;
-import cn.tr.module.excel.sheet.dto.SysExportSheetDTO;
+import cn.tr.module.excel.sheet.po.SysExportSheetPO;
 import cn.tr.module.excel.sheet.service.ISysExportSheetService;
-import org.springframework.beans.factory.annotation.Autowired;
+import org.redisson.api.RBucket;
+import org.redisson.api.RList;
+import org.redisson.api.RMap;
+import org.redisson.api.RedissonClient;
 import org.springframework.cache.Cache;
 import org.springframework.cache.CacheManager;
 import org.springframework.stereotype.Component;
 
-import java.util.HashMap;
+import java.time.Duration;
+import java.time.temporal.ChronoUnit;
 import java.util.Map;
 import java.util.Optional;
+import java.util.WeakHashMap;
+import java.util.concurrent.TimeUnit;
 
 /**
  * @ClassName : ExcelSheetManage
@@ -21,40 +27,30 @@ import java.util.Optional;
  */
 @Component
 public class ExcelSheetManage {
-    private CacheManager cacheManager;
+    private RedissonClient redissonClient;
     private Map<String, Cache> cacheMap;
     private ISysExportSheetService exportSheetService;
-    public ExcelSheetManage(CacheManager cacheManager, ISysExportSheetService exportSheetService) {
-        this.cacheManager = cacheManager;
-        this.cacheMap = new HashMap<>();
+    public ExcelSheetManage(RedissonClient redissonClient, ISysExportSheetService exportSheetService) {
+        this.redissonClient = redissonClient;
+        this.cacheMap = new WeakHashMap<>();
         this.exportSheetService=exportSheetService;
     }
 
-    /**
-     * 初始化工作簿
-     * @param sheetId
-     */
-    public void init(String sheetId){
-        getCache(sheetId);
-    }
-
     /**
      * 解析完成后判断工作簿数据是否处理完成
-     * @param sheetId
+     * @param sheet
      * @param totalCount
      */
-    public boolean handleFinishByParseFinish(String sheetId,int totalCount) throws Exception {
-        Cache cache = getCache(sheetId);
+    public boolean handleFinishByParseFinish(SysExportSheetPO sheet, int totalCount) throws Exception {
         //导入失败的数量
-        Integer failCount = Optional.ofNullable(cache.get(ExcelConstant.SHEET_FAIL_COUNT, Integer.class)).orElse(0);
+        Integer failCount = getFailCount(sheet.getId());
         //导入成功的数量
-        Integer successCount =Optional.ofNullable( cache.get(ExcelConstant.SHEET_SUCCESS_COUNT, Integer.class)).orElse(0);
-        cache.put(ExcelConstant.SHEET_TOTAL_COUNT,totalCount);
-        if(handleFinishByRowResult(sheetId,null)){
+        Integer successCount =getSuccessCount(sheet.getId());
+        putTotalCount(sheet.getId(),totalCount);
+        if(handleFinishByRowResult(sheet.getId(),null)){
             //导入完成
-            SysExportSheetDTO exportSheet = exportSheetService.selectSysExportSheetById(sheetId);
-
-            exportSheetService.handleSheetFinish(SysExportSheetMapper.INSTANCE.convertPO(exportSheet));
+            sheet.setTotalCount(totalCount);
+            exportSheetService.handleSheetFinish(sheet);
         }
         return totalCount==(failCount+successCount);
     }
@@ -65,47 +61,68 @@ public class ExcelSheetManage {
      * @param success
      */
     public boolean handleFinishByRowResult(String sheetId,Boolean success){
-        Cache cache = getCache(sheetId);
         //导入失败的数量
-        Integer failCount = Optional.ofNullable(cache.get(ExcelConstant.SHEET_FAIL_COUNT, Integer.class)).orElse(0);
+        Integer failCount = getFailCount(sheetId);
         //导入成功的数量
-        Integer successCount =Optional.ofNullable( cache.get(ExcelConstant.SHEET_SUCCESS_COUNT, Integer.class)).orElse(0);
+        Integer successCount =getSuccessCount(sheetId);
         if(Boolean.TRUE.equals(success)){
-            ++successCount;
-            cache.put(ExcelConstant.SHEET_SUCCESS_COUNT,successCount);
+            putSuccessCount(sheetId,++successCount);
         }else if(Boolean.FALSE.equals(success)){
-            ++failCount;
-            cache.put(ExcelConstant.SHEET_FAIL_COUNT,failCount);
+            putFailCount(sheetId,++failCount);
         }
         //导入总数
-        Integer totalCount =Optional.ofNullable( cache.get(ExcelConstant.SHEET_TOTAL_COUNT, Integer.class)).orElse(0);
-        cache.put(ExcelConstant.SHEET_TOTAL_COUNT,totalCount);
-        return totalCount==(failCount+successCount);
+        return getTotalCount(sheetId)==(failCount+successCount);
     }
 
     public void clear(String sheetId){
-        Cache cache = cacheMap.remove(getKey(sheetId));
-        if(cache!=null){
-            cache.invalidate();
+        RBucket<Object> cache = getCache(sheetId, "sheet-total");
+        RList<String> successList= getCacheList(sheetId, "sheet-success");
+        RList<String> failList= getCacheList(sheetId, "sheet-fail");
+        cache.delete();
+        successList.delete();
+        failList.delete();
+    }
+
+    public int getTotalCount(String sheetId){
+        RBucket<Object> cache = getCache(sheetId, "sheet-total");
+        String result = String.valueOf(cache.get());
+        if(NumberUtil.isNumber(result)){
+            return Integer.valueOf(result);
         }
+        return 0;
     }
 
     public int getSuccessCount(String sheetId){
-        Cache cache = getCache(sheetId);
-        return Optional.ofNullable( cache.get(ExcelConstant.SHEET_SUCCESS_COUNT, Integer.class)).orElse(0);
+        return getCacheList(sheetId, "sheet-success").size();
     }
 
     public int getFailCount(String sheetId){
-        Cache cache = getCache(sheetId);
-        return Optional.ofNullable( cache.get(ExcelConstant.SHEET_FAIL_COUNT, Integer.class)).orElse(0);
+        return getCacheList(sheetId, "sheet-fail").size();
+    }
+
+    public void putFailCount(String sheetId,Integer failCount){
+        getCacheList(sheetId, "sheet-fail").add(String.valueOf(failCount));
+    }
+
+    public void putSuccessCount(String sheetId,Integer successCount){
+        getCacheList(sheetId, "sheet-success").add(String.valueOf(successCount));
+    }
+
+    public void putTotalCount(String sheetId,Integer totalCount){
+        RBucket<Object> cache = getCache(sheetId, "sheet-total");
+        cache.expire(Duration.of(7,ChronoUnit.DAYS));
+        cache.set(totalCount);
     }
 
 
-    private Cache getCache(String sheetId){
-        return cacheMap.computeIfAbsent(getKey(sheetId), k -> cacheManager.getCache(k));
+    private RList<String> getCacheList(String sheetId,String prefix){
+        RList<String> result = redissonClient.getList(prefix + ":" + sheetId);
+        //默认七天
+        result.expireAsync(Duration.of(7, ChronoUnit.DAYS));
+        return result;
     }
 
-    private String getKey(String sheetId){
-        return "excel-sheet:"+sheetId;
+    private RBucket<Object> getCache(String sheetId,String prefix){
+        return  redissonClient.getBucket(prefix + ":" + sheetId);
     }
 }

+ 17 - 6
tr-modules/tr-module-export/src/main/java/cn/tr/module/excel/sheet/service/impl/SysExportSheetServiceImpl.java

@@ -3,6 +3,7 @@ package cn.tr.module.excel.sheet.service.impl;
 import cn.hutool.core.collection.CollectionUtil;
 import cn.hutool.core.io.FileUtil;
 import cn.hutool.core.io.IoUtil;
+import cn.hutool.core.util.ObjUtil;
 import cn.hutool.poi.excel.ExcelUtil;
 import cn.hutool.poi.excel.ExcelWriter;
 import cn.hutool.poi.excel.cell.CellUtil;
@@ -60,7 +61,16 @@ public class SysExportSheetServiceImpl implements ISysExportSheetService {
      */
     @Override
     public SysExportSheetDTO selectSysExportSheetById(String id){
-        return SysExportSheetMapper.INSTANCE.convertDto(baseRepository.selectById(id));
+        SysExportSheetDTO result = SysExportSheetMapper.INSTANCE.convertDto(baseRepository.selectById(id));
+        if(ObjUtil.isNotNull(result)){
+            if(ObjUtil.isNull(result.getSuccessCount())||ObjUtil.equal(result.getSuccessCount(),0)){
+                result.setSuccessCount(excelSheetManage.getSuccessCount(id));
+            }
+            if(ObjUtil.isNull(result.getFailCount())||ObjUtil.equal(result.getFailCount(),0)){
+                result.setFailCount(excelSheetManage.getFailCount(id));
+            }
+        }
+        return result;
     }
 
     @Override
@@ -114,8 +124,8 @@ public class SysExportSheetServiceImpl implements ISysExportSheetService {
         if(excelSheetManage.handleFinishByRowResult(sheetId,success)){
             //导入完成
             handleSheetFinish(sheet);
+            baseRepository.updateById(sheet);
         }
-        baseRepository.updateById(sheet);
     }
 
     @Override
@@ -125,13 +135,14 @@ public class SysExportSheetServiceImpl implements ISysExportSheetService {
         if(sheet==null){
             return;
         }
-        if(excelSheetManage.handleFinishByParseFinish(sheetId,totalCount)){
-            handleSheetFinish(sheet);
-        }else if(!SheetImportStatus.importCancel.getValue().equals(sheet.getStatus())){
+        sheet.setTotalCount(totalCount);
+        //解析完成后判断是否处理完成
+        if(excelSheetManage.handleFinishByParseFinish(sheet,totalCount)){
+
+        } else if(!SheetImportStatus.importCancel.getValue().equals(sheet.getStatus())){
             //没有导入成功且导入未取消
             sheet.setStatus(SheetImportStatus.parsingSuccess.getValue());
         }
-        sheet.setTotalCount(totalCount);
         baseRepository.updateById(sheet);
     }