tuqin
2026-10-29 0a3129cb06fcea6677245097715dcc6fa3de65e5
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
package com.ruoyi.lims.controller;
 
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.lims.pojo.DataCollection;
import com.ruoyi.lims.pojo.DataInterface;
import com.ruoyi.lims.pojo.InterfaceField;
import com.ruoyi.lims.pojo.InterfaceLog;
import com.ruoyi.lims.service.DataCollectionService;
import com.ruoyi.lims.service.DataInterfaceService;
import com.ruoyi.lims.service.InterfaceFieldService;
import com.ruoyi.lims.service.InterfaceLogService;
import com.ruoyi.lims.service.LimsCleanService;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import okhttp3.ResponseBody;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;
 
import java.io.InputStream;
import java.io.OutputStream;
import java.math.BigDecimal;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.nio.charset.StandardCharsets;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;
 
@RestController
@AllArgsConstructor
@RequestMapping("/lims/dataInterface")
@Api(value = "DataInterface", tags = "数据接口管理表")
public class DataInterfaceController {
 
    private DataInterfaceService dataInterfaceservice;
    private InterfaceFieldService fieldService;
    private InterfaceLogService logService;
    private DataCollectionService collectionService;
    private LimsCleanService cleanService;
 
    private static final ObjectMapper JSON = new ObjectMapper();
 
    private static final OkHttpClient HTTP = new OkHttpClient.Builder()
            .connectTimeout(8, TimeUnit.SECONDS)
            .readTimeout(15, TimeUnit.SECONDS)
            .writeTimeout(15, TimeUnit.SECONDS)
            .build();
 
    @GetMapping("/listPage")
    @ApiOperation("分页查询所有数据接口管理表")
    public AjaxResult listPage(Page<DataInterface> page, DataInterface dataInterface) {
        IPage<DataInterface> listPage = dataInterfaceservice.listPage(page, dataInterface);
        return AjaxResult.success(listPage);
    }
 
    @GetMapping("/{id}")
    @ApiOperation("根据ID查询数据接口")
    public AjaxResult getDataInterface(@PathVariable Long id) {
        return AjaxResult.success(dataInterfaceservice.getById(id));
    }
 
    @PostMapping("/add")
    @ApiOperation("新增数据接口管理表")
    public AjaxResult add(@RequestBody DataInterface dataInterface) {
        return AjaxResult.success(dataInterfaceservice.save(dataInterface));
    }
 
    @PostMapping("/update")
    @ApiOperation("修改数据接口管理表")
    public AjaxResult update(@RequestBody DataInterface dataInterface) {
        return AjaxResult.success(dataInterfaceservice.updateById(dataInterface));
    }
 
    @DeleteMapping("/delete")
    @ApiOperation("删除数据接口管理表")
    public AjaxResult delete(@RequestBody List<Long> ids) {
        if (CollectionUtils.isEmpty(ids)) return AjaxResult.error("请传入要删除的ID");
        boolean ok = dataInterfaceservice.removeBatchByIds(ids);
        if (ok && !CollectionUtils.isEmpty(ids)) {
            QueryWrapper<InterfaceField> fw = new QueryWrapper<>();
            fw.in("interface_id", ids);
            fieldService.remove(fw);
        }
        return AjaxResult.success(ok);
    }
 
    /* ==================== 测试连接 ==================== */
 
    /**
     * 测试连接:HTTP 发一次 GET/HEAD,TCP 建一次连接,MQTT 当前环境不支持
     */
    @PostMapping("/test/{id}")
    @ApiOperation("测试接口连通性")
    public AjaxResult testConnection(@PathVariable Long id) {
        DataInterface di = dataInterfaceservice.getById(id);
        if (di == null) {
            return AjaxResult.error("接口不存在");
        }
        long start = System.currentTimeMillis();
        String protocol = lower(di.getProtocol());
        String result;
        boolean ok = false;
 
        try {
            if ("http".equals(protocol) || "https".equals(protocol)) {
                String url = buildHttpUrl(di);
                Request req = new Request.Builder().url(url).get().build();
                try (Response resp = HTTP.newCall(req).execute()) {
                    ok = resp.isSuccessful();
                    result = "HTTP " + resp.code() + " " + resp.message()
                            + ",耗时 " + (System.currentTimeMillis() - start) + " ms";
                }
            } else if ("tcp".equals(protocol)) {
                String host = di.getConnectionUrl() == null ? "" : di.getConnectionUrl().trim();
                int port = parseInt(di.getPort(), 0);
                if (host.isEmpty() || port <= 0) {
                    return AjaxResult.error("TCP 需要填写主机地址和端口");
                }
                // http:// 前缀兼容误填
                if (host.startsWith("http://")) host = host.substring(7);
                if (host.startsWith("https://")) host = host.substring(8);
                if (host.contains("/")) host = host.substring(0, host.indexOf('/'));
                if (host.contains(":")) host = host.substring(0, host.indexOf(':'));
                try (Socket socket = new Socket()) {
                    socket.connect(new InetSocketAddress(host, port), 5000);
                    ok = true;
                    result = "TCP 连接成功 " + host + ":" + port
                            + ",耗时 " + (System.currentTimeMillis() - start) + " ms";
                }
            } else if ("mqtt".equals(protocol)) {
                return AjaxResult.error("当前环境未集成 MQTT 客户端库(离线打包限制),"
                        + "如需 MQTT 采集请把 org.eclipse.paho.client.mqttv3 依赖加入 pom 后重新打包");
            } else {
                return AjaxResult.error("暂不支持协议:" + di.getProtocol());
            }
        } catch (Exception e) {
            saveLog(di, "test", null, null, false, e.getMessage(), System.currentTimeMillis() - start, 0);
            return AjaxResult.error("连接失败:" + shortMsg(e));
        }
 
        saveLog(di, "test", null, result, ok, ok ? null : result, System.currentTimeMillis() - start, 0);
        return ok ? AjaxResult.success(result) : AjaxResult.error(result);
    }
 
    /* ==================== 手动采集 ==================== */
 
    /**
     * 手动采集一次:按协议拉数据 -> 按字段映射转换 -> 写入采集记录 -> 记日志
     */
    @PostMapping("/collect/{id}")
    @ApiOperation("立即采集一次数据")
    public AjaxResult collect(@PathVariable Long id) {
        DataInterface di = dataInterfaceservice.getById(id);
        if (di == null) {
            return AjaxResult.error("接口不存在");
        }
        String protocol = lower(di.getProtocol());
        if ("mqtt".equals(protocol)) {
            return AjaxResult.error("当前环境未集成 MQTT 客户端库,暂不支持 MQTT 采集");
        }
 
        long start = System.currentTimeMillis();
        String rawJson;
        String requestDesc;
 
        try {
            if ("http".equals(protocol) || "https".equals(protocol)) {
                String url = buildHttpUrl(di);
                requestDesc = "GET " + url;
                Request req = new Request.Builder().url(url).get().build();
                try (Response resp = HTTP.newCall(req).execute()) {
                    ResponseBody body = resp.body();
                    rawJson = body == null ? "" : body.string();
                    if (!resp.isSuccessful()) {
                        saveLog(di, "manual", requestDesc, rawJson, false,
                                "HTTP " + resp.code(), System.currentTimeMillis() - start, 0);
                        return AjaxResult.error("采集失败:HTTP " + resp.code());
                    }
                }
            } else if ("tcp".equals(protocol)) {
                String host = di.getConnectionUrl() == null ? "" : di.getConnectionUrl().trim();
                int port = parseInt(di.getPort(), 0);
                if (host.startsWith("http://")) host = host.substring(7);
                if (host.startsWith("https://")) host = host.substring(8);
                if (host.contains("/")) host = host.substring(0, host.indexOf('/'));
                if (host.contains(":")) host = host.substring(0, host.indexOf(':'));
                requestDesc = "TCP " + host + ":" + port;
                try (Socket socket = new Socket()) {
                    socket.connect(new InetSocketAddress(host, port), 5000);
                    socket.setSoTimeout(8000);
                    OutputStream out = socket.getOutputStream();
                    // 默认发一个换行,很多仪器收到回车才吐数据
                    out.write("\r\n".getBytes(StandardCharsets.UTF_8));
                    out.flush();
                    InputStream in = socket.getInputStream();
                    byte[] buf = new byte[8192];
                    int n = in.read(buf);
                    rawJson = n <= 0 ? "" : new String(buf, 0, n, StandardCharsets.UTF_8).trim();
                }
            } else {
                return AjaxResult.error("暂不支持协议:" + di.getProtocol());
            }
        } catch (Exception e) {
            saveLog(di, "manual", null, null, false, shortMsg(e), System.currentTimeMillis() - start, 0);
            return AjaxResult.error("采集失败:" + shortMsg(e));
        }
 
        // 字段映射
        QueryWrapper<InterfaceField> fw = new QueryWrapper<>();
        fw.eq("interface_id", di.getId()).orderByAsc("sort_no").orderByAsc("id");
        List<InterfaceField> fields = fieldService.list(fw);
 
        int saved = 0;
        Map<String, Object> ret_clean = null;
        List<String> warnings = new ArrayList<>();
        try {
            if (fields.isEmpty()) {
                // 没有配置映射:整条原文放进 rawData,做个兜底记录
                DataCollection dc = new DataCollection();
                dc.setCollectionCode("AUTO-" + System.currentTimeMillis());
                dc.setDeviceName(di.getDeviceName());
                dc.setDeviceCode(di.getInterfaceCode());
                dc.setRawData(rawJson);
                dc.setCollectionTime(LocalDateTime.now());
                dc.setCollectionStatus("completed");
                dc.setDataQuality("pending");
                dc.setProcessMethod("autoClean");
                collectionService.save(dc);
                saved = 1;
                warnings.add("未配置字段映射,已把原始响应整体写入 rawData");
            } else {
                JsonNode root = null;
                try {
                    root = JSON.readTree(rawJson);
                } catch (Exception ignore) {
                    // 不是 JSON 也允许,按纯文本处理
                }
                DataCollection dc = new DataCollection();
                dc.setCollectionCode("AUTO-" + System.currentTimeMillis());
                dc.setDeviceName(di.getDeviceName());
                dc.setDeviceCode(di.getInterfaceCode());
                dc.setRawData(rawJson);
                dc.setCollectionTime(LocalDateTime.now());
                dc.setCollectionStatus("completed");
                dc.setProcessMethod("dataTransform");
 
                for (InterfaceField f : fields) {
                    String target = f.getTargetField();
                    if (target == null) continue;
                    String val = extractValue(root, rawJson, f.getSourcePath(), f.getSourceField());
                    val = applyTransform(val, f);
                    if (val == null && f.getDefaultValue() != null) {
                        val = f.getDefaultValue();
                    }
                    if (val == null) continue;
                    switch (target) {
                        case "collectionValue":
                            try {
                                dc.setCollectionValue(new BigDecimal(val));
                            } catch (Exception e) {
                                warnings.add("字段 " + f.getSourceField() + " 的值 " + val + " 不是数字,未写入采集数值");
                            }
                            break;
                        case "deviceName": dc.setDeviceName(val); break;
                        case "deviceCode": dc.setDeviceCode(val); break;
                        case "dataType": dc.setDataType(val); break;
                        case "unit": dc.setUnit(val); break;
                        case "rawData": dc.setRawData(val); break;
                        case "processedData": dc.setProcessedData(val); break;
                        default: break;
                    }
                }
 
                // 质量判定:配置了上下限就比一下,否则待校验
                dc.setDataQuality(dc.getCollectionValue() == null ? "pending" : "qualified");
                collectionService.save(dc);
                saved = 1;
            }
        } catch (Exception e) {
            saveLog(di, "manual", requestDesc, rawJson, false, "映射写入失败:" + shortMsg(e),
                    System.currentTimeMillis() - start, 0);
            return AjaxResult.error("映射写入失败:" + shortMsg(e));
        }
 
        // 采集成功后自动清洗(可用 autoClean=false 关闭)
        if (saved > 0) {
            try {
                java.util.List<Long> newIds = new java.util.ArrayList<>();
                com.baomidou.mybatisplus.core.conditions.query.QueryWrapper<DataCollection> nw =
                        new com.baomidou.mybatisplus.core.conditions.query.QueryWrapper<>();
                nw.eq("clean_status", "pending").orderByDesc("id").last("limit " + saved);
                for (DataCollection x : collectionService.list(nw)) {
                    newIds.add(x.getId());
                }
                if (!newIds.isEmpty()) {
                    Map<String, Object> cr = cleanService.cleanBatch(newIds);
                    ret_clean = cr;
                }
            } catch (Exception ignore) {
            }
        }
        saveLog(di, "manual", requestDesc, rawJson, true, null, System.currentTimeMillis() - start, saved);
 
        Map<String, Object> ret = new LinkedHashMap<>();
        ret.put("saved", saved);
        ret.put("rawLength", rawJson == null ? 0 : rawJson.length());
        ret.put("warnings", warnings);
        if (ret_clean != null) { ret.put("clean", ret_clean); }
        return AjaxResult.success("采集完成,写入 " + saved + " 条", ret);
    }
 
    /* ==================== 字段映射管理 ==================== */
 
    @GetMapping("/fields/{interfaceId}")
    @ApiOperation("查询接口的字段映射")
    public AjaxResult listFields(@PathVariable Long interfaceId) {
        QueryWrapper<InterfaceField> w = new QueryWrapper<>();
        w.eq("interface_id", interfaceId).orderByAsc("sort_no").orderByAsc("id");
        return AjaxResult.success(fieldService.list(w));
    }
 
    @PostMapping("/field/saveBatch")
    @ApiOperation("批量保存字段映射(先删后插)")
    public AjaxResult saveFields(@RequestBody Map<String, Object> body) {
        Long interfaceId = toLong(body.get("interfaceId"));
        if (interfaceId == null) {
            return AjaxResult.error("interfaceId 不能为空");
        }
        Object raw = body.get("fields");
        if (!(raw instanceof List)) {
            return AjaxResult.error("fields 必须是数组");
        }
        QueryWrapper<InterfaceField> dw = new QueryWrapper<>();
        dw.eq("interface_id", interfaceId);
        fieldService.remove(dw);
 
        int idx = 0;
        for (Object o : (List<?>) raw) {
            if (!(o instanceof Map)) continue;
            Map<?, ?> m = (Map<?, ?>) o;
            InterfaceField f = new InterfaceField();
            f.setInterfaceId(interfaceId);
            f.setInterfaceCode(str(m.get("interfaceCode")));
            f.setSourceField(str(m.get("sourceField")));
            f.setSourcePath(str(m.get("sourcePath")));
            f.setTargetField(str(m.get("targetField")));
            f.setTransformType(str(m.get("transformType")) == null ? "none" : str(m.get("transformType")));
            f.setTransformValue(str(m.get("transformValue")));
            f.setDefaultValue(str(m.get("defaultValue")));
            f.setSortNo(toInt(m.get("sortNo")) == null ? (idx + 1) : toInt(m.get("sortNo")));
            f.setRemark(str(m.get("remark")));
            fieldService.save(f);
            idx++;
        }
        return AjaxResult.success("已保存 " + idx + " 条映射");
    }
 
    /* ==================== 调用日志 ==================== */
 
    @GetMapping("/logs")
    @ApiOperation("查询接口调用日志")
    public AjaxResult logs(Page<InterfaceLog> page, InterfaceLog query) {
        QueryWrapper<InterfaceLog> w = new QueryWrapper<>();
        if (query != null) {
            if (query.getInterfaceId() != null) w.eq("interface_id", query.getInterfaceId());
            if (str(query.getInterfaceCode()) != null) w.like("interface_code", query.getInterfaceCode());
            if (str(query.getInvokeType()) != null) w.eq("invoke_type", query.getInvokeType());
            if (str(query.getSuccess()) != null) w.eq("success", query.getSuccess());
        }
        w.orderByDesc("invoke_time").orderByDesc("id");
        return AjaxResult.success(logService.page(page, w));
    }
 
    /**
     * 供定时任务调用的采集入口(不经 HTTP),返回成功写入条数
     */
    public Map<String, Object> collectInternal(Long id, String invokeType) {
        Map<String, Object> r = new LinkedHashMap<>();
        AjaxResult ar = collect(id);
        Object code = ar.get("code");
        boolean ok = code != null && "200".equals(String.valueOf(code));
        r.put("ok", ok);
        r.put("msg", ar.get("msg"));
        if (!ok) {
            return r;
        }
        Object data = ar.get("data");
        if (data instanceof Map) {
            r.put("saved", ((Map<?, ?>) data).get("saved"));
            r.put("clean", ((Map<?, ?>) data).get("clean"));
        }
        return r;
    }
    /* ==================== helpers ==================== */
 
    private String buildHttpUrl(DataInterface di) {
        String base = di.getConnectionUrl() == null ? "" : di.getConnectionUrl().trim();
        String port = di.getPort() == null ? "" : di.getPort().trim();
        if (!base.startsWith("http://") && !base.startsWith("https://")) {
            base = "http://" + base;
        }
        // 地址里没带端口时才拼接
        if (!port.isEmpty() && !base.matches(".*:\\d+.*")) {
            int idx = base.indexOf('/', base.indexOf("://") + 3);
            if (idx < 0) {
                base = base + ":" + port;
            } else {
                base = base.substring(0, idx) + ":" + port + base.substring(idx);
            }
        }
        return base;
    }
 
    private String extractValue(JsonNode root, String rawText, String path, String fieldName) {
        if (root != null && path != null && !path.trim().isEmpty()) {
            String p = path.trim();
            if (p.startsWith("$.")) p = p.substring(2);
            p = p.replace("/", ".");
            JsonNode node = root;
            for (String seg : p.split("\\.")) {
                if (seg.isEmpty()) continue;
                node = node.path(seg);
            }
            if (!node.isMissingNode() && !node.isNull()) {
                return node.asText();
            }
        }
        if (root != null && fieldName != null && !fieldName.trim().isEmpty()) {
            JsonNode node = root.path(fieldName.trim());
            if (!node.isMissingNode() && !node.isNull()) {
                return node.asText();
            }
        }
        // 纯文本协议:整个响应就是值
        if (root == null && fieldName != null && !fieldName.trim().isEmpty()) {
            return rawText;
        }
        return null;
    }
 
    private String applyTransform(String val, InterfaceField f) {
        if (val == null) return null;
        String type = lower(f.getTransformType());
        String tv = f.getTransformValue();
        if (type == null || "none".equals(type) || tv == null || tv.trim().isEmpty()) {
            return val;
        }
        try {
            BigDecimal num = new BigDecimal(val.trim());
            BigDecimal arg = new BigDecimal(tv.trim());
            switch (type) {
                case "multiply": return num.multiply(arg).stripTrailingZeros().toPlainString();
                case "divide":   return num.divide(arg, 6, BigDecimal.ROUND_HALF_UP).stripTrailingZeros().toPlainString();
                case "offset":   return num.add(arg).stripTrailingZeros().toPlainString();
                default: break;
            }
        } catch (Exception ignore) { }
        if ("map".equals(type)) {
            try {
                JsonNode m = JSON.readTree(tv);
                JsonNode hit = m.get(val);
                if (hit != null) return hit.asText();
            } catch (Exception ignore) { }
        }
        return val;
    }
 
    private void saveLog(DataInterface di, String type, String req, String resp,
                         boolean success, String err, long duration, int count) {
        try {
            InterfaceLog log = new InterfaceLog();
            log.setInterfaceId(di.getId());
            log.setInterfaceCode(di.getInterfaceCode());
            log.setInterfaceName(di.getInterfaceName());
            log.setInvokeType(type);
            log.setRequestData(req == null ? null : truncate(req, 8000));
            log.setResponseData(resp == null ? null : truncate(resp, 8000));
            log.setSuccess(success ? "1" : "0");
            log.setErrorMsg(err == null ? null : truncate(err, 1900));
            log.setDurationMs((int) duration);
            log.setRecordCount(count);
            log.setOperator(currentUser());
            log.setInvokeTime(LocalDateTime.now());
            logService.save(log);
        } catch (Exception ignore) {
            // 日志失败不影响主流程
        }
    }
 
    private static String truncate(String s, int max) {
        return s.length() <= max ? s : s.substring(0, max) + "...(truncated)";
    }
 
    private String currentUser() {
        try {
            return SecurityUtils.getUsername();
        } catch (Exception e) {
            return "system";
        }
    }
 
    private static String lower(String s) {
        return s == null ? null : s.trim().toLowerCase();
    }
 
    private static String shortMsg(Exception e) {
        String m = e.getMessage();
        if (m == null) m = e.getClass().getSimpleName();
        return m.length() > 300 ? m.substring(0, 300) : m;
    }
 
    private static String str(Object v) {
        if (v == null) return null;
        String s = String.valueOf(v).trim();
        return s.isEmpty() ? null : s;
    }
 
    private static Long toLong(Object v) {
        if (v == null) return null;
        try { return Long.valueOf(String.valueOf(v).trim()); } catch (NumberFormatException e) { return null; }
    }
 
    private static Integer toInt(Object v) {
        if (v == null) return null;
        try { return Integer.valueOf(String.valueOf(v).trim()); } catch (NumberFormatException e) { return null; }
    }
 
    private static int parseInt(String s, int def) {
        try { return Integer.parseInt(s.trim()); } catch (Exception e) { return def; }
    }
 
    /* ==================== ⑤ 自动采集配置 ==================== */
 
    /**
     * 开启/关闭自动采集,并设置采集周期(cron)
     */
    @PostMapping("/autoConfig")
    @ApiOperation("配置自动采集")
    public AjaxResult autoConfig(@RequestBody Map<String, Object> body) {
        Long id = null;
        try { id = Long.valueOf(String.valueOf(body.get("id"))); } catch (Exception ignore) {}
        if (id == null) return AjaxResult.error("id 不能为空");
        DataInterface di = dataInterfaceservice.getById(id);
        if (di == null) return AjaxResult.error("接口不存在");
 
        String enabled = body.get("autoEnabled") == null ? "0" : String.valueOf(body.get("autoEnabled"));
        String cron = body.get("collectCron") == null ? null : String.valueOf(body.get("collectCron"));
        if ("1".equals(enabled) && (cron == null || cron.trim().isEmpty())) {
            return AjaxResult.error("开启自动采集必须填写 cron 表达式,如 0 */5 * * * ?(每 5 分钟)");
        }
 
        DataInterface upd = new DataInterface();
        upd.setId(id);
        upd.setAutoEnabled(enabled);
        upd.setCollectCron(cron);
        dataInterfaceservice.updateById(upd);
        return AjaxResult.success("1".equals(enabled) ? "已开启自动采集" : "已关闭自动采集");
    }
 
    /**
     * 查询当前启用自动采集的接口(定时任务用,也可前端展示)
     */
    @GetMapping("/autoList")
    @ApiOperation("查询启用自动采集的接口")
    public AjaxResult autoList() {
        com.baomidou.mybatisplus.core.conditions.query.QueryWrapper<DataInterface> w =
                new com.baomidou.mybatisplus.core.conditions.query.QueryWrapper<>();
        w.eq("auto_enabled", "1").orderByAsc("id");
        return AjaxResult.success(dataInterfaceservice.list(w));
    }}