zhangqian
2023-12-08 b9abe45f791bea70a059e37186907190afcec350
model/model.go
@@ -2,12 +2,17 @@
import (
   "apsClient/pkg/snowflake"
   "encoding/json"
   "github.com/jinzhu/gorm"
   "strconv"
   "time"
)
type BigID uint
type CommonModel struct {
   ID        uint `gorm:"primary_key"`
   ID        uint  `gorm:"primary_key" json:"-"`
   BigInt    BigID `json:"ID"`
   CreatedAt time.Time
   UpdatedAt time.Time
   DeletedAt *time.Time `sql:"index"`
@@ -17,8 +22,29 @@
   if c.ID == 0 {
      id := snowflake.GenerateID()
      if id < 0 {
         // 处理 ID 为负数的情况(可选)
         id = snowflake.GenerateID()
      }
      c.ID = uint(id)
   }
}
func (id *BigID) UnmarshalJSON(b []byte) error {
   var idString string
   if err := json.Unmarshal(b, &idString); err != nil {
      return err
   }
   idValue, err := strconv.ParseUint(idString, 10, 64)
   if err != nil {
      return err
   }
   *id = BigID(idValue)
   return nil
}
func (id *BigID) MarshalJSON() ([]byte, error) {
   idString := strconv.FormatUint(uint64(*id), 10)
   return []byte(idString), nil
}