瀏覽代碼

stater相关配置优化

zhangchenglong 2 年之前
父節點
當前提交
a9d6783450

+ 1 - 14
pom.xml

@@ -34,11 +34,6 @@
             <scope>compile</scope>
         </dependency>
 
-        <dependency>
-            <groupId>org.springframework.boot</groupId>
-            <artifactId>spring-boot-starter-jdbc</artifactId>
-        </dependency>
-
         <!--mysql 驱动-->
         <dependency>
             <groupId>mysql</groupId>
@@ -75,7 +70,7 @@
         <dependency>
             <groupId>org.springframework.boot</groupId>
             <artifactId>spring-boot-configuration-processor</artifactId>
-            <version>2.1.5.RELEASE</version>
+            <version>2.7.2</version>
         </dependency>
         <!--实现自动化配置-->
         <dependency>
@@ -83,14 +78,6 @@
             <artifactId>spring-boot-autoconfigure</artifactId>
             <version>2.7.2</version>
         </dependency>
-
-        <!--配置文件处理器-->
-        <dependency>
-            <groupId>org.springframework.boot</groupId>
-            <artifactId>spring-boot-configuration-processor</artifactId>
-            <optional>true</optional>
-        </dependency>
-
     </dependencies>
 
     <build>

+ 11 - 11
src/main/java/com/syjy/calculate/config/StarterAutoConfigure.java

@@ -1,27 +1,27 @@
 package com.syjy.calculate.config;
 
-import com.syjy.calculate.service.StarterService;
-import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
+import com.syjy.calculate.repository.repositoryImpl.CalculationFormulaRepositoryImpl;
+import com.syjy.calculate.service.AccuracyPassRateCalculateService;
 import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
 import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
-import org.springframework.boot.context.properties.EnableConfigurationProperties;
 import org.springframework.context.annotation.Bean;
 import org.springframework.context.annotation.Configuration;
-import javax.annotation.Resource;
 
 @Configuration
-@ConditionalOnClass(StarterService.class)
-@EnableConfigurationProperties(StarterServiceProperties.class)
 public class StarterAutoConfigure {
 
-    @Resource
-    private StarterServiceProperties properties;
+    @Bean
+    @ConditionalOnMissingBean
+    @ConditionalOnProperty(prefix = "calculate.service", value = "enabled", havingValue = "true")
+    AccuracyPassRateCalculateService accuracyPassRateCalculateService (){
+        return new AccuracyPassRateCalculateService();
+    }
 
     @Bean
     @ConditionalOnMissingBean
-    @ConditionalOnProperty(prefix = "com.syjy.calculate.service", value = "enabled", havingValue = "true")
-    StarterService starterService (){
-        return new StarterService(properties.getConfig());
+    @ConditionalOnProperty(prefix = "calculate.service", value = "enabled", havingValue = "true")
+    CalculationFormulaRepositoryImpl calculationFormulaRepositoryImpl (){
+        return new CalculationFormulaRepositoryImpl();
     }
 
 }

+ 0 - 17
src/main/java/com/syjy/calculate/config/StarterServiceProperties.java

@@ -1,17 +0,0 @@
-package com.syjy.calculate.config;
-
-import org.springframework.boot.context.properties.ConfigurationProperties;
-import org.springframework.stereotype.Component;
-
-@ConfigurationProperties("com.syjy.calculate.service")
-public class StarterServiceProperties {
-    private String config;
-
-    public void setConfig(String config) {
-        this.config = config;
-    }
-
-    public String getConfig() {
-        return config;
-    }
-}

+ 17 - 10
src/main/java/com/syjy/calculate/service/AccuracyPassRateCalculateService.java

@@ -7,6 +7,7 @@ import com.syjy.calculate.repository.CalculationFormulaRepository;
 import lombok.extern.slf4j.Slf4j;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
+
 import java.math.BigDecimal;
 import java.util.ArrayList;
 import java.util.HashMap;
@@ -25,7 +26,6 @@ import java.util.Map;
 public class AccuracyPassRateCalculateService {
     BigDecimal ZERO = new BigDecimal("0");
     BigDecimal HUNDRED = new BigDecimal("100");
-
     @Autowired
     private CalculationFormulaRepository calculationFormulaRepository;
 
@@ -39,11 +39,12 @@ public class AccuracyPassRateCalculateService {
     public JSONObject calculate(List<Map<String, Object>> powerData, BigDecimal rl, String provinceEnum, String type) {
         JSONObject jsonResult = new JSONObject();
         String lastResult = "";
-        jsonResult.put("result","fail");
+        jsonResult.put("result", "fail");
         // 获取公式
         List<CalculationFormula> calculationFormulaList = getCalculationFormulaData(type);
         if (calculationFormulaList == null || calculationFormulaList.size() == 0) {
-            jsonResult.put("msg","计算失败,未匹配到公式");
+            jsonResult.put("msg", "计算失败,未匹配到公式");
+            return jsonResult;
         }
         BigDecimal count = BigDecimal.valueOf(calculationFormulaList.size());
         // 获取当前省调的公式变量值
@@ -105,21 +106,26 @@ public class AccuracyPassRateCalculateService {
         }
         // 获取最后一个公式计算的结果
         lastResult = String.valueOf(resultMap.get("result" + calculationFormulaList.size()));
+        if (lastResult == null) {
+            jsonResult.put("msg", "计算失败,结果为空");
+            return jsonResult;
+        }
+        log.info("最后一个公式计算的结果:" + lastResult);
         BigDecimal bResult = new BigDecimal(lastResult);
         bResult = bResult.setScale(2, BigDecimal.ROUND_HALF_UP);
         // 过滤准确率
         bResult = filterResult(bResult);
         lastResult = bResult + "%";
-        jsonResult.put("result","success");
-        jsonResult.put("value",lastResult);
-        log.info("准确率:" + lastResult );
+        jsonResult.put("result", "success");
+        jsonResult.put("value", lastResult);
+        log.info("准确率:" + lastResult);
         return jsonResult;
     }
 
     /**
      * 获取当前省调的公式列表
      *
-     * @param type         类型
+     * @param type 类型
      * @return
      */
     public List<CalculationFormula> getCalculationFormulaData(String type) {
@@ -194,16 +200,17 @@ public class AccuracyPassRateCalculateService {
 
     /**
      * 过滤结果 <0=0  >100=100
+     *
      * @param result 过滤前的结果
      * @return 过滤后的结果
      */
-    private BigDecimal filterResult(BigDecimal result){
+    private BigDecimal filterResult(BigDecimal result) {
         //当结果为负数时,说明偏差过大,准确率为0
-        if(result.compareTo(BigDecimal.ZERO)==-1 || result.compareTo(BigDecimal.ZERO)==0){
+        if (result.compareTo(BigDecimal.ZERO) == -1 || result.compareTo(BigDecimal.ZERO) == 0) {
             result = BigDecimal.ZERO;
         }
 
-        if(result.compareTo(this.HUNDRED)==1){
+        if (result.compareTo(this.HUNDRED) == 1) {
             result = this.HUNDRED;
         }
         return result;

+ 0 - 13
src/main/java/com/syjy/calculate/service/StarterService.java

@@ -1,13 +0,0 @@
-package com.syjy.calculate.service;
-
-public class StarterService {
-    private String config;
-
-    public StarterService(String config) {
-        this.config = config;
-    }
-
-    public String[] split(String splitChar) {
-        return config.split(splitChar);
-    }
-}