feat(server-go): 和风天气接入与高发病天气预警规则(#12)

This commit is contained in:
weijuesen
2026-08-12 17:40:24 +08:00
parent fa86542e55
commit c7f18c1381
10 changed files with 464 additions and 0 deletions
+52
View File
@@ -0,0 +1,52 @@
package handler
import (
"net/http"
"strconv"
"silk-server-go/internal/middleware"
"silk-server-go/internal/model"
"silk-server-go/internal/service"
"github.com/gin-gonic/gin"
"gorm.io/gorm"
)
// RegisterWeatherRoutes 注册天气与天气预警路由
func RegisterWeatherRoutes(rg *gin.RouterGroup, db *gorm.DB, weather *service.WeatherService) {
read := middleware.RequirePermission(db, "weather:read")
rg.GET("/weather/now", read, weatherNow(weather))
rg.GET("/weather/alerts", read, listWeatherAlerts(db))
}
// weatherNow 实时天气 + 规则评估(实时触发;stage 可选:late5 等)
func weatherNow(weather *service.WeatherService) gin.HandlerFunc {
return func(c *gin.Context) {
now, err := weather.FetchNow(c.Request.Context())
if err != nil {
c.JSON(http.StatusBadGateway, gin.H{"error": err.Error()})
return
}
daily, err := weather.FetchDaily(c.Request.Context())
if err != nil {
c.JSON(http.StatusBadGateway, gin.H{"error": err.Error()})
return
}
stage := c.Query("stage")
alerts := service.EvaluateWeatherRules(*now, daily, stage)
c.JSON(http.StatusOK, gin.H{"weather": now, "alerts": alerts})
}
}
// listWeatherAlerts 最近天气预警
func listWeatherAlerts(db *gorm.DB) gin.HandlerFunc {
return func(c *gin.Context) {
limit := 20
if l, err := strconv.Atoi(c.Query("limit")); err == nil && l > 0 && l <= 100 {
limit = l
}
var list []model.WeatherAlert
db.Order("created_at DESC").Limit(limit).Find(&list)
c.JSON(http.StatusOK, list)
}
}