go_dreamfactory/modules/activity/model_hdlist.go
2023-08-31 16:38:25 +08:00

93 lines
2.3 KiB
Go

package activity
import (
"context"
"go_dreamfactory/comm"
"go_dreamfactory/lego/core"
"go_dreamfactory/modules"
"go_dreamfactory/pb"
"sync"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/x/bsonx"
)
type modelHdList struct {
modules.MCompModel
module *Activity
hlock sync.RWMutex
activity map[pb.HdType]*pb.DBHuodong
}
func (this *modelHdList) Init(service core.IService, module core.IModule, comp core.IModuleComp, options core.IModuleOptions) (err error) {
this.TableName = string(comm.TableHdInfo)
err = this.MCompModel.Init(service, module, comp, options)
this.module = module.(*Activity)
this.DB.CreateIndex(core.SqlTable(this.TableName), mongo.IndexModel{
Keys: bsonx.Doc{{Key: "hdid", Value: bsonx.Int32(1)}},
})
this.DB.CreateIndex(core.SqlTable(this.TableName), mongo.IndexModel{
Keys: bsonx.Doc{{Key: "itype", Value: bsonx.Int32(1)}},
})
return
}
func (this *modelHdList) getHdInfo() (activity map[pb.HdType]*pb.DBHuodong) {
this.hlock.RLock()
defer this.hlock.RUnlock()
return this.activity
}
// 通过活动ID查找
func (this *modelHdList) getHdListByHdId(oid string) (result *pb.DBHuodong, err error) {
_data := this.DBModel.DB.FindOne(comm.TableHdInfo, bson.M{"_id": oid})
result = &pb.DBHuodong{}
if err = _data.Decode(result); err != nil {
this.module.Errorln(err)
}
return
}
// 不需要修改 此接口不调用
func (this *modelHdList) modifyHdInfo(uid string, data map[string]interface{}) error {
return this.Change(uid, data)
}
func (this *modelHdList) addHdInfo(hd *pb.DBHuodong) error {
if _, err := this.DB.InsertOne(core.SqlTable("hdinfo"), hd); err != nil {
return err
}
return nil
}
func (this *modelHdList) LoadActivityData() {
if c, err := this.DB.Find(core.SqlTable(this.TableName), bson.M{}); err != nil {
return
} else {
this.hlock.Lock()
defer this.hlock.Unlock()
this.activity = make(map[pb.HdType]*pb.DBHuodong)
for c.Next(context.Background()) {
hd := &pb.DBHuodong{}
if err = c.Decode(hd); err != nil {
this.module.Errorf("err:%v", err)
continue
}
this.activity[hd.Itype] = hd
}
}
}
func (this *modelHdList) getHdInfoByType(itype pb.HdType) (activity *pb.DBHuodong) {
var ok bool
this.hlock.RLock()
activity, ok = this.activity[itype]
this.hlock.RUnlock()
if ok {
return nil
}
return nil
}