doum
9 天以前 0201c32312f6478b2bde706607c8c6338e9e1d06
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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
package com.doumee.core.conditoner;
 
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.doumee.core.conditoner.model.ConditionerConstant;
import com.doumee.core.conditoner.model.request.*;
import com.doumee.core.conditoner.model.response.*;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;
 
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
 
/**
 * 智精灵空调平台 HTTP 工具类。
 * <p>
 * 协议:GET 读取(Query 参数);POST 控制/写入(JSON Body)。
 * 成功标识 {@code code=200}。登录后公共参数 {@code kt_token}、{@code kt_dwid}、{@code kt_sonid}。
 * </p>
 */
@Slf4j
public class ConditionerUtil {
 
    private static final int HTTP_RETRY = 3;
 
    private ConditionerUtil() {
    }
 
    // ==================== 二、基础控制 ====================
 
    public static ConditionerBaseResponse<LoginDataResponse> login() {
        return login(null);
    }
 
    public static ConditionerBaseResponse<LoginDataResponse> login(LoginRequest req) {
        LoginRequest r = req != null ? req : new LoginRequest();
        JSONObject body = new JSONObject();
        body.put("username", StringUtils.isNotBlank(r.getUsername()) ? r.getUsername() : ConditionerConstant.username);
        body.put("password", StringUtils.isNotBlank(r.getPassword()) ? r.getPassword() : ConditionerConstant.password);
        try {
            String raw = doPostRaw("/login", body.toJSONString());
            ConditionerBaseResponse<LoginDataResponse> resp = parseObjectResponse(raw, LoginDataResponse.class);
            applyLoginSession(resp);
            return resp;
        } catch (Exception e) {
            log.error("conditioner login failed", e);
            return null;
        }
    }
 
    public static ConditionerBaseResponse<List<DeviceStatusResponse>> getDevList(ConditionerSessionRequest req) {
        return getList("/getDevList", req, DeviceStatusResponse.class);
    }
 
    public static ConditionerBaseResponse<DeviceStatusResponse> getDevOne(GetDevOneRequest req) {
        if (req == null) {
            return null;
        }
        req.fillSessionDefaults();
        try {
            String raw = doGetRaw("/getDevOne", toQueryMap(req));
            return parseObjectResponse(raw, DeviceStatusResponse.class);
        } catch (Exception e) {
            log.error("conditioner getDevOne failed", e);
            return null;
        }
    }
 
    public static ConditionerBaseResponse<Object> devCtr(DevControlRequest req) {
        return postJson("/devCtr", req, Object.class);
    }
 
    public static ConditionerBaseResponse<Object> devManyCtr(DevManyControlRequest req) {
        return postJson("/devManyCtr", req, Object.class);
    }
    public static ConditionerBaseResponse<Object> devLockManyCtr(DevLockControlRequest req) {
        return postJson("/devManyCtr", req, Object.class);
    }
 
    // ==================== 三、设备管理 ====================
 
    public static ConditionerBaseResponse<List<GatewayInfoResponse>> getWg(ConditionerSessionRequest req) {
        return getList("/getWg", req, GatewayInfoResponse.class);
    }
 
    public static ConditionerBaseResponse<Object> addWg(WgManageRequest req) {
        return postJson("/addWg", req, Object.class);
    }
 
    public static ConditionerBaseResponse<Object> changeWg(WgManageRequest req) {
        return postJson("/changeWg", req, Object.class);
    }
 
    public static ConditionerBaseResponse<Object> delWg(WgManageRequest req) {
        return postJson("/delWg", req, Object.class);
    }
 
    public static ConditionerBaseResponse<Object> wgWithArea(WgWithAreaRequest req) {
        return postJson("/wgWithArea", req, Object.class);
    }
 
    public static ConditionerBaseResponse<List<DeviceArchiveResponse>> getDev(ConditionerSessionRequest req) {
        return getList("/getDev", req, DeviceArchiveResponse.class);
    }
 
    public static ConditionerBaseResponse<Object> addDev(DevManageRequest req) {
        return postJson("/addDev", req, Object.class);
    }
 
    public static ConditionerBaseResponse<Object> changeDev(DevManageRequest req) {
        return postJson("/changeDev", req, Object.class);
    }
 
    public static ConditionerBaseResponse<Object> delDev(DevManageRequest req) {
        return postJson("/delDev", req, Object.class);
    }
 
    // ==================== 四、区域管理 ====================
 
    public static ConditionerBaseResponse<List<RoomInfoResponse>> getRoom(ConditionerSessionRequest req) {
        return getList("/getRoom", req, RoomInfoResponse.class);
    }
 
    public static ConditionerBaseResponse<Object> addRoom(RoomManageRequest req) {
        return postJson("/addRoom", req, Object.class);
    }
 
    public static ConditionerBaseResponse<Object> changeRoom(RoomManageRequest req) {
        return postJson("/changeRoom", req, Object.class);
    }
 
    public static ConditionerBaseResponse<Object> delRoom(RoomManageRequest req) {
        return postJson("/delRoom", req, Object.class);
    }
 
    public static ConditionerBaseResponse<List<FloorInfoResponse>> getFloor(ConditionerPageRequest req) {
        return getList("/getFloor", req, FloorInfoResponse.class);
    }
 
    public static ConditionerBaseResponse<Object> addFloor(FloorManageRequest req) {
        return postJson("/addFloor", req, Object.class);
    }
 
    public static ConditionerBaseResponse<Object> changeFloor(FloorManageRequest req) {
        return postJson("/changeFloor", req, Object.class);
    }
 
    public static ConditionerBaseResponse<Object> delFloor(FloorManageRequest req) {
        return postJson("/delFloor", req, Object.class);
    }
 
    public static ConditionerBaseResponse<List<UnitInfoResponse>> getUnit(ConditionerPageRequest req) {
        return getList("/getUnit", req, UnitInfoResponse.class);
    }
 
    public static ConditionerBaseResponse<Object> addUnit(UnitManageRequest req) {
        return postJson("/addUnit", req, Object.class);
    }
 
    public static ConditionerBaseResponse<Object> changeUnit(UnitManageRequest req) {
        return postJson("/changeUnit", req, Object.class);
    }
 
    public static ConditionerBaseResponse<Object> delUnit(UnitManageRequest req) {
        return postJson("/delUnit", req, Object.class);
    }
 
    public static ConditionerBaseResponse<List<BuildingInfoResponse>> getBuilding(ConditionerPageRequest req) {
        return getList("/getBuilding", req, BuildingInfoResponse.class);
    }
 
    public static ConditionerBaseResponse<Object> addBuilding(BuildingManageRequest req) {
        return postJson("/addBuilding", req, Object.class);
    }
 
    public static ConditionerBaseResponse<Object> changeBuilding(BuildingManageRequest req) {
        return postJson("/changeBuilding", req, Object.class);
    }
 
    public static ConditionerBaseResponse<Object> delBuilding(BuildingManageRequest req) {
        return postJson("/delBuilding", req, Object.class);
    }
 
    public static ConditionerBaseResponse<List<AreaInfoResponse>> getArea(ConditionerPageRequest req) {
        return getList("/getArea", req, AreaInfoResponse.class);
    }
 
    public static ConditionerBaseResponse<Object> addArea(AreaManageRequest req) {
        return postJson("/addArea", req, Object.class);
    }
 
    public static ConditionerBaseResponse<Object> changeArea(AreaManageRequest req) {
        return postJson("/changeArea", req, Object.class);
    }
 
    public static ConditionerBaseResponse<Object> delArea(AreaManageRequest req) {
        return postJson("/delArea", req, Object.class);
    }
 
    // ==================== 五、定时管理 ====================
 
    public static ConditionerBaseResponse<List<TimingInfoResponse>> getTiming(ConditionerSessionRequest req) {
        return getList("/getTiming", req, TimingInfoResponse.class);
    }
 
    public static ConditionerBaseResponse<Object> addTiming(TimingManageRequest req) {
        return postJson("/addTiming", req, Object.class);
    }
 
    public static ConditionerBaseResponse<Object> changeTiming(TimingManageRequest req) {
        return postJson("/changeTiming", req, Object.class);
    }
 
    public static ConditionerBaseResponse<Object> delTiming(TimingManageRequest req) {
        return postJson("/delTiming", req, Object.class);
    }
 
    public static ConditionerBaseResponse<Object> timingWithArea(TimingWithAreaRequest req) {
        return postJson("/timingWithArea", req, Object.class);
    }
 
    // ==================== 六、数据查询 ====================
 
    public static ConditionerBaseResponse<List<Object>> getLogWg(LogQueryRequest req) {
        return getList("/getLogWg", req, Object.class);
    }
 
    public static ConditionerBaseResponse<List<Object>> getLogDev(LogQueryRequest req) {
        return getList("/getLogDev", req, Object.class);
    }
 
    // ==================== 七、计量管理 ====================
 
    public static ConditionerBaseResponse<List<DlSjXsResponse>> getDlSjXs(ConditionerSessionRequest req) {
        return getList("/getDlSjXs", req, DlSjXsResponse.class);
    }
 
    public static ConditionerBaseResponse<Object> changeDlSjXs(ChangeDlSjXsRequest req) {
        return postJson("/changeDlSjXs", req, Object.class);
    }
 
    public static ConditionerBaseResponse<List<MeterDbInfoResponse>> getDb(MeterDbManageRequest req) {
        return getList("/glDb/getDb", req, MeterDbInfoResponse.class);
    }
 
    public static ConditionerBaseResponse<Object> addDb(MeterDbManageRequest req) {
        return postJson("/glDb/addDb", req, Object.class);
    }
 
    public static ConditionerBaseResponse<Object> changeDb(MeterDbManageRequest req) {
        return postJson("/glDb/changeDb", req, Object.class);
    }
 
    public static ConditionerBaseResponse<Object> delDb(MeterDbManageRequest req) {
        return postJson("/glDb/delDb", req, Object.class);
    }
 
    public static ConditionerBaseResponse<List<Object>> getDbDaySum(DbDaySumQueryRequest req) {
        return getList("/getDbDaySum", req, Object.class);
    }
 
    public static ConditionerBaseResponse<List<Object>> getDayDl(DayDlQueryRequest req) {
        return getList("/getDayDl", req, Object.class);
    }
 
    public static ConditionerBaseResponse<List<Object>> getMoonDl(MoonDlQueryRequest req) {
        return getList("/getMoonDl", req, Object.class);
    }
 
    public static ConditionerBaseResponse<List<CompanyGsInfoResponse>> getGs(CompanyGsManageRequest req) {
        return getList("/getGs", req, CompanyGsInfoResponse.class);
    }
 
    public static ConditionerBaseResponse<Object> addGs(CompanyGsManageRequest req) {
        if (req == null) {
            return null;
        }
        req.fillSessionDefaults();
        JSONObject body = new JSONObject(true);
        putSessionFields(body, req);
        body.put("is_pwr", req.getIs_pwr() != null ? req.getIs_pwr() : 1);
        if (req.getLi_dev() != null && !req.getLi_dev().isEmpty()) {
            body.put("li_dev", req.getLi_dev());
        }
        if (req.getD_dev() != null && !req.getD_dev().isEmpty()) {
            body.put("d_dev", req.getD_dev());
        }
        body.put("gs_name", req.getGs_name());
        body.put("is_rest_stop", req.getIs_rest_stop() != null ? req.getIs_rest_stop() : 0);
        body.put("gs_bz", StringUtils.defaultString(req.getGs_bz()));
        putStopMoney(body, req.getStop_money());
        return postJsonBody("/addGs", body, Object.class);
    }
 
    public static ConditionerBaseResponse<Object> changeGs(CompanyGsManageRequest req) {
        if (req == null || req.getId() == null) {
            return null;
        }
        req.fillSessionDefaults();
        JSONObject body = new JSONObject(true);
        body.put("id", req.getId());
        body.put("is_pwr", req.getIs_pwr() != null ? req.getIs_pwr() : 1);
        body.put("is_rest_stop", req.getIs_rest_stop() != null ? req.getIs_rest_stop() : 0);
        body.put("gs_name", req.getGs_name());
        if (req.getLeft_money() != null) {
            body.put("left_money", req.getLeft_money());
        }
        body.put("is_stop", req.getIs_stop() != null ? req.getIs_stop() : 0);
        body.put("li_dev", req.getLi_dev());
        body.put("d_dev", req.getD_dev());
        body.put("gs_bz", StringUtils.defaultString(req.getGs_bz()));
        putStopMoney(body, req.getStop_money());
        return postJsonBody("/changeGs", body, Object.class);
    }
 
    private static void putStopMoney(JSONObject body, Object stopMoney) {
        if (stopMoney != null) {
            body.put("stop_money", stopMoney);
        }
    }
 
    public static ConditionerBaseResponse<Object> delGs(CompanyGsManageRequest req) {
        return postJson("/delGs", req, Object.class);
    }
 
    public static ConditionerBaseResponse<Object> gsWithArea(GsWithAreaRequest req) {
        return postJson("/gsWithArea", req, Object.class);
    }
 
    public static ConditionerBaseResponse<Object> addMoney(AddMoneyRequest req) {
        if (req == null) {
            return null;
        }
        req.fillSessionDefaults();
        JSONObject body = new JSONObject(true);
        putSessionFields(body, req);
        body.put("id", req.getId());
        body.put("cz_money", req.getCz_money());
        return postJsonBody("/addMoney", body, Object.class);
    }
 
    public static ConditionerBaseResponse<Object> cleanMoney(CompanyGsManageRequest req) {
        if (req == null || req.getId() == null) {
            return null;
        }
        req.fillSessionDefaults();
        JSONObject body = new JSONObject(true);
        putSessionFields(body, req);
        body.put("id", req.getId());
        return postJsonBody("/cleanMoney", body, Object.class);
    }
 
    public static ConditionerBaseResponse<List<Object>> getCzLog(LogQueryRequest req) {
        return getList("/getCzLog", req, Object.class);
    }
 
    // ==================== 八、账号管理 ====================
 
    public static ConditionerBaseResponse<List<UserInfoResponse>> getUser(ConditionerSessionRequest req) {
        return getList("/getUser", req, UserInfoResponse.class);
    }
 
    public static ConditionerBaseResponse<Object> addUser(UserManageRequest req) {
        return postJson("/addUser", req, Object.class);
    }
 
    public static ConditionerBaseResponse<Object> changeUser(UserManageRequest req) {
        return postJson("/changeUser", req, Object.class);
    }
 
    public static ConditionerBaseResponse<Object> changeUserPwd(UserManageRequest req) {
        return postJson("/changeUserPwd", req, Object.class);
    }
 
    public static ConditionerBaseResponse<Object> delUser(UserManageRequest req) {
        return postJson("/delUser", req, Object.class);
    }
 
    // ==================== 会话 ====================
 
    public static void applyLoginSession(ConditionerBaseResponse<LoginDataResponse> resp) {
        if (resp == null || !resp.isSuccess() || resp.getData() == null) {
            return;
        }
        LoginDataResponse data = resp.getData();
        if (StringUtils.isNotBlank(data.getKt_token())) {
            ConditionerConstant.kt_token = data.getKt_token();
        }
        if (data.getKt_dwid() != null) {
            ConditionerConstant.kt_dwid = String.valueOf(data.getKt_dwid());
        }
        if (data.getKt_sonid() != null) {
            ConditionerConstant.kt_sonid = String.valueOf(data.getKt_sonid());
        }
    }
 
    // ==================== HTTP / 解析 ====================
 
    private static <T> ConditionerBaseResponse<List<T>> getList(String path, ConditionerSessionRequest req, Class<T> itemClass) {
        if (req != null) {
            req.fillSessionDefaults();
        } else {
            req = new ConditionerSessionRequest();
            req.fillSessionDefaults();
        }
        try {
            String raw = doGetRaw(path, toQueryMap(req));
            return parseListResponse(raw, itemClass);
        } catch (Exception e) {
            log.error("conditioner GET {} failed", path, e);
            return null;
        }
    }
 
    private static <T> ConditionerBaseResponse<T> postJson(String path, ConditionerSessionRequest req, Class<T> dataClass) {
        if (req != null) {
            req.fillSessionDefaults();
        }
        try {
            String raw = doPostRaw(path, JSON.toJSONString(req));
            return parseObjectResponse(raw, dataClass);
        } catch (Exception e) {
            log.error("conditioner POST {} failed", path, e);
            return null;
        }
    }
 
    private static <T> ConditionerBaseResponse<T> postJsonBody(String path, JSONObject body, Class<T> dataClass) {
        try {
            String raw = doPostRaw(path, body.toJSONString());
            return parseObjectResponse(raw, dataClass);
        } catch (Exception e) {
            log.error("conditioner POST {} failed", path, e);
            return null;
        }
    }
 
    private static void putSessionFields(JSONObject body, ConditionerSessionRequest req) {
        body.put("kt_token", req.getKt_token());
        body.put("kt_dwid", req.getKt_dwid());
        body.put("kt_unit", StringUtils.defaultIfBlank(req.getKt_unit(), ConditionerConstant.DEFAULT_KT_UNIT));
        body.put("kt_sonid", req.getKt_sonid());
    }
 
    private static String resolveUrl(String path) {
        String base = StringUtils.defaultIfBlank(ConditionerConstant.base_url, ConditionerConstant.DEFAULT_BASE_URL);
        if (base.endsWith("/")) {
            base = base.substring(0, base.length() - 1);
        }
        if (!path.startsWith("/")) {
            path = "/" + path;
        }
        return base + path;
    }
 
    private static String doGetRaw(String path, Map<String, String> params) throws Exception {
        String url = buildGetUrl(path, params);
        log.info("conditioner GET url={}", maskUrl(url));
        HttpClient client = HttpClientBuilder.create().build();
        HttpGet get = new HttpGet(url);
        HttpResponse response = executeWithRetry(client, get);
        String resp = EntityUtils.toString(response.getEntity(), StandardCharsets.UTF_8);
        log.info("conditioner GET response={}", abbreviate(resp));
        return resp;
    }
 
    private static String doPostRaw(String path, String jsonBody) throws Exception {
        String url = resolveUrl(path);
        log.info("conditioner POST url={}, body={}", url, maskJson(jsonBody));
        HttpClient client = HttpClientBuilder.create().build();
        HttpPost post = new HttpPost(url);
        post.setHeader("Content-Type", "application/json;charset=UTF-8");
        post.setEntity(new StringEntity(jsonBody, StandardCharsets.UTF_8));
        HttpResponse response = executeWithRetry(client, post);
        String resp = EntityUtils.toString(response.getEntity(), StandardCharsets.UTF_8);
        log.info("conditioner POST response={}", abbreviate(resp));
        return resp;
    }
 
    private static HttpResponse executeWithRetry(HttpClient client, org.apache.http.client.methods.HttpUriRequest request) throws Exception {
        HttpResponse execute = null;
        int retry = HTTP_RETRY;
        while (retry-- > 0) {
            try {
                execute = client.execute(request);
                break;
            } catch (Exception e) {
                if (retry <= 0) {
                    throw e;
                }
                Thread.sleep(5000);
            }
        }
        if (execute == null) {
            throw new Exception("conditioner http request failed");
        }
        return execute;
    }
 
    private static String buildGetUrl(String path, Map<String, String> params) throws Exception {
        StringBuilder sb = new StringBuilder(resolveUrl(path));
        if (params == null || params.isEmpty()) {
            return sb.toString();
        }
        sb.append("?");
        boolean first = true;
        for (Map.Entry<String, String> entry : params.entrySet()) {
            if (StringUtils.isBlank(entry.getValue())) {
                continue;
            }
            if (!first) {
                sb.append("&");
            }
            sb.append(urlEncode(entry.getKey())).append("=").append(urlEncode(entry.getValue()));
            first = false;
        }
        return sb.toString();
    }
 
    private static Map<String, String> toQueryMap(Object obj) {
        Map<String, String> map = new LinkedHashMap<>();
        if (obj == null) {
            return map;
        }
        JSONObject json = (JSONObject) JSON.toJSON(obj);
        for (Map.Entry<String, Object> entry : json.entrySet()) {
            Object val = entry.getValue();
            if (val != null) {
                map.put(entry.getKey(), String.valueOf(val));
            }
        }
        return map;
    }
 
    private static <T> ConditionerBaseResponse<T> parseObjectResponse(String raw, Class<T> dataClass) {
        if (StringUtils.isBlank(raw)) {
            return null;
        }
        try {
            JSONObject root = JSON.parseObject(raw);
            ConditionerBaseResponse<T> resp = new ConditionerBaseResponse<>();
            resp.setCode(root.getInteger("code"));
            resp.setMessage(root.getString("message"));
            Object data = root.get("data");
            if (data != null && dataClass != null) {
                if (data instanceof JSONObject) {
                    resp.setData(((JSONObject) data).toJavaObject(dataClass));
                } else if (data instanceof String) {
                    String text = ((String) data).trim();
                    if (text.startsWith("{")) {
                        resp.setData(JSON.parseObject(text, dataClass));
                    }
                } else if (!(data instanceof JSONArray)) {
                    resp.setData(JSON.parseObject(JSON.toJSONString(data), dataClass));
                }
            }
            return resp;
        } catch (Exception e) {
            log.error("conditioner parse object response failed: {}", abbreviate(raw), e);
            return null;
        }
    }
 
    private static <T> ConditionerBaseResponse<List<T>> parseListResponse(String raw, Class<T> itemClass) {
        if (StringUtils.isBlank(raw)) {
            return null;
        }
        try {
            JSONObject root = JSON.parseObject(raw);
            ConditionerBaseResponse<List<T>> resp = new ConditionerBaseResponse<>();
            resp.setCode(root.getInteger("code"));
            resp.setMessage(root.getString("message"));
            Object data = root.get("data");
            if (data instanceof JSONArray) {
                resp.setData(((JSONArray) data).toJavaList(itemClass));
            } else if (data instanceof String) {
                String text = ((String) data).trim();
                if (text.startsWith("[")) {
                    resp.setData(JSON.parseArray(text, itemClass));
                }
            }
            return resp;
        } catch (Exception e) {
            log.error("conditioner parse list response failed: {}", abbreviate(raw), e);
            return null;
        }
    }
 
    private static String urlEncode(String value) throws Exception {
        return URLEncoder.encode(value, StandardCharsets.UTF_8.name());
    }
 
    private static String maskUrl(String url) {
        if (url == null) {
            return null;
        }
        return url.replaceAll("(kt_token=)[^&]+", "$1***");
    }
 
    private static String maskJson(String json) {
        if (json == null) {
            return null;
        }
        return json.replaceAll("(\\\"password\\\"\\s*:\\s*\\\")[^\\\"]+(\\\")", "$1***$2")
                .replaceAll("(\\\"kt_token\\\"\\s*:\\s*\\\")[^\\\"]+(\\\")", "$1***$2");
    }
 
    private static String abbreviate(String text) {
        if (text == null) {
            return null;
        }
        return text.length() > 500 ? text.substring(0, 500) + "..." : text;
    }
}