forked from all_user/stat_center
Initial commit
This commit is contained in:
@@ -0,0 +1,220 @@
|
||||
package advertiseMod
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"stat/common/log"
|
||||
"stat/common/mongos"
|
||||
"stat/common/stdLog"
|
||||
"stat/common/utils"
|
||||
"time"
|
||||
|
||||
"go.mongodb.org/mongo-driver/bson"
|
||||
"go.mongodb.org/mongo-driver/mongo"
|
||||
"go.mongodb.org/mongo-driver/mongo/options"
|
||||
)
|
||||
|
||||
func Init() {
|
||||
// 获取数据库表
|
||||
db = mongos.New(mongos.MgoManager, CollAdvertise)
|
||||
|
||||
// 初始化索引
|
||||
indexes := []mongo.IndexModel{
|
||||
{
|
||||
Keys: bson.D{{Key: "appId", Value: 1}},
|
||||
},
|
||||
{
|
||||
Keys: bson.D{{Key: "appId", Value: 1}, {Key: "isActive", Value: 1}, {Key: "startTime", Value: 1}, {Key: "endTime", Value: 1}},
|
||||
},
|
||||
}
|
||||
|
||||
if _, err := db.CreateIndex(context.Background(), indexes); err != nil {
|
||||
panic(fmt.Sprintf("Model %s InitIndex error %+v", CollAdvertise, err))
|
||||
}
|
||||
}
|
||||
|
||||
// 新增广告
|
||||
func AddAdvertise(ctx context.Context, doc *Advertise) (err error) {
|
||||
if _, err = db.InsertOne(ctx, doc); err != nil {
|
||||
log.Error("advertise AddAdvertise InsertOne err", log.E(err))
|
||||
return
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// 修改广告
|
||||
func UpdateAdvertise(ctx context.Context, id int64, doc UpdateAds) (err error) {
|
||||
filter := bson.M{"_id": id}
|
||||
|
||||
update, _ := utils.ToBsonM(doc)
|
||||
|
||||
if _, err = db.UpdateOne(ctx, filter, bson.M{"$set": update}); err != nil {
|
||||
log.Error("advertise UpdateAdvertise UpdateOne err", log.E(err))
|
||||
return
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
// 删除广告
|
||||
func DelAdvertise(ctx context.Context, id int64) (err error) {
|
||||
filter := bson.M{"_id": id}
|
||||
|
||||
if _, err = db.DeleteOne(ctx, filter); err != nil {
|
||||
log.Error("advertise DelAdvertise DeleteOne err", log.E(err))
|
||||
return
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
func FindAdvertiseList(ctx context.Context, appId *int32, isActive *bool, locId *int32, skip, limit int64,
|
||||
link, title, daoLiangTitle *string) (count int64, data []Advertise, err error) {
|
||||
filter := bson.M{}
|
||||
if appId != nil {
|
||||
filter["appId"] = appId
|
||||
}
|
||||
if isActive != nil {
|
||||
filter["isActive"] = isActive
|
||||
}
|
||||
if locId != nil {
|
||||
filter["locId"] = *locId
|
||||
}
|
||||
if link != nil {
|
||||
filter["links"] = bson.M{"$elemMatch": bson.M{"link": *link}}
|
||||
}
|
||||
if title != nil {
|
||||
filter["title"] = bson.M{"$regex": *title}
|
||||
}
|
||||
if daoLiangTitle != nil {
|
||||
filter["daoLiangTitle"] = bson.M{"$regex": *daoLiangTitle}
|
||||
}
|
||||
sort := bson.D{{Key: "sort", Value: 1}}
|
||||
|
||||
opts := options.Find().SetSort(sort).SetSkip(skip).SetLimit(limit)
|
||||
|
||||
if err = db.Find(ctx, &data, filter, opts); err != nil {
|
||||
log.Error("advertise FindAdvertiseList Find err", log.E(err))
|
||||
return
|
||||
}
|
||||
|
||||
if count, err = db.Count(ctx, filter); err != nil {
|
||||
log.Error("advertise FindAdvertiseList Count err", log.E(err))
|
||||
return
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
// 根据appId和locId更新位置id
|
||||
func UpdateLocInfo(ctx context.Context, appId, oldLocId, newLocID int32) (err error) {
|
||||
filter := bson.M{"appId": appId, "locId": oldLocId}
|
||||
|
||||
update := bson.M{"$set": bson.M{"locId": newLocID}}
|
||||
|
||||
if _, err = db.UpdateMany(ctx, filter, update); err != nil {
|
||||
log.Error("advertise UpdateLocInfo UpdateMany err", log.E(err))
|
||||
return
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// 根据app和位置获取广告
|
||||
func FindAdsByAppAndLocation(ctx context.Context, appId int32, locId *int32) (data []Advertise, err error) {
|
||||
now := time.Now()
|
||||
filter := bson.M{"appId": appId, "isActive": true, "startTime": bson.M{"$lt": now}, "endTime": bson.M{"$gte": now}}
|
||||
if locId != nil {
|
||||
filter["locId"] = locId
|
||||
}
|
||||
|
||||
sort := bson.D{{Key: "sort", Value: 1}}
|
||||
|
||||
opts := options.Find().SetSort(sort)
|
||||
|
||||
if err = db.Find(ctx, &data, filter, opts); err != nil {
|
||||
log.Error("advertise FindAdsByAppAndLocation Find err", log.E(err))
|
||||
return
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// 根据app和位置获取广告
|
||||
func FindAdsById(ctx context.Context, id int64) (data Advertise, err error) {
|
||||
filter := bson.M{"_id": id}
|
||||
|
||||
if err = db.FindOne(ctx, &data, filter); err != nil {
|
||||
log.Error("advertise FindAdsById FindOne err", log.E(err))
|
||||
return
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
// 增加广告点击量
|
||||
func IncAdsClickCount(ctx context.Context, id int64) (err error) {
|
||||
filter := bson.M{"_id": id}
|
||||
|
||||
update := bson.M{"$inc": bson.M{"clickCount": 1}}
|
||||
|
||||
if _, err = db.UpdateOne(ctx, filter, update); err != nil {
|
||||
log.Error("advertise IncAdsClickCount UpdateOne err", log.E(err))
|
||||
return
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
// FindAds 根据app和位置获取广告
|
||||
func FindAds(ctx context.Context) (data []Advertise, err error) {
|
||||
filter := bson.M{}
|
||||
|
||||
sort := bson.D{{Key: "sort", Value: 1}}
|
||||
|
||||
opts := options.Find().SetSort(sort)
|
||||
|
||||
if err = db.Find(ctx, &data, filter, opts); err != nil {
|
||||
log.Error("advertise FindAdsByAppAndLocation Find err", log.E(err))
|
||||
return
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// FindOneAdvertise 查找一个应用
|
||||
func FindOneAdvertise(ctx context.Context, filter bson.M) (
|
||||
*Advertise, error) {
|
||||
var advertise Advertise
|
||||
if err := db.FindOneEx(ctx, &advertise, filter); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &advertise, nil
|
||||
}
|
||||
|
||||
// GetAllAdvertiseMap 获取ads id map
|
||||
func GetAllAdvertiseMap(ctx context.Context, filter bson.M) (map[int64]*Advertise, error) {
|
||||
dict := make(map[int64]*Advertise)
|
||||
list := make([]*Advertise, 0)
|
||||
if err := db.Find(ctx, &list, filter); err != nil {
|
||||
log.Error("Advertise GetAllAdvertiseMap Find err", log.E(err))
|
||||
return dict, err
|
||||
}
|
||||
for _, ads := range list {
|
||||
dict[ads.Id] = ads
|
||||
}
|
||||
return dict, nil
|
||||
}
|
||||
|
||||
// FindAds 根据app和位置获取广告
|
||||
func FindAdvertiselist(ctx context.Context, filter bson.M, opts ...*options.FindOptions) (data []Advertise, serr *stdLog.SrvErr) {
|
||||
|
||||
if err := db.Find(ctx, &data, filter, opts...); err != nil {
|
||||
if err != mongo.ErrNoDocuments {
|
||||
log.Error("advertise FindAdvertiselist err", log.E(err))
|
||||
return nil, stdLog.New(stdLog.DataEmpty, "", err.Error())
|
||||
}
|
||||
|
||||
log.Error("advertise FindAdvertiselist Find err", log.E(err))
|
||||
return nil, stdLog.New(stdLog.QueryDataError, "", err.Error())
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
Reference in New Issue
Block a user