Browse Source

调整批量导出类目的逻辑,优化处理速度。

yuj 7 years ago
parent
commit
0e69b0c4f1

+ 10 - 5
src/main/java/com/uas/platform/b2c/core/utils/PoiUtil.java

@@ -174,17 +174,22 @@ 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);
-            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);
+            int perTime = 0;
+            if (i < (sheetCount - 1)) {
+                perTime = ExcelConstant.PER_SHEET_WRITE_COUNT;
+            } else {
+                int remainder = totalRowCount - i * ExcelConstant.PER_SHEET_ROW_COUNT;
+                perTime = remainder / ExcelConstant.PER_WRITE_ROW_COUNT;
+                if ((remainder % ExcelConstant.PER_WRITE_ROW_COUNT) != 0) {
+                    perTime++;
+                }
+            }
             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

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

@@ -4206,13 +4206,12 @@ public class GoodsServiceImpl implements GoodsService {
             where.append(" and pr_create_time < '").append(new java.sql.Date(endTime.getTime() + IntegerConstant.ONE_DAY_MILLISECONDS)).append("' ");
         }
         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");
+        StringBuffer sql = new StringBuffer("create temporary table temp_kind select pr_kind as thirdKind, pr_kindid as kindId, count(1) as count").append(where).append(group);
+        jdbcTemplate.execute(sql.toString());
+        Integer totalRowCount = commonDao.queryForObject("select count(*) from temp_kind", Integer.class);
+
+        String querySQL = "select * from temp_kind order by count desc limit %d, %d";
 
         String[] titles = {"一级类目", "二级类目", "三级类目", "产品数(条)"};
 
@@ -4223,7 +4222,7 @@ public class GoodsServiceImpl implements GoodsService {
             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));
+                List<KindHierarchicalCount> kindHierarchicals = jdbcTemplate.query(String.format(querySQL, startNumber, pageSize), new BeanPropertyRowMapper<>(KindHierarchicalCount.class));
                 long l3 = System.currentTimeMillis();
                 logger.info("获取KindHierarchicalCount需要多少时间:" + (l3 - l2));
                 List<Long> ids = new ArrayList<>();
@@ -4272,7 +4271,7 @@ public class GoodsServiceImpl implements GoodsService {
                     }
                 }
                 long l5 = System.currentTimeMillis();
-                logger.info("需要消耗的时间是:" + (l5 - l4));
+                logger.info("获取父级类目的时间:" + (l5 - l4));
 
                 if (!CollectionUtils.isEmpty(kindHierarchicals)) {
                     for (int i = startRowCount; i <= endRowCount; i++) {