From 5b0c2c87593c39c997f048159eeedb38e1628a0c Mon Sep 17 00:00:00 2001 From: Gordon <1432970085@qq.com> Date: Wed, 26 May 2021 20:00:55 +0800 Subject: [PATCH] msg model --- .../im_mysql_msg_model/chat_log_model.go | 48 +++++++++++++++++++ .../im_mysql_msg_model/hash_code.go | 36 ++++++++++++++ .../im_mysql_msg_model/receive_model.go | 36 ++++++++++++++ 3 files changed, 120 insertions(+) create mode 100644 src/common/db/mysql_model/im_mysql_msg_model/chat_log_model.go create mode 100644 src/common/db/mysql_model/im_mysql_msg_model/hash_code.go create mode 100644 src/common/db/mysql_model/im_mysql_msg_model/receive_model.go diff --git a/src/common/db/mysql_model/im_mysql_msg_model/chat_log_model.go b/src/common/db/mysql_model/im_mysql_msg_model/chat_log_model.go new file mode 100644 index 000000000..ca8c1c0b8 --- /dev/null +++ b/src/common/db/mysql_model/im_mysql_msg_model/chat_log_model.go @@ -0,0 +1,48 @@ +/* +** description(""). +** copyright('tuoyun,www.tuoyun.net'). +** author("fg,Gordon@tuoyun.net"). +** time(2021/3/4 11:18). + */ +package im_mysql_msg_model + +import ( + "Open_IM/src/common/db" + pbMsg "Open_IM/src/proto/chat" + "Open_IM/src/utils" + "database/sql" + "time" +) + +// ChatLog Chat information table structure +type ChatLog struct { + MsgId string `gorm:"primary_key"` // Chat history primary key ID + SendID string `gorm:"column:send_id"` // Send ID + RecvID string `gorm:"column:recv_id"` //Receive ID + SendTime time.Time `gorm:"column:send_time"` // Send time + SessionType int32 `gorm:"column:session_type"` // Session type + ContentType int32 `gorm:"column:content_type"` // Message content type + MsgFrom int32 `gorm:"column:msg_from"` // Source, user, system + Content string `gorm:"column:content"` // Chat content + SenderPlatformID int32 `gorm:"column:sender_platform_id"` //The sender's platform ID + Remark sql.NullString `gorm:"column:remark"` // remark +} + +func InsertMessageToChatLog(msgData pbMsg.WSToMsgSvrChatMsg) error { + dbConn, err := db.DB.MysqlDB.DefaultGormDB() + if err != nil { + return err + } + chatLog := ChatLog{ + MsgId: msgData.MsgID, + SendID: msgData.SendID, + RecvID: msgData.RecvID, + SendTime: utils.UnixSecondToTime(msgData.SendTime), + SessionType: msgData.SessionType, + ContentType: msgData.ContentType, + MsgFrom: msgData.MsgFrom, + Content: msgData.Content, + SenderPlatformID: msgData.PlatformID, + } + return dbConn.Table("chat_log").Create(chatLog).Error +} diff --git a/src/common/db/mysql_model/im_mysql_msg_model/hash_code.go b/src/common/db/mysql_model/im_mysql_msg_model/hash_code.go new file mode 100644 index 000000000..b349e7787 --- /dev/null +++ b/src/common/db/mysql_model/im_mysql_msg_model/hash_code.go @@ -0,0 +1,36 @@ +package im_mysql_msg_model + +import ( + "Open_IM/src/common/config" + "Open_IM/src/common/db" + "hash/crc32" + "strconv" +) + +func getHashMsgDBAddr(userID string) string { + hCode := crc32.ChecksumIEEE([]byte(userID)) + return config.Config.Mysql.DBAddress[hCode%uint32(len(config.Config.Mysql.DBAddress))] +} + +func getHashMsgTableIndex(userID string) int { + hCode := crc32.ChecksumIEEE([]byte(userID)) + return int(hCode % uint32(config.Config.Mysql.DBMsgTableNum)) +} + +func QueryUserMsgID(userID string) ([]string, error) { + dbAddress, dbTableIndex := getHashMsgDBAddr(userID), getHashMsgTableIndex(userID) + dbTableName := "receive" + strconv.Itoa(dbTableIndex) + + dbConn, _ := db.DB.MysqlDB.GormDB(dbAddress, config.Config.Mysql.DBTableName) + + var msgID string + var msgIDList []string + rows, _ := dbConn.Raw("select msg_id from ? where user_id = ?", dbTableName, userID).Rows() + defer rows.Close() + for rows.Next() { + rows.Scan(&msgID) + msgIDList = append(msgIDList, msgID) + } + + return msgIDList, nil +} diff --git a/src/common/db/mysql_model/im_mysql_msg_model/receive_model.go b/src/common/db/mysql_model/im_mysql_msg_model/receive_model.go new file mode 100644 index 000000000..3973f71ef --- /dev/null +++ b/src/common/db/mysql_model/im_mysql_msg_model/receive_model.go @@ -0,0 +1,36 @@ +/* +** description(""). +** copyright('tuoyun,www.tuoyun.net'). +** author("fg,Gordon@tuoyun.net"). +** time(2021/3/4 11:18). + */ +package im_mysql_msg_model + +import ( + "time" +) + +// Receive Inbox table structure +type Receive struct { + UserId string `gorm:"primary_key"` // 收件箱主键ID + Seq int64 `gorm:"primary_key"` // 收件箱主键ID + MsgId string + CreateTime *time.Time +} + +//func InsertMessageToReceive(seq int64, userid, msgid string) error { +// conn := db.NewDbConnection() +// receive := Receive{ +// UID: userid, +// Seq: seq, +// MsgId: msgid, +// } +// err := conn.Table("receive").Create(&receive).Error +// return err +//} +//func GetBiggestSeqFromReceive(userid string) (seq int64, err error) { +// //得到数据库的连接(并非真连接,调用时才连接,由gorm自动维护数据库连接池) +// conn := db.NewDbConnection() +// err = conn.Raw("select max(seq) from receive where user_id = ?", userid).Row().Scan(&seq) +// return seq, err +//}