go_dreamfactory/modules/friend/model_friend.go
2023-02-10 10:08:27 +08:00

59 lines
1.4 KiB
Go

package friend
import (
"context"
"go_dreamfactory/comm"
"go_dreamfactory/lego/core"
"go_dreamfactory/lego/sys/redis"
"go_dreamfactory/modules"
"go_dreamfactory/pb"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/x/bsonx"
)
type ModelFriend struct {
modules.MCompModel
moduleFriend *Friend
}
func (this *ModelFriend) Init(service core.IService, module core.IModule, comp core.IModuleComp, options core.IModuleOptions) (err error) {
this.TableName = comm.TableFriend
err = this.MCompModel.Init(service, module, comp, options)
this.moduleFriend = module.(*Friend)
this.DB.CreateIndex(core.SqlTable(this.TableName), mongo.IndexModel{
Keys: bsonx.Doc{{Key: "uid", Value: bsonx.Int32(1)}},
})
return
}
// 查询好友
func (this *ModelFriend) GetFriend(uid string) *pb.DBFriend {
friend := &pb.DBFriend{Uid: uid}
if this.moduleFriend.IsCross() {
err := this.Get(uid, friend)
if err != nil {
if err == redis.RedisNil || err == mongo.ErrNoDocuments {
if err := this.Add(uid, friend); err != nil {
return nil
}
}
// log.Error("未获得好友数据", log.Fields{"uid": uid})
}
} else {
friend := &pb.DBFriend{}
if err := this.moduleFriend.service.AcrossClusterRpcCall(
context.Background(),
this.moduleFriend.GetCrossTag(),
comm.Service_Worker,
string(comm.Rpc_ModuleFriendDB),
pb.RPCGeneralReqA1{Param1: uid},
friend); err != nil {
this.moduleFriend.Errorln(err)
}
}
return friend
}