This commit is contained in:
oneao committed 2026-02-06 22:28:44 +08:00
1 parent b8c8a04c85
commit bce259f165
3 files changed
+31 -10

No files matched your search

@@ -14,10 +14,10 @@ import (
"allapp/utils/timex" "allapp/utils/timex"
"context" "context"
"fmt" "fmt"
"time"
"github.com/cloudwego/hertz/pkg/app" "github.com/cloudwego/hertz/pkg/app"
"github.com/cloudwego/hertz/pkg/protocol/consts" "github.com/cloudwego/hertz/pkg/protocol/consts"
"github.com/jackc/pgx/v5/pgtype"
) )
// ListMoneyRecords 账目列表 // ListMoneyRecords 账目列表
@@ -42,6 +42,18 @@ func ListMoneyRecords(ctx context.Context, c *app.RequestContext) {
return 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) userID := middleware.GetUserID(ctx)
// 查询账目列表(统一 SQL) // 查询账目列表(统一 SQL)
@@ -49,8 +61,8 @@ func ListMoneyRecords(ctx context.Context, c *app.RequestContext) {
ctx, ctx,
repo.ListMoneyRecordsParams{ repo.ListMoneyRecordsParams{
UserID: userID, UserID: userID,
RecordTime: pgtype.Date{Time: startTime, Valid: true}, RecordTime: pgtypex.TimePtrToPgDate(startTime),
RecordTime_2: pgtype.Date{Time: endTime, Valid: true}, RecordTime_2: pgtypex.TimePtrToPgDate(endTime),
}, },
) )
if err != nil { if err != nil {
@@ -149,3 +149,13 @@ func Int32ToPgInt4(value int32) pgtype.Int4 {
Valid: true, 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,
}
}
+6 -7
View File
@@ -5,9 +5,9 @@ import (
"time" "time"
) )
func ParseTimeFlexible(value string) (time.Time, error) { func ParseTimeFlexible(value string) (*time.Time, error) {
if value == "" { if value == "" {
return time.Time{}, fmt.Errorf("empty time string") return nil, nil // ⭐ 空字符串 → 不限时间
} }
layouts := []string{ layouts := []string{
@@ -18,13 +18,12 @@ func ParseTimeFlexible(value string) (time.Time, error) {
time.RFC3339, time.RFC3339,
} }
var err error
for _, layout := range layouts { for _, layout := range layouts {
var t time.Time t, err := time.Parse(layout, value)
t, err = time.Parse(layout, value)
if err == nil { 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)
} }