From 14f0dd2b22a24c4df202e046a04c81f3c1b38de0 Mon Sep 17 00:00:00 2001 From: eson <474420502@qq.com> Date: Sun, 10 Sep 2023 02:58:56 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B5=8B=E8=AF=95=E6=8E=A5=E5=8F=A3=E6=88=90?= =?UTF-8?q?=E5=8A=9F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- model/gmodel/fs_user_logic.go | 2 +- utils/fssql/metadata.go | 69 ++++++++++++++++++++++++++++++++--- utils/fssql/metadata_test.go | 28 ++++++++++++++ 3 files changed, 93 insertions(+), 6 deletions(-) create mode 100644 utils/fssql/metadata_test.go diff --git a/model/gmodel/fs_user_logic.go b/model/gmodel/fs_user_logic.go index 158f1503..dd0cf675 100644 --- a/model/gmodel/fs_user_logic.go +++ b/model/gmodel/fs_user_logic.go @@ -228,7 +228,7 @@ func (u *FsUserModel) RegisterByFusen(ctx context.Context, token *auth.RegisterT } else { - err = fssql.MetadataPATCH(txUserInfo, "profile", FsUser{}, metadata, "user_id = ?", *uinfo.UserId) + err = fssql.MetadataModulePATCH(txUserInfo, "profile", FsUser{}, metadata, "user_id = ?", *uinfo.UserId) if err != nil { return err } diff --git a/utils/fssql/metadata.go b/utils/fssql/metadata.go index bc97da9d..e56385b0 100644 --- a/utils/fssql/metadata.go +++ b/utils/fssql/metadata.go @@ -47,7 +47,7 @@ import ( // } // tableStructPointer 表结构 updateMetadata 可以是json的[]byte也可以结构体 -func MetadataPATCH(tx *gorm.DB, module string, tableStructPointer any, updateMetadata any, WhereKeysCond string, value ...any) error { +func MetadataModulePATCH(tx *gorm.DB, module string, tableStructPointer any, updateMetadata any, WhereKeysCond string, values ...any) error { stype := reflect.TypeOf(tableStructPointer) if stype.Kind() == reflect.Pointer { stype = stype.Elem() @@ -75,14 +75,73 @@ func MetadataPATCH(tx *gorm.DB, module string, tableStructPointer any, updateMet } } - var values []any + var args []any - values = append(values, metadata, metadata) - values = append(values, value...) + args = append(args, metadata, metadata) + args = append(args, values...) updatesql = fmt.Sprintf(updatesql, tx.NamingStrategy.TableName(stype.Name()), module, WhereKeysCond) // logx.Error(updatesql) - err = tx.Exec(updatesql, values).Error + err = tx.Exec(updatesql, args...).Error + if err != nil { + return err + } + + return nil +} + +func MetadataResourcePATCH(tx *gorm.DB, rid string, updateMetadata any, values ...any) error { + // stype := reflect.TypeOf(tableStructPointer) + // if stype.Kind() == reflect.Pointer { + // stype = stype.Elem() + // } + + var updatesql string + + if len(values) == 0 { + updatesql = `UPDATE fs_resource + SET metadata = CASE + WHEN metadata IS NULL THEN ? + ELSE JSON_MERGE_PATCH(metadata, ?) + END + WHERE resource_id = '%s';` + } else { + cond, ok := values[0].(string) + if !ok { + return fmt.Errorf("values[0] must be wherecond") + } + updatesql = `UPDATE fs_resource + SET metadata = CASE + WHEN metadata IS NULL THEN ? + ELSE JSON_MERGE_PATCH(metadata, ?) + END + cond, ok := values[0].(string) + WHERE resource_id = '%s'` + "and " + cond + `;` + } + + var err error + var metadata []byte + + switch mdata := updateMetadata.(type) { + case []byte: + metadata = mdata + case string: + metadata = []byte(mdata) + default: + metadata, err = json.Marshal(updateMetadata) + if err != nil { + return err + } + } + + var args []any + + args = append(args, metadata, metadata) + args = append(args, values...) + + updatesql = fmt.Sprintf(updatesql, rid) + // logx.Error(updatesql) + err = tx.Exec(updatesql, args...).Error if err != nil { return err } diff --git a/utils/fssql/metadata_test.go b/utils/fssql/metadata_test.go new file mode 100644 index 00000000..4c12011d --- /dev/null +++ b/utils/fssql/metadata_test.go @@ -0,0 +1,28 @@ +package fssql_test + +import ( + "fusenapi/initalize" + "fusenapi/model/gmodel" + "fusenapi/utils/fssql" + "testing" +) + +func TestCase1(t *testing.T) { + u := gmodel.UserProfile{ + FirstName: "h", + LastName: "sm", + } + + conn := initalize.InitMysql("fsreaderwriter:XErSYmLELKMnf3Dh@tcp(fusen.cdmigcvz3rle.us-east-2.rds.amazonaws.com:3306)/fusen") + err := fssql.MetadataModulePATCH(conn, "logo", gmodel.FsChangeCode{}, u, "id = ?", 6) + if err != nil { + panic(err) + } + + err = fssql.MetadataResourcePATCH(conn, + "f8932c0379fa5aa3397dc0a963696ca90536cc273ea10119b0137fd15ecfe673", u) + + if err != nil { + panic(err) + } +}