go_dreamfactory/modules/warorder/modelWarorder.go
2023-07-12 15:49:30 +08:00

79 lines
2.0 KiB
Go

package warorder
import (
"go_dreamfactory/comm"
"go_dreamfactory/lego/core"
"go_dreamfactory/lego/sys/log"
"go_dreamfactory/lego/sys/mgo"
"go_dreamfactory/modules"
"go_dreamfactory/pb"
"sync"
"go.mongodb.org/mongo-driver/bson/primitive"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/x/bsonx"
)
type modelWarorder struct {
modules.MCompModel
module *Warorder
lock sync.RWMutex
opentime map[int32]int64
}
func (this *modelWarorder) 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.TableWarorder
this.module = module.(*Warorder)
this.lock.Lock()
this.opentime = make(map[int32]int64)
this.lock.Unlock()
this.DB.CreateIndex(core.SqlTable(this.TableName), mongo.IndexModel{
Keys: bsonx.Doc{{Key: "uid", Value: bsonx.Int32(1)}},
})
return
}
func (this *modelWarorder) getopentime(wtype int32) (otime int64, open bool) {
this.lock.RLock()
otime, open = this.opentime[wtype]
this.lock.RUnlock()
return
}
func (this *modelWarorder) setopentime(wtype int32, opentime int64) {
this.lock.RLock()
this.opentime[wtype] = opentime
this.lock.RUnlock()
return
}
// 获取用户全部的埋点数据
func (this *modelWarorder) getUserWarorders(uid string) (results *pb.DBWarorders, err error) {
results = &pb.DBWarorders{}
if err = this.Get(uid, results); err != nil && err != mgo.MongodbNil {
this.module.Errorln(err)
return
}
if err == mgo.MongodbNil {
err = nil
results = &pb.DBWarorders{
Id: primitive.NewObjectID().Hex(),
Uid: uid,
Items: make(map[int32]*pb.Warorder),
}
err = this.Add(uid, results)
}
return
}
func (this *modelWarorder) updateUserWarorders(uid string, data *pb.DBWarorders) (err error) {
if err = this.Change(uid, map[string]interface{}{
"items": data.Items,
}); err != nil {
this.module.Error("更新用户任务数据 错误!", log.Field{Key: "err", Value: err.Error()})
return
}
return
}