diff --git a/go.mod b/go.mod index 01c94f65..069b20a9 100644 --- a/go.mod +++ b/go.mod @@ -123,4 +123,5 @@ require ( google.golang.org/grpc v1.56.2 // indirect google.golang.org/protobuf v1.31.0 // indirect gopkg.in/yaml.v2 v2.4.0 + gorm.io/datatypes v1.2.0 ) diff --git a/go.sum b/go.sum index 67d9f1b0..cabd1d03 100644 --- a/go.sum +++ b/go.sum @@ -986,6 +986,8 @@ gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= +gorm.io/datatypes v1.2.0 h1:5YT+eokWdIxhJgWHdrb2zYUimyk0+TaFth+7a0ybzco= +gorm.io/datatypes v1.2.0/go.mod h1:o1dh0ZvjIjhH/bngTpypG6lVRJ5chTBxE09FH/71k04= gorm.io/driver/mysql v1.5.1 h1:WUEH5VF9obL/lTtzjmML/5e6VfFR/788coz2uaVCAZw= gorm.io/driver/mysql v1.5.1/go.mod h1:Jo3Xu7mMhCyj8dlrb3WoCaRd1FhsVh+yMXb1jUInf5o= gorm.io/gorm v1.25.1 h1:nsSALe5Pr+cM3V1qwwQ7rOkw+6UeLrX5O4v3llhHa64= diff --git a/model/gmodel/fs_user_logic.go b/model/gmodel/fs_user_logic.go index c72bb9d9..e795973d 100644 --- a/model/gmodel/fs_user_logic.go +++ b/model/gmodel/fs_user_logic.go @@ -9,6 +9,7 @@ import ( "time" "github.com/zeromicro/go-zero/core/logx" + "gorm.io/gorm" ) @@ -146,15 +147,16 @@ func (u *FsUserModel) RegisterByGoogleOAuth(ctx context.Context, token *auth.Reg // SubscriptionStatus 订阅状态 type SubscriptionStatus struct { SubEmail bool `json:"all_emails"` - ItemMap struct { + ItemMap *struct { } `json:"item_map"` } // UserProfile 个人信息 type UserProfile struct { - FirstName string `json:"first_name"` - LastName string `json:"last_name"` - Resetaurant string `json:"resetaurant"` + FirstName string `json:"first_name"` + LastName string `json:"last_name"` + Resetaurant string `json:"resetaurant"` + SubStatus SubscriptionStatus `json:"sub_status"` } // 自平台的注册流程 @@ -188,6 +190,7 @@ func (u *FsUserModel) RegisterByFusen(ctx context.Context, token *auth.RegisterT // 继承guest_id的资源表 err = InheritGuestIdResource(tx, user.Id, token.GuestId, func(txResouce, txUserMaterial, txUserInfo *gorm.DB) error { + userProfile := &UserProfile{ FirstName: FirstName, LastName: LastName, @@ -197,6 +200,9 @@ func (u *FsUserModel) RegisterByFusen(ctx context.Context, token *auth.RegisterT if err != nil { return err } + + // txUserInfo.Where("user_id = ?", user.Id).Row().Err() + now := time.Now() uinfo := &FsUserInfo{ Module: FsString("profile"), @@ -207,11 +213,36 @@ func (u *FsUserModel) RegisterByFusen(ctx context.Context, token *auth.RegisterT Utime: &now, } - err = txUserInfo.Create(uinfo).Error - if err == gorm.ErrRecordNotFound { - return nil + err = txUserInfo.Where("user_id = ?", uinfo.UserId).Take(nil).Error + // txUserInfo.Statement.Table + + if err == nil { + + updatesql := `UPDATE %s + SET metadata = CASE + WHEN metadata IS NULL THEN '%s' + ELSE JSON_MERGE_PATCH(metadata, '%s') + END + WHERE id = ?;` + updatesql = fmt.Sprintf(updatesql, txUserInfo.Statement.Table) + + txUserInfo.Raw(`UPDATE fusen.fs_change_code + SET metadata = CASE + WHEN metadata IS NULL THEN '%s' + ELSE JSON_MERGE_PATCH(metadata, '%s') + END + WHERE id = ?;`) } + + if err == gorm.ErrRecordNotFound { + err = txUserInfo.Create(uinfo).Error + if err == gorm.ErrRecordNotFound { + return nil + } + } + return err + }) if err == gorm.ErrRecordNotFound { diff --git a/server/info/internal/logic/infologic_test.go b/server/info/internal/logic/infologic_test.go index fec09e59..0c11f2d4 100644 --- a/server/info/internal/logic/infologic_test.go +++ b/server/info/internal/logic/infologic_test.go @@ -4,7 +4,9 @@ import ( "encoding/json" "fmt" "fusenapi/initalize" + "fusenapi/model/gmodel" "fusenapi/utils/check" + "log" "strings" "testing" @@ -96,3 +98,31 @@ func TestMain(t *testing.T) { } } + +func TestCaseJSON_EXTRACT(t *testing.T) { + + userProfile := &gmodel.UserProfile{ + FirstName: "FirstName", + LastName: "LastName", + Resetaurant: "Resetaurant", + } + metadata, err := json.Marshal(userProfile) + if err != nil { + panic(err) + } + + updatesql := `UPDATE fusen.fs_change_code + SET metadata = CASE + WHEN metadata IS NULL THEN '%s' + ELSE JSON_MERGE_PATCH(metadata, '%s') + END + WHERE id = ?;` + + updatesql = fmt.Sprintf(updatesql, metadata, metadata) + log.Println(string(updatesql)) + + conn := initalize.InitMysql("fsreaderwriter:XErSYmLELKMnf3Dh@tcp(fusen.cdmigcvz3rle.us-east-2.rds.amazonaws.com:3306)/fusen") + // err = conn.Exec(updatesql, 6).Error + log.Println(conn.Model(&gmodel.FsChangeCode{}).Select("id").Where("id = 5").Take(nil).Error) + log.Println(err) +}