|
@@ -1,17 +1,23 @@
|
|
|
package cn.tr.module.excel.core.service;
|
|
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.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 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.Cache;
|
|
|
import org.springframework.cache.CacheManager;
|
|
import org.springframework.cache.CacheManager;
|
|
|
import org.springframework.stereotype.Component;
|
|
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.Map;
|
|
|
import java.util.Optional;
|
|
import java.util.Optional;
|
|
|
|
|
+import java.util.WeakHashMap;
|
|
|
|
|
+import java.util.concurrent.TimeUnit;
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
|
* @ClassName : ExcelSheetManage
|
|
* @ClassName : ExcelSheetManage
|
|
@@ -21,40 +27,30 @@ import java.util.Optional;
|
|
|
*/
|
|
*/
|
|
|
@Component
|
|
@Component
|
|
|
public class ExcelSheetManage {
|
|
public class ExcelSheetManage {
|
|
|
- private CacheManager cacheManager;
|
|
|
|
|
|
|
+ private RedissonClient redissonClient;
|
|
|
private Map<String, Cache> cacheMap;
|
|
private Map<String, Cache> cacheMap;
|
|
|
private ISysExportSheetService exportSheetService;
|
|
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;
|
|
this.exportSheetService=exportSheetService;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- /**
|
|
|
|
|
- * 初始化工作簿
|
|
|
|
|
- * @param sheetId
|
|
|
|
|
- */
|
|
|
|
|
- public void init(String sheetId){
|
|
|
|
|
- getCache(sheetId);
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
/**
|
|
/**
|
|
|
* 解析完成后判断工作簿数据是否处理完成
|
|
* 解析完成后判断工作簿数据是否处理完成
|
|
|
- * @param sheetId
|
|
|
|
|
|
|
+ * @param sheet
|
|
|
* @param totalCount
|
|
* @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);
|
|
return totalCount==(failCount+successCount);
|
|
|
}
|
|
}
|
|
@@ -65,47 +61,68 @@ public class ExcelSheetManage {
|
|
|
* @param success
|
|
* @param success
|
|
|
*/
|
|
*/
|
|
|
public boolean handleFinishByRowResult(String sheetId,Boolean 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)){
|
|
if(Boolean.TRUE.equals(success)){
|
|
|
- ++successCount;
|
|
|
|
|
- cache.put(ExcelConstant.SHEET_SUCCESS_COUNT,successCount);
|
|
|
|
|
|
|
+ putSuccessCount(sheetId,++successCount);
|
|
|
}else if(Boolean.FALSE.equals(success)){
|
|
}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){
|
|
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){
|
|
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){
|
|
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);
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|