package forum 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" "go.mongodb.org/mongo-driver/bson" "go.mongodb.org/mongo-driver/mongo" "go.mongodb.org/mongo-driver/x/bsonx" ) ///论坛 数据组件 type modelForumComp struct { modules.MCompModel module *Forum } //组件初始化接口 func (this *modelForumComp) Init(service core.IService, module core.IModule, comp core.IModuleComp, opt core.IModuleOptions) (err error) { this.TableName = comm.TableForum this.MCompModel.Init(service, module, comp, opt) this.module = module.(*Forum) //创建uid索引 _, err = this.DB.CreateIndex(core.SqlTable(this.TableName), mongo.IndexModel{ Keys: bsonx.Doc{{Key: "heroid", Value: bsonx.Int32(1)}}, }) return } //查询用户未读消息 func (this *modelForumComp) getComment(herocId string) (result []*pb.DBComment, err error) { var ( key string c *mongo.Cursor n int max_chat int32 ) result = make([]*pb.DBComment, 0) key = fmt.Sprintf("%s:%s", this.TableName, herocId) err = this.GetQueues(key, 10, &result) if err == redis.RedisNil { //query from mgo if c, err = this.DB.Find(core.SqlTable(this.TableName), bson.M{"heroid": herocId}); err != nil { return } else { result = make([]*pb.DBComment, c.RemainingBatchLength()) for c.Next(context.Background()) { chat := &pb.DBComment{} if err = c.Decode(chat); err != nil { this.module.Errorf("err:%v", err) } result[n] = chat n++ } if len(result) > 0 { this.addCommentChache(key, int64(max_chat), result...) } } } return } ///发布评论 func (this *modelForumComp) releaseComment(comment *pb.DBComment) (err error) { key := fmt.Sprintf("%s:%s", this.TableName, comment.Heroid) if err = this.addCommentChache(key, 99, comment); err != nil { this.module.Errorln(err) return } if _, err = this.DB.InsertOne(core.SqlTable(this.TableName), comment); err != nil { this.module.Errorln(err) return } return } //添加评论到缓存中 func (this *modelForumComp) addCommentChache(key string, count int64, msgs ...*pb.DBComment) (err error) { var ( data map[string]*pb.DBComment = make(map[string]*pb.DBComment, len(msgs)) ) for _, v := range msgs { data[fmt.Sprintf("%s-%s", key, v.Id)] = v } if _, err = this.AddQueues(key, count, data); err != nil { this.module.Errorln(err) return } return } //点赞 func (this *modelForumComp) like(heroid, id string, islike bool) (comment *pb.DBComment, err error) { comment = &pb.DBComment{} key := fmt.Sprintf("%s:%s-%s", this.TableName, heroid, id) if err = this.Redis.HGetAll(key, comment); err != nil && err != redis.RedisNil { this.module.Errorln(err) return } if err == redis.RedisNil { if err = this.DB.FindOne(comm.TableHero, bson.M{"_id": id}).Decode(comment); err != nil { return } } if islike { comment.Starlist++ } else { comment.Starlist-- } if err = this.Redis.HMSet(key, map[string]interface{}{ "starlist": comment.Starlist, }); err != nil { return } this.DB.UpdateOne(comm.TableHero, bson.M{"_id": id}, bson.M{"starlist": comment.Starlist}) return } //查看 func (this *modelForumComp) watchHero(stage string, uid string, herocid string) (hero *pb.DBHero, err error) { var ( tcoon *db.DBConn c *mongo.Cursor ) if tcoon, err = db.ServerDBConn(stage); err != nil { this.module.Errorf("stage:%s err:%v", stage, err) return } if c, err = tcoon.Mgo.Find(comm.TableHero, bson.M{"uid": uid, "heroID": herocid}); err != nil { return } else { n := 0 result := make([]*pb.DBHero, c.RemainingBatchLength()) for c.Next(context.Background()) { hero := &pb.DBHero{} if err = c.Decode(hero); err != nil { this.module.Errorf("err:%v", err) } result[n] = hero n++ } for _, v := range result { if hero == nil || hero.Lv < v.Lv { hero = v } } } return } func (this *modelForumComp) watchHeroEquip(stage string, uid string, equipIds ...string) (equips []*pb.DB_Equipment) { if tcoon, err := db.ServerDBConn(stage); err == nil { for _, id := range equipIds { if id == "" { equips = append(equips, nil) continue } sr := tcoon.Mgo.FindOne(comm.TableEquipment, bson.M{ "_id": id, }) equip := &pb.DB_Equipment{} if err = sr.Decode(equip); err == nil { equips = append(equips, equip) } else { this.module.Errorf("find hero equip error: %v", err) } } } else { this.module.Errorf("stage:%s err:%v", stage, err) } return }