fix: 修正雨量存储

This commit is contained in:
yarnom 2025-07-10 12:19:35 +08:00
parent 0067c2d15e
commit c6c9015cd3
3 changed files with 19 additions and 20 deletions

View File

@ -3,12 +3,11 @@ package db
import (
"database/sql"
"fmt"
_ "gi
"log"
/go-sql-driver/mysql"
"rain_monitor/models"
_ "github.com/go-sql-driver/mysql"
"log"
"math"
"rain_monitor/models"
"time"
)
var db *sql.DB
@ -93,28 +92,28 @@ func SaveRainGaugeData(data *models.RainGaugeData) (int64, error) {
FROM rain_gauge_data
WHERE timestamp BETWEEN ? AND ?
ORDER BY timestamp DESC
`, fiveMinutesAgo, data.Timestamp)
if err != nil {
log.Printf("查询最近雨量计数据失败: %v", err)
// 即使查询失败,我们仍然尝试插入新数据
} else {
defer rows.Close()
// 检查是否有相似数据
for rows.Next() {
var id int64
var ts time.Time
var rainfall float64
if err := rows.Scan(&id, &ts, &rainfall); err != nil {
log.Printf("扫描雨量计数据失败: %v", err)
continue
}
// 如果时间非常接近小于1分钟且雨量数据相同或非常接近则认为是重复数据
timeDiff := data.Timestamp.Sub(ts)
rainfallDiff := math.Abs(data.TotalRainfall - rainfall)
if timeDiff < time.Minute && rainfallDiff < 0.1 {
log.Printf("检测到重复的雨量计数据跳过插入。ID=%d, 时间=%v", id, ts)
@ -255,10 +254,10 @@ func GetAggregatedData(start, end time.Time) ([]models.AggregatedData, error) {
if err != nil {
log.Printf("扫描行数据失败: %v", err)
return nil, fmt.Errorf("扫描聚合数据失败: %v", err)
}
// 设置格式化时间字符串
data.FormattedTime = timestampStr
// 尝试解析时间字符串为time.Time类型
timestamp, err := time.Parse("2006-01-02 15:04:05", timestampStr)
@ -267,7 +266,7 @@ func GetAggregatedData(start, end time.Time) ([]models.AggregatedData, error) {
// 即使解析失败也继续处理前端会使用FormattedTime
} else {
data.Timestamp = timestamp
}
result = append(result, data)
}

View File

@ -160,7 +160,7 @@ LIMIT 1
const QueryAggregatedDataSQL = `
SELECT
w.time_hour as timestamp,
MAX(r.daily_rainfall) as rainfall,
MAX(r.hourly_rainfall) as rainfall,
AVG(w.temperature) as avg_temperature,
AVG(w.humidity) as avg_humidity,
AVG(w.wind_speed) as avg_wind_speed,
@ -176,7 +176,7 @@ FROM
LEFT JOIN
(SELECT
DATE_FORMAT(timestamp, '%Y-%m-%d %H:00:00') as time_hour,
daily_rainfall
hourly_rainfall
FROM rain_gauge_data
WHERE timestamp BETWEEN ? AND ?
) r ON w.time_hour = r.time_hour

View File

@ -247,7 +247,7 @@
<thead>
<tr>
<th>时间</th>
<th>降雨量(mm)</th>
<th>小时降雨量(mm)</th>
<th>温度(℃)</th>
<th>湿度(%)</th>
<th>风速(m/s)</th>
@ -600,7 +600,7 @@
datasets: [
{
type: 'bar',
label: '降雨量(mm)',
label: '小时降雨量(mm)',
data: data.map(item => item.rainfall !== null && !isNaN(item.rainfall) ? item.rainfall : 0),
backgroundColor: 'rgba(54, 162, 235, 0.5)',
borderColor: 'rgba(54, 162, 235, 1)',
@ -630,7 +630,7 @@
position: 'left',
title: {
display: true,
text: '降雨量(mm)'
text: '小时降雨量(mm)'
},
grid: {
drawOnChartArea: false
@ -715,13 +715,13 @@
function exportData() {
// 从表格中获取完整数据
const tableRows = document.querySelectorAll('#tableBody tr');
let csv = '时间,降雨量(mm),温度(℃),湿度(%),风速(m/s),大气压(kPa),太阳辐射(W/m²)\n';
let csv = '时间,小时降雨量(mm),温度(℃),湿度(%),风速(m/s),大气压(kPa),太阳辐射(W/m²)\n';
tableRows.forEach(row => {
const cells = row.querySelectorAll('td');
const rowData = [
cells[0].textContent, // 时间
cells[1].textContent, // 降雨量
cells[1].textContent, // 小时降雨量
cells[2].textContent, // 温度
cells[3].textContent, // 湿度
cells[4].textContent, // 风速