parent
574ed54d6b
commit
5b0c2c8759
@ -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
|
||||
}
|
||||
@ -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
|
||||
}
|
||||
Loading…
Reference in new issue