From bce259f1657f86941c78cd96cd339244c8f29359 Mon Sep 17 00:00:00 2001 From: oneao Date: Fri, 6 Feb 2026 22:28:44 +0800 Subject: [PATCH] u --- .../biz/handler/money/money_record_service.go | 18 +++++++++++++++--- code/allapp/allapp-go/utils/pgtypex/pgtypex.go | 10 ++++++++++ code/allapp/allapp-go/utils/timex/index.go | 13 ++++++------- 3 files changed, 31 insertions(+), 10 deletions(-) diff --git a/code/allapp/allapp-go/biz/handler/money/money_record_service.go b/code/allapp/allapp-go/biz/handler/money/money_record_service.go index 1fe05223..aa13efc4 100644 --- a/code/allapp/allapp-go/biz/handler/money/money_record_service.go +++ b/code/allapp/allapp-go/biz/handler/money/money_record_service.go @@ -14,10 +14,10 @@ import ( "allapp/utils/timex" "context" "fmt" + "time" "github.com/cloudwego/hertz/pkg/app" "github.com/cloudwego/hertz/pkg/protocol/consts" - "github.com/jackc/pgx/v5/pgtype" ) // ListMoneyRecords 账目列表 @@ -42,6 +42,18 @@ func ListMoneyRecords(ctx context.Context, c *app.RequestContext) { return } + // 开始时间兜底 + if startTime == nil { + t := time.Unix(0, 0) + startTime = &t + } + + // 结束时间兜底 + if endTime == nil { + t := time.Now().AddDate(10, 0, 0) + endTime = &t + } + userID := middleware.GetUserID(ctx) // 查询账目列表(统一 SQL) @@ -49,8 +61,8 @@ func ListMoneyRecords(ctx context.Context, c *app.RequestContext) { ctx, repo.ListMoneyRecordsParams{ UserID: userID, - RecordTime: pgtype.Date{Time: startTime, Valid: true}, - RecordTime_2: pgtype.Date{Time: endTime, Valid: true}, + RecordTime: pgtypex.TimePtrToPgDate(startTime), + RecordTime_2: pgtypex.TimePtrToPgDate(endTime), }, ) if err != nil { diff --git a/code/allapp/allapp-go/utils/pgtypex/pgtypex.go b/code/allapp/allapp-go/utils/pgtypex/pgtypex.go index 9d45b3b3..012fd682 100644 --- a/code/allapp/allapp-go/utils/pgtypex/pgtypex.go +++ b/code/allapp/allapp-go/utils/pgtypex/pgtypex.go @@ -149,3 +149,13 @@ func Int32ToPgInt4(value int32) pgtype.Int4 { Valid: true, } } + +func TimePtrToPgDate(t *time.Time) pgtype.Date { + if t == nil { + return pgtype.Date{Valid: false} // ⭐ SQL NULL → 不限时间 + } + return pgtype.Date{ + Time: *t, // ⭐ 关键:解引用 + Valid: true, + } +} diff --git a/code/allapp/allapp-go/utils/timex/index.go b/code/allapp/allapp-go/utils/timex/index.go index 7ac145a8..a1a6fe66 100644 --- a/code/allapp/allapp-go/utils/timex/index.go +++ b/code/allapp/allapp-go/utils/timex/index.go @@ -5,9 +5,9 @@ import ( "time" ) -func ParseTimeFlexible(value string) (time.Time, error) { +func ParseTimeFlexible(value string) (*time.Time, error) { if value == "" { - return time.Time{}, fmt.Errorf("empty time string") + return nil, nil // ⭐ 空字符串 → 不限时间 } layouts := []string{ @@ -18,13 +18,12 @@ func ParseTimeFlexible(value string) (time.Time, error) { time.RFC3339, } - var err error for _, layout := range layouts { - var t time.Time - t, err = time.Parse(layout, value) + t, err := time.Parse(layout, value) if err == nil { - return t, nil + return &t, nil } } - return time.Time{}, fmt.Errorf("unsupported time format: %s", value) + + return nil, fmt.Errorf("unsupported time format: %s", value) }