@@ -0,0 +1,86 @@
|
||||
package eventmod
|
||||
|
||||
import (
|
||||
"91porn-server/common/db"
|
||||
"91porn-server/common/log"
|
||||
"91porn-server/models"
|
||||
"fmt"
|
||||
|
||||
"go.mongodb.org/mongo-driver/bson"
|
||||
"go.mongodb.org/mongo-driver/bson/primitive"
|
||||
"go.mongodb.org/mongo-driver/mongo"
|
||||
"go.mongodb.org/mongo-driver/mongo/options"
|
||||
)
|
||||
|
||||
var mdb *db.MongoDB
|
||||
|
||||
const table = models.Event
|
||||
|
||||
func coll(t *db.MongoTool) *db.MongoTool {
|
||||
if t == nil {
|
||||
return mdb.Coll(table)
|
||||
}
|
||||
return t.Coll(table)
|
||||
}
|
||||
|
||||
// initCollectIndex 初始化索引
|
||||
func initIndex() {
|
||||
many := []mongo.IndexModel{
|
||||
{
|
||||
Keys: bson.D{{"eventId", 1}},
|
||||
},
|
||||
{
|
||||
Keys: bson.D{{"createdAt", 1}},
|
||||
},
|
||||
{
|
||||
Keys: bson.D{{"status", 1}, {"retryCount", 1}},
|
||||
},
|
||||
}
|
||||
_, err := coll(nil).CreateIndex(many)
|
||||
if err != nil {
|
||||
panic(fmt.Sprintf("%s model set index err ==>[%+v]", table, err))
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// Insert 插入记录
|
||||
func Insert(t *db.MongoTool, d *Event) (err error) {
|
||||
_, err = coll(t).InsertOne(&d)
|
||||
if err != nil {
|
||||
log.Error(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "Insert", table, "InsertOne", err))
|
||||
return
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func Exists(eventID string) (bool, error) {
|
||||
var event Event
|
||||
if err := coll(nil).FindOne(&event, bson.M{"eventId": eventID}); err != nil {
|
||||
return false, err
|
||||
}
|
||||
return !event.ID.IsZero(), nil
|
||||
}
|
||||
|
||||
// GetList 获取列表
|
||||
func GetList(cond bson.M, limit int64) (res []*Event, err error) {
|
||||
opts := options.Find()
|
||||
opts = opts.SetSort(bson.D{{Key: "_id", Value: 1}}).SetLimit(limit)
|
||||
if err = coll(nil).Find(&res, cond, opts); err != nil {
|
||||
return
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// UpdateMany 更新数据
|
||||
func UpdateMany(cond primitive.M, data bson.M) error {
|
||||
_, err := coll(nil).UpdateMany(cond, data)
|
||||
if err != nil {
|
||||
log.Error(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "Update", table, "UpdateMany", err),
|
||||
log.Any("cond", cond),
|
||||
log.Any("update", data),
|
||||
)
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
package eventmod
|
||||
|
||||
import (
|
||||
"91porn-server/common/db"
|
||||
"time"
|
||||
|
||||
"go.mongodb.org/mongo-driver/bson/primitive"
|
||||
)
|
||||
|
||||
func Init() {
|
||||
mdb = db.Init(table)
|
||||
initIndex()
|
||||
}
|
||||
|
||||
type EventStatus int
|
||||
|
||||
const (
|
||||
Default EventStatus = 0 // 默认状态,等待上报
|
||||
Success EventStatus = 1 // 成功
|
||||
Failure EventStatus = 2 // 失败
|
||||
)
|
||||
|
||||
type Event struct {
|
||||
ID primitive.ObjectID `bson:"_id,omitempty"`
|
||||
EventId string `json:"eventId" bson:"eventId"`
|
||||
EventType string `json:"eventType" bson:"eventType"`
|
||||
Data string `json:"data" bson:"data"`
|
||||
Status EventStatus `json:"status" bson:"status"` //
|
||||
RetryCount int `json:"retryCount" bson:"retryCount"` // 重试次数
|
||||
CreatedAt time.Time `json:"createdAt"bson:"createdAt"` // 创建事件
|
||||
UpdatedAt time.Time `bson:"updatedAt"` // 修改时间
|
||||
}
|
||||
Reference in New Issue
Block a user