@@ -0,0 +1,121 @@
|
||||
package dailyretentionmod
|
||||
|
||||
import (
|
||||
"91porn-server/common/db"
|
||||
"91porn-server/common/log"
|
||||
"91porn-server/models"
|
||||
"errors"
|
||||
"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"
|
||||
)
|
||||
|
||||
const table = models.DailyRetention
|
||||
|
||||
func coll(t *db.MongoTool) *db.MongoTool {
|
||||
if t == nil {
|
||||
return mdb.Coll(table)
|
||||
}
|
||||
return t.Coll(table)
|
||||
}
|
||||
|
||||
func initIndex() {
|
||||
many := []mongo.IndexModel{
|
||||
{
|
||||
Keys: bson.D{
|
||||
{Key: "date", Value: -1},
|
||||
},
|
||||
},
|
||||
{
|
||||
Keys: bson.D{
|
||||
{Key: "createdAt", Value: -1},
|
||||
},
|
||||
},
|
||||
{
|
||||
Keys: bson.D{
|
||||
{Key: "adGroup", Value: 1},
|
||||
{Key: "date", Value: -1},
|
||||
},
|
||||
Options: options.Index().SetUnique(true),
|
||||
},
|
||||
}
|
||||
if _, err := coll(nil).CreateIndex(many); err != nil {
|
||||
panic(fmt.Sprintf("%s model set index err ==>[%+v]", table, err))
|
||||
}
|
||||
}
|
||||
|
||||
// Init 初始化索引
|
||||
func Init() {
|
||||
mdb = db.Init(table)
|
||||
initIndex()
|
||||
}
|
||||
|
||||
// QueryAllList 分页查询文档
|
||||
func QueryAllList(filter primitive.M, opts ...*options.FindOptions) (out []*DailyRetention, err error) {
|
||||
if err = coll(nil).Find(&out, filter, opts...); err != nil {
|
||||
log.Error(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "QueryAllList", table, "Find", err),
|
||||
log.Any("filter", filter),
|
||||
log.Any("opts", opts),
|
||||
)
|
||||
return nil, err
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// QueryOne
|
||||
func QueryOne(filter bson.M) (*DailyRetention, error) {
|
||||
v := DailyRetention{}
|
||||
if err := coll(nil).FindOne(&v, filter); err != nil {
|
||||
log.Error(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "QueryOne", table, "FindOne", err),
|
||||
log.Any("filter", filter),
|
||||
)
|
||||
return &v, err
|
||||
}
|
||||
|
||||
if v.Id.IsZero() {
|
||||
return nil, errors.New("record not found")
|
||||
}
|
||||
|
||||
return &v, nil
|
||||
}
|
||||
|
||||
// UpsertOne
|
||||
func UpsertOne(filter bson.M, updateData bson.M) (err error) {
|
||||
if _, err = coll(nil).UpsertOne(filter, updateData); err != nil {
|
||||
log.Error(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "UpsertOne", table, "UpdateOne", err),
|
||||
log.Any("filter", filter),
|
||||
log.Any("updateData", updateData),
|
||||
)
|
||||
return
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func BulkWrite(models []mongo.WriteModel, opts ...*options.BulkWriteOptions) (err error) {
|
||||
if len(models) == 0 {
|
||||
return nil
|
||||
}
|
||||
if _, err = coll(nil).Bulk(models, opts...); err != nil {
|
||||
log.Error(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "BulkWrite", table, "BulkWrite", err),
|
||||
log.Any("modelsCount", len(models)),
|
||||
)
|
||||
return
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func Distinct(filter bson.M, fieldName string, opts ...*options.DistinctOptions) (out []*DailyRetention, err error) {
|
||||
raw, err := coll(nil).Distinct(fieldName, filter, opts...)
|
||||
if err != nil {
|
||||
log.Error(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "Distinct", table, "UpdateOne", err),
|
||||
log.Any("fieldName", fieldName),
|
||||
log.Any("filter", filter),
|
||||
)
|
||||
return
|
||||
}
|
||||
out = make([]*DailyRetention, len(raw))
|
||||
return
|
||||
}
|
||||
Reference in New Issue
Block a user