|
@@ -4,6 +4,7 @@ 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.json.JSONUtil;
|
|
|
import cn.hutool.poi.excel.ExcelReader;
|
|
|
import cn.hutool.poi.excel.ExcelUtil;
|
|
|
import com.alibaba.druid.sql.SQLUtils;
|
|
@@ -13,6 +14,7 @@ import com.syjy.calculate.entity.CalculateResult;
|
|
|
import com.syjy.calculate.entity.CalculationFormula;
|
|
|
import com.syjy.calculate.entity.CalculationInfo;
|
|
|
import com.syjy.calculate.repository.CalculationFormulaRepository;
|
|
|
+import com.syjy.calculate.util.DBUtil;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.dao.DataAccessException;
|
|
@@ -23,6 +25,7 @@ import org.springframework.jdbc.core.JdbcTemplate;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
import org.springframework.util.ClassUtils;
|
|
|
|
|
|
+import javax.annotation.PostConstruct;
|
|
|
import javax.annotation.Resource;
|
|
|
import java.io.InputStream;
|
|
|
import java.net.URL;
|
|
@@ -43,10 +46,19 @@ import java.util.List;
|
|
|
@Service
|
|
|
@Slf4j
|
|
|
public class CalculationFormulaRepositoryImpl implements CalculationFormulaRepository {
|
|
|
- @Autowired
|
|
|
- StarterProperties properties;
|
|
|
+
|
|
|
@Resource
|
|
|
public JdbcTemplate jdbcTemplate;
|
|
|
+ @Autowired
|
|
|
+ StarterProperties properties;
|
|
|
+
|
|
|
+ private String dbType;
|
|
|
+
|
|
|
+ @PostConstruct
|
|
|
+ public void init() {
|
|
|
+ this.dbType = DBUtil.getDatabaseType(jdbcTemplate.getDataSource());
|
|
|
+ }
|
|
|
+
|
|
|
|
|
|
/**
|
|
|
* 创建公式表
|
|
@@ -54,8 +66,6 @@ public class CalculationFormulaRepositoryImpl implements CalculationFormulaRepos
|
|
|
@Override
|
|
|
public void initTable() {
|
|
|
log.info("初始化准确率计算公式表");
|
|
|
- // 获取数据库类型
|
|
|
- String dbType = properties.getDbType();
|
|
|
// 如果是金仓数据库,直接初始化表
|
|
|
if (CalculateResult.DB_KINGBASE.equals(dbType)) {
|
|
|
this.createTable();
|
|
@@ -414,7 +424,7 @@ public class CalculationFormulaRepositoryImpl implements CalculationFormulaRepos
|
|
|
try {
|
|
|
log.info("---------------开始创建准确率计算表---------------");
|
|
|
// 从配置文件中获取版本
|
|
|
- String dbType = properties.getDbType();
|
|
|
+
|
|
|
String path;
|
|
|
// 获取初始化公式表sql
|
|
|
switch (dbType) {
|
|
@@ -448,36 +458,39 @@ public class CalculationFormulaRepositoryImpl implements CalculationFormulaRepos
|
|
|
}
|
|
|
excelInputStream = excelUrl.openStream();
|
|
|
ExcelReader excelReader = ExcelUtil.getReader(excelInputStream, 0, true);
|
|
|
- List<CalculationFormula> list = excelReader.readAll(CalculationFormula.class);
|
|
|
+ List<CalculationFormula> formulas = excelReader.readAll(CalculationFormula.class);
|
|
|
BatchPreparedStatementSetter pss = new BatchPreparedStatementSetter() {
|
|
|
@Override
|
|
|
public void setValues(PreparedStatement ps, int i) throws SQLException {
|
|
|
- ps.setInt(1, i + 1);
|
|
|
- ps.setString(2, list.get(i).getType());
|
|
|
- ps.setInt(3, ObjectUtil.defaultIfNull(list.get(i).getOrderNo(), 1));
|
|
|
- //String formula = StrUtil.removeAll(list.get(i).getFormula(), '\r', '\n');
|
|
|
- ps.setObject(4, list.get(i).getFormula());
|
|
|
- ps.setString(5, list.get(i).getProvince());
|
|
|
- ps.setString(6, list.get(i).getElectricType());
|
|
|
- ps.setString(7, list.get(i).getStationCode());
|
|
|
- ps.setString(8, list.get(i).getRuleFormula());
|
|
|
- ps.setString(9, list.get(i).getIsRate());
|
|
|
- ps.setString(10, list.get(i).getMaxMinLimit());
|
|
|
- ps.setString(11, list.get(i).getState());
|
|
|
- ps.setString(12, list.get(i).getDataSourceType());
|
|
|
- ps.setString(13, list.get(i).getHowManyDays());
|
|
|
- ps.setObject(14, list.get(i).getCreateTime());
|
|
|
- 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);
|
|
|
+ CalculationFormula formula = formulas.get(i);
|
|
|
+ int id = i + 1;
|
|
|
+ ps.setInt(1, id);
|
|
|
+ ps.setString(2, formula.getType());
|
|
|
+ ps.setInt(3, ObjectUtil.defaultIfNull(formula.getOrderNo(), 1));
|
|
|
+ //String formula = StrUtil.removeAll(formula.getFormula(), '\r', '\n');
|
|
|
+ ps.setObject(4, formula.getFormula());
|
|
|
+ ps.setString(5, formula.getProvince());
|
|
|
+ ps.setString(6, formula.getElectricType());
|
|
|
+ ps.setString(7, formula.getStationCode());
|
|
|
+ ps.setString(8, formula.getRuleFormula());
|
|
|
+ ps.setString(9, formula.getIsRate());
|
|
|
+ ps.setString(10, formula.getMaxMinLimit());
|
|
|
+ ps.setString(11, formula.getState());
|
|
|
+ ps.setString(12, formula.getDataSourceType());
|
|
|
+ ps.setString(13, formula.getHowManyDays());
|
|
|
+ ps.setObject(14, formula.getCreateTime());
|
|
|
+ ps.setString(15, formula.getCreator());
|
|
|
+ ps.setString(16, formula.getLastModifier());
|
|
|
+ ps.setObject(17, formula.getLastModifyTime());
|
|
|
+ log.info("PS ## {} ## {}", id, ps);
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public int getBatchSize() {
|
|
|
- return list.size();
|
|
|
+ return formulas.size();
|
|
|
}
|
|
|
};
|
|
|
+ String sql = "INSERT INTO T_CALCULATION_FORMULA (ID, `TYPE`, ORDER_NO, FORMULA, PROVINCE, ELECTRIC_TYPE, STATION_CODE, RULE_FORMULA, IS_RATE, MAX_MIN_LIMIT, STATE, DATA_SOURCE_TYPE, HOW_MANY_DAYS, CREATE_TIME, CREATOR, LAST_MODIFIER, LAST_MODIFY_TIME) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
|
|
|
switch (dbType) {
|
|
|
case CalculateResult.DB_KINGBASE:
|
|
|
case CalculateResult.DB_POSTGRES:
|
|
@@ -486,14 +499,16 @@ public class CalculationFormulaRepositoryImpl implements CalculationFormulaRepos
|
|
|
String initSQL = SqlUtil.formatSql(IoUtil.read(sqlInputStream, StandardCharsets.UTF_8));
|
|
|
List<String> ddls = StrUtil.splitTrim(initSQL, ";");
|
|
|
for (String ddl : ddls) {
|
|
|
- jdbcTemplate.execute(ddl);
|
|
|
+ if (!ddl.trim().isEmpty()) {
|
|
|
+ jdbcTemplate.execute(ddl);
|
|
|
+ }
|
|
|
}
|
|
|
- jdbcTemplate.batchUpdate("INSERT INTO T_CALCULATION_FORMULA VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)", pss);
|
|
|
+ jdbcTemplate.batchUpdate(SQLUtils.format(sql, dbType), pss);
|
|
|
break;
|
|
|
case CalculateResult.DB_MYSQL:
|
|
|
- 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);
|
|
|
+ String ddl = "SET NAMES utf8mb4;\nSET FOREIGN_KEY_CHECKS = 0;\n" + IoUtil.read(sqlInputStream, StandardCharsets.UTF_8);
|
|
|
+ jdbcTemplate.execute(SQLUtils.formatMySql(ddl));
|
|
|
+ jdbcTemplate.batchUpdate(SQLUtils.format(sql, dbType), pss);
|
|
|
jdbcTemplate.execute(SQLUtils.formatMySql("SET FOREIGN_KEY_CHECKS = 1;"));
|
|
|
break;
|
|
|
default:
|
|
@@ -503,7 +518,7 @@ public class CalculationFormulaRepositoryImpl implements CalculationFormulaRepos
|
|
|
log.info("建表成功");
|
|
|
//
|
|
|
} catch (Exception ex) {
|
|
|
- log.error("建表失败:{}", ex.getMessage());
|
|
|
+ log.error("建表失败:{} ", ex);
|
|
|
} finally {
|
|
|
IoUtil.closeIfPosible(excelInputStream);
|
|
|
IoUtil.closeIfPosible(sqlInputStream);
|
|
@@ -518,7 +533,7 @@ public class CalculationFormulaRepositoryImpl implements CalculationFormulaRepos
|
|
|
*/
|
|
|
private String getVersion() {
|
|
|
String version = "";
|
|
|
- String dbType = properties.getDbType();
|
|
|
+
|
|
|
String sql = " SELECT * FROM t_calculation_formula where type = 'VERSION' ";
|
|
|
switch (dbType) {
|
|
|
case CalculateResult.DB_KINGBASE:
|
|
@@ -544,7 +559,7 @@ public class CalculationFormulaRepositoryImpl implements CalculationFormulaRepos
|
|
|
*/
|
|
|
@Override
|
|
|
public List<CalculationFormula> getUser() {
|
|
|
- String dbType = properties.getDbType();
|
|
|
+
|
|
|
String sql = " SELECT * FROM t_calculation_formula where type = 'USER' ";
|
|
|
switch (dbType) {
|
|
|
case CalculateResult.DB_KINGBASE:
|
|
@@ -570,7 +585,7 @@ public class CalculationFormulaRepositoryImpl implements CalculationFormulaRepos
|
|
|
*/
|
|
|
private void updateVersion() {
|
|
|
String version = properties.getVersion();
|
|
|
- String dbType = properties.getDbType();
|
|
|
+
|
|
|
String sql = " UPDATE t_calculation_formula set FORMULA = '" + version + "' where type = 'VERSION' ";
|
|
|
switch (dbType) {
|
|
|
case CalculateResult.DB_KINGBASE:
|