From da7d1876123da5827d079922194e15c78bf6dc11 Mon Sep 17 00:00:00 2001 From: wenxu12345 <44203734@qq.com> Date: Fri, 31 Dec 2021 10:40:32 +0800 Subject: [PATCH] Refactor code --- .../im_mysql_model => }/model_struct.go | 2 +- pkg/common/db/mongoModel.go | 6 ++-- pkg/common/db/mysql.go | 20 +++++++++-- .../im_mysql_model/friend_model.go | 10 +++--- .../im_mysql_model/friend_request_model.go | 16 ++++----- .../im_mysql_model/group_member_model.go | 34 +++++++++---------- .../mysql_model/im_mysql_model/group_model.go | 8 ++--- .../im_mysql_model/group_request_model.go | 18 +++++----- .../im_mysql_model/user_black_list_model.go | 8 ++--- .../mysql_model/im_mysql_model/user_model.go | 12 +++---- 10 files changed, 75 insertions(+), 59 deletions(-) rename pkg/common/db/{mysql_model/im_mysql_model => }/model_struct.go (99%) diff --git a/pkg/common/db/mysql_model/im_mysql_model/model_struct.go b/pkg/common/db/model_struct.go similarity index 99% rename from pkg/common/db/mysql_model/im_mysql_model/model_struct.go rename to pkg/common/db/model_struct.go index d47ca5334..c2ab763c5 100644 --- a/pkg/common/db/mysql_model/im_mysql_model/model_struct.go +++ b/pkg/common/db/model_struct.go @@ -1,4 +1,4 @@ -package im_mysql_model +package db import "time" diff --git a/pkg/common/db/mongoModel.go b/pkg/common/db/mongoModel.go index 99da02b0b..bb8650309 100644 --- a/pkg/common/db/mongoModel.go +++ b/pkg/common/db/mongoModel.go @@ -28,7 +28,7 @@ type UserChat struct { Msg []MsgInfo } -type GroupMember struct { +type GroupMember_x struct { GroupID string UIDList []string } @@ -248,7 +248,7 @@ func (d *DataBases) MgoSkipUID(count int) (string, error) { } func (d *DataBases) GetGroupMember(groupID string) []string { - groupInfo := GroupMember{} + groupInfo := GroupMember_x{} groupInfo.GroupID = groupID groupInfo.UIDList = make([]string, 0) @@ -282,7 +282,7 @@ func (d *DataBases) AddGroupMember(groupID, uid string) error { } if n == 0 { - groupInfo := GroupMember{} + groupInfo := GroupMember_x{} groupInfo.GroupID = groupID groupInfo.UIDList = append(groupInfo.UIDList, uid) err = c.Insert(&groupInfo) diff --git a/pkg/common/db/mysql.go b/pkg/common/db/mysql.go index 152273f97..173843ec1 100644 --- a/pkg/common/db/mysql.go +++ b/pkg/common/db/mysql.go @@ -23,19 +23,22 @@ func initMysqlDB() { var err1 error db, err := gorm.Open("mysql", dsn) if err != nil { - log.Error("", "", dsn) + log.NewError("0", "Open failed ", err.Error(), dsn) } if err != nil { time.Sleep(time.Duration(30) * time.Second) db, err1 = gorm.Open("mysql", dsn) if err1 != nil { + log.NewError("0", "Open failed ", err1.Error(), dsn) panic(err1.Error()) } } + //Check the database and table during initialization - sql := fmt.Sprintf("CREATE DATABASE IF NOT EXISTS %s ;", config.Config.Mysql.DBDatabaseName) + sql := fmt.Sprintf("CREATE DATABASE IF NOT EXISTS %s ;", config.Config.Mysql.DBDatabaseName+"test1") err = db.Exec(sql).Error if err != nil { + log.NewError("0", "Exec failed ", err.Error(), sql) panic(err.Error()) } db.Close() @@ -44,9 +47,22 @@ func initMysqlDB() { config.Config.Mysql.DBUserName, config.Config.Mysql.DBPassword, config.Config.Mysql.DBAddress[0], config.Config.Mysql.DBDatabaseName) db, err = gorm.Open("mysql", dsn) if err != nil { + log.NewError("0", "Open failed ", err.Error(), dsn) panic(err.Error()) } + log.NewInfo("open db ok ", dsn) + + db.AutoMigrate(&Friend{}, + &FriendRequest{}, + &Group{}, + &GroupMember{}, + &GroupRequest{}, + &User{}, + &Black{}) + + return + sqlTable := "CREATE TABLE IF NOT EXISTS `user` (" + " `uid` varchar(64) NOT NULL," + " `name` varchar(64) DEFAULT NULL," + diff --git a/pkg/common/db/mysql_model/im_mysql_model/friend_model.go b/pkg/common/db/mysql_model/im_mysql_model/friend_model.go index a5e6a5de2..089224e79 100644 --- a/pkg/common/db/mysql_model/im_mysql_model/friend_model.go +++ b/pkg/common/db/mysql_model/im_mysql_model/friend_model.go @@ -6,7 +6,7 @@ import ( "time" ) -func InsertToFriend(toInsertFollow *Friend) error { +func InsertToFriend(toInsertFollow *db.Friend) error { dbConn, err := db.DB.MysqlDB.DefaultGormDB() if err != nil { return err @@ -20,12 +20,12 @@ func InsertToFriend(toInsertFollow *Friend) error { return nil } -func GetFriendRelationshipFromFriend(OwnerUserID, FriendUserID string) (*Friend, error) { +func GetFriendRelationshipFromFriend(OwnerUserID, FriendUserID string) (*db.Friend, error) { dbConn, err := db.DB.MysqlDB.DefaultGormDB() if err != nil { return nil, err } - var friend Friend + var friend db.Friend err = dbConn.Table("friend").Where("owner_user_id=? and friend_user_id=?", OwnerUserID, FriendUserID).Find(&friend).Error if err != nil { return nil, err @@ -33,12 +33,12 @@ func GetFriendRelationshipFromFriend(OwnerUserID, FriendUserID string) (*Friend, return &friend, err } -func GetFriendListByUserID(OwnerUserID string) ([]Friend, error) { +func GetFriendListByUserID(OwnerUserID string) ([]db.Friend, error) { dbConn, err := db.DB.MysqlDB.DefaultGormDB() if err != nil { return nil, err } - var friends []Friend + var friends []db.Friend err = dbConn.Table("friend").Where("owner_user_id=?", OwnerUserID).Find(&friends).Error if err != nil { return nil, err 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 5e99dcf5d..299cdfc69 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 @@ -19,12 +19,12 @@ import ( //} // who apply to add me -func GetReceivedFriendsApplicationListByUserID(ToUserID string) ([]FriendRequest, error) { +func GetReceivedFriendsApplicationListByUserID(ToUserID string) ([]db.FriendRequest, error) { dbConn, err := db.DB.MysqlDB.DefaultGormDB() if err != nil { return nil, err } - var usersInfo []FriendRequest + var usersInfo []db.FriendRequest err = dbConn.Table("friend_request").Where("to_user_id=?", ToUserID).Find(&usersInfo).Error if err != nil { return nil, err @@ -33,12 +33,12 @@ func GetReceivedFriendsApplicationListByUserID(ToUserID string) ([]FriendRequest } //I apply to add somebody -func GetSendFriendApplicationListByUserID(FromUserID string) ([]FriendRequest, error) { +func GetSendFriendApplicationListByUserID(FromUserID string) ([]db.FriendRequest, error) { dbConn, err := db.DB.MysqlDB.DefaultGormDB() if err != nil { return nil, err } - var usersInfo []FriendRequest + var usersInfo []db.FriendRequest err = dbConn.Table("friend_request").Where("from_user_id=?", FromUserID).Find(&usersInfo).Error if err != nil { return nil, err @@ -47,12 +47,12 @@ func GetSendFriendApplicationListByUserID(FromUserID string) ([]FriendRequest, e } //FromUserId apply to add ToUserID -func GetFriendApplicationByBothUserID(FromUserID, ToUserID string) (*FriendRequest, error) { +func GetFriendApplicationByBothUserID(FromUserID, ToUserID string) (*db.FriendRequest, error) { dbConn, err := db.DB.MysqlDB.DefaultGormDB() if err != nil { return nil, err } - var friendRequest FriendRequest + var friendRequest db.FriendRequest err = dbConn.Table("friend_request").Where("from_user_id=? and to_user_id=?", FromUserID, ToUserID).Find(&friendRequest).Error if err != nil { return nil, err @@ -60,7 +60,7 @@ func GetFriendApplicationByBothUserID(FromUserID, ToUserID string) (*FriendReque return &friendRequest, nil } -func UpdateFriendApplication(friendRequest *FriendRequest) error { +func UpdateFriendApplication(friendRequest *db.FriendRequest) error { dbConn, err := db.DB.MysqlDB.DefaultGormDB() if err != nil { return err @@ -71,7 +71,7 @@ func UpdateFriendApplication(friendRequest *FriendRequest) error { friendRequest.FromUserID, friendRequest.ToUserID).Update(&friendRequest).Error } -func InsertFriendApplication(friendRequest *FriendRequest) error { +func InsertFriendApplication(friendRequest *db.FriendRequest) error { dbConn, err := db.DB.MysqlDB.DefaultGormDB() if err != nil { return err diff --git a/pkg/common/db/mysql_model/im_mysql_model/group_member_model.go b/pkg/common/db/mysql_model/im_mysql_model/group_member_model.go index 95f4c5723..ce115fcda 100644 --- a/pkg/common/db/mysql_model/im_mysql_model/group_member_model.go +++ b/pkg/common/db/mysql_model/im_mysql_model/group_member_model.go @@ -18,7 +18,7 @@ import ( // Ex string `gorm:"column:ex"` //} -func InsertIntoGroupMember(toInsertInfo GroupMember) error { +func InsertIntoGroupMember(toInsertInfo db.GroupMember) error { dbConn, err := db.DB.MysqlDB.DefaultGormDB() if err != nil { return err @@ -34,12 +34,12 @@ func InsertIntoGroupMember(toInsertInfo GroupMember) error { return nil } -func GetGroupMemberListByUserID(userID string) ([]GroupMember, error) { +func GetGroupMemberListByUserID(userID string) ([]db.GroupMember, error) { dbConn, err := db.DB.MysqlDB.DefaultGormDB() if err != nil { return nil, err } - var groupMemberList []GroupMember + var groupMemberList []db.GroupMember err = dbConn.Table("group_member").Where("user_id=?", userID).Find(&groupMemberList).Error if err != nil { return nil, err @@ -47,12 +47,12 @@ func GetGroupMemberListByUserID(userID string) ([]GroupMember, error) { return groupMemberList, nil } -func GetGroupMemberListByGroupID(groupID string) ([]GroupMember, error) { +func GetGroupMemberListByGroupID(groupID string) ([]db.GroupMember, error) { dbConn, err := db.DB.MysqlDB.DefaultGormDB() if err != nil { return nil, err } - var groupMemberList []GroupMember + var groupMemberList []db.GroupMember err = dbConn.Table("group_member").Where("group_id=?", groupID).Find(&groupMemberList).Error if err != nil { return nil, err @@ -60,12 +60,12 @@ func GetGroupMemberListByGroupID(groupID string) ([]GroupMember, error) { return groupMemberList, nil } -func GetGroupMemberListByGroupIDAndRoleLevel(groupID string, roleLevel int32) ([]GroupMember, error) { +func GetGroupMemberListByGroupIDAndRoleLevel(groupID string, roleLevel int32) ([]db.GroupMember, error) { dbConn, err := db.DB.MysqlDB.DefaultGormDB() if err != nil { return nil, err } - var groupMemberList []GroupMember + var groupMemberList []db.GroupMember err = dbConn.Table("group_member").Where("group_id=? and role_level=?", groupID, roleLevel).Find(&groupMemberList).Error if err != nil { return nil, err @@ -73,12 +73,12 @@ func GetGroupMemberListByGroupIDAndRoleLevel(groupID string, roleLevel int32) ([ return groupMemberList, nil } -func GetGroupMemberInfoByGroupIDAndUserID(groupID, userID string) (*GroupMember, error) { +func GetGroupMemberInfoByGroupIDAndUserID(groupID, userID string) (*db.GroupMember, error) { dbConn, err := db.DB.MysqlDB.DefaultGormDB() if err != nil { return nil, err } - var groupMember GroupMember + var groupMember db.GroupMember err = dbConn.Table("group_member").Where("group_id=? and user_id=? ", groupID, userID).Limit(1).Find(&groupMember).Error if err != nil { return nil, err @@ -91,14 +91,14 @@ func DeleteGroupMemberByGroupIDAndUserID(groupID, userID string) error { if err != nil { return err } - err = dbConn.Table("group_member").Where("group_id=? and user_id=? ", groupID, userID).Delete(&GroupMember{}).Error + err = dbConn.Table("group_member").Where("group_id=? and user_id=? ", groupID, userID).Delete(&db.GroupMember{}).Error if err != nil { return err } return nil } -func UpdateGroupMemberInfo(groupMemberInfo GroupMember) error { +func UpdateGroupMemberInfo(groupMemberInfo db.GroupMember) error { dbConn, err := db.DB.MysqlDB.DefaultGormDB() if err != nil { return err @@ -110,12 +110,12 @@ func UpdateGroupMemberInfo(groupMemberInfo GroupMember) error { return nil } -func GetOwnerManagerByGroupID(groupID string) ([]GroupMember, error) { +func GetOwnerManagerByGroupID(groupID string) ([]db.GroupMember, error) { dbConn, err := db.DB.MysqlDB.DefaultGormDB() if err != nil { return nil, err } - var groupMemberList []GroupMember + var groupMemberList []db.GroupMember err = dbConn.Table("group_member").Where("group_id=? and role_level>?", groupID, constant.GroupOrdinaryUsers).Find(&groupMemberList).Error if err != nil { return nil, err @@ -136,7 +136,7 @@ func GetGroupMemberNumByGroupID(groupID string) uint32 { return number } -func GetGroupOwnerInfoByGroupID(groupID string) (*GroupMember, error) { +func GetGroupOwnerInfoByGroupID(groupID string) (*db.GroupMember, error) { omList, err := GetOwnerManagerByGroupID(groupID) if err != nil { return nil, err @@ -169,12 +169,12 @@ func RemoveGroupMember(groupID string, UserID string) error { return DeleteGroupMemberByGroupIDAndUserID(groupID, UserID) } -func GetMemberInfoByID(groupID string, userID string) (*GroupMember, error) { +func GetMemberInfoByID(groupID string, userID string) (*db.GroupMember, error) { return GetGroupMemberInfoByGroupIDAndUserID(groupID, userID) } -func GetGroupMemberByGroupID(groupID string, filter int32, begin int32, maxNumber int32) ([]GroupMember, error) { - var memberList []GroupMember +func GetGroupMemberByGroupID(groupID string, filter int32, begin int32, maxNumber int32) ([]db.GroupMember, error) { + var memberList []db.GroupMember var err error if filter >= 0 { memberList, err = GetGroupMemberListByGroupIDAndRoleLevel(groupID, filter) //sorted by join time diff --git a/pkg/common/db/mysql_model/im_mysql_model/group_model.go b/pkg/common/db/mysql_model/im_mysql_model/group_model.go index 12c809c28..18eba61f8 100644 --- a/pkg/common/db/mysql_model/im_mysql_model/group_model.go +++ b/pkg/common/db/mysql_model/im_mysql_model/group_model.go @@ -18,7 +18,7 @@ import ( // Ex string `gorm:"column:ex"` //} -func InsertIntoGroup(groupInfo Group) error { +func InsertIntoGroup(groupInfo db.Group) error { dbConn, err := db.DB.MysqlDB.DefaultGormDB() if err != nil { return err @@ -34,12 +34,12 @@ func InsertIntoGroup(groupInfo Group) error { return nil } -func GetGroupInfoByGroupID(groupId string) (*Group, error) { +func GetGroupInfoByGroupID(groupId string) (*db.Group, error) { dbConn, err := db.DB.MysqlDB.DefaultGormDB() if err != nil { return nil, err } - var groupInfo Group + var groupInfo db.Group err = dbConn.Table("group").Where("group_id=?", groupId).Find(&groupInfo).Error if err != nil { return nil, err @@ -47,7 +47,7 @@ func GetGroupInfoByGroupID(groupId string) (*Group, error) { return &groupInfo, nil } -func SetGroupInfo(groupInfo Group) error { +func SetGroupInfo(groupInfo db.Group) error { dbConn, err := db.DB.MysqlDB.DefaultGormDB() if err != nil { return err diff --git a/pkg/common/db/mysql_model/im_mysql_model/group_request_model.go b/pkg/common/db/mysql_model/im_mysql_model/group_request_model.go index 9ccffeaba..7ee352718 100644 --- a/pkg/common/db/mysql_model/im_mysql_model/group_request_model.go +++ b/pkg/common/db/mysql_model/im_mysql_model/group_request_model.go @@ -19,7 +19,7 @@ import ( // Ex string `gorm:"column:ex"` //} -func UpdateGroupRequest(groupRequest GroupRequest) error { +func UpdateGroupRequest(groupRequest db.GroupRequest) error { dbConn, err := db.DB.MysqlDB.DefaultGormDB() if err != nil { return err @@ -30,7 +30,7 @@ func UpdateGroupRequest(groupRequest GroupRequest) error { return dbConn.Table("group_request").Where("group_id=? and user_id=?", groupRequest.GroupID, groupRequest.UserID).Update(&groupRequest).Error } -func InsertIntoGroupRequest(toInsertInfo GroupRequest) error { +func InsertIntoGroupRequest(toInsertInfo db.GroupRequest) error { dbConn, err := db.DB.MysqlDB.DefaultGormDB() if err != nil { return err @@ -48,12 +48,12 @@ func InsertIntoGroupRequest(toInsertInfo GroupRequest) error { return nil } -func GetGroupRequestByGroupIDAndUserID(groupID, userID string) (*GroupRequest, error) { +func GetGroupRequestByGroupIDAndUserID(groupID, userID string) (*db.GroupRequest, error) { dbConn, err := db.DB.MysqlDB.DefaultGormDB() if err != nil { return nil, err } - var groupRequest GroupRequest + var groupRequest db.GroupRequest err = dbConn.Table("group_request").Where("user_id=? and group_id=?", userID, groupID).Find(&groupRequest).Error if err != nil { return nil, err @@ -66,19 +66,19 @@ func DelGroupRequestByGroupIDAndUserID(groupID, userID string) error { if err != nil { return err } - err = dbConn.Table("group_request").Where("group_id=? and user_id=?", groupID, userID).Delete(&GroupRequest{}).Error + err = dbConn.Table("group_request").Where("group_id=? and user_id=?", groupID, userID).Delete(&db.GroupRequest{}).Error if err != nil { return err } return nil } -func GetGroupRequestByGroupID(groupID string) ([]GroupRequest, error) { +func GetGroupRequestByGroupID(groupID string) ([]db.GroupRequest, error) { dbConn, err := db.DB.MysqlDB.DefaultGormDB() if err != nil { return nil, err } - var groupRequestList []GroupRequest + var groupRequestList []db.GroupRequest err = dbConn.Table("group_request").Where("group_id=?", groupID).Find(&groupRequestList).Error if err != nil { return nil, err @@ -87,8 +87,8 @@ func GetGroupRequestByGroupID(groupID string) ([]GroupRequest, error) { } //received -func GetGroupApplicationList(userID string) ([]GroupRequest, error) { - var groupRequestList []GroupRequest +func GetGroupApplicationList(userID string) ([]db.GroupRequest, error) { + var groupRequestList []db.GroupRequest memberList, err := GetGroupMemberListByUserID(userID) if err != nil { return nil, err diff --git a/pkg/common/db/mysql_model/im_mysql_model/user_black_list_model.go b/pkg/common/db/mysql_model/im_mysql_model/user_black_list_model.go index 42a7c2fa9..fe161c851 100644 --- a/pkg/common/db/mysql_model/im_mysql_model/user_black_list_model.go +++ b/pkg/common/db/mysql_model/im_mysql_model/user_black_list_model.go @@ -5,7 +5,7 @@ import ( "time" ) -func InsertInToUserBlackList(black Black) error { +func InsertInToUserBlackList(black db.Black) error { dbConn, err := db.DB.MysqlDB.DefaultGormDB() if err != nil { return err @@ -29,7 +29,7 @@ func CheckBlack(ownerUserID, blockUserID string) error { if err != nil { return err } - var black Black + var black db.Black err = dbConn.Table("user_black_list").Where("owner_user_id=? and block_user_id=?", ownerUserID, blockUserID).Find(&black).Error return err } @@ -44,12 +44,12 @@ func RemoveBlackList(ownerUserID, blockUserID string) error { return err } -func GetBlackListByUserID(ownerUserID string) ([]Black, error) { +func GetBlackListByUserID(ownerUserID string) ([]db.Black, error) { dbConn, err := db.DB.MysqlDB.DefaultGormDB() if err != nil { return nil, err } - var blackListUsersInfo []Black + var blackListUsersInfo []db.Black err = dbConn.Table("user_black_list").Where("owner_user_id=?", ownerUserID).Find(&blackListUsersInfo).Error if err != nil { return nil, err diff --git a/pkg/common/db/mysql_model/im_mysql_model/user_model.go b/pkg/common/db/mysql_model/im_mysql_model/user_model.go index 6c9069558..3ae38f4b2 100644 --- a/pkg/common/db/mysql_model/im_mysql_model/user_model.go +++ b/pkg/common/db/mysql_model/im_mysql_model/user_model.go @@ -19,7 +19,7 @@ func init() { } else { continue } - var appMgr User + var appMgr db.User appMgr.UserID = v appMgr.Nickname = "AppManager" + utils.IntToString(k+1) appMgr.AppMangerLevel = constant.AppAdmin @@ -31,7 +31,7 @@ func init() { } } -func UserRegister(user User) error { +func UserRegister(user db.User) error { dbConn, err := db.DB.MysqlDB.DefaultGormDB() if err != nil { return err @@ -67,16 +67,16 @@ func DeleteUser(userID string) (i int64) { if err != nil { return 0 } - i = dbConn.Table("user").Where("user_id=?", userID).Delete(User{}).RowsAffected + i = dbConn.Table("user").Where("user_id=?", userID).Delete(db.User{}).RowsAffected return i } -func GetUserByUserID(userID string) (*User, error) { +func GetUserByUserID(userID string) (*db.User, error) { dbConn, err := db.DB.MysqlDB.DefaultGormDB() if err != nil { return nil, err } - var user User + var user db.User err = dbConn.Table("user").Where("user_id=?", userID).First(&user).Error if err != nil { return nil, err @@ -84,7 +84,7 @@ func GetUserByUserID(userID string) (*User, error) { return &user, nil } -func UpdateUserInfo(user User) error { +func UpdateUserInfo(user db.User) error { dbConn, err := db.DB.MysqlDB.DefaultGormDB() if err != nil { return err