u
This commit is contained in:
1 parent
b8c8a04c85
commit
bce259f165
3 files changed
+31
-10
No files matched your search
@@ -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 {
|
||||
|
||||
@@ -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,
|
||||
}
|
||||
}
|
||||
@@ -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)
|
||||
}
|
||||
Reference in new issue
Block a user