From 7f25d329e4f1f2c22cc7569401e405cb2eabfe73 Mon Sep 17 00:00:00 2001 From: "daniel.w" Date: Fri, 26 Jul 2024 01:05:23 +0800 Subject: [PATCH] feat: finish all member func --- generate/protobuf/member.proto | 2 + internal/logic/update_user_info_logic.go | 16 +++++- internal/model/account_model.go | 3 +- internal/model/user_table_model.go | 72 ++++++++++++++++++++++++ internal/svc/service_context.go | 3 +- 5 files changed, 91 insertions(+), 5 deletions(-) diff --git a/generate/protobuf/member.proto b/generate/protobuf/member.proto index df24185..a70280f 100644 --- a/generate/protobuf/member.proto +++ b/generate/protobuf/member.proto @@ -95,6 +95,8 @@ message UpdateUserInfoReq { optional int64 birthday = 6; optional VerifyType verify_type = 7; optional AlarmType alarm_type = 8; + optional string role_id = 9; + optional MemberStatus status = 10; } message GetUIDByAccountReq { diff --git a/internal/logic/update_user_info_logic.go b/internal/logic/update_user_info_logic.go index 9d93691..f85238a 100644 --- a/internal/logic/update_user_info_logic.go +++ b/internal/logic/update_user_info_logic.go @@ -2,8 +2,9 @@ package logic import ( "context" - "member/gen_result/pb/member" + "member/internal/domain" + ers "member/internal/lib/error" "member/internal/svc" "github.com/zeromicro/go-zero/core/logx" @@ -25,7 +26,16 @@ func NewUpdateUserInfoLogic(ctx context.Context, svcCtx *svc.ServiceContext) *Up // UpdateUserInfo 更新 User Info func (l *UpdateUserInfoLogic) UpdateUserInfo(in *member.UpdateUserInfoReq) (*member.Response, error) { - // todo: add your logic here and delete this line + err := l.svcCtx.UserModel.UpdateSome(l.ctx, in) + if err != nil { + return nil, ers.DBError(err.Error()) + } - return &member.Response{}, nil + return &member.Response{ + Status: &member.BaseResp{ + Code: domain.CodeOk.ToString(), + Message: "success", + Error: "", + }, + }, nil } diff --git a/internal/model/account_model.go b/internal/model/account_model.go index 85aebb8..bd27aa7 100755 --- a/internal/model/account_model.go +++ b/internal/model/account_model.go @@ -4,6 +4,7 @@ import ( "context" "database/sql" "fmt" + "github.com/zeromicro/go-zero/core/stores/cache" "github.com/zeromicro/go-zero/core/stores/sqlx" ) @@ -32,7 +33,7 @@ func (m *defaultAccountModel) UpdateTokenByLoginID(ctx context.Context, account accountAccountKey := fmt.Sprintf("%s%v", cacheAccountAccountPrefix, data.Account) accountIdKey := fmt.Sprintf("%s%v", cacheAccountIdPrefix, data.Id) _, err = m.ExecCtx(ctx, func(ctx context.Context, conn sqlx.SqlConn) (result sql.Result, err error) { - query := fmt.Sprintf("update %s set `token` = ? where `id` = ?", m.table, accountRowsWithPlaceHolder) + query := fmt.Sprintf("update %s set `token` = ? where `id` = ?", m.table) return conn.ExecCtx(ctx, query, token, data.Id) }, accountAccountKey, accountIdKey) return err diff --git a/internal/model/user_table_model.go b/internal/model/user_table_model.go index fd80b2a..51f00a3 100755 --- a/internal/model/user_table_model.go +++ b/internal/model/user_table_model.go @@ -7,7 +7,9 @@ import ( "github.com/zeromicro/go-zero/core/stores/cache" "github.com/zeromicro/go-zero/core/stores/sqlc" "github.com/zeromicro/go-zero/core/stores/sqlx" + "member/gen_result/pb/member" "strings" + "time" ) var _ UserTableModel = (*customUserTableModel)(nil) @@ -21,6 +23,7 @@ type ( ListMembers(ctx context.Context, params *UserQueryParams) ([]*UserTable, error) Count(ctx context.Context) (int64, error) UpdateStatus(ctx context.Context, uid string, status int32) error + UpdateSome(ctx context.Context, newData *member.UpdateUserInfoReq) error } customUserTableModel struct { @@ -146,3 +149,72 @@ func (m *defaultUserTableModel) UpdateStatus(ctx context.Context, uid string, st }, userTableIdKey, userTableUidKey) return err } + +func (m *defaultUserTableModel) UpdateSome(ctx context.Context, newData *member.UpdateUserInfoReq) error { + data, err := m.FindOneByUid(ctx, newData.Uid) + if err != nil { + return err + } + + // 初始化缓存键 + userTableIdKey := fmt.Sprintf("%s%v", cacheUserTableIdPrefix, data.Id) + userTableUidKey := fmt.Sprintf("%s%v", cacheUserTableUidPrefix, data.Uid) + + query := fmt.Sprintf("update %s set ", m.table) + var args []interface{} + var updates []string + + if newData.VerifyType != nil { + updates = append(updates, "verify_type = ?") + args = append(args, *newData.VerifyType) + } + if newData.AlarmType != nil { + updates = append(updates, "alarm_type = ?") + args = append(args, *newData.AlarmType) + } + if newData.Status != nil { + updates = append(updates, "status = ?") + args = append(args, *newData.Status) + } + if newData.RoleId != nil { + updates = append(updates, "role_id = ?") + args = append(args, *newData.RoleId) + } + if newData.Language != nil { + updates = append(updates, "language = ?") + args = append(args, *newData.Language) + } + if newData.Currency != nil { + updates = append(updates, "currency = ?") + args = append(args, *newData.Currency) + } + if newData.NickName != nil { + updates = append(updates, "nick_name = ?") + args = append(args, *newData.NickName) + } + if newData.Gender != nil { + updates = append(updates, "gender = ?") + args = append(args, *newData.Gender) + } + if newData.Birthday != nil { + updates = append(updates, "birthday = ?") + args = append(args, *newData.Birthday) + } + + if len(updates) == 0 { + return nil + } + + update := time.Now().UTC().Unix() + updates = append(updates, "update_time = ?") + args = append(args, &update) + + query += strings.Join(updates, ", ") + " where `id` = ?" + args = append(args, data.Id) + + _, err = m.ExecCtx(ctx, func(ctx context.Context, conn sqlx.SqlConn) (result sql.Result, err error) { + return conn.ExecCtx(ctx, query, args...) + }, userTableIdKey, userTableUidKey) + + return err +} diff --git a/internal/svc/service_context.go b/internal/svc/service_context.go index 393ad9a..6d5120e 100644 --- a/internal/svc/service_context.go +++ b/internal/svc/service_context.go @@ -1,12 +1,13 @@ package svc import ( - "github.com/zeromicro/go-zero/core/stores/redis" "member/internal/config" "member/internal/domain" "member/internal/lib/required" "member/internal/model" + "github.com/zeromicro/go-zero/core/stores/redis" + "github.com/bwmarrin/snowflake" ers "member/internal/lib/error"