| | |
| | | package com.zy.acs.hex.controller; |
| | | |
| | | import com.zy.acs.common.domain.mq.DeviceMessage; |
| | | 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.web.bind.annotation.GetMapping; |
| | | import org.springframework.web.bind.annotation.RequestMapping; |
| | | import org.springframework.web.bind.annotation.ResponseBody; |
| | | import org.springframework.web.bind.annotation.RestController; |
| | | 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 |
| | |
| | | */ |
| | | @GetMapping(value = "/query") |
| | | @ResponseBody |
| | | public R query() { |
| | | List<DeviceMessage> deviceMessages = influxDBService.queryPoints("select * from device order by time desc limit 10", DeviceMessage.class); |
| | | return R.ok(deviceMessages); |
| | | 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); |
| | | } |
| | | |
| | | } |