67 lines
1.7 KiB
Go
67 lines
1.7 KiB
Go
package notify
|
|
|
|
import (
|
|
"context"
|
|
"go_dreamfactory/lego/core"
|
|
"go_dreamfactory/lego/sys/log"
|
|
"go_dreamfactory/modules"
|
|
"go_dreamfactory/pb"
|
|
|
|
"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 *Notification
|
|
}
|
|
|
|
//组件初始化接口
|
|
func (this *modelNotifyComp) Init(service core.IService, module core.IModule, comp core.IModuleComp, opt core.IModuleOptions) (err error) {
|
|
this.MCompModel.Init(service, module, comp, opt)
|
|
this.module = module.(*Notification)
|
|
this.TableName = "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 {
|
|
log.Errorf("GetFullNotify err:%v", err)
|
|
break
|
|
}
|
|
notifys[notify.Id] = notify
|
|
}
|
|
if len(notifys) > 0 {
|
|
if err = this.Redis.HMSet(this.TableName, notifys); err != nil {
|
|
log.Errorf("GetFullNotify err:%v", err)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
if len(notifys) > 0 {
|
|
n := 0
|
|
result = make([]*pb.DBSystemNotify, len(notifys))
|
|
for _, v := range notifys {
|
|
result[n] = v
|
|
n++
|
|
}
|
|
}
|
|
return
|
|
}
|