|
@@ -2,20 +2,76 @@ package com.syjy.calculate.repository.repositoryImpl;
|
|
|
|
|
|
import com.syjy.calculate.entity.CalculationFormula;
|
|
|
import com.syjy.calculate.repository.CalculationFormulaRepository;
|
|
|
-import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.springframework.dao.DataAccessException;
|
|
|
import org.springframework.jdbc.core.BeanPropertyRowMapper;
|
|
|
import org.springframework.jdbc.core.JdbcTemplate;
|
|
|
+import org.springframework.jdbc.core.ResultSetExtractor;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
-
|
|
|
+import javax.annotation.Resource;
|
|
|
+import java.sql.ResultSet;
|
|
|
+import java.sql.SQLException;
|
|
|
import java.util.List;
|
|
|
|
|
|
@Service
|
|
|
+@Slf4j
|
|
|
public class CalculationFormulaRepositoryImpl implements CalculationFormulaRepository {
|
|
|
|
|
|
- @Autowired
|
|
|
+ @Resource
|
|
|
public JdbcTemplate jdbcTemplate;
|
|
|
|
|
|
@Override
|
|
|
+ public void createTable() {
|
|
|
+ // 判断表是否存在
|
|
|
+ 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();
|
|
|
+ }
|
|
|
+ });
|
|
|
+
|
|
|
+ try {
|
|
|
+ // 如果表存在,删除表
|
|
|
+ if (created) {
|
|
|
+ String dropSql = "DROP table t_calculation_formula";
|
|
|
+ jdbcTemplate.execute(dropSql);
|
|
|
+ }
|
|
|
+ // 创建表
|
|
|
+ String createSql = "create table t_calculation_formula ( ID int(0) not null auto_increment primary key, TYPE varchar(50) , `ORDER` int(0), FORMULA varchar(255) not null , PROVINCE_ENUM varchar(10), STATE varchar(10), CREATE_TIME datetime(0), CREATOR varchar(50), LAST_MODIFIER varchar(50), LAST_MODIFY_TIME datetime(0));";
|
|
|
+
|
|
|
+ jdbcTemplate.execute(createSql);
|
|
|
+
|
|
|
+ // 拼接插入语句,配置文件要增加&allowMultiQueries=true ,允许一次提交多条sql语句
|
|
|
+ String insertSql =
|
|
|
+ "INSERT INTO `t_calculation_formula` VALUES (1, 'DQ', 1, 'sum:math.abs(VariateA-VariateB)', 'E62', NULL, NULL, NULL, NULL, NULL);" +
|
|
|
+ "INSERT INTO `t_calculation_formula` VALUES (2, 'DQ', 2, 'sum:math.pow((VariateA-VariateB),2)*math.abs(VariateA-VariateB)/result1', 'E62', NULL, NULL, NULL, NULL, NULL);" +
|
|
|
+ "INSERT INTO `t_calculation_formula` VALUES (3, 'DQ', 3, '(1-(math.sqrt(result2)/VariateC))*100', 'E62', NULL, NULL, NULL, NULL, NULL);" +
|
|
|
+ "INSERT INTO `t_calculation_formula` VALUES (4, 'VariateA', NULL, 'sj', 'E62', NULL, NULL, NULL, NULL, NULL);" +
|
|
|
+ "INSERT INTO `t_calculation_formula` VALUES (5, 'VariateB', NULL, 'yc', 'E62', NULL, NULL, NULL, NULL, NULL);" +
|
|
|
+ "INSERT INTO `t_calculation_formula` VALUES (6, 'VariateC', NULL, 'rl', 'E62', NULL, NULL, NULL, NULL, NULL);" +
|
|
|
+ "INSERT INTO `t_calculation_formula` VALUES (7, 'VariateD', NULL, 'count', 'E62', NULL, NULL, NULL, NULL, NULL);" +
|
|
|
+ "INSERT INTO `t_calculation_formula` VALUES (8, 'Point', 1, 'math.abs((VariateA-VariateB)/VariateC)*100', 'E62', NULL, NULL, NULL, NULL, NULL);" +
|
|
|
+ "INSERT INTO `t_calculation_formula` VALUES (9, 'MAE', 1, 'sum:math.abs((VariateA-VariateB)', 'E62', NULL, NULL, NULL, NULL, NULL);" +
|
|
|
+ "INSERT INTO `t_calculation_formula` VALUES (10, 'MAE', 2, '1-(result1/(VariateC*VariateD))', 'E62', NULL, NULL, NULL, NULL, NULL);" +
|
|
|
+ "INSERT INTO `t_calculation_formula` VALUES (11, 'RMSE', 1, 'sum:math.pow((VariateA-VariateB)/VariateC,2)', 'E62', NULL, NULL, NULL, NULL, NULL);" +
|
|
|
+ "INSERT INTO `t_calculation_formula` VALUES (12, 'RMSE', 2, '(1-math.sqrt((result1/VariateD)))*100', 'E62', NULL, NULL, NULL, NULL, NULL);" +
|
|
|
+ "INSERT INTO `t_calculation_formula` VALUES (13, 'RMSES', 1, 'sum:math.pow((VariateA-VariateB),2)', 'E62', NULL, NULL, NULL, NULL, NULL);" +
|
|
|
+ "INSERT INTO `t_calculation_formula` VALUES (14, 'RMSES', 2, '(1-(result1/VariateC*math.sqrt(VariateD)))*100', 'E62', NULL, NULL, NULL, NULL, NULL);";
|
|
|
+
|
|
|
+ jdbcTemplate.execute(insertSql);
|
|
|
+ } catch (DataAccessException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ log.info("删除/创建表错误:" + e.toString());
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void updateTable() {
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
public void save(CalculationFormula calculationFormula) {
|
|
|
|
|
|
}
|
|
@@ -26,16 +82,23 @@ public class CalculationFormulaRepositoryImpl implements CalculationFormulaRepos
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
- public List<CalculationFormula> findByTypeAndProvince( String province, String type) {
|
|
|
- String sql = "SELECT * from t_calculation_formula where PROVINCE_ENUM = ? and TYPE = ? ";
|
|
|
- List<CalculationFormula> calculationFormulaList = jdbcTemplate.query(sql,new BeanPropertyRowMapper<>(CalculationFormula.class),province,type);
|
|
|
+ public List<CalculationFormula> findByTypeAndProvince(String type) {
|
|
|
+ String sql = "SELECT * from t_calculation_formula where TYPE = ? ";
|
|
|
+
|
|
|
+ List<CalculationFormula> calculationFormulaList = null;
|
|
|
+ try {
|
|
|
+ calculationFormulaList = jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(CalculationFormula.class), type);
|
|
|
+ } catch (DataAccessException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+
|
|
|
return calculationFormulaList;
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public List<CalculationFormula> findVariate(String provinceEnum) {
|
|
|
String sql = "SELECT * from t_calculation_formula where TYPE in ('VariateA','VariateB','VariateC') and PROVINCE_ENUM = ?";
|
|
|
- List<CalculationFormula> calculationFormulaList = jdbcTemplate.query(sql,new BeanPropertyRowMapper<>(CalculationFormula.class),provinceEnum);
|
|
|
+ List<CalculationFormula> calculationFormulaList = jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(CalculationFormula.class), provinceEnum);
|
|
|
return calculationFormulaList;
|
|
|
}
|
|
|
|