Commit 857dff0a authored by chenfm's avatar chenfm

增加模型历史数据查询

parent 2f3b3f4c
...@@ -2,10 +2,12 @@ package com.esv.datacenter.iot.module.dashboard.controller; ...@@ -2,10 +2,12 @@ package com.esv.datacenter.iot.module.dashboard.controller;
import com.esv.datacenter.iot.common.response.EResponse; import com.esv.datacenter.iot.common.response.EResponse;
import com.esv.datacenter.iot.module.dashboard.req.DashboardReq; import com.esv.datacenter.iot.module.dashboard.req.DashboardReq;
import com.esv.datacenter.iot.module.dashboard.req.HistoryDashboardReq;
import com.esv.datacenter.iot.module.dashboard.service.DashboardService; import com.esv.datacenter.iot.module.dashboard.service.DashboardService;
import com.esv.datacenter.iot.module.dashboard.vo.ModelDataVO; import com.esv.datacenter.iot.module.dashboard.vo.ModelDataVO;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMapping;
...@@ -40,4 +42,10 @@ public class DashboardController { ...@@ -40,4 +42,10 @@ public class DashboardController {
return EResponse.ok(list); return EResponse.ok(list);
} }
@PostMapping("modelHistory")
public EResponse<List<ModelDataVO>> modelHistory(@RequestBody @Validated HistoryDashboardReq historyDashboardReq) {
List<ModelDataVO> list = dashboardService.modelHistory(historyDashboardReq);
return EResponse.ok(list);
}
} }
package com.esv.datacenter.iot.module.dashboard.req;
import lombok.Data;
import javax.validation.constraints.NotNull;
import java.util.List;
/**
* @description:
* @project: datacenter-iot-service
* @name: com.esv.datacenter.iot.module.dashboard.req.HistoryDashboardReq
* @author: chenfm
* @email: chenfengman@esvtek.com
* @createTime: 2020/8/11 10:08
* @version: 1.0
*/
@Data
public class HistoryDashboardReq {
@NotNull
private Long beginTime;
@NotNull
private Long endTime;
List<DashboardReq> dashboardReqList;
}
package com.esv.datacenter.iot.module.dashboard.service; package com.esv.datacenter.iot.module.dashboard.service;
import com.esv.datacenter.iot.module.dashboard.req.DashboardReq; import com.esv.datacenter.iot.module.dashboard.req.DashboardReq;
import com.esv.datacenter.iot.module.dashboard.req.HistoryDashboardReq;
import com.esv.datacenter.iot.module.dashboard.vo.ModelDataVO; import com.esv.datacenter.iot.module.dashboard.vo.ModelDataVO;
import java.util.List; import java.util.List;
...@@ -18,4 +19,6 @@ public interface DashboardService { ...@@ -18,4 +19,6 @@ public interface DashboardService {
List<ModelDataVO> modelData(List<DashboardReq> dashboardReqList); List<ModelDataVO> modelData(List<DashboardReq> dashboardReqList);
List<ModelDataVO> modelHistory(HistoryDashboardReq historyDashboardReq);
} }
...@@ -2,6 +2,7 @@ package com.esv.datacenter.iot.module.dashboard.service.impl; ...@@ -2,6 +2,7 @@ package com.esv.datacenter.iot.module.dashboard.service.impl;
import com.esv.datacenter.iot.common.component.TimescaleComponent; import com.esv.datacenter.iot.common.component.TimescaleComponent;
import com.esv.datacenter.iot.module.dashboard.req.DashboardReq; import com.esv.datacenter.iot.module.dashboard.req.DashboardReq;
import com.esv.datacenter.iot.module.dashboard.req.HistoryDashboardReq;
import com.esv.datacenter.iot.module.dashboard.service.DashboardService; import com.esv.datacenter.iot.module.dashboard.service.DashboardService;
import com.esv.datacenter.iot.module.dashboard.vo.ModelDataVO; import com.esv.datacenter.iot.module.dashboard.vo.ModelDataVO;
import com.zaxxer.hikari.HikariDataSource; import com.zaxxer.hikari.HikariDataSource;
...@@ -10,7 +11,10 @@ import org.springframework.beans.factory.annotation.Autowired; ...@@ -10,7 +11,10 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import java.util.*; import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
/** /**
* @description: * @description:
...@@ -40,7 +44,7 @@ public class DashboardServiceImpl implements DashboardService { ...@@ -40,7 +44,7 @@ public class DashboardServiceImpl implements DashboardService {
Long modelId = dashboardReq.getModelId(); Long modelId = dashboardReq.getModelId();
String tableName = "iot_data_model_" + modelId; String tableName = "iot_data_model_" + modelId;
String sql = "select time" String sql = "select time"
+ getSqlParams(dashboardReq.getPropertyCodeList(), false) + getSqlParams(dashboardReq.getPropertyCodeList())
+ " from " + " from "
+ tableName + tableName
+ " where device_id = ?" + " where device_id = ?"
...@@ -52,17 +56,37 @@ public class DashboardServiceImpl implements DashboardService { ...@@ -52,17 +56,37 @@ public class DashboardServiceImpl implements DashboardService {
return modelDataVOList; return modelDataVOList;
} }
private String getSqlParams(Collection collection, boolean isString) { @Override
public List<ModelDataVO> modelHistory(HistoryDashboardReq historyDashboardReq) {
Timestamp beginTime = new Timestamp(historyDashboardReq.getBeginTime());
Timestamp endTime = new Timestamp(historyDashboardReq.getEndTime());
List<DashboardReq> dashboardReqList = historyDashboardReq.getDashboardReqList();
List<ModelDataVO> modelDataVOList = new ArrayList<>();
for (DashboardReq dashboardReq : dashboardReqList) {
Long modelId = dashboardReq.getModelId();
String tableName = "iot_data_model_" + modelId;
String sql = "select time"
+ getSqlParams(dashboardReq.getPropertyCodeList())
+ " from "
+ tableName
+ " where device_id = ?"
+ " and (time > ?)"
+ " order by time";
log.info("select modelData sql: {}", sql);
List<Map<String, Object>> dataList =
jdbcTemplate.queryForList(sql, dashboardReq.getDeviceId(),
beginTime);
ModelDataVO modelDataVO = new ModelDataVO(modelId, dataList);
modelDataVOList.add(modelDataVO);
}
return modelDataVOList;
}
private String getSqlParams(List<String> collection) {
StringBuilder builder = new StringBuilder(); StringBuilder builder = new StringBuilder();
for (Object key : collection) { for (Object key : collection) {
builder.append(","); builder.append(",");
if (isString) {
builder.append("'");
}
builder.append(key.toString()); builder.append(key.toString());
if (isString) {
builder.append("'");
}
} }
return builder.toString(); return builder.toString();
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment