62 lines
1.4 KiB
Go
62 lines
1.4 KiB
Go
package friend
|
|
|
|
import (
|
|
"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/mongo"
|
|
"go.mongodb.org/mongo-driver/x/bsonx"
|
|
)
|
|
|
|
type ModelFriend struct {
|
|
modules.MCompModel
|
|
moduole *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.moduole = 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) (friend *pb.DBFriend, err error) {
|
|
friend = &pb.DBFriend{Uid: uid}
|
|
if this.moduole.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
|
|
}
|
|
}
|
|
// log.Error("未获得好友数据", log.Fields{"uid": uid})
|
|
}
|
|
} else {
|
|
var (
|
|
model *db.DBModel
|
|
)
|
|
if model, err = this.moduole.GetCrossDBModel(this.TableName); err != nil {
|
|
return
|
|
}
|
|
model.Get(uid, friend)
|
|
if err != nil {
|
|
if err == redis.RedisNil || err == mongo.ErrNoDocuments {
|
|
if err = model.Add(uid, friend); err != nil {
|
|
return
|
|
}
|
|
}
|
|
}
|
|
}
|
|
return
|
|
}
|