新增自定义数据库映射类型 LocalDateTime,完善套餐查询接口
This commit is contained in:
@@ -1,7 +1,74 @@
|
||||
package common
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"database/sql/driver"
|
||||
"time"
|
||||
)
|
||||
|
||||
// ErrResp 定义通用错误响应格式
|
||||
type ErrResp struct {
|
||||
Message string `json:"message"`
|
||||
Error bool `json:"error"`
|
||||
}
|
||||
|
||||
type PageReq struct {
|
||||
Page int `json:"page"`
|
||||
Size int `json:"size"`
|
||||
}
|
||||
|
||||
type PageResp struct {
|
||||
Total int `json:"total"`
|
||||
Page int `json:"page"`
|
||||
Size int `json:"size"`
|
||||
List any `json:"list"`
|
||||
}
|
||||
|
||||
// region LocalDateTime
|
||||
|
||||
type LocalDateTime time.Time
|
||||
|
||||
func (ldt *LocalDateTime) Scan(value interface{}) (err error) {
|
||||
|
||||
nullTime := &sql.NullTime{}
|
||||
err = nullTime.Scan(value)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if nullTime == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
t := nullTime.Time
|
||||
*ldt = LocalDateTime(time.Date(
|
||||
t.Year(), t.Month(), t.Day(), t.Hour(), t.Minute(), t.Second(), t.Nanosecond(), time.Local,
|
||||
))
|
||||
return
|
||||
}
|
||||
|
||||
func (ldt LocalDateTime) Value() (driver.Value, error) {
|
||||
return time.Time(ldt).In(time.Local), nil
|
||||
}
|
||||
|
||||
// GormDataType gorm common data type
|
||||
func (ldt LocalDateTime) GormDataType() string {
|
||||
return "ldt"
|
||||
}
|
||||
|
||||
func (ldt LocalDateTime) GobEncode() ([]byte, error) {
|
||||
return time.Time(ldt).GobEncode()
|
||||
}
|
||||
|
||||
func (ldt *LocalDateTime) GobDecode(b []byte) error {
|
||||
return (*time.Time)(ldt).GobDecode(b)
|
||||
}
|
||||
|
||||
func (ldt LocalDateTime) MarshalJSON() ([]byte, error) {
|
||||
return time.Time(ldt).MarshalJSON()
|
||||
}
|
||||
|
||||
func (ldt *LocalDateTime) UnmarshalJSON(b []byte) error {
|
||||
return (*time.Time)(ldt).UnmarshalJSON(b)
|
||||
}
|
||||
|
||||
// endregion
|
||||
|
||||
Reference in New Issue
Block a user