feat(order): 订单列表支持创建时间筛选
This commit is contained in:
@@ -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}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user