go_dreamfactory/modules/notify/modelNotify.go
2022-11-09 14:18:03 +08:00

71 lines
1.8 KiB
Go

package notify
import (
"context"
"go_dreamfactory/comm"
"go_dreamfactory/lego/core"
"go_dreamfactory/modules"
"go_dreamfactory/pb"
"go_dreamfactory/sys/configure"
"github.com/go-redis/redis/v8"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/x/bsonx"
)
///论坛 数据组件
type modelNotifyComp struct {
modules.MCompModel
module *Notify
}
//组件初始化接口
func (this *modelNotifyComp) Init(service core.IService, module core.IModule, comp core.IModuleComp, opt core.IModuleOptions) (err error) {
this.TableName = comm.TableNotify
this.MCompModel.Init(service, module, comp, opt)
this.module = module.(*Notify)
//创建uid索引
this.DB.CreateIndex(core.SqlTable(this.TableName), mongo.IndexModel{
Keys: bsonx.Doc{{Key: "uid", Value: bsonx.Int32(1)}},
})
return
}
//获取全部公告
func (this *modelNotifyComp) GetFullNotify() (result []*pb.DBSystemNotify, err error) {
var (
notifys map[string]*pb.DBSystemNotify = make(map[string]*pb.DBSystemNotify)
c *mongo.Cursor
)
if err = this.Redis.HGetAll(this.TableName, notifys); err != nil && err == redis.Nil {
if c, err = this.DB.Find(core.SqlTable(this.TableName), bson.M{}); err == nil {
for c.Next(context.Background()) {
notify := &pb.DBSystemNotify{}
if err = c.Decode(notify); err != nil {
this.module.Errorf("GetFullNotify err:%v", err)
break
}
notifys[notify.Id] = notify
}
if len(notifys) > 0 {
if err = this.Redis.HMSet(this.TableName, notifys); err != nil {
this.module.Errorf("GetFullNotify err:%v", err)
}
}
}
}
if len(notifys) > 0 {
n := 0
now := configure.Now().Unix()
result = make([]*pb.DBSystemNotify, len(notifys))
for _, v := range notifys {
if now >= v.Rtime {
result[n] = v
n++
}
}
}
return
}