go_dreamfactory/modules/island/model.go
2023-10-30 17:06:21 +08:00

51 lines
1.3 KiB
Go

package island
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"
"go.mongodb.org/mongo-driver/x/bsonx"
)
type modelComp struct {
modules.MCompModel
module *IsLand
}
func (this *modelComp) Init(service core.IService, module core.IModule, comp core.IModuleComp, options core.IModuleOptions) (err error) {
err = this.MCompModel.Init(service, module, comp, options)
this.TableName = comm.TableIsLand
this.module = module.(*IsLand)
this.DB.CreateIndex(core.SqlTable(this.TableName), mongo.IndexModel{
Keys: bsonx.Doc{{Key: "uid", Value: bsonx.Int32(1)}},
})
return
}
// 读取数据
func (this *modelComp) getmodel(uid string) (result *pb.DBIsland, err error) {
result = &pb.DBIsland{}
if err = this.Get(uid, result); err != nil && err != mgo.MongodbNil {
this.module.Errorln(err)
}
if err == mgo.MongodbNil {
result = &pb.DBIsland{
Id: primitive.NewObjectID().Hex(),
Uid: uid,
Islands: make(map[int32]*pb.DBIslandItem),
Nodes: make(map[int32]int32),
}
if err = this.Add(uid, result); err != nil {
this.module.Errorln(err)
return
}
}
err = nil
return
}