go_dreamfactory/modules/chat/modelChat.go
2022-07-19 19:35:58 +08:00

168 lines
4.3 KiB
Go

package chat
import (
"context"
"fmt"
"go_dreamfactory/comm"
"go_dreamfactory/lego/core"
"go_dreamfactory/lego/sys/codec"
"go_dreamfactory/modules"
"go_dreamfactory/pb"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/x/bsonx"
)
var worldchatkey = "chat:world"
var unionchatkey = "chat:union"
var crosschatkey = "chat:cross"
var systemchatkey = "chat:system"
///论坛 数据组件
type modelChatComp struct {
modules.MCompModel
module *Chat
}
//组件初始化接口
func (this *modelChatComp) Init(service core.IService, module core.IModule, comp core.IModuleComp, opt core.IModuleOptions) (err error) {
this.MCompModel.Init(service, module, comp, opt)
this.module = module.(*Chat)
this.TableName = "chat"
//创建uid索引
this.DB.CreateIndex(core.SqlTable(this.TableName), mongo.IndexModel{
Keys: bsonx.Doc{{Key: "ruid", Value: bsonx.Int32(1)}},
})
return
}
//查询用户未读消息
func (this *modelChatComp) QueryUserMsg(uid string) (result []*pb.DBChat, err error) {
var (
c *mongo.Cursor
)
if c, err = this.DB.Find(core.SqlTable(this.TableName), bson.M{"ruid": uid}); err != nil {
this.module.Errorf("err:%v", err)
return
}
result = make([]*pb.DBChat, c.RemainingBatchLength())
n := 0
for c.Next(context.TODO()) {
temp := &pb.DBChat{}
if err = c.Decode(temp); err != nil {
this.module.Errorf("err:%v", err)
return
}
result[n] = temp
n++
}
this.DB.DeleteMany(core.SqlTable(this.TableName), bson.M{"ruid": uid}) //清理数据
return
}
//添加跨服频道成员
func (this *modelChatComp) AddCrossChannelMember(session comm.IUserSession) (channel int32, err error) {
udata := &pb.CacheUser{
Uid: session.GetUserId(),
SessionId: session.GetSessionId(),
ServiceTag: session.GetServiecTag(),
GatewayServiceId: session.GetGatewayServiceId(),
Ip: session.GetIP(),
}
channel = 0
count := 0
for {
key := fmt.Sprintf("%s:%d-member", crosschatkey, channel)
if count, err = this.Redis.Hlen(key); err != nil {
this.module.Errorf("err:%v", err)
return
}
if count < 3000 {
if err = this.Redis.HMSet(key, map[string]interface{}{session.GetUserId(): udata}); err != nil {
this.module.Errorf("err:%v", err)
return
}
break
} else {
channel++
}
}
return
}
//读取跨服聊天频道下成员
func (this *modelChatComp) GetCrossChannelMember(channel int32) (result []*pb.CacheUser, err error) {
key := fmt.Sprintf("%s:%d-member", crosschatkey, channel)
result = make([]*pb.CacheUser, 0)
if err = this.Redis.HGetAll(key, &result); err != nil {
this.module.Errorf("err:%v", err)
return
}
return
}
//移除频道成员
func (this *modelChatComp) RemoveCrossChannelMember(uid string) (err error) {
var (
result *pb.DBUserExpand
)
if result, err = this.GetUserExpand(uid); err != nil {
this.module.Errorf("err:%v", err)
return
}
key := fmt.Sprintf("%s:%d-member", crosschatkey, result.ChatChannel)
if err = this.Redis.HDel(key, uid); err != nil {
this.module.Errorf("err:%v", err)
return
}
return
}
//添加聊天消息到数据库中
func (this *modelChatComp) AddChatMsg(msg *pb.DBChat) (err error) {
switch msg.Channel {
case pb.ChatChannel_World:
this.addChatMsg(worldchatkey, 99, msg)
break
case pb.ChatChannel_Union:
this.addChatMsg(unionchatkey, 99, msg)
break
case pb.ChatChannel_CrossServer:
this.addChatMsg(fmt.Sprintf("%s:%d", crosschatkey, msg.AreaId), 99, msg)
break
case pb.ChatChannel_System:
// this.addChatMsg(systemchatkey, 99, msg)
break
}
return
}
func (this *modelChatComp) SaveUserMsg(msg *pb.DBChat) (err error) {
if _, err = this.DB.InsertOne(core.SqlTable(this.TableName), msg); err != nil {
this.module.Errorf("err:%v", err)
return
}
return
}
func (this *modelChatComp) addChatMsg(key string, count int, msg *pb.DBChat) (err error) {
var (
tempdata map[string]string
outkey []string
)
if tempdata, err = codec.MarshalMapJson(msg); err != nil {
this.module.Errorf("err:%v", err)
return
}
if outkey, err = this.Batchsetqueues(key, count, []string{fmt.Sprintf("%s-%s", key, msg.Id)}, []map[string]string{tempdata}); err != nil {
this.module.Errorf("err:%v", err)
return
}
if err = this.DeleteModelLogs(this.TableName, msg.Ruid, bson.M{"_id": bson.M{"$in": outkey}}); err != nil {
this.module.Errorf("err:%v", err)
return
}
return
}