121 lines
3.2 KiB
Go
121 lines
3.2 KiB
Go
package friend
|
|
|
|
import (
|
|
"go_dreamfactory/comm"
|
|
"go_dreamfactory/lego/core"
|
|
"go_dreamfactory/lego/sys/mgo"
|
|
"go_dreamfactory/modules"
|
|
"go_dreamfactory/pb"
|
|
"go_dreamfactory/sys/db"
|
|
|
|
"go.mongodb.org/mongo-driver/bson/primitive"
|
|
"go.mongodb.org/mongo-driver/mongo"
|
|
mgooptions "go.mongodb.org/mongo-driver/mongo/options"
|
|
"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)}},
|
|
Options: mgooptions.Index().SetUnique(true),
|
|
})
|
|
this.DB.CreateIndex(core.SqlTable(this.TableName), mongo.IndexModel{
|
|
Keys: bsonx.Doc{{Key: "assistHeroId", Value: bsonx.Int32(1)}},
|
|
})
|
|
return
|
|
}
|
|
|
|
// 查询好友
|
|
func (this *ModelFriend) GetFriend(uid string) (info *pb.DBFriend, err error) {
|
|
// 增加跨服处理
|
|
if db.IsCross() {
|
|
info = &pb.DBFriend{}
|
|
if err = this.Get(uid, info); err != nil && err != mgo.MongodbNil {
|
|
this.moduole.Errorln(err)
|
|
return
|
|
}
|
|
var user *pb.DBUser
|
|
if err == mgo.MongodbNil {
|
|
if user, err = this.moduole.ModuleUser.GetUser(uid); err != nil {
|
|
this.moduole.Errorln(err)
|
|
return
|
|
}
|
|
info = &pb.DBFriend{
|
|
Id: primitive.NewObjectID().Hex(),
|
|
Uid: uid,
|
|
Info: comm.GetUserBaseInfo(user),
|
|
FriendIds: make([]string, 0),
|
|
ApplyIds: make([]string, 0),
|
|
BlackIds: make([]string, 0),
|
|
GetZandIds: make([]string, 0),
|
|
Record: make([]*pb.AssistRecord, 0),
|
|
Beblackids: make([]string, 0),
|
|
}
|
|
err = this.Add(uid, info)
|
|
}
|
|
} else {
|
|
var (
|
|
conn_ *db.DBConn
|
|
)
|
|
if conn_, err = db.Cross(); err == nil {
|
|
model := db.NewDBModel(db.CrossTag(), this.TableName, conn_)
|
|
|
|
info = &pb.DBFriend{}
|
|
if err = model.Get(uid, info); err != nil && err != mgo.MongodbNil {
|
|
this.moduole.Errorln(err)
|
|
return
|
|
}
|
|
var user *pb.DBUser
|
|
if err == mgo.MongodbNil {
|
|
if user, err = this.moduole.ModuleUser.GetUser(uid); err != nil {
|
|
this.moduole.Errorln(err)
|
|
return
|
|
}
|
|
info = &pb.DBFriend{
|
|
Id: primitive.NewObjectID().Hex(),
|
|
Uid: uid,
|
|
Info: comm.GetUserBaseInfo(user),
|
|
FriendIds: make([]string, 0),
|
|
ApplyIds: make([]string, 0),
|
|
BlackIds: make([]string, 0),
|
|
GetZandIds: make([]string, 0),
|
|
Record: make([]*pb.AssistRecord, 0),
|
|
Beblackids: make([]string, 0),
|
|
}
|
|
err = model.Add(uid, info)
|
|
}
|
|
}
|
|
}
|
|
|
|
return
|
|
}
|
|
|
|
func (this *ModelFriend) GetFriends(uids []string) (friends []*pb.DBFriend, err error) {
|
|
friends = make([]*pb.DBFriend, 0)
|
|
if db.IsCross() {
|
|
if _, err = this.GetByUids(uids, &friends); err != nil {
|
|
this.moduole.Errorln(err)
|
|
}
|
|
} else {
|
|
var (
|
|
conn_ *db.DBConn
|
|
)
|
|
if conn_, err = db.Cross(); err == nil {
|
|
model := db.NewDBModel(db.CrossTag(), this.TableName, conn_)
|
|
if _, err = model.GetByUids(uids, &friends); err != nil {
|
|
this.moduole.Errorln(err)
|
|
}
|
|
}
|
|
}
|
|
|
|
return
|
|
}
|