From 6fcd5bac9cd8f9b8a3115c1df93420a99e61d93c Mon Sep 17 00:00:00 2001 From: yml Date: Sun, 22 Nov 2026 15:53:23 +0800 Subject: [PATCH] =?UTF-8?q?feat(order):=20=E8=AE=A2=E5=8D=95=E5=88=97?= =?UTF-8?q?=E8=A1=A8=E6=94=AF=E6=8C=81=E5=88=9B=E5=BB=BA=E6=97=B6=E9=97=B4?= =?UTF-8?q?=E7=AD=9B=E9=80=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backend/internal/modules/order/dto.go | 2 + .../internal/modules/order/handler_admin.go | 5 +- .../internal/modules/order/handler_parse.go | 63 ++++++++++++++- .../modules/order/handler_parse_test.go | 80 +++++++++++++++++++ backend/internal/modules/order/queries.go | 6 ++ .../order/repository_integration_test.go | 44 ++++++++++ .../internal/modules/order/repository_test.go | 7 ++ .../features/admin/views/AdminOrdersView.vue | 28 +++++++ frontend/src/features/orders/api/orders.ts | 2 + 9 files changed, 235 insertions(+), 2 deletions(-) create mode 100644 backend/internal/modules/order/handler_parse_test.go diff --git a/backend/internal/modules/order/dto.go b/backend/internal/modules/order/dto.go index def0c219..2b4eb676 100644 --- a/backend/internal/modules/order/dto.go +++ b/backend/internal/modules/order/dto.go @@ -239,6 +239,8 @@ type AdminOrderQuery struct { HandoffMode string SettlementStatus string OfflineSettlementStatus string + StartTime *time.Time + EndTime *time.Time // UserID 精确匹配该用户作为号主或租客参与的订单(用户管理「明细」入口使用)。 UserID uint64 } diff --git a/backend/internal/modules/order/handler_admin.go b/backend/internal/modules/order/handler_admin.go index 7fcb5c85..86a9214e 100644 --- a/backend/internal/modules/order/handler_admin.go +++ b/backend/internal/modules/order/handler_admin.go @@ -13,7 +13,10 @@ import ( ) func (h *Handler) AdminList(c *gin.Context) { - query := parseAdminOrderQuery(c) + query, ok := parseAdminOrderQuery(c) + if !ok { + return + } result, err := h.service.ListAdmin(c.Request.Context(), query) if err != nil { writeOrderError(c, err) diff --git a/backend/internal/modules/order/handler_parse.go b/backend/internal/modules/order/handler_parse.go index 9e6b0837..eaa54c9c 100644 --- a/backend/internal/modules/order/handler_parse.go +++ b/backend/internal/modules/order/handler_parse.go @@ -3,8 +3,10 @@ package order import ( "strconv" "strings" + "time" "hfb_sys/backend/internal/middleware" + "hfb_sys/backend/internal/timeutil" "hfb_sys/backend/pkg/response" "github.com/gin-gonic/gin" @@ -52,9 +54,13 @@ func parsePagination(c *gin.Context) (int, int) { return page, pageSize } -func parseAdminOrderQuery(c *gin.Context) AdminOrderQuery { +func parseAdminOrderQuery(c *gin.Context) (AdminOrderQuery, bool) { page, pageSize := parsePagination(c) userID, _ := strconv.ParseUint(strings.TrimSpace(c.Query("user_id")), 10, 64) + startTime, endTime, ok := parseAdminOrderTimeRange(c) + if !ok { + return AdminOrderQuery{}, false + } return AdminOrderQuery{ Page: page, PageSize: pageSize, @@ -64,6 +70,61 @@ func parseAdminOrderQuery(c *gin.Context) AdminOrderQuery { HandoffMode: strings.TrimSpace(c.Query("handoff_mode")), SettlementStatus: strings.TrimSpace(c.Query("settlement_status")), OfflineSettlementStatus: strings.TrimSpace(c.Query("offline_settlement_status")), + StartTime: startTime, + EndTime: endTime, UserID: userID, + }, true +} + +func parseAdminOrderTimeRange(c *gin.Context) (*time.Time, *time.Time, bool) { + rawStart := strings.TrimSpace(c.Query("start_date")) + if rawStart == "" { + rawStart = strings.TrimSpace(c.Query("start_time")) } + rawEnd := strings.TrimSpace(c.Query("end_date")) + if rawEnd == "" { + rawEnd = strings.TrimSpace(c.Query("end_time")) + } + + start, err := parseAdminOrderTime(rawStart, false) + if err != nil { + response.BadRequest(c, "开始日期不正确") + return nil, nil, false + } + end, err := parseAdminOrderTime(rawEnd, true) + if err != nil { + response.BadRequest(c, "结束日期不正确") + return nil, nil, false + } + if start != nil && end != nil && start.After(*end) { + response.BadRequest(c, "开始日期不能晚于结束日期") + return nil, nil, false + } + return start, end, true +} + +func parseAdminOrderTime(raw string, endOfDay bool) (*time.Time, error) { + if raw == "" { + return nil, nil + } + loc := timeutil.ShanghaiLocation() + layouts := []string{ + "2006-01-02 15:04:05", + "2006-01-02T15:04:05", + time.RFC3339, + "2006-01-02 15:04", + "2006-01-02T15:04", + "2006-01-02", + } + for _, layout := range layouts { + parsed, err := time.ParseInLocation(layout, raw, loc) + if err != nil { + continue + } + if endOfDay && layout == "2006-01-02" { + parsed = parsed.AddDate(0, 0, 1).Add(-time.Nanosecond) + } + return &parsed, nil + } + return nil, &time.ParseError{Layout: "2006-01-02", Value: raw} } diff --git a/backend/internal/modules/order/handler_parse_test.go b/backend/internal/modules/order/handler_parse_test.go new file mode 100644 index 00000000..6785aa4e --- /dev/null +++ b/backend/internal/modules/order/handler_parse_test.go @@ -0,0 +1,80 @@ +package order + +import ( + "net/http" + "net/http/httptest" + "testing" + "time" + + "hfb_sys/backend/internal/timeutil" + + "github.com/gin-gonic/gin" +) + +func TestParseAdminOrderTimeRange(t *testing.T) { + gin.SetMode(gin.TestMode) + loc := timeutil.ShanghaiLocation() + + tests := []struct { + name string + target string + wantStart time.Time + wantEnd time.Time + wantOK bool + wantStatus int + }{ + { + name: "日期按北京时间自然日解析", + target: "/admin/orders?start_date=2026-11-01&end_date=2026-11-02", + wantStart: time.Date(2026, 11, 1, 0, 0, 0, 0, loc), + wantEnd: time.Date(2026, 11, 2, 23, 59, 59, int(time.Second-time.Nanosecond), loc), + wantOK: true, + wantStatus: http.StatusOK, + }, + { + name: "精确时间保持原值", + target: "/admin/orders?start_time=2026-11-01%2009:30:00&end_time=2026-11-01%2018:45:59", + wantStart: time.Date(2026, 11, 1, 9, 30, 0, 0, loc), + wantEnd: time.Date(2026, 11, 1, 18, 45, 59, 0, loc), + wantOK: true, + wantStatus: http.StatusOK, + }, + { + name: "开始日期格式错误", + target: "/admin/orders?start_date=invalid", + wantOK: false, + wantStatus: http.StatusBadRequest, + }, + { + name: "结束日期早于开始日期", + target: "/admin/orders?start_date=2026-11-03&end_date=2026-11-02", + wantOK: false, + wantStatus: http.StatusBadRequest, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + recorder := httptest.NewRecorder() + c, _ := gin.CreateTestContext(recorder) + c.Request = httptest.NewRequest(http.MethodGet, tt.target, nil) + + start, end, ok := parseAdminOrderTimeRange(c) + if ok != tt.wantOK { + t.Fatalf("parseAdminOrderTimeRange() ok = %v, want %v", ok, tt.wantOK) + } + if recorder.Code != tt.wantStatus { + t.Fatalf("response status = %d, want %d", recorder.Code, tt.wantStatus) + } + if !tt.wantOK { + return + } + if start == nil || !start.Equal(tt.wantStart) { + t.Fatalf("start = %v, want %v", start, tt.wantStart) + } + if end == nil || !end.Equal(tt.wantEnd) { + t.Fatalf("end = %v, want %v", end, tt.wantEnd) + } + }) + } +} diff --git a/backend/internal/modules/order/queries.go b/backend/internal/modules/order/queries.go index cb776be0..12437b35 100644 --- a/backend/internal/modules/order/queries.go +++ b/backend/internal/modules/order/queries.go @@ -328,6 +328,12 @@ func applyAdminOrderFilters(db *gorm.DB, query AdminOrderQuery) *gorm.DB { if query.OfflineSettlementStatus != "" { db = db.Where("COALESCE(NULLIF(o.offline_settlement_status, ''), 'none') = ?", query.OfflineSettlementStatus) } + if query.StartTime != nil { + db = db.Where("o.created_at >= ?", *query.StartTime) + } + if query.EndTime != nil { + db = db.Where("o.created_at <= ?", *query.EndTime) + } if keyword := strings.TrimSpace(query.Keyword); keyword != "" { upper := strings.ToUpper(keyword) if isPhoneKeyword(keyword) { diff --git a/backend/internal/modules/order/repository_integration_test.go b/backend/internal/modules/order/repository_integration_test.go index 448771a6..c699252a 100644 --- a/backend/internal/modules/order/repository_integration_test.go +++ b/backend/internal/modules/order/repository_integration_test.go @@ -10,6 +10,7 @@ import ( "hfb_sys/backend/internal/database" "hfb_sys/backend/internal/model" + "hfb_sys/backend/internal/timeutil" "gorm.io/datatypes" "gorm.io/gorm" @@ -2331,6 +2332,49 @@ func TestListAdminArbitratedWithoutPayoutReturnsZero(t *testing.T) { } } +func TestListAdminFiltersByCreatedAtRange(t *testing.T) { + db := setupOrderTestDB(t) + repo := NewRepository(db) + _, _, order := createPlatformManagedCheckoutOrder(t, db, 7006) + + createdAt := time.Date(2026, 11, 20, 12, 30, 0, 0, timeutil.ShanghaiLocation()) + if err := db.Model(&model.RentalOrder{}). + Where("id = ?", order.ID). + Update("created_at", createdAt).Error; err != nil { + t.Fatalf("update order created_at failed: %v", err) + } + + start := time.Date(2026, 11, 20, 0, 0, 0, 0, timeutil.ShanghaiLocation()) + end := time.Date(2026, 11, 20, 23, 59, 59, int(time.Second-time.Nanosecond), timeutil.ShanghaiLocation()) + hit, err := repo.ListAdmin(t.Context(), AdminOrderQuery{ + Page: 1, + PageSize: 1, + StartTime: &start, + EndTime: &end, + }) + if err != nil { + t.Fatalf("ListAdmin() with matching date range error = %v", err) + } + if hit.Total != 1 || len(hit.Items) != 1 { + t.Fatalf("matching date range returned total/items = %d/%d, want 1/1", hit.Total, len(hit.Items)) + } + + missStart := start.AddDate(0, 0, 1) + missEnd := end.AddDate(0, 0, 1) + miss, err := repo.ListAdmin(t.Context(), AdminOrderQuery{ + Page: 1, + PageSize: 1, + StartTime: &missStart, + EndTime: &missEnd, + }) + if err != nil { + t.Fatalf("ListAdmin() with non-matching date range error = %v", err) + } + if miss.Total != 0 || len(miss.Items) != 0 { + t.Fatalf("non-matching date range returned total/items = %d/%d, want 0/0", miss.Total, len(miss.Items)) + } +} + func TestAdminPlatformCheckoutConfirmRequiresExplicitConfirmationForZeroOwnerIncome(t *testing.T) { db := setupOrderTestDB(t) repo := NewRepository(db, Dependencies{ diff --git a/backend/internal/modules/order/repository_test.go b/backend/internal/modules/order/repository_test.go index 64bed4c0..79f1a9e1 100644 --- a/backend/internal/modules/order/repository_test.go +++ b/backend/internal/modules/order/repository_test.go @@ -504,6 +504,8 @@ func TestNewOrderNoUsesShanghaiTimeWhenLocalIsUTC(t *testing.T) { func TestApplyAdminOrderFiltersSmartRouting(t *testing.T) { db := database.NewDryRunDB() + start := time.Date(2026, 11, 1, 0, 0, 0, 0, timeutil.ShanghaiLocation()) + end := time.Date(2026, 11, 2, 23, 59, 59, int(time.Second-time.Nanosecond), timeutil.ShanghaiLocation()) tests := []struct { name string @@ -540,6 +542,11 @@ func TestApplyAdminOrderFiltersSmartRouting(t *testing.T) { query: AdminOrderQuery{Keyword: "测试账号"}, wantSQLFrag: "o.order_no LIKE ? OR l.listing_no LIKE ? OR a.title LIKE ? OR owner.phone LIKE ? OR renter.phone LIKE ?", }, + { + name: "创建时间范围同时限制起止时间", + query: AdminOrderQuery{StartTime: &start, EndTime: &end}, + wantSQLFrag: "o.created_at >= ? AND o.created_at <= ?", + }, } for _, tt := range tests { diff --git a/frontend/src/features/admin/views/AdminOrdersView.vue b/frontend/src/features/admin/views/AdminOrdersView.vue index 3fe7ef95..512ad198 100644 --- a/frontend/src/features/admin/views/AdminOrdersView.vue +++ b/frontend/src/features/admin/views/AdminOrdersView.vue @@ -23,6 +23,8 @@ const filters = reactive({ handoff_mode: '', settlement_status: '', offline_settlement_status: '', + start_date: '', + end_date: '', }) const loading = ref(false) const orders = ref([]) @@ -32,6 +34,14 @@ const currentPageSize = ref(20) const route = useRoute() const router = useRouter() const canReturnToWithdrawals = computed(() => route.query.from === 'withdrawals') +const createdDateRange = computed<[string, string] | null>({ + get: (): [string, string] | null => + filters.start_date && filters.end_date ? [filters.start_date, filters.end_date] : null, + set: (value: [string, string] | null) => { + filters.start_date = value?.[0] || '' + filters.end_date = value?.[1] || '' + }, +}) const { restoreScroll } = useAdminListState({ key: 'orders', filters, @@ -104,6 +114,8 @@ async function loadOrders() { handoff_mode: filters.handoff_mode, settlement_status: filters.settlement_status, offline_settlement_status: filters.offline_settlement_status, + start_date: filters.start_date, + end_date: filters.end_date, }) orders.value = result.items total.value = result.total @@ -128,6 +140,8 @@ function resetFilters() { filters.handoff_mode = '' filters.settlement_status = '' filters.offline_settlement_status = '' + filters.start_date = '' + filters.end_date = '' void queryOrders() } @@ -222,6 +236,19 @@ function siteRoleLabel(row: AdminOrderListItem) { /> + + +