新增自定义数据库映射类型 LocalDateTime,完善套餐查询接口

This commit is contained in:
2025-04-10 17:49:11 +08:00
parent 02897db890
commit c1b4f8d605
52 changed files with 532 additions and 447 deletions

View File

@@ -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