Browse Source

抽稀计算液量对比完成

lloyd 3 months ago
parent
commit
9fe71a3273

+ 25 - 28
src/main/java/com/gct/edge/actuator/config/FeignConfig.java

@@ -1,14 +1,14 @@
1 1
 package com.gct.edge.actuator.config;
2 2
 
3 3
 
4
+import com.gct.common.util.StringUtil;
4 5
 import feign.RequestInterceptor;
5 6
 import feign.RequestTemplate;
6 7
 import lombok.extern.slf4j.Slf4j;
7
-import org.springframework.context.EnvironmentAware;
8
+import org.springframework.beans.factory.annotation.Value;
8 9
 import org.springframework.context.annotation.AnnotationConfigApplicationContext;
9 10
 import org.springframework.context.annotation.Bean;
10 11
 import org.springframework.context.annotation.Configuration;
11
-import org.springframework.core.env.Environment;
12 12
 import org.springframework.web.client.RestTemplate;
13 13
 
14 14
 import java.util.Arrays;
@@ -16,43 +16,40 @@ import java.util.List;
16 16
 import java.util.concurrent.ThreadLocalRandom;
17 17
 
18 18
 
19
-/**
20
- * feign 调用配置,统一添加token请求头
21
- * @author: NewMeanning
22
- * @create: 2021-01-29 00:10
23
- **/
24 19
 @Slf4j
25 20
 @Configuration
26
-public class FeignConfig implements RequestInterceptor, EnvironmentAware {
21
+public class FeignConfig implements RequestInterceptor {
27 22
 
23
+    @Value("${auth.hostList}")
24
+    private String tokeAuthUrl;
28 25
     private static String token;
29 26
 
30
-    private static Environment env;
27
+    private final static String lockStr = "token";
31 28
 
32 29
     @Bean(name = "restTemplate")
33
-    RestTemplate getRestTemplate(){
30
+    RestTemplate getRestTemplate() {
34 31
         return new RestTemplate();
35 32
     }
36 33
 
37 34
     @Override
38 35
     public void apply(RequestTemplate requestTemplate) {
39
-        requestTemplate.header("token",token);//请求头添加token
40
-    }
41
-
42
-    @Override
43
-    public void setEnvironment(Environment environment) {
44
-        //获取配置的认证中心主机地址  todo 升级为自动从Nacos中读取,目前这里读取不到
45
-        this.env = environment;
46
-        List<String> hostList = Arrays.asList(this.env.getProperty("auth.hostList").split(","));
47
-        //手动获取RestTemplate 实例
48
-        AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext();
49
-        applicationContext.register(RestTemplate.class);
50
-        applicationContext.refresh();
51
-        RestTemplate restTemplate= (RestTemplate) applicationContext.getBean("restTemplate");
52
-        //自定义轮询方式负载
53
-        int index = ThreadLocalRandom.current().nextInt(hostList.size());
54
-        String url = "http://"+hostList.get(index)+"/token";
55
-        token = restTemplate.getForObject(url,String.class);
56
-        log.info("token:{}",token);
36
+        if (StringUtil.isBlankOrEmpty(token)) {
37
+            synchronized (lockStr) {
38
+                if (StringUtil.isBlankOrEmpty(token)) {
39
+                    List<String> hostList = Arrays.asList(tokeAuthUrl.split(","));
40
+                    //手动获取RestTemplate 实例
41
+                    AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext();
42
+                    applicationContext.register(RestTemplate.class);
43
+                    applicationContext.refresh();
44
+                    RestTemplate restTemplate = (RestTemplate) applicationContext.getBean("restTemplate");
45
+                    //自定义轮询方式负载
46
+                    int index = ThreadLocalRandom.current().nextInt(hostList.size());
47
+                    String url = "http://" + hostList.get(index) + "/token";
48
+                    token = restTemplate.getForObject(url, String.class);
49
+                    log.info("token:{}", token);
50
+                }
51
+            }
52
+        }
53
+        requestTemplate.header("token", token);//请求头添加token
57 54
     }
58 55
 }

+ 8 - 0
src/main/java/com/gct/edge/actuator/controller/YieldDailyController.java

@@ -90,5 +90,13 @@ public class YieldDailyController {
90 90
             return Result.error("请求参数不合法");
91 91
         return yieldDailyService.calDailyYieldWithDilution(moduleKey, wellId, date, dilution, wellName);
92 92
     }
93
+    @GetMapping("/daily-yield/one")
94
+    public Result selectOne(@RequestParam String moduleKey,
95
+                                            @RequestParam String wellId,
96
+                                            @RequestParam String date) {
97
+        if (StringUtil.isBlankOrEmpty(moduleKey) || StringUtil.isBlankOrEmpty(wellId) ||  StringUtil.isBlankOrEmpty(date))
98
+            return Result.error("请求参数不合法");
99
+        return yieldDailyService.selectOne(moduleKey, wellId, date);
100
+    }
93 101
 
94 102
 }

+ 12 - 0
src/main/java/com/gct/edge/actuator/entity/Date2.java

@@ -0,0 +1,12 @@
1
+package com.gct.edge.actuator.entity;
2
+
3
+@Deprecated
4
+public class Date2 {
5
+    public String start;
6
+    public String end;
7
+
8
+    public Date2(String start, String end) {
9
+        this.start = start;
10
+        this.end = end;
11
+    }
12
+}

+ 2 - 1
src/main/java/com/gct/edge/actuator/mapper/YieldRealTimeMapper.java

@@ -1,6 +1,7 @@
1 1
 package com.gct.edge.actuator.mapper;
2 2
 
3 3
 import com.baomidou.mybatisplus.core.mapper.BaseMapper;
4
+import com.gct.edge.actuator.entity.Date2;
4 5
 import com.gct.edge.actuator.entity.YieldRealTime;
5 6
 import org.apache.ibatis.annotations.Mapper;
6 7
 import org.apache.ibatis.annotations.Param;
@@ -16,5 +17,5 @@ public interface YieldRealTimeMapper extends BaseMapper<YieldRealTime> {
16 17
 
17 18
     int insertOneOnConflictDoNothing(@Param("one") YieldRealTime one);
18 19
 
19
-    List<YieldRealTime> queryRangeFirst(String moduleKey, String wellId, ArrayList<String[]> dateList);
20
+//    List<YieldRealTime> queryRangeFirst(String moduleKey, String wellId, ArrayList<Date2> dateList);
20 21
 }

+ 5 - 4
src/main/java/com/gct/edge/actuator/mapper/YieldRealTimeMapper.xml

@@ -53,10 +53,11 @@
53 53
              on conflict (well_id,pro_time) do nothing
54 54
 
55 55
     </insert>
56
-    <select id="queryRangeFirst" resultMap="YieldRealTimeMapper">
56
+<!--    sharding jdbc 不支持 union all,下面方法不可行-->
57
+<!--    <select id="queryRangeFirst" resultMap="YieldRealTimeMapper">
57 58
         <foreach collection="dateList" separator=" union all " item="date">
58
-            select * from yield_real_time where module_key = #{moduleKey} and well_id = #{wellId} and pro_time between
59
-            #{date[0]} and #{date[1]} limit 1
59
+            (select * from yield_real_time where module_key = #{moduleKey} and well_id = #{wellId} and pro_time between
60
+            #{date.start} and #{date.end} limit 1)
60 61
         </foreach>
61
-    </select>
62
+    </select>-->
62 63
 </mapper>

+ 2 - 0
src/main/java/com/gct/edge/actuator/service/YieldDailyService.java

@@ -23,4 +23,6 @@ public interface YieldDailyService extends IService<YieldDaily> {
23 23
     List<YieldDaily> getAoidDailyYieldInfoByWellId(String moduleKey,String wellName, String startDate);
24 24
 
25 25
     Result calDailyYieldWithDilution(String moduleKey, String wellId, String date,int dilution,String wellName);
26
+
27
+    Result selectOne(String moduleKey, String wellId, String date);
26 28
 }

+ 13 - 0
src/main/java/com/gct/edge/actuator/service/impl/YieldDailyServiceImpl.java

@@ -101,6 +101,17 @@ public class YieldDailyServiceImpl extends ServicePlusImpl<YieldDailyMapper, Yie
101 101
     }
102 102
 
103 103
     @Override
104
+    public Result selectOne(String moduleKey, String wellId, String date) {
105
+        LambdaQueryWrapper<YieldDaily> queryWrapper = new LambdaQueryWrapper<YieldDaily>()
106
+                .eq(YieldDaily::getModuleKey, moduleKey)
107
+                .eq(YieldDaily::getWellId, wellId)
108
+                .eq(YieldDaily::getProTime, DateTimeUtil.formatDateTime(date+" 00:00:00"));
109
+        YieldDaily one = yieldDailyMapper.selectOne(queryWrapper);
110
+        if (Objects.isNull(one)) return Result.error("查无数据");
111
+        return Result.ok(one);
112
+    }
113
+
114
+    @Override
104 115
     public Result calDailyYieldWithDilution(String moduleKey, String wellId, String date, int dilution, String wellName) {
105 116
         List<YieldRealTime> lis = yieldService.getRealYieldWithDilution(moduleKey, wellId, date, dilution);
106 117
         List<AoidProdState> states = aoidProdStateService.getAoidProdStateList(moduleKey, wellId, date);
@@ -255,6 +266,8 @@ public class YieldDailyServiceImpl extends ServicePlusImpl<YieldDailyMapper, Yie
255 266
         if (Objects.nonNull(re.getBx())) re.setBx(Math.round(re.getBx() * 10) / 10d);
256 267
         if (Objects.nonNull(re.getLlpl())) re.setLlpl(Math.round(re.getLlpl() * 10) / 10d);
257 268
         if (Objects.nonNull(re.getWaterCut())) re.setWaterCut(Math.round(re.getWaterCut() * 10) / 10d);
269
+        re.setYxgts(calNormalCount);
270
+        re.setErrorgts(lis.size() - calNormalCount);
258 271
         //endregion
259 272
 
260 273
 

+ 19 - 6
src/main/java/com/gct/edge/actuator/service/impl/YieldServiceImpl.java

@@ -8,6 +8,7 @@ import com.gct.common.core.result.Result;
8 8
 import com.gct.common.util.DateTimeUtil;
9 9
 import com.gct.common.util.StringUtil;
10 10
 import com.gct.edge.actuator.algorithm.CalIntegral;
11
+import com.gct.edge.actuator.entity.Date2;
11 12
 import com.gct.edge.actuator.entity.YieldRealTime;
12 13
 import com.gct.edge.actuator.mapper.YieldRealTimeMapper;
13 14
 import com.gct.edge.actuator.service.ServicePlusImpl;
@@ -117,15 +118,27 @@ public class YieldServiceImpl extends ServicePlusImpl<YieldRealTimeMapper, Yield
117 118
         LocalDateTime start = DateTimeUtil.formatDateTime(date + " 00:00:00");
118 119
         LocalDateTime end = DateTimeUtil.formatDateTime(date + " 23:59:59");
119 120
         interval = interval / dilutionVal;
120
-        ArrayList<String[]> dateList = new ArrayList<>();
121
-        while (Duration.between(start.minusSeconds(interval), end).getSeconds() > 0) {
122
-            dateList.add(new String[]{DateTimeUtil.formatString(start), DateTimeUtil.formatString(start.minusSeconds(interval))});
123
-            start = start.minusSeconds(interval);
121
+        ArrayList<LocalDateTime[]> dateList = new ArrayList<>();
122
+        while (Duration.between(start.plusMinutes(interval), end).getSeconds() > 0) {
123
+            dateList.add(new LocalDateTime[]{start, start.plusMinutes(interval)});
124
+            start = start.plusMinutes(interval);
124 125
         }
125 126
         //最多只能补一个,够数了就不用补
126 127
         if (dateList.size() < dilutionVal)
127
-            dateList.add(new String[]{DateTimeUtil.formatString(start), DateTimeUtil.formatString(end)});
128
-        return yieldRealTimeMapper.queryRangeFirst(moduleKey, wellId, dateList);
128
+            dateList.add(new LocalDateTime[]{start, end});
129
+
130
+        List<YieldRealTime> lis = new ArrayList<>();
131
+        for (LocalDateTime[] item : dateList) {
132
+            LambdaQueryWrapper<YieldRealTime> wrapper = new LambdaQueryWrapper<YieldRealTime>()
133
+                    .eq(YieldRealTime::getModuleKey, moduleKey)
134
+                    .eq(YieldRealTime::getWellId, wellId)
135
+                    .between(YieldRealTime::getProTime, item[0], item[1])
136
+                    .last(" limit 1 ");
137
+            YieldRealTime one = yieldRealTimeMapper.selectOne(wrapper);
138
+            if (Objects.nonNull(one)) lis.add(one);
139
+        }
140
+
141
+        return lis;
129 142
     }
130 143
 
131 144
     @Override

+ 6 - 2
src/main/resources/application-dev.yml

@@ -8,8 +8,8 @@ server:
8 8
 logging:
9 9
   level:
10 10
     root: info
11
-    com.gct.edge.actuator.mapper: off
12
-    com.gct.edge.actuator.service: off
11
+    com.gct.edge.actuator.mapper: debug
12
+    com.gct.edge.actuator.service: debug
13 13
 
14 14
 management:
15 15
   endpoints:
@@ -110,3 +110,7 @@ auth:
110 110
 
111 111
 sample-source-path: ${user.home}/dl/samplesource/
112 112
 
113
+mybatis-plus:
114
+  configuration:
115
+    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
116
+    default-statement-timeout: