Browse Source

清除多余字符

jyyw 11 tháng trước cách đây
mục cha
commit
9dbca177c9

+ 16 - 17
src/main/java/com/syjy/calculate/repository/repositoryImpl/CalculationFormulaRepositoryImpl.java

@@ -2,6 +2,8 @@ package com.syjy.calculate.repository.repositoryImpl;
 
 import cn.hutool.core.io.IoUtil;
 import cn.hutool.core.util.ObjectUtil;
+import cn.hutool.core.util.StrUtil;
+import cn.hutool.db.sql.SqlUtil;
 import cn.hutool.poi.excel.ExcelReader;
 import cn.hutool.poi.excel.ExcelUtil;
 import com.alibaba.druid.sql.SQLUtils;
@@ -401,12 +403,12 @@ public class CalculationFormulaRepositoryImpl implements CalculationFormulaRepos
             assert resource != null;
             sqlInputStream = resource.openStream();
             //
-            StringBuilder sql = new StringBuilder();
-            //
             URL excelUrl = defaultClassLoader.getResource("sql/t_calculation_formula.xlsx");
-            assert excelUrl != null;
+            if (null == excelUrl) {
+                return;
+            }
             excelInputStream = excelUrl.openStream();
-            ExcelReader excelReader = ExcelUtil.getReader(excelInputStream);
+            ExcelReader excelReader = ExcelUtil.getReader(excelInputStream, 0, true);
             List<CalculationFormula> list = excelReader.readAll(CalculationFormula.class);
             BatchPreparedStatementSetter pss = new BatchPreparedStatementSetter() {
                 @Override
@@ -414,7 +416,8 @@ public class CalculationFormulaRepositoryImpl implements CalculationFormulaRepos
                     ps.setInt(1, i + 1);
                     ps.setString(2, list.get(i).getType());
                     ps.setInt(3, ObjectUtil.defaultIfNull(list.get(i).getOrderNo(), 1));
-                    ps.setObject(4, list.get(i).getFormula());
+                    String formula = StrUtil.removeAll(list.get(i).getFormula(), '\r', '\n');
+                    ps.setObject(4, formula);
                     ps.setString(5, list.get(i).getProvince());
                     ps.setString(6, list.get(i).getElectricType());
                     ps.setString(7, list.get(i).getStationCode());
@@ -428,6 +431,7 @@ public class CalculationFormulaRepositoryImpl implements CalculationFormulaRepos
                     ps.setString(15, list.get(i).getCreator());
                     ps.setString(16, list.get(i).getLastModifier());
                     ps.setObject(17, list.get(i).getLastModifyTime());
+                    log.info("PS ## {} ## {}", i + 1, ps);
                 }
 
                 @Override
@@ -437,19 +441,14 @@ public class CalculationFormulaRepositoryImpl implements CalculationFormulaRepos
             };
             switch (dbType) {
                 case CalculateResult.DB_KINGBASE:
-                    sql.append(IoUtil.read(sqlInputStream, StandardCharsets.UTF_8));
-                    jdbcTemplate.execute(sql.toString());
-                    jdbcTemplate.batchUpdate("INSERT INTO T_CALCULATION_FORMULA VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?);", pss);
+                    jdbcTemplate.execute(SqlUtil.formatSql(IoUtil.read(sqlInputStream, StandardCharsets.UTF_8)));
+                    jdbcTemplate.batchUpdate("INSERT INTO T_CALCULATION_FORMULA VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)", pss);
                     break;
                 case CalculateResult.DB_MYSQL:
-                    sql.append("SET NAMES utf8mb4;").append("\n");
-                    sql.append("SET FOREIGN_KEY_CHECKS = 0;").append("\n");
-                    sql.append(SQLUtils.formatMySql(IoUtil.read(sqlInputStream, StandardCharsets.UTF_8))).append("\n");
-                    jdbcTemplate.execute(sql.toString());
-                    sql.setLength(0);
-                    jdbcTemplate.batchUpdate("INSERT INTO T_CALCULATION_FORMULA VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?);", pss);
-                    sql.append("SET FOREIGN_KEY_CHECKS = 1;").append("\n");
-                    jdbcTemplate.execute(sql.toString());
+                    String sql = "SET NAMES utf8mb4;\nSET FOREIGN_KEY_CHECKS = 0;\n" + IoUtil.read(sqlInputStream, StandardCharsets.UTF_8);
+                    jdbcTemplate.execute(SQLUtils.formatMySql(sql));
+                    jdbcTemplate.batchUpdate("INSERT INTO T_CALCULATION_FORMULA VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)", pss);
+                    jdbcTemplate.execute(SQLUtils.formatMySql("SET FOREIGN_KEY_CHECKS = 1;"));
                     break;
                 default:
                     throw new Exception("未知数据库类型");
@@ -458,7 +457,7 @@ public class CalculationFormulaRepositoryImpl implements CalculationFormulaRepos
             log.info("建表成功");
             //
         } catch (Exception ex) {
-            log.error("建表失败:{}", ex.getLocalizedMessage());
+            log.error("建表失败:{}", ex.getMessage());
         } finally {
             IoUtil.closeIfPosible(excelInputStream);
             IoUtil.closeIfPosible(sqlInputStream);

BIN
src/main/resources/sql/t_calculation_formula.xlsx