From 45816fa84f615e35009e9b37be1d7cee467c090a Mon Sep 17 00:00:00 2001 From: wenxu12345 <44203734@qq.com> Date: Fri, 11 Feb 2022 19:34:18 +0800 Subject: [PATCH] Refactor code --- internal/rpc/friend/firend.go | 2 +- .../im_mysql_model/friend_request_model.go | 50 ++++++++++++------- 2 files changed, 32 insertions(+), 20 deletions(-) diff --git a/internal/rpc/friend/firend.go b/internal/rpc/friend/firend.go index ebd86cb05..89578ef4a 100644 --- a/internal/rpc/friend/firend.go +++ b/internal/rpc/friend/firend.go @@ -101,7 +101,7 @@ func (s *friendServer) AddFriend(ctx context.Context, req *pbFriend.AddFriendReq } //Establish a latest relationship in the friend request table - friendRequest := db.FriendRequest{ReqMsg: req.ReqMsg, HandleResult: 0} + friendRequest := db.FriendRequest{ReqMsg: req.ReqMsg, HandleResult: 0, CreateTime: time.Now()} utils.CopyStructFields(&friendRequest, req.CommID) // {openIM001 openIM002 0 test add friend 0001-01-01 00:00:00 +0000 UTC 0001-01-01 00:00:00 +0000 UTC }] log.NewDebug(req.CommID.OperationID, "UpdateFriendApplication args ", friendRequest) diff --git a/pkg/common/db/mysql_model/im_mysql_model/friend_request_model.go b/pkg/common/db/mysql_model/im_mysql_model/friend_request_model.go index bcb734281..983b55328 100644 --- a/pkg/common/db/mysql_model/im_mysql_model/friend_request_model.go +++ b/pkg/common/db/mysql_model/im_mysql_model/friend_request_model.go @@ -3,6 +3,7 @@ package im_mysql_model import ( "Open_IM/pkg/common/db" "Open_IM/pkg/utils" + "errors" "time" ) @@ -71,32 +72,43 @@ func UpdateFriendApplication(friendRequest *db.FriendRequest) error { friendRequest.FromUserID, friendRequest.ToUserID).Update(&friendRequest).Error } -func InsertFriendApplication(friendRequest *db.FriendRequest, args map[string]interface{}) error { +func InsertFriendApplication(friendRequest *db.FriendRequest) error { dbConn, err := db.DB.MysqlDB.DefaultGormDB() if err != nil { return err } - friendRequest.CreateTime = time.Now() - args["create_time"] = friendRequest.CreateTime - u := dbConn.Model(friendRequest).Updates(args) - //u := dbConn.Table("friend_requests").Where("from_user_id=? and to_user_id=?", - // friendRequest.FromUserID, friendRequest.ToUserID).Update(&friendRequest) - //u := dbConn.Table("friend_requests").Where("from_user_id=? and to_user_id=?", - // friendRequest.FromUserID, friendRequest.ToUserID).Update(&friendRequest) - if u.RowsAffected != 0 { + if err = dbConn.Table("friend_requests").Create(friendRequest).Error; err == nil { return nil } - if friendRequest.CreateTime.Unix() < 0 { - friendRequest.CreateTime = time.Now() - } - if friendRequest.HandleTime.Unix() < 0 { - friendRequest.HandleTime = utils.UnixSecondToTime(0) - } - err = dbConn.Table("friend_requests").Create(friendRequest).Error - if err != nil { - return err + t := dbConn.Model(friendRequest).Select("*").Updates(*friendRequest) + if t.RowsAffected == 0 { + return utils.Wrap(errors.New("RowsAffected == 0"), "no update") } - return nil + return utils.Wrap(t.Error, "") + + // + //friendRequest.CreateTime = time.Now() + //args["create_time"] = friendRequest.CreateTime + //u := dbConn.Model(friendRequest).Updates(args) + ////u := dbConn.Table("friend_requests").Where("from_user_id=? and to_user_id=?", + //// friendRequest.FromUserID, friendRequest.ToUserID).Update(&friendRequest) + ////u := dbConn.Table("friend_requests").Where("from_user_id=? and to_user_id=?", + //// friendRequest.FromUserID, friendRequest.ToUserID).Update(&friendRequest) + //if u.RowsAffected != 0 { + // return nil + //} + // + //if friendRequest.CreateTime.Unix() < 0 { + // friendRequest.CreateTime = time.Now() + //} + //if friendRequest.HandleTime.Unix() < 0 { + // friendRequest.HandleTime = utils.UnixSecondToTime(0) + //} + //err = dbConn.Table("friend_requests").Create(friendRequest).Error + //if err != nil { + // return err + //} + //return nil }