go_dreamfactory/modules/shopcenter/modelShop.go
2023-08-15 16:54:14 +08:00

67 lines
1.8 KiB
Go

package shopcenter
import (
"go_dreamfactory/comm"
"go_dreamfactory/lego/core"
"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 ModelShop struct {
modules.MCompModel
module *ShopCenter
lock sync.RWMutex
activitys map[pb.HdType]*pb.DBHuodong
}
func (this *ModelShop) 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.TableShopCenter
this.module = module.(*ShopCenter)
this.DB.CreateIndex(core.SqlTable(this.TableName), mongo.IndexModel{
Keys: bsonx.Doc{{Key: "uid", Value: bsonx.Int32(1)}},
})
this.activitys = make(map[pb.HdType]*pb.DBHuodong)
return
}
func (this *ModelShop) addactivity(id pb.HdType, activity *pb.DBHuodong) {
this.lock.Lock()
this.activitys[id] = activity
this.lock.Unlock()
}
func (this *ModelShop) delactivity(id pb.HdType, activity *pb.DBHuodong) {
this.lock.Lock()
delete(this.activitys, id)
this.lock.Unlock()
}
func (this *ModelShop) getactivity() map[pb.HdType]*pb.DBHuodong {
this.lock.RLock()
defer this.lock.RUnlock()
return this.activitys
}
// 获取用户全部的埋点数据
func (this *ModelShop) getUserShopCenter(uid string) (results *pb.DBShopCenter, err error) {
results = &pb.DBShopCenter{}
if err = this.Get(uid, results); err != nil && err != mgo.MongodbNil {
this.module.Errorln(err)
return
}
if err == mgo.MongodbNil {
results = &pb.DBShopCenter{
Id: primitive.NewObjectID().Hex(),
Uid: uid,
Item: make(map[int32]*pb.DBShopCenterItem),
}
err = this.Add(uid, results)
}
return
}