go_dreamfactory/modules/forum/modelForum.go
2022-08-11 15:53:11 +08:00

153 lines
3.8 KiB
Go

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索引
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) {
tcoon := db.ServerDBConn(stage)
var (
c *mongo.Cursor
)
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
}