diff --git a/internal/forecast/cma.go b/internal/forecast/cma.go index 036d2f1..73a3a6d 100644 --- a/internal/forecast/cma.go +++ b/internal/forecast/cma.go @@ -252,9 +252,8 @@ func RunCMAFetch(ctx context.Context) error { if loc == nil { loc = time.FixedZone("CST", 8*3600) } - issuedAt := time.Now().In(loc) - startHour := issuedAt.Truncate(time.Hour) - targets := []time.Time{startHour.Add(1 * time.Hour), startHour.Add(2 * time.Hour), startHour.Add(3 * time.Hour)} + issuedAt := time.Now().In(loc) + base := issuedAt.Truncate(time.Hour) // 请求CMA form := url.Values{} @@ -323,7 +322,29 @@ func RunCMAFetch(ctx context.Context) error { } } - // 读取站点列表(全部) + // 从可用数据中选择“接下来可用的三个未来时次”(按接口实际返回) + timeSet := map[time.Time]struct{}{} + for i := range tables { + for t := range tables[i] { + if t.After(base) { + timeSet[t] = struct{}{} + } + } + } + var targets []time.Time + for t := range timeSet { + targets = append(targets, t) + } + targets = sortTimesAsc(targets) + if len(targets) > 3 { + targets = targets[:3] + } + if len(targets) == 0 { + // 无未来时次则不写库,避免写入“假想整点”。 + return nil + } + + // 读取站点列表(全部) db := database.GetDB() stationIDs, err := loadAllStationIDs(ctx, db) if err != nil {