131 lines
3.3 KiB
Go
131 lines
3.3 KiB
Go
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"
|
|
)
|
|
|
|
func currentUserID(c *gin.Context) (uint64, bool) {
|
|
value, ok := c.Get(middleware.ContextUserID)
|
|
if !ok {
|
|
return 0, false
|
|
}
|
|
userID, ok := value.(uint64)
|
|
return userID, ok
|
|
}
|
|
|
|
func currentAdminID(c *gin.Context) (uint64, bool) {
|
|
value, ok := c.Get(middleware.ContextAdminID)
|
|
if !ok {
|
|
return 0, false
|
|
}
|
|
adminID, ok := value.(uint64)
|
|
return adminID, ok
|
|
}
|
|
|
|
func parseID(c *gin.Context) (uint64, bool) {
|
|
id, err := strconv.ParseUint(c.Param("id"), 10, 64)
|
|
if err != nil || id == 0 {
|
|
response.BadRequest(c, "ID 不正确")
|
|
return 0, false
|
|
}
|
|
return id, true
|
|
}
|
|
|
|
func parsePagination(c *gin.Context) (int, int) {
|
|
page, _ := strconv.Atoi(c.DefaultQuery("page", "1"))
|
|
pageSize, _ := strconv.Atoi(c.DefaultQuery("page_size", "20"))
|
|
if page < 1 {
|
|
page = 1
|
|
}
|
|
if pageSize < 1 {
|
|
pageSize = 20
|
|
}
|
|
if pageSize > 100 {
|
|
pageSize = 100
|
|
}
|
|
return page, pageSize
|
|
}
|
|
|
|
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,
|
|
Keyword: strings.TrimSpace(c.Query("keyword")),
|
|
Status: strings.TrimSpace(c.Query("status")),
|
|
HandoffStatus: strings.TrimSpace(c.Query("handoff_status")),
|
|
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}
|
|
}
|