Browse Source

调整批量导出的逻辑

yuj 7 years ago
parent
commit
1ddec29afc

+ 3 - 3
src/main/java/com/uas/platform/b2c/core/constant/ExcelConstant.java

@@ -13,15 +13,15 @@ public class ExcelConstant {
     public static final Integer PER_SHEET_ROW_COUNT = 1000000;
 
     /**
-     * 每次向EXCEL写入的记录数(查询每页数据大小) 20W
+     * 每次向EXCEL写入的记录数(查询每页数据大小) 1W
      */
     public static final Integer PER_WRITE_ROW_COUNT = 10000;
 
 
     /**
-     * 每个sheet的写入次数 5
+     * 每个sheet的写入次数 100
      */
-    public static final Integer PER_SHEET_WRITE_COUNT = PER_SHEET_ROW_COUNT / PER_WRITE_ROW_COUNT;
+    public static final Integer PER_SHEET_WRITE_COUNT = 100;
 
 
 }

+ 6 - 2
src/main/java/com/uas/platform/b2c/core/utils/PoiUtil.java

@@ -174,13 +174,17 @@ public class PoiUtil {
 
         // 调用委托类分批写数据
         int sheetCount = wb.getNumberOfSheets();
+        int totalSheet = totalRowCount / ExcelConstant.PER_SHEET_ROW_COUNT;
         for (int i = 0; i < sheetCount; i++) {
             Sheet eachSheet = wb.getSheetAt(i);
-
-            for (int j = 1; j <= ExcelConstant.PER_SHEET_WRITE_COUNT; j++) {
+            long l = System.currentTimeMillis();
+            int perTime = i < totalSheet ? ExcelConstant.PER_SHEET_WRITE_COUNT : Math.floorDiv((totalRowCount - (totalSheet - i) * ExcelConstant.PER_SHEET_ROW_COUNT), ExcelConstant.PER_WRITE_ROW_COUNT);
+            for (int j = 1; j <= perTime; j++) {
                 writeExcelDataDelegated.writeExcelData(eachSheet, ((j - 1) * ExcelConstant.PER_WRITE_ROW_COUNT + 1), (j * ExcelConstant.PER_WRITE_ROW_COUNT),
                         (i * ExcelConstant.PER_SHEET_WRITE_COUNT + j), ExcelConstant.PER_WRITE_ROW_COUNT);
             }
+            long l1 = System.currentTimeMillis();
+            logger.info("一次生成excel的时间是:" + (l1 - l));
         }
 
         // 下载EXCEL

+ 9 - 0
src/main/java/com/uas/platform/b2c/prod/commodity/service/impl/GoodsServiceImpl.java

@@ -4207,7 +4207,10 @@ public class GoodsServiceImpl implements GoodsService {
         }
         StringBuffer group = new StringBuffer("group by pr_kind, pr_kindid");
         StringBuffer countSql = new StringBuffer("select count(pr_id) ").append(where);
+        long l = System.currentTimeMillis();
         Integer totalRowCount = commonDao.queryForObject(countSql.toString(), Integer.class);
+        long l1 = System.currentTimeMillis();
+        logger.info("统计需要导出的数据耗时:" + (l1 - l));
 
         StringBuffer sql = new StringBuffer("select pr_kind as thirdKind, pr_kindid as kindId, count(1) as count").append(where).append(group).append(" order by count desc limit %d, %d");
 
@@ -4219,13 +4222,17 @@ public class GoodsServiceImpl implements GoodsService {
             @Override
             public void writeExcelData(Sheet eachSheet, Integer startRowCount, Integer endRowCount, Integer currentPage, Integer pageSize) throws Exception {
                 Integer startNumber = (currentPage - 1) * ExcelConstant.PER_WRITE_ROW_COUNT;
+                long l2 = System.currentTimeMillis();
                 List<KindHierarchicalCount> kindHierarchicals = jdbcTemplate.query(String.format(sql.toString(), startNumber, pageSize), new BeanPropertyRowMapper<>(KindHierarchicalCount.class));
+                long l3 = System.currentTimeMillis();
+                logger.info("获取KindHierarchicalCount需要多少时间:" + (l3 - l2));
                 List<Long> ids = new ArrayList<>();
                 for (KindHierarchicalCount kindHierarchical : kindHierarchicals) {
                     if (kindHierarchical.getKindId() != null) {
                         ids.add(kindHierarchical.getKindId());
                     }
                 }
+                long l4 = System.currentTimeMillis();
                 Map<String, List<Long>> map = null;
                 for (int i = 0; (i < 3) && CollectionUtils.isNotEmpty(ids); i++) {
                     // i = 0 时表示 获取本级, = 1 时代表获取第二层,= 2 时代表获取顶层
@@ -4264,6 +4271,8 @@ public class GoodsServiceImpl implements GoodsService {
 
                     }
                 }
+                long l5 = System.currentTimeMillis();
+                logger.info("需要消耗的时间是:" + (l5 - l4));
 
                 if (!CollectionUtils.isEmpty(kindHierarchicals)) {
                     for (int i = startRowCount; i <= endRowCount; i++) {

+ 1 - 1
src/main/resources/pre/account.properties

@@ -3,7 +3,7 @@ account.us.save.url=https://sso.ubtob.com/api/userspace
 account.user.save.url=https://sso.ubtob.com/api/user
 account.user.getPartners.url=https://sso.ubtob.com/api/partners
 ### sso config
-sso.app=dev
+sso.app=mall
 # token secretkey
 sso.secretkey=0taQcW073Z7G628g5H
 sso.cookie.secure=false