ke
2024-05-13 659d3e60c6fe5e5bcd8c4cbb3dd8077e29f13a38
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
package cn.gistack.nky.service.impl;
 
import cn.gistack.common.utils.SpringContextUtil;
import cn.gistack.nky.resultpojo.*;
import cn.gistack.nky.service.IZtApiService;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.springblade.core.tool.utils.ObjectUtil;
import org.springblade.core.tool.utils.StringUtil;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
 
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
 
/**
 * @PROJECT_NAME: skjcmanager
 * @DESCRIPTION:
 * @USER: aix
 * @DATE: 2023/7/28 17:16
 */
@Service
@Slf4j
public class ZtApiServiceImpl implements IZtApiService {
 
    //本地测试
    private static final String LOCAL = "https://sk.hubeishuiyi.cn";
    //线上
    private static final String ONLINE = "http://10.42.6.192";
 
 
    private static final String API_KEY = "F1DBECD719108635189480CF60E6553ADB3109616426BD537F25A430DFC613B491A025C4A51E77FD08C6E5B7CBE05917A461286E7B6D69F1AB1B14F946149D2065B0C675F8FEDF4B9B05C1496881BC5A";
 
    /**
     * 实时渗压监测
     */
    private static final String LASTEST_OSMOTIC_PRESSURE = "/services/1234567890ABCDEFGHIJKLMN/monitor/lastest_osmotic_pressure/api?res_nm=&cd=&city_cd=&county_cd=&pageNo=1&pageSize=9999";
 
    /**
     * 位移监测
     */
    private static final String WY_API ="/services/1234567890ABCDEFGHIJKLMN/monitor/wy/api?res_nm=&cd=&city_cd=&county_cd=&pageNo=1&pageSize=9999";
 
    /**
     * 渗流监测
     */
    private static final String SL_API ="/services/1234567890ABCDEFGHIJKLMN/monitor/sl/api?res_nm=&cd=&city_cd=&county_cd=&pageNo=1&pageSize=9999";
 
    private static final String RES_DAM_MONITOR ="/services/1234567890ABCDEFGHIJKLMN/res_dam_monitor/api?province_cd=420000000000&res_nm=";
    private static final String BASE_CD ="/services/1234567890ABCDEFGHIJKLMN/base/cd/api";
 
 
    int getSyCount = 0;
    @Override
    public List<DataResChildrenPo> getSy() {
        getSyCount = getSyCount +1;
        Map<String, Object> params = new LinkedHashMap<>();
        try {
            log.info("第{}次请求",getSyCount);
            List<DataResChildrenPo> get = getData(LASTEST_OSMOTIC_PRESSURE, "get", params);
            getSyCount = 0;
            return get;
        }catch (Exception e){
            e.printStackTrace();
            if (getSyCount<=5){
                return this.getSy();
            }else {
                List<DataResChildrenPo> list = new ArrayList<>();
                return list;
            }
        }
    }
 
    @Override
    public List<DataResChildrenPo> getWy() {
        Map<String, Object> params = new LinkedHashMap<>();
        return getData(WY_API, "get", params);
    }
 
    int getResSyCount = 0;
    @Override
    public List<DataResPo> getResSy() {
        getResSyCount = getResSyCount +1;
        Map<String, Object> params = new LinkedHashMap<>();
        try {
            log.info("第{}次请求",getResSyCount);
            List<DataResPo> get = getResData(LASTEST_OSMOTIC_PRESSURE, "get", params);
            getResSyCount = 0;
            return get;
        }catch (Exception e){
            e.printStackTrace();
            if (getResSyCount<=5){
                return this.getResSy();
            }else {
                List<DataResPo> list = new ArrayList<>();
                return list;
            }
        }
    }
 
    @Override
    public List<DataResPo> getResWy() {
        Map<String, Object> params = new LinkedHashMap<>();
        return getResData(WY_API, "get", params);
    }
 
    @Override
    public List<DataResPo> getResSl() {
        Map<String, Object> params = new LinkedHashMap<>();
        return getResData(SL_API, "get", params);
    }
 
    @Override
    public List<DataResChildrenPo> getSl() {
        Map<String, Object> params = new LinkedHashMap<>();
        return getData(SL_API, "get", params);
    }
 
    @Override
    public List<ResDamMonitorResult> getResDamMonitor() {
        Map<String, Object> params = new LinkedHashMap<>();
        return getResDamMonitorResult(RES_DAM_MONITOR, "get", params);
    }
 
    @Override
    public List<BaseCd> getBaseCd(String resGuid, String type) {
        Map<String, Object> params = new LinkedHashMap<>();
        String url = BASE_CD + StringUtil.format("?res_cd={}&type={}",resGuid,type);
        // 获取环境
        String activeProfile = SpringContextUtil.getActiveProfile();
        if (activeProfile.equals("dev")) {
            url = LOCAL+ url ;
        }
        if (activeProfile.equals("prod")) {
            url = ONLINE+ url ;
        }
        if (activeProfile.equals("test")) {
            url = ONLINE + url;
        }
 
        HttpMethod method;
            method = HttpMethod.GET;
        // 声明一个header变量
        HttpHeaders headers = new HttpHeaders();
        // 设置apikey
        headers.set("apikey",API_KEY);
 
        HttpEntity<String> httpEntity = new HttpEntity(null, headers);
        log.info("中台请求地址:{}", url);
        RestTemplate template = new RestTemplate();
        ResponseEntity<ZtResultPo2> ztResultPojoResponseEntity = template.exchange(url, method,httpEntity, ZtResultPo2.class,params);
        List<BaseCd> returnResult = new ArrayList<>();
        if (ztResultPojoResponseEntity.getStatusCodeValue() == 200) {
            returnResult = ztResultPojoResponseEntity.getBody().getData();
        }
        return returnResult;
    }
 
    private List<DataResChildrenPo> getData(String url, String type, Map<String, Object> params) {
        // 获取环境
        String activeProfile = SpringContextUtil.getActiveProfile();
        if (activeProfile.equals("dev")) {
            url = LOCAL+ url ;
        }
        if (activeProfile.equals("prod")) {
            url = ONLINE+ url ;
        }
        if (activeProfile.equals("test")) {
            url = ONLINE + url;
        }
 
        HttpMethod method;
        if (type.toLowerCase().equals("get".toLowerCase())) {
            method = HttpMethod.GET;
        } else {
            method = HttpMethod.POST;
        }
        // 声明一个header变量
        HttpHeaders headers = new HttpHeaders();
        // 设置apikey
        headers.set("apikey",API_KEY);
 
        HttpEntity<String> httpEntity = new HttpEntity(null, headers);
 
        RestTemplate template = new RestTemplate();
        log.info("中台请求地址:{}", url);
        ResponseEntity<ZtResultPo> ztResultPojoResponseEntity = template.exchange(url, method,httpEntity, ZtResultPo.class,params);
        if (ztResultPojoResponseEntity.getStatusCodeValue() == 200) {
            List<DataResChildrenPo> children = new ArrayList<>();
            List<DataResPo> data = ztResultPojoResponseEntity.getBody().getData().getData();
            //遍历数据,存为测站子集
            data.forEach(dataResPo -> {
                if (dataResPo.getChildren() != null){
                    dataResPo.getChildren().forEach(dataResChildrenPo -> {
                        children.add(dataResChildrenPo);
                    });
                }
            });
            return children;
        }
        return null;
    }
 
    private List<DataResPo> getResData(String url, String type, Map<String, Object> params){
 
        // 获取环境
        String activeProfile = SpringContextUtil.getActiveProfile();
        if (activeProfile.equals("dev")) {
            url = LOCAL+ url ;
        }
        if (activeProfile.equals("prod")) {
            url = ONLINE+ url ;
        }
        if (activeProfile.equals("test")) {
            url = ONLINE + url;
        }
 
 
        HttpMethod method;
        if (type.toLowerCase().equals("get".toLowerCase())) {
            method = HttpMethod.GET;
        } else {
            method = HttpMethod.POST;
        }
        // 声明一个header变量
        HttpHeaders headers = new HttpHeaders();
        // 设置apikey
        headers.set("apikey",API_KEY);
 
        HttpEntity<String> httpEntity = new HttpEntity(null, headers);
        log.info("中台请求地址:{}", url);
        RestTemplate template = new RestTemplate();
        ResponseEntity<ZtResultPo> ztResultPojoResponseEntity = template.exchange(url, method,httpEntity, ZtResultPo.class,params);
        if (ztResultPojoResponseEntity.getStatusCodeValue() == 200) {
            List<DataResPo> data = ztResultPojoResponseEntity.getBody().getData().getData();
            return data;
        }
        return null;
    }
 
    private List<ResDamMonitorResult> getResDamMonitorResult(String url, String type, Map<String, Object> params){
 
        // 获取环境
        String activeProfile = SpringContextUtil.getActiveProfile();
        if (activeProfile.equals("dev")) {
            url = LOCAL+ url ;
        }
        if (activeProfile.equals("prod")) {
            url = ONLINE+ url ;
        }
        if (activeProfile.equals("test")) {
            url = ONLINE + url;
        }
 
 
        HttpMethod method;
        if (type.toLowerCase().equals("get".toLowerCase())) {
            method = HttpMethod.GET;
        } else {
            method = HttpMethod.POST;
        }
        // 声明一个header变量
        HttpHeaders headers = new HttpHeaders();
        // 设置apikey
        headers.set("apikey",API_KEY);
 
        HttpEntity<String> httpEntity = new HttpEntity(null, headers);
        log.info("中台请求地址:{}", url);
        RestTemplate template = new RestTemplate();
        ResponseEntity<ZtResult> ztResultPojoResponseEntity = template.exchange(url, method,httpEntity, ZtResult.class,params);
        if (ztResultPojoResponseEntity.getStatusCodeValue() == 200) {
            List<ResDamMonitorResult> resDamMonitorResultList = ztResultPojoResponseEntity.getBody().getResultList();
            return resDamMonitorResultList;
        }
        return null;
    }
 
}