1
zhang
3 天以前 9671fa60b69a5b749bfbd989f0aa281aa284dde6
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
package com.zy.acs.hex.controller;
 
import com.influxdb.v3.client.PointValues;
import com.zy.acs.framework.common.R;
import com.zy.acs.hex.domain.DeviceLog;
import com.zy.component.influxdb.service.InfluxDBService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.*;
 
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Stream;
 
@RestController
@Slf4j
@RequestMapping(value = "/deviceLog")
public class DeviceLogController {
 
    @Autowired
    private InfluxDBService influxDBService;
 
 
    /**
     * 查询最新的十条数据
     *
     * @return
     */
    @GetMapping(value = "/query")
    @ResponseBody
    public R query(@RequestParam(required = false, defaultValue = "device") String measurement,
                   @RequestParam(required = false) Map<String, Object> conditions,
                   @RequestParam(required = false, defaultValue = "100") Integer limit,
                   @RequestParam(required = false, defaultValue = "time") String orderBy,
                   @RequestParam(required = false, defaultValue = "DESC") String orderDirection) {
        return R.ok(getData(measurement, conditions, limit, orderBy, orderDirection));
    }
 
 
    /**
     * 通用查询方法,支持动态条件
     */
    private List<DeviceLog> getData(String measurement, Map<String, Object> conditions,int limit, String orderBy, String orderDirection) {
        // 构建查询语句
        StringBuilder sqlBuilder = new StringBuilder("SELECT * FROM ").append(measurement).append(" WHERE 1=1");
        Map<String, Object> params = new HashMap<>();
 
        // 动态添加条件
        if (conditions != null && !conditions.isEmpty()) {
            if (conditions.get("startTime") != null) {
                if (conditions.get("startTime") != null) {
                    sqlBuilder.append(" AND ").append("time").append(" >= :").append("startTime");
                    params.put("startTime", conditions.get("startTime"));
                }
            }else if (conditions.get("endTime") != null) {
                if (conditions.get("endTime") != null) {
                    sqlBuilder.append(" AND ").append("time").append(" <= :").append("endTime");
                    params.put("endTime", conditions.get("endTime"));
                }
            }else {
                conditions.forEach((key, value) -> {
                    if (value != null) {
                        sqlBuilder.append(" AND ").append(key).append(" = :").append(key);
                        params.put(key, value);
                    }
                });
            }
        }
        // 添加排序和限制
        sqlBuilder.append(" ORDER BY ").append(orderBy).append(" ").append(orderDirection);
        sqlBuilder.append(" LIMIT :limit");
        params.put("limit", limit);
        return influxDBService.queryPoints(sqlBuilder.toString(),params, DeviceLog.class);
    }
 
}