From 2dea693b0d78246e0db41e8e2daaf6ad9cc3ed8c Mon Sep 17 00:00:00 2001
From: zhangqian <zhangqian@123.com>
Date: 星期五, 08 十二月 2023 16:49:29 +0800
Subject: [PATCH] fix

---
 model/model.go |   30 ++++++++++++++++++++++--------
 1 files changed, 22 insertions(+), 8 deletions(-)

diff --git a/model/model.go b/model/model.go
index 920a3e6..533c35d 100644
--- a/model/model.go
+++ b/model/model.go
@@ -9,7 +9,7 @@
 )
 
 type CommonModel struct {
-	ID        uint   `gorm:"primary_key;autoIncrement:false" json:"-"`
+	ID        uint   `gorm:"primary_key" json:"-"`
 	IDStr     string `json:"ID" gorm:"-"`
 	CreatedAt time.Time
 	UpdatedAt time.Time
@@ -20,21 +20,35 @@
 	if c.ID == 0 {
 		id := snowflake.GenerateID()
 		if id < 0 {
+			// 澶勭悊 ID 涓鸿礋鏁扮殑鎯呭喌锛堝彲閫夛級
 			id = snowflake.GenerateID()
 		}
 		c.ID = uint(id)
 	}
 }
-func (c CommonModel) UnmarshalJSON(b []byte) (err error) {
-	id, err := strconv.ParseUint(c.IDStr, 10, 64)
-	if err != nil {
+
+func (c *CommonModel) UnmarshalJSON(b []byte) error {
+	var data map[string]interface{}
+	if err := json.Unmarshal(b, &data); err != nil {
 		return err
 	}
-	c.ID = uint(id)
-	return
+
+	if idStr, ok := data["ID"].(string); ok {
+		id, err := strconv.ParseUint(idStr, 10, 64)
+		if err != nil {
+			return err
+		}
+		c.ID = uint(id)
+		c.IDStr = idStr
+	}
+
+	return nil
 }
 
-func (c CommonModel) MarshalJSON() ([]byte, error) {
-	c.IDStr = strconv.FormatUint(uint64(c.ID), 10)
+func (c *CommonModel) MarshalJSON() ([]byte, error) {
+	if c.ID != 0 && c.IDStr == "" {
+		c.IDStr = strconv.FormatUint(uint64(c.ID), 10)
+	}
+
 	return json.Marshal(c)
 }

--
Gitblit v1.8.0