Files
open-im-server/pkg/common/db/relation/chat_log_model.go
T

87 lines
2.8 KiB
Go
Raw Normal View History

2023-01-28 13:19:36 +08:00
package relation
2022-02-09 16:22:58 +08:00
import (
"fmt"
2023-03-23 19:02:20 +08:00
2023-03-16 10:46:06 +08:00
"github.com/OpenIMSDK/Open-IM-Server/pkg/common/constant"
"github.com/OpenIMSDK/Open-IM-Server/pkg/common/db/table/relation"
pbMsg "github.com/OpenIMSDK/Open-IM-Server/pkg/proto/msg"
sdkws "github.com/OpenIMSDK/Open-IM-Server/pkg/proto/sdkws"
"github.com/OpenIMSDK/Open-IM-Server/pkg/utils"
2023-01-16 20:14:26 +08:00
"github.com/golang/protobuf/jsonpb"
"github.com/jinzhu/copier"
2023-04-28 18:33:33 +08:00
"google.golang.org/protobuf/proto"
2023-01-11 16:23:16 +08:00
"gorm.io/gorm"
2022-02-09 16:22:58 +08:00
)
2023-02-02 16:11:24 +08:00
type ChatLogGorm struct {
2023-03-15 18:35:06 +08:00
*MetaDB
2023-01-11 16:23:16 +08:00
}
2023-03-03 17:42:26 +08:00
func NewChatLogGorm(db *gorm.DB) relation.ChatLogModelInterface {
2023-03-15 18:35:06 +08:00
return &ChatLogGorm{NewMetaDB(db, &relation.ChatLogModel{})}
2023-01-11 16:23:16 +08:00
}
2023-03-23 19:02:20 +08:00
func (c *ChatLogGorm) Create(msg *pbMsg.MsgDataToMQ) error {
2023-02-03 12:16:48 +08:00
chatLog := new(relation.ChatLogModel)
2023-01-16 20:14:26 +08:00
copier.Copy(chatLog, msg.MsgData)
switch msg.MsgData.SessionType {
case constant.GroupChatType, constant.SuperGroupChatType:
chatLog.RecvID = msg.MsgData.GroupID
case constant.SingleChatType:
chatLog.RecvID = msg.MsgData.RecvID
}
if msg.MsgData.ContentType >= constant.NotificationBegin && msg.MsgData.ContentType <= constant.NotificationEnd {
2023-02-09 20:36:34 +08:00
var tips sdkws.TipsComm
2023-01-16 20:14:26 +08:00
_ = proto.Unmarshal(msg.MsgData.Content, &tips)
marshaler := jsonpb.Marshaler{
OrigName: true,
EnumsAsInts: false,
EmitDefaults: false,
}
chatLog.Content, _ = marshaler.MarshalToString(&tips)
} else {
chatLog.Content = string(msg.MsgData.Content)
}
chatLog.CreateTime = utils.UnixMillSecondToTime(msg.MsgData.CreateTime)
chatLog.SendTime = utils.UnixMillSecondToTime(msg.MsgData.SendTime)
2023-01-31 18:55:22 +08:00
return c.DB.Create(chatLog).Error
2023-01-16 20:14:26 +08:00
}
2023-02-03 12:16:48 +08:00
func (c *ChatLogGorm) GetChatLog(chatLog *relation.ChatLogModel, pageNumber, showNumber int32, contentTypeList []int32) (int64, []relation.ChatLogModel, error) {
2023-01-31 18:55:22 +08:00
mdb := c.DB.Model(chatLog)
2022-09-02 12:01:10 +08:00
if chatLog.SendTime.Unix() > 0 {
2022-11-25 12:02:14 +08:00
mdb = mdb.Where("send_time > ? and send_time < ?", chatLog.SendTime, chatLog.SendTime.AddDate(0, 0, 1))
2022-09-02 12:01:10 +08:00
}
2022-09-02 02:01:42 +08:00
if chatLog.Content != "" {
2022-11-25 12:02:14 +08:00
mdb = mdb.Where(" content like ? ", fmt.Sprintf("%%%s%%", chatLog.Content))
2022-09-02 02:01:42 +08:00
}
if chatLog.SessionType == 1 {
2022-11-25 12:02:14 +08:00
mdb = mdb.Where("session_type = ?", chatLog.SessionType)
2022-09-02 02:01:42 +08:00
} else if chatLog.SessionType == 2 {
2022-11-25 12:02:14 +08:00
mdb = mdb.Where("session_type in (?)", []int{constant.GroupChatType, constant.SuperGroupChatType})
2022-02-17 19:35:17 +08:00
}
2022-09-02 02:01:42 +08:00
if chatLog.ContentType != 0 {
2022-11-25 12:02:14 +08:00
mdb = mdb.Where("content_type = ?", chatLog.ContentType)
2022-02-17 19:35:17 +08:00
}
if chatLog.SendID != "" {
2022-11-25 12:02:14 +08:00
mdb = mdb.Where("send_id = ?", chatLog.SendID)
2022-02-17 19:35:17 +08:00
}
if chatLog.RecvID != "" {
2022-11-25 12:02:14 +08:00
mdb = mdb.Where("recv_id = ?", chatLog.RecvID)
2022-09-02 02:01:42 +08:00
}
2022-11-25 12:02:14 +08:00
if len(contentTypeList) > 0 {
mdb = mdb.Where("content_type in (?)", contentTypeList)
2022-02-17 19:35:17 +08:00
}
2022-11-25 12:02:14 +08:00
var count int64
if err := mdb.Count(&count).Error; err != nil {
return 0, nil, err
2022-02-17 19:35:17 +08:00
}
2023-02-03 12:16:48 +08:00
var chatLogs []relation.ChatLogModel
2022-11-25 12:02:14 +08:00
mdb = mdb.Limit(int(showNumber)).Offset(int(showNumber * (pageNumber - 1)))
if err := mdb.Find(&chatLogs).Error; err != nil {
return 0, nil, err
2022-02-17 19:35:17 +08:00
}
2022-11-25 12:02:14 +08:00
return count, chatLogs, nil
2022-04-06 15:54:59 +08:00
}