业务场景:
- 动态字段显示通过字典表, 通过redisService 获取值
- 获取的值每5天推一条数据
实现步骤
1.从数据库中获取起始时间到结束时间的数据
SELECT info.point,
info.data_time,
mo.type,
mo.`value`
FROM agricultural_monitor_info info
LEFT JOIN agricultural_monitor mo ON info.id = mo.monitor_id
WHERE info.point = #{point}
AND info.data_time >= #{startTime}
AND info.data_time <= #{endTime}
ORDER BY info.data_time.
2. 对获取的数据进行遍历, 添加到新的数组里面(时间数组)
for (AgrMonitorHistoryResp agr : agrMonitorHistory) {
if (!stringList.contains(agr.getDataTime())) {
stringList.add(agr.getDataTime());
}
}
3. 创建一个List<Map<String, Object>> list, 对时间数组进行遍历
for (String time : stringList) {
Map<String, Object> map = new HashMap<>();
map.put("dateTime", time);
for (AgrMonitorHistoryResp resp : agrMonitorHistory) {
if (time.equals(resp.getDataTime())) {
map.put(resp.getType(), resp.getValue());
}
}
list.add(map);
}
不知道大家有什么好的方法, 热恋欢迎分享!!!