go_dreamfactory/modules/friend/model_friend.go
2023-12-29 15:49:26 +08:00

92 lines
2.5 KiB
Go

package friend
import (
"go_dreamfactory/comm"
"go_dreamfactory/lego/core"
"go_dreamfactory/lego/sys/mgo"
"go_dreamfactory/modules"
"go_dreamfactory/pb"
"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) {
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)
}
return
}
func (this *ModelFriend) GetFriends(uids []string) (friends []*pb.DBFriend, err error) {
friends = make([]*pb.DBFriend, 0)
var onfound []string
if onfound, err = this.Gets(uids, &friends); err != nil {
this.moduole.Errorln(err)
}
for _, v := range onfound {
var user *pb.DBUser
if user, err = this.moduole.ModuleUser.GetUser(v); err != nil {
this.moduole.Errorln(err)
return
}
info := &pb.DBFriend{
Id: primitive.NewObjectID().Hex(),
Uid: v,
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(v, info)
friends = append(friends, info)
}
return
}