package chat import ( "context" "fmt" "go_dreamfactory/comm" "go_dreamfactory/lego/core" "go_dreamfactory/lego/sys/redis" "go_dreamfactory/modules" "go_dreamfactory/pb" "go_dreamfactory/sys/db" "strings" "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:area" 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.TableName = comm.TableChat this.MCompModel.Init(service, module, comp, opt) this.module = module.(*Chat) //创建uid索引 _, err = 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, stag, union string, area int32) (result []*pb.DBChat, err error) { var ( key string find bson.M c *mongo.Cursor n int max_chat int32 readmax_chat int32 ) switch channel { case pb.ChatChannel_World: // key = worldchatkey key = fmt.Sprintf("%s-%s-%s", db.CrossTag(), worldchatkey, stag) find = bson.M{"channel": channel} break case pb.ChatChannel_Union: key = fmt.Sprintf("%s-%s-%s", db.CrossTag(), unionchatkey, union) find = bson.M{"channel": channel, "unionId": union} break case pb.ChatChannel_Area: key = fmt.Sprintf("%s-%s-%d", db.CrossTag(), crosschatkey, area) find = bson.M{"channel": channel, "areaId": area} break case pb.ChatChannel_System: key = fmt.Sprintf("%s-%s", db.CrossTag(), systemchatkey) find = bson.M{"channel": channel} break } if max_chat, err = this.module.configure.GetChannelRecordMax(); err != nil { return } if readmax_chat, err = this.module.configure.GetChannelReadRecordNum(); err != nil { return } result = make([]*pb.DBChat, 0) err = this.GetQueues(key, int(readmax_chat), &result) 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, int64(max_chat), result...) } } } else if err != nil { this.module.Errorf("err:%v", err) } 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(), } var ( count int maxnum int32 ) channel = 1 if maxnum, err = this.module.configure.GetAutoIntoChannelMax(); err != nil { return } for { key := fmt.Sprintf("%s-%s-%d-member", db.CrossTag(), crosschatkey, channel) if count, err = this.Redis.Hlen(key); err != nil { this.module.Errorf("err:%v", err) return } if int32(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(), } var ( maxnum int32 ) key := fmt.Sprintf("%s-%s-%d-member", db.CrossTag(), crosschatkey, channel) count := 0 if maxnum, err = this.module.configure.GetChanageChannelMax(); err != nil { return } if count, err = this.Redis.Hlen(key); err != nil { this.module.Errorf("err:%v", err) return } if int32(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(channel int32) (result []*pb.CacheUser, err error) { key := fmt.Sprintf("%s-%s-%d-member", db.CrossTag(), crosschatkey, channel) temp := make(map[string]*pb.CacheUser, 0) if err = this.Redis.HGetAll(key, &temp); err != nil { this.module.Errorf("err:%v", err) return } n := 0 result = make([]*pb.CacheUser, len(temp)) for _, v := range temp { result[n] = v n++ } return } // 移除频道成员 func (this *modelChatComp) removeCrossChannelMember(uid string) (err error) { var ( result *pb.DBUserExpand ) if result, err = this.module.ModuleUser.GetUserExpand(uid); err != nil { this.module.Errorf("err:%v", err) return } key := fmt.Sprintf("%s-%s-%d-member", db.CrossTag(), crosschatkey, result.Chatchannel) if err = this.Redis.HDel(key, uid); 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) sendWorldChat(msg *pb.DBChat) (errdata *pb.ErrorData) { // var ( // err error // max int32 // ) // if max, err = this.module.configure.GetChannelRecordMax(); err != nil { // errdata = &pb.ErrorData{ // Code: pb.ErrorCode_ConfigNoFound, // Title: pb.ErrorCode_ConfigNoFound.ToString(), // Message: err.Error(), // } // return // } // if err = this.module.modelChat.addChatMsg(fmt.Sprintf("%s-%s", worldchatkey, msg.Stag), int64(max), msg); err != nil { // errdata = &pb.ErrorData{ // Code: pb.ErrorCode_DBError, // Title: pb.ErrorCode_DBError.ToString(), // Message: err.Error(), // } // return // } // data, _ := anypb.New(&pb.ChatMessagePush{Chat: msg}) // if err = this.module.service.AcrossClusterBroadcast(context.Background(), msg.Stag, comm.Service_Gateway, string(comm.Rpc_GatewaySendRadioMsg), pb.UserMessage{ // MainType: string(this.module.GetType()), // SubType: "message", // Data: data, // }, nil); err != nil { // this.module.Errorf("err:%v", err) // code = pb.ErrorCode_SystemError // } // return // } // //发送系统频道聊天 // func (this *modelChatComp) sendSystemChat(msg *pb.DBChat) (errdata *pb.ErrorData) { // var ( // err error // ) // data, _ := anypb.New(&pb.ChatMessagePush{Chat: msg}) // if err = this.module.service.AcrossClusterBroadcast(context.Background(), msg.Stag, comm.Service_Gateway, string(comm.Rpc_GatewaySendRadioMsg), pb.UserMessage{ // MainType: string(this.module.GetType()), // SubType: "message", // Data: data, // }, nil); err != nil { // this.module.Errorf("err:%v", err) // code = pb.ErrorCode_SystemError // } // return // } func (this *modelChatComp) addChatMsg(key string, count int64, msgs ...*pb.DBChat) (err error) { var ( data map[string]*pb.DBChat = make(map[string]*pb.DBChat, len(msgs)) values []interface{} = make([]interface{}, len(msgs)) outkey []string ) for i, v := range msgs { data[fmt.Sprintf("%s-%s", key, v.Id)] = v values[i] = v } if outkey, err = this.AddQueues(key, count, data); 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 { delkeys := make([]string, 0) for _, v := range outkey { temp := strings.Split(v, "_") if len(temp) == 2 { delkeys = append(delkeys, temp[1]) } } if err = this.DeleteModelLogs(this.TableName, "", bson.M{"_id": bson.M{"$in": delkeys}}); err != nil { this.module.Errorf("err:%v", err) return } } return } // 发送聊天消息到世界频道 func (this *modelChatComp) sendChatToWorld(chat *pb.DBChat, queuecount int32) (err error) { if err = this.module.modelChat.addChatMsg(fmt.Sprintf("%s-%s-%s", db.CrossTag(), worldchatkey, chat.Stag), int64(queuecount), chat); err != nil { this.module.Errorln(err) return } if err = this.module.pushChatToWorld(chat); err != nil { this.module.Errorln(err) return } return } // 发送聊天消息到工会频道 func (this *modelChatComp) sendChatToUnion(chat *pb.DBChat, queuecount int32) (errdata *pb.ErrorData) { var ( err error ) if err = this.module.modelChat.addChatMsg(fmt.Sprintf("%s-%s-%s", db.CrossTag(), unionchatkey, chat.UnionId), int64(queuecount), chat); err != nil { this.module.Errorln(err) errdata = &pb.ErrorData{ Code: pb.ErrorCode_DBError, Title: pb.ErrorCode_DBError.ToString(), Message: err.Error(), } return } if errdata = this.module.pushChatToUnion(chat); errdata != nil { this.module.Errorln(errdata) return } return } // 发送聊天消息到工会频道 func (this *modelChatComp) sendChatToCrossServer(chat *pb.DBChat, queuecount int32) (err error) { if err = this.module.modelChat.addChatMsg(fmt.Sprintf("%s-%s-%d", db.CrossTag(), crosschatkey, chat.Suser.Group), int64(queuecount), chat); err != nil { this.module.Errorln(err) return } if err = this.module.pushChatToArea(chat); err != nil { return } return } // 发送聊天消息到系统平道 func (this *modelChatComp) sendChatToSystem(chat *pb.DBChat) (err error) { if err = this.module.pushChatToSystem(chat); err != nil { this.module.Errorln(err) return } return } // 发送聊天消息到私聊频道 func (this *modelChatComp) sendChatToPrivate(chat *pb.DBChat) (err error) { if err = this.module.pushChatToPrivate(chat); err != nil { this.module.Errorln(err) return } return }