|
@@ -1,5 +1,6 @@
|
|
|
package com.syjy.calculate.repository.repositoryImpl;
|
|
|
|
|
|
+import com.googlecode.aviator.AviatorEvaluator;
|
|
|
import com.syjy.calculate.config.StarterProperties;
|
|
|
import com.syjy.calculate.entity.CalculateResult;
|
|
|
import com.syjy.calculate.entity.CalculationFormula;
|
|
@@ -13,7 +14,6 @@ import org.springframework.jdbc.core.JdbcTemplate;
|
|
|
import org.springframework.jdbc.core.ResultSetExtractor;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
import org.springframework.util.ClassUtils;
|
|
|
-
|
|
|
import javax.annotation.Resource;
|
|
|
import java.io.*;
|
|
|
import java.net.URL;
|
|
@@ -22,10 +22,16 @@ import java.sql.SQLException;
|
|
|
import java.util.ArrayList;
|
|
|
import java.util.List;
|
|
|
|
|
|
+/**
|
|
|
+ * 公式计算
|
|
|
+ *
|
|
|
+ * @author zcl
|
|
|
+ * @version 1.0
|
|
|
+ * @since 2022/9/9 10:34
|
|
|
+ */
|
|
|
@Service
|
|
|
@Slf4j
|
|
|
public class CalculationFormulaRepositoryImpl implements CalculationFormulaRepository {
|
|
|
-
|
|
|
@Autowired
|
|
|
StarterProperties properties;
|
|
|
@Resource
|
|
@@ -35,23 +41,148 @@ public class CalculationFormulaRepositoryImpl implements CalculationFormulaRepos
|
|
|
* 创建公式表
|
|
|
*/
|
|
|
@Override
|
|
|
- public void createTable() {
|
|
|
- String config = properties.getDropTableFlag();
|
|
|
- // 如果不删除表,通过页面更改
|
|
|
- if ("false".equals(config)) {
|
|
|
- // 判断表是否存在
|
|
|
- String checkSql = "SHOW TABLES LIKE 't_calculation_formula'";
|
|
|
- Boolean created = jdbcTemplate.query(checkSql, null, null, new ResultSetExtractor<Boolean>() {
|
|
|
- @Override
|
|
|
- public Boolean extractData(ResultSet rs) throws SQLException, DataAccessException {
|
|
|
- return rs.next();
|
|
|
- }
|
|
|
- });
|
|
|
- // 如果表存在,不对表做任何操作
|
|
|
- if (created) {
|
|
|
+ public void initTable() {
|
|
|
+ // 从配置文件中获取版本
|
|
|
+ String version = properties.getVersion();
|
|
|
+ // 判断表是否存在
|
|
|
+ String checkSql = "SHOW TABLES LIKE 't_calculation_formula'";
|
|
|
+ Boolean created = jdbcTemplate.query(checkSql, null, null, new ResultSetExtractor<Boolean>() {
|
|
|
+ @Override
|
|
|
+ public Boolean extractData(ResultSet rs) throws SQLException, DataAccessException {
|
|
|
+ return rs.next();
|
|
|
+ }
|
|
|
+ });
|
|
|
+
|
|
|
+ // 如果表存在
|
|
|
+ if (created) {
|
|
|
+ // 从表中获取旧版本
|
|
|
+ String oldVersion = getVersion();
|
|
|
+ // 如果版本没变化,不进行初始化表操作
|
|
|
+ if(oldVersion.equals(version)){
|
|
|
return;
|
|
|
}
|
|
|
+ // 如果版本不同,则初始化表
|
|
|
+ this.createTable();
|
|
|
+ // 如果表不存在,初始化表
|
|
|
+ }else{
|
|
|
+ this.createTable();
|
|
|
}
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 查询所有公式
|
|
|
+ *
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ public List<CalculationFormula> select() {
|
|
|
+ String sql = "SELECT * from t_calculation_formula";
|
|
|
+ List<CalculationFormula> calculationFormulaList = new ArrayList<>();
|
|
|
+ try {
|
|
|
+ calculationFormulaList = jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(CalculationFormula.class));
|
|
|
+ } catch (DataAccessException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ return calculationFormulaList;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 更新公式
|
|
|
+ *
|
|
|
+ * @param calculationFormula
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ public int update(CalculationFormula calculationFormula) {
|
|
|
+ Integer id = calculationFormula.getId();
|
|
|
+ String type = calculationFormula.getType();
|
|
|
+ String formula = calculationFormula.getFormula();
|
|
|
+ String province = calculationFormula.getProvince();
|
|
|
+ String electricType = calculationFormula.getElectricType();
|
|
|
+ String state = calculationFormula.getState();
|
|
|
+ int count = 0;
|
|
|
+ try {
|
|
|
+ count = jdbcTemplate.update("update t_calculation_formula set type=?,formula=?,province=?,electric_type=?,state=? where id=?", type, formula, province, electricType, state, id);
|
|
|
+ } catch (DataAccessException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ if (count > 0) {
|
|
|
+ // 清除缓存公式
|
|
|
+ AviatorEvaluator.clearExpressionCache();
|
|
|
+ }
|
|
|
+ return count;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 新增公式
|
|
|
+ *
|
|
|
+ * @param calculationFormula
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ public int add(CalculationFormula calculationFormula) {
|
|
|
+ String type = calculationFormula.getType();
|
|
|
+ String formula = calculationFormula.getFormula();
|
|
|
+ String province = calculationFormula.getProvince();
|
|
|
+ int count = 0;
|
|
|
+ try {
|
|
|
+ count = jdbcTemplate.update(
|
|
|
+ "INSERT INTO t_calculation_formula(TYPE,FORMULA,PROVINCE) VALUES (?, ?,?,?)", type, formula, province);
|
|
|
+ } catch (DataAccessException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ return count;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 删除公式
|
|
|
+ *
|
|
|
+ * @param id
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ public int delete(int id) {
|
|
|
+ int count = 0;
|
|
|
+ try {
|
|
|
+ count = jdbcTemplate.update("delete from t_calculation_formula where id=?", id);
|
|
|
+ } catch (DataAccessException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ return count;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 查询公式数据
|
|
|
+ *
|
|
|
+ * @param type 类型
|
|
|
+ * @param province 省调
|
|
|
+ * @param formulaType 公式类型
|
|
|
+ * @param electricType 场站类型
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ public CalculationFormula findByTypeAndProvince(String type, String formulaType, String province, String electricType) {
|
|
|
+
|
|
|
+ String sql = "";
|
|
|
+ // 查询sql
|
|
|
+ if (CalculateResult.FORMULA.equals(type)) {
|
|
|
+ sql = "SELECT * from t_calculation_formula where ELECTRIC_TYPE = ? and PROVINCE = ? and TYPE = ? limit 1 ";
|
|
|
+ } else if (CalculateResult.RULES.equals(type)) {
|
|
|
+ sql = "SELECT * from t_calculation_formula where TYPE = 'RULES' and ELECTRIC_TYPE = ? and PROVINCE = ? and RULE_FORMULA = ? limit 1";
|
|
|
+ }
|
|
|
+ // 根据类型和省调查询公式
|
|
|
+ try {
|
|
|
+ CalculationFormula calculationFormula = jdbcTemplate.queryForObject(sql, new BeanPropertyRowMapper<>(CalculationFormula.class), electricType, province, formulaType);
|
|
|
+ return calculationFormula;
|
|
|
+ } catch (EmptyResultDataAccessException e) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 创建表
|
|
|
+ */
|
|
|
+ private void createTable(){
|
|
|
try {
|
|
|
// 获取初始化公式表sql
|
|
|
String path = "sql/t_calculation_formula.sql";
|
|
@@ -72,41 +203,39 @@ public class CalculationFormulaRepositoryImpl implements CalculationFormulaRepos
|
|
|
inputStream.close();
|
|
|
// 执行sql文件
|
|
|
jdbcTemplate.execute(builder.toString());
|
|
|
- } catch (DataAccessException e) {
|
|
|
- e.printStackTrace();
|
|
|
- log.info("删除/创建表错误:" + e.toString());
|
|
|
- } catch (FileNotFoundException e) {
|
|
|
- throw new RuntimeException(e);
|
|
|
} catch (IOException e) {
|
|
|
- throw new RuntimeException(e);
|
|
|
+ e.printStackTrace();
|
|
|
}
|
|
|
+ // 更新公式
|
|
|
+ updateVersion();
|
|
|
}
|
|
|
|
|
|
+
|
|
|
/**
|
|
|
- * 查询公式数据
|
|
|
- *
|
|
|
- * @param type 类型
|
|
|
- * @param provinceEnum 省调
|
|
|
- * @param formulaType 公式类型
|
|
|
- * @param electricType 场站类型
|
|
|
- * @return
|
|
|
+ * 获取版本
|
|
|
*/
|
|
|
- @Override
|
|
|
- public CalculationFormula findByTypeAndProvince(String type, String formulaType, String provinceEnum, String electricType) {
|
|
|
-
|
|
|
- String sql = "";
|
|
|
- // 查询sql
|
|
|
- if (CalculateResult.FORMULA.equals(type)) {
|
|
|
- sql = "SELECT * from t_calculation_formula where ELECTRIC_TYPE = ? and PROVINCE_ENUM = ? and TYPE = ? limit 1 ";
|
|
|
- } else if (CalculateResult.RULES.equals(type)) {
|
|
|
- sql = "SELECT * from t_calculation_formula where TYPE = 'RULES' and ELECTRIC_TYPE = ? and PROVINCE_ENUM = ? and RULE_FORMULA = ? limit 1";
|
|
|
+ private String getVersion(){
|
|
|
+ String version = "";
|
|
|
+ String sql = " SELECT * FROM t_calculation_formula where type = 'VERSION' ";
|
|
|
+ try {
|
|
|
+ CalculationFormula calculationFormula = jdbcTemplate.queryForObject(sql, new BeanPropertyRowMapper<>(CalculationFormula.class));
|
|
|
+ version = calculationFormula.getFormula();
|
|
|
+ } catch (DataAccessException e) {
|
|
|
+ e.printStackTrace();
|
|
|
}
|
|
|
- // 根据类型和省调查询公式
|
|
|
+ return version;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 更新版本
|
|
|
+ */
|
|
|
+ private void updateVersion(){
|
|
|
+ String version = properties.getVersion();
|
|
|
+ String sql = " UPDATE t_calculation_formula set FORMULA = '" + version +"' where type = 'VERSION' ";
|
|
|
try {
|
|
|
- CalculationFormula calculationFormula = jdbcTemplate.queryForObject(sql, new BeanPropertyRowMapper<>(CalculationFormula.class), electricType, provinceEnum, formulaType);
|
|
|
- return calculationFormula;
|
|
|
- } catch (EmptyResultDataAccessException e) {
|
|
|
- return null;
|
|
|
+ jdbcTemplate.update(sql);
|
|
|
+ } catch (DataAccessException e) {
|
|
|
+ e.printStackTrace();
|
|
|
}
|
|
|
}
|
|
|
|