Skip to content

Commit

Permalink
完成日期转换
Browse files Browse the repository at this point in the history
  • Loading branch information
onenewcode committed Apr 16, 2024
1 parent adf80bf commit 974072a
Show file tree
Hide file tree
Showing 7 changed files with 97 additions and 16 deletions.
20 changes: 20 additions & 0 deletions internal/models/common/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ package common

import (
"encoding/json"
"fmt"
"reggie/internal/utils"
"strings"
"time"
)

type Result struct {
Expand All @@ -21,3 +25,19 @@ type PageResult struct {
Total int64 `json:"total,omitempty"` //总记录数
Records interface{} `json:"records,omitempty"` //当前页数据集合
}

// 设置时间格式 2006-01-02 15:04:05
type DateTime time.Time

func (t DateTime) MarshalJSON() ([]byte, error) {
var stamp = fmt.Sprintf("\"%s\"", time.Time(t).Format("2006-01-02 15:04:05"))
return []byte(stamp), nil
}
func (t *DateTime) UnmarshalJSON(b []byte) error {
parse, err := time.Parse("2006-01-02 15:04:05", strings.Trim(utils.Bytes2String(b), "\""))
if err != nil {
return err
}
*t = (DateTime)(parse)
return err
}
14 changes: 0 additions & 14 deletions internal/models/dto/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,17 +121,3 @@ type SetmealPageQueryDTO struct {
//状态 0表示禁用 1表示启用
Status *int `json:"status,omitempty" form:"status,omitempty"`
}

/*
添加分页id
*/
type OrderPageQueryDTO struct {
Page int `json:"page,omitempty"`
PageSize int `json:"pageSize,omitempty"`
Number string `json:"number,omitempty"`
Phone string `json:"phone,omitempty"`
Status int32 `json:"status,omitempty"`
BeginTime time.Time `json:"beginTime" format:"2006-01-02 15:04:05" `
EndTime time.Time `json:"endTime" format:"2006-01-02 15:04:05"`
UserId int64 `json:"userId,omitempty"`
}
48 changes: 48 additions & 0 deletions internal/models/dto/order_page_query.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package dto

import (
"encoding/json"
"reggie/internal/models/common"
"time"
)

type OrdersPageQueryDTO struct {
Page int `json:"page,omitempty"`
PageSize int `json:"page_size,omitempty"`
Number string `json:"number,omitempty"`
Phone string `json:"phone,omitempty"`
Status int `json:"status,omitempty"`
BeginTime time.Time `json:"begin_time"`
EndTime time.Time `json:"end_time"`
UserId int64 `json:"user_id,omitempty"`
}
type tmpJSON OrdersPageQueryDTO
type tmp struct {
tmpJSON
BeginTime common.DateTime `json:"begin_time"`
EndTime common.DateTime `json:"end_time"`
}

func (o OrdersPageQueryDTO) MarshalJSON() ([]byte, error) {
p := &tmp{
tmpJSON: (tmpJSON)(o),
BeginTime: common.DateTime(o.BeginTime),
EndTime: common.DateTime(o.EndTime),
}
marshal, err := json.Marshal(p)
if err != nil {
return nil, err
}
return marshal, err
}

func (o *OrdersPageQueryDTO) UnmarshalJSON(b []byte) error {
var t tmp
err := json.Unmarshal(b, &t)
t.tmpJSON.BeginTime, t.tmpJSON.EndTime = (time.Time)(t.BeginTime), (time.Time)(t.EndTime)
if err != nil {
return err
}
o = (*OrdersPageQueryDTO)(&t.tmpJSON)
return nil
}
14 changes: 14 additions & 0 deletions internal/router/admin/order_router.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package admin

import (
"context"
"github.com/cloudwego/hertz/pkg/app"
"reggie/internal/models/dto"
)

// @Summary 订单搜索
func ConditionSearchOrder(ctx context.Context, c *app.RequestContext) {
var oq dto.OrdersPageQueryDTO
c.Bind(&oq)

}
5 changes: 4 additions & 1 deletion internal/router/service/order_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ func SubmitOrder(order *model.Order) (*vo.OrderSubmitVO, error) {
return &orderSubmitVO, nil
}

func PageQuery4UserOrder(page *dto.OrderPageQueryDTO) (common.PageResult, error) {
func PageQuery4UserOrder(page *dto.OrdersPageQueryDTO) (common.PageResult, error) {
// 分页条件查询
query, err := db.OrderDao.PageQuery(page)
if err != nil {
Expand All @@ -83,3 +83,6 @@ func PageQuery4UserOrder(page *dto.OrderPageQueryDTO) (common.PageResult, error)

return common.PageResult{int64(l), list}, nil
}
func ConditionSearchOrder() {

}
2 changes: 1 addition & 1 deletion internal/utils/gen.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package utils

import (
"fmt"
Expand Down
10 changes: 10 additions & 0 deletions internal/utils/str.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package utils

import "unsafe"

func String2Bytes(s string) []byte {
return *(*[]byte)(unsafe.Pointer(&s))
}
func Bytes2String(b []byte) string {
return *(*string)(unsafe.Pointer(&b))
}

0 comments on commit 974072a

Please sign in to comment.