package chat import ( "context" "fmt" "go_dreamfactory/comm" "go_dreamfactory/lego/core" "go_dreamfactory/lego/sys/codec" "go_dreamfactory/lego/sys/redis" "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) GetChatQueue(channel pb.ChatChannel, union, group, area int32) (result []*pb.DBChat, err error) { var ( key string cdata []map[string]string find bson.M c *mongo.Cursor n int ) switch channel { case pb.ChatChannel_World: key = worldchatkey find = bson.M{"channel": channel} break case pb.ChatChannel_Union: key = unionchatkey find = bson.M{"channel": channel, "unionId": union} break case pb.ChatChannel_CrossServer: key = fmt.Sprintf("%s:%d--%d", crosschatkey, group, area) find = bson.M{"channel": channel, "group": group, "areaId": area} break case pb.ChatChannel_System: key = systemchatkey find = bson.M{"channel": channel} break } if cdata, err = this.Batchgetqueues(key); err == nil { result = make([]*pb.DBChat, len(cdata)) for i, v := range cdata { chat := &pb.DBChat{} if err = codec.UnmarshalMapJson(v, chat); err != nil { return } result[i] = chat } } if err == redis.RedisNil { //query from mgo if c, err = this.DB.Find(core.SqlTable(this.TableName), find); err != nil { return } else { result = make([]*pb.DBChat, c.RemainingBatchLength()) for c.Next(context.Background()) { chat := &pb.DBChat{} if err = c.Decode(chat); err != nil { this.module.Errorf("err:%v", err) } result[n] = chat n++ } if len(result) > 0 { this.addChatMsg(key, this.module.configure.GetChannelRecordMax(), result...) } } } 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 group := this.module.configure.GetServiecTagGroup(session.GetServiecTag()) maxnum := this.module.configure.GetAutoIntoChannelMax() for { key := fmt.Sprintf("%s:%d--%d-member", crosschatkey, group, channel) if count, err = this.Redis.Hlen(key); err != nil { this.module.Errorf("err:%v", err) return } if count < maxnum { 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) ChanageChannel(session comm.IUserSession, channel int32) (err error, ok bool) { udata := &pb.CacheUser{ Uid: session.GetUserId(), SessionId: session.GetSessionId(), ServiceTag: session.GetServiecTag(), GatewayServiceId: session.GetGatewayServiceId(), Ip: session.GetIP(), } group := this.module.configure.GetServiecTagGroup(session.GetServiecTag()) key := fmt.Sprintf("%s:%d--%d-member", crosschatkey, group, channel) count := 0 maxnum := this.module.configure.GetChanageChannelMax() if count, err = this.Redis.Hlen(key); err != nil { this.module.Errorf("err:%v", err) return } if count < maxnum { if err = this.Redis.HMSet(key, map[string]interface{}{session.GetUserId(): udata}); err != nil { this.module.Errorf("err:%v", err) return } ok = true } return } //读取跨服聊天频道下成员 func (this *modelChatComp) GetCrossChannelMember(group, channel int32) (result []*pb.CacheUser, err error) { key := fmt.Sprintf("%s:%d--%d-member", crosschatkey, group, 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(session comm.IUserSession) (err error) { var ( result *pb.DBUserExpand ) if result, err = this.GetUserExpand(session.GetUserId()); err != nil { this.module.Errorf("err:%v", err) return } group := this.module.configure.GetServiecTagGroup(session.GetServiecTag()) key := fmt.Sprintf("%s:%d--%d-member", crosschatkey, group, result.Chatchannel) if err = this.Redis.HDel(key, session.GetUserId()); err != nil { this.module.Errorf("err:%v", err) return } 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, msgs ...*pb.DBChat) (err error) { var ( tempdata map[string]string outkey []string ks []string vs []map[string]string values []interface{} ) ks = make([]string, len(msgs)) vs = make([]map[string]string, len(msgs)) values = make([]interface{}, len(msgs)) for i, v := range msgs { if tempdata, err = codec.MarshalMapJson(v); err != nil { this.module.Errorf("err:%v", err) return } ks[i] = fmt.Sprintf("%s-%s", key, v.Id) vs[i] = tempdata values[i] = v } if outkey, err = this.Batchsetqueues(key, count, ks, vs); err != nil { this.module.Errorf("err:%v", err) return } if _, err = this.DB.InsertMany(core.SqlTable(this.TableName), values); err != nil { this.module.Errorf("err:%v", err) return } if len(outkey) > 0 { if err = this.DeleteModelLogs(this.TableName, "", bson.M{"_id": bson.M{"$in": outkey}}); err != nil { this.module.Errorf("err:%v", err) return } } return }