@@ -0,0 +1,141 @@
|
||||
package adsmod
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"91porn-server/common/db"
|
||||
"91porn-server/common/log"
|
||||
"91porn-server/common/pageopt"
|
||||
"91porn-server/models"
|
||||
|
||||
"go.mongodb.org/mongo-driver/bson"
|
||||
"go.mongodb.org/mongo-driver/bson/primitive"
|
||||
"go.mongodb.org/mongo-driver/mongo"
|
||||
)
|
||||
|
||||
const table = models.Ads
|
||||
|
||||
var mdb *db.MongoDB
|
||||
|
||||
func coll(t *db.MongoTool) *db.MongoTool {
|
||||
if t == nil {
|
||||
return mdb.Coll(table)
|
||||
}
|
||||
return t.Coll(table)
|
||||
}
|
||||
|
||||
// InitIndex 初始化索引
|
||||
func initAdsIndex() {
|
||||
many := []mongo.IndexModel{
|
||||
{
|
||||
Keys: bson.D{{Key: "createdAt", Value: -1}},
|
||||
},
|
||||
{
|
||||
Keys: bson.D{{Key: "active", Value: 1}},
|
||||
},
|
||||
{
|
||||
Keys: bson.D{{Key: "updatedAt", Value: 1}},
|
||||
},
|
||||
{
|
||||
Keys: bson.D{{Key: "sortCode", Value: 1}},
|
||||
},
|
||||
{
|
||||
Keys: bson.D{{Key: "type", Value: 1}},
|
||||
},
|
||||
}
|
||||
if _, err := coll(nil).CreateIndex(many); err != nil {
|
||||
panic(fmt.Sprintf("%s model set index err ==>[%+v]", table, err))
|
||||
}
|
||||
}
|
||||
|
||||
// InsertOne 增加广告
|
||||
func InsertOne(t *db.MongoTool, doc InsertDoc) (ObjectID, error) {
|
||||
if doc.CreatedAt.IsZero() {
|
||||
doc.CreatedAt = time.Now()
|
||||
}
|
||||
res, err := coll(t).InsertOne(doc)
|
||||
if err != nil {
|
||||
log.Error(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "InsertOne", table, "InsertMany", err))
|
||||
return ObjectID{}, err
|
||||
}
|
||||
return res.InsertedID.(ObjectID), nil
|
||||
}
|
||||
|
||||
// DeleteOne 删除广告
|
||||
func DeleteOne(t *db.MongoTool, id ObjectID) error {
|
||||
if _, err := coll(t).DeleteOne(bson.M{"_id": id}); err != nil {
|
||||
log.Error(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "DeleteOne", table, "DeleteOne", err), log.Any("ids", id))
|
||||
if err == mongo.ErrNoDocuments {
|
||||
return ADSNotExistError{}
|
||||
}
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func updateOne(t *db.MongoTool, doc UpdateDoc, mats ...Matcher) error {
|
||||
filter := pageopt.MergeM(mats)
|
||||
now := time.Now()
|
||||
if doc.UpdatedAt.IsZero() {
|
||||
doc.UpdatedAt = now
|
||||
}
|
||||
res, err := coll(t).UpdateOne(filter, bson.M{"$set": doc})
|
||||
if err != nil {
|
||||
log.Error(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "UpdateAds", table, "UpdateOne", err), log.Any("doc", doc), log.Any("filter", filter))
|
||||
return err
|
||||
}
|
||||
if res.MatchedCount == 0 {
|
||||
return ADSNotExistError{}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func UpdateOneByID(t *db.MongoTool, id ObjectID, doc UpdateDoc) error {
|
||||
return updateOne(t, doc, (&IDMatch{ID: &id}).New())
|
||||
}
|
||||
|
||||
func findOne(mats ...Matcher) (Ads, error) {
|
||||
filter := pageopt.MergeM(mats)
|
||||
ads := Ads{}
|
||||
if err := coll(nil).FindOne(&ads, filter); err != nil {
|
||||
return Ads{}, err
|
||||
}
|
||||
if ads.ID.IsZero() {
|
||||
return Ads{}, ADSNotExistError{}
|
||||
}
|
||||
return ads, nil
|
||||
}
|
||||
|
||||
func FindOneByID(id ObjectID) (Ads, error) {
|
||||
return findOne((&IDMatch{ID: &id}).New())
|
||||
}
|
||||
|
||||
// IncAdsClick 增加广告一次点击
|
||||
func IncAdsClick(id primitive.ObjectID) error {
|
||||
filter := bson.M{"_id": id}
|
||||
update := bson.M{"$inc": bson.M{"click": 1}}
|
||||
if _, err := coll(nil).UpdateOne(filter, update); err != nil {
|
||||
log.Error(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "IncAdsClick", table, "UpdateOne", err), log.Any("id", id))
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
var ActiveIsTrue = true
|
||||
|
||||
// ActiveList ActiveList
|
||||
func ActiveList(sort Sort, skip, limit *int64, matchs ...Matcher) (AdsSlice, error) {
|
||||
matchs = append(matchs, (&ActiveMatch{Active: &ActiveIsTrue}).New())
|
||||
return List(sort, skip, limit, matchs...)
|
||||
}
|
||||
|
||||
// ActiveStartAndEndList ActiveStartAndEndList
|
||||
func ActiveStartAndEndList(sort Sort, skip, limit *int64, matchs ...Matcher) (AdsSlice, error) {
|
||||
now := time.Now()
|
||||
matchs = append(matchs,
|
||||
(&StartLTEMatch{t: now}).New(),
|
||||
(&EndGTMatch{t: now}).New(),
|
||||
)
|
||||
return ActiveList(sort, skip, limit, matchs...)
|
||||
}
|
||||
Reference in New Issue
Block a user