From 78ce8c41bd72b01bb643ec705aa7121c6e6c2101 Mon Sep 17 00:00:00 2001 From: eson <9673575+githubcontent@user.noreply.gitee.com> Date: Thu, 26 Oct 2023 11:29:58 +0800 Subject: [PATCH] info --- model/gmodel/fs_user_info_logic.go | 40 +++++++++++++++++++++++++++--- 1 file changed, 37 insertions(+), 3 deletions(-) diff --git a/model/gmodel/fs_user_info_logic.go b/model/gmodel/fs_user_info_logic.go index 31101b78..e88a5969 100644 --- a/model/gmodel/fs_user_info_logic.go +++ b/model/gmodel/fs_user_info_logic.go @@ -50,7 +50,29 @@ func (m *FsUserInfoModel) MergeMetadata(userId int64, meta any) error { return fssql.MetadataModulePATCH(m.db, "profile", FsUserInfo{}, meta, "user_id = ?", userId) } -func (m *FsUserInfoModel) GetProfile(ctx context.Context, pkey string, userId int64) (map[string]any, error) { +func (m *FsUserInfoModel) GetDefaultProfile(ctx context.Context, tname string) (map[string]any, error) { + var baseinfo map[string]any + condUser := "user_id = 0 and guest_id = 0" + rawsql := fmt.Sprintf("select JSON_EXTRACT(metadata,'$.') as query from %s where %s and module = 'profile' order by ctime DESC limit 1", tname, condUser) + err := m.db.WithContext(ctx).Raw(rawsql).Take(&baseinfo).Error + if err != nil { + return nil, err + } + + v, ok := baseinfo["query"].(string) + if !ok { + return nil, fmt.Errorf("default userinfo profile is not exists") + } + + var info map[string]any + err = json.Unmarshal([]byte(v), &info) + if err != nil { + return nil, err + } + return info, nil +} + +func (m *FsUserInfoModel) GetProfile(ctx context.Context, pkey string, userId int64, guestId int64) (map[string]any, error) { var baseinfo map[string]any tname := fssql.GetGormTableName(m.db, FsUserInfo{}) @@ -61,7 +83,14 @@ func (m *FsUserInfoModel) GetProfile(ctx context.Context, pkey string, userId in pkey = "." + pkey } - rawsql := fmt.Sprintf("select JSON_EXTRACT(metadata,'$%s') as query from %s where user_id = ? and module = 'profile' order by ctime DESC limit 1", pkey, tname) + var condUser string + if userId == 0 { + condUser = fmt.Sprintf("user_id = 0 and guest_id = %d", guestId) + } else { + condUser = fmt.Sprintf("user_id = %d", userId) + } + + rawsql := fmt.Sprintf("select JSON_EXTRACT(metadata,'$%s') as query from %s where %s and module = 'profile' order by ctime DESC limit 1", pkey, tname, condUser) err := m.db.WithContext(ctx).Raw(rawsql, userId).Take(&baseinfo).Error if err != nil { return nil, err @@ -69,7 +98,7 @@ func (m *FsUserInfoModel) GetProfile(ctx context.Context, pkey string, userId in v, ok := baseinfo["query"].(string) if !ok { - return nil, nil + return m.GetDefaultProfile(ctx, tname) } var info map[string]any @@ -77,6 +106,11 @@ func (m *FsUserInfoModel) GetProfile(ctx context.Context, pkey string, userId in if err != nil { return nil, err } + + if len(info) == 0 { + return m.GetDefaultProfile(ctx, tname) + } + return info, nil } func (m *FsUserInfoModel) FindOneByUser(ctx context.Context, userId, guestId int64, module string) (resp *FsUserInfo, err error) {