|
@@ -1,6 +1,7 @@
|
|
|
package com.rongwei.zhsw.system.service.impl;
|
|
|
|
|
|
import com.alibaba.excel.EasyExcel;
|
|
|
+import com.google.common.collect.Lists;
|
|
|
import com.rongwe.zhsw.system.domain.SwWaterUsageEntryDo;
|
|
|
import com.rongwe.zhsw.system.vo.ImportMeterReadingRecordVo;
|
|
|
import com.rongwei.rwadmincommon.system.vo.SysUserVo;
|
|
@@ -12,18 +13,20 @@ import com.rongwei.zhsw.system.importListener.MeterReadingRecordListener;
|
|
|
import com.rongwei.zhsw.system.service.ImportExcelService;
|
|
|
import com.rongwei.zhsw.system.utils.ZHSWCommonUtils;
|
|
|
import org.apache.commons.lang.StringUtils;
|
|
|
+import org.apache.ibatis.session.ExecutorType;
|
|
|
+import org.apache.ibatis.session.SqlSession;
|
|
|
+import org.apache.ibatis.session.SqlSessionFactory;
|
|
|
import org.slf4j.Logger;
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.beans.factory.annotation.Qualifier;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
-import org.springframework.transaction.annotation.Propagation;
|
|
|
-import org.springframework.transaction.annotation.Transactional;
|
|
|
-import org.springframework.web.context.request.RequestAttributes;
|
|
|
-import org.springframework.web.context.request.RequestContextHolder;
|
|
|
+import org.springframework.transaction.support.TransactionTemplate;
|
|
|
|
|
|
-import javax.servlet.http.HttpServletRequest;
|
|
|
import java.io.File;
|
|
|
import java.util.List;
|
|
|
+import java.util.concurrent.CompletableFuture;
|
|
|
+import java.util.concurrent.Executor;
|
|
|
|
|
|
import static com.rongwei.rwcommon.utils.UtilsChecks.parameterCheck;
|
|
|
|
|
@@ -43,6 +46,14 @@ public class ImportExcelServiceImpl implements ImportExcelService {
|
|
|
private SwWaterUsageEntryServiceImpl waterUsageEntryService;
|
|
|
@Autowired
|
|
|
private BillGenerationServiceImpl billGenerationService;
|
|
|
+ @Autowired
|
|
|
+ private SqlSessionFactory sqlSessionFactory;
|
|
|
+
|
|
|
+
|
|
|
+ @Qualifier("zhswThreadPool")
|
|
|
+ @Autowired
|
|
|
+ private Executor asyncTaskExecutor;
|
|
|
+
|
|
|
private static final Logger log = LoggerFactory.getLogger(ImportExcelServiceImpl.class);
|
|
|
|
|
|
/**
|
|
@@ -62,27 +73,50 @@ public class ImportExcelServiceImpl implements ImportExcelService {
|
|
|
// 获取处理之后的数据
|
|
|
long entTime = System.currentTimeMillis();
|
|
|
log.error("excel解析时长:{}", startTime - entTime);
|
|
|
- startTime = System.currentTimeMillis();
|
|
|
List<SwWaterUsageEntryDo> saveList = meterReadingRecordListener.getData();
|
|
|
- this.save(saveList);
|
|
|
- entTime = System.currentTimeMillis();
|
|
|
- log.error("数据保存时长:{}", startTime - entTime);
|
|
|
- startTime = System.currentTimeMillis();
|
|
|
SysUserVo currentUser = ZHSWCommonUtils.getCurrentUser();
|
|
|
- ContextHolder.setValue("dsKey",currentUser.getTenantDo().getDskey());
|
|
|
- billGenerationService.asyncGenerateBill(saveList);
|
|
|
- entTime = System.currentTimeMillis();
|
|
|
- log.error("账单生成时长:{}", startTime - entTime);
|
|
|
- ContextHolder.clear();
|
|
|
+ String dskey = currentUser.getTenantDo().getDskey();
|
|
|
+ ContextHolder.setValue("dsKey", dskey);
|
|
|
+ this.dataSaveAndPostProcessing(saveList, dskey);
|
|
|
+
|
|
|
return R.ok();
|
|
|
}
|
|
|
|
|
|
- @Transactional(propagation = Propagation.REQUIRES_NEW)
|
|
|
- public void save(List<SwWaterUsageEntryDo> datas) {
|
|
|
- waterUsageEntryService.saveBatch(datas, 1000);
|
|
|
- }
|
|
|
+ // @Transactional(propagation = Propagation.REQUIRES_NEW)
|
|
|
+ public void dataSaveAndPostProcessing(List<SwWaterUsageEntryDo> datas, String dskey) {
|
|
|
+ long asyncTasksStart = System.currentTimeMillis();
|
|
|
+ List<List<SwWaterUsageEntryDo>> partition = Lists.partition(datas, 1000); // 动态分片
|
|
|
+ CompletableFuture<Void> allFutures = CompletableFuture.allOf(
|
|
|
+ partition.stream()
|
|
|
+ .map(data ->
|
|
|
+ CompletableFuture.runAsync(() -> {
|
|
|
+ long startTime = System.currentTimeMillis();
|
|
|
+ waterUsageEntryService.getBaseMapper().ListBatchSave(data, dskey);
|
|
|
+ log.debug("数据保存耗时: {}ms,总条数:{}", System.currentTimeMillis() - startTime, data.size());
|
|
|
+ },
|
|
|
+ asyncTaskExecutor).whenComplete((result, ex) -> {
|
|
|
+ }))
|
|
|
+ .toArray(CompletableFuture[]::new)
|
|
|
+ );
|
|
|
+
|
|
|
+ allFutures.thenRun(() -> {
|
|
|
+ log.debug("数据保存总耗时: {}ms", System.currentTimeMillis() - asyncTasksStart);
|
|
|
+ billGenerationService.asyncGenerateBill(datas); // 同步生成账单
|
|
|
+ ContextHolder.clear();
|
|
|
+ });
|
|
|
+ // partition.parallelStream().forEach(data -> {
|
|
|
+ // waterUsageEntryService.getBaseMapper().ListBatchSave(data, dskey);
|
|
|
+ // });
|
|
|
+
|
|
|
+ // CompletableFuture<?>[] futures = partition.parallelStream()
|
|
|
+ // .map(data -> CompletableFuture.runAsync(() ->
|
|
|
+ // waterUsageEntryService.getBaseMapper().ListBatchSave(data, dskey), asyncTaskExecutor)
|
|
|
+ // )
|
|
|
+ // .toArray(CompletableFuture[]::new);
|
|
|
|
|
|
|
|
|
+ }
|
|
|
+
|
|
|
public File readFile(String fileId) {
|
|
|
parameterCheck(() -> StringUtils.isBlank(fileId), "请上传文件", "文件ID为空");
|
|
|
SysFileItemDo fileItemDo = sysFileItemDao.selectById(fileId);
|