forked from all_user/stat_center
Initial commit
This commit is contained in:
@@ -0,0 +1,135 @@
|
||||
package advertiseLocMod
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"stat/common/log"
|
||||
"stat/common/mongos"
|
||||
"stat/common/utils"
|
||||
|
||||
"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, CollAdvertiseLocation)
|
||||
|
||||
// 初始化索引
|
||||
indexes := []mongo.IndexModel{
|
||||
{
|
||||
Keys: bson.D{{Key: "appId", Value: 1}, {Key: "locId", Value: 1}},
|
||||
Options: options.Index().SetUnique(true),
|
||||
},
|
||||
}
|
||||
|
||||
if _, err := db.CreateIndex(context.Background(), indexes); err != nil {
|
||||
panic(fmt.Sprintf("Model %s InitIndex error %+v", CollAdvertiseLocation, err))
|
||||
}
|
||||
}
|
||||
|
||||
// 新增广告位置信息
|
||||
func AddAdvertiseLocation(ctx context.Context, doc *AdvertiseLocation) (err error) {
|
||||
if _, err = db.InsertOne(ctx, doc); err != nil {
|
||||
log.Error("advertise location AddAdvertiseLocation InsertOne err", log.E(err))
|
||||
return
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// 修改广告位置信息就
|
||||
func UpdateAdvertiseLocation(ctx context.Context, id int64, doc UpdateAdsLoc) (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 location UpdateAdvertiseLocation UpdateOne err", log.E(err))
|
||||
return
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
// 删除广告位置信息就
|
||||
func DelAdvertiseLocation(ctx context.Context, id int64) (err error) {
|
||||
filter := bson.M{"_id": id}
|
||||
|
||||
if _, err = db.DeleteOne(ctx, filter); err != nil {
|
||||
log.Error("advertise location DelAdvertiseLocation UpdateOne err", log.E(err))
|
||||
return
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
// 删除广告位置信息就
|
||||
func FindAdvertiseLocationList(ctx context.Context, appId *int32, skip, limit int64) (count int64, data []AdvertiseLocation, err error) {
|
||||
filter := bson.M{}
|
||||
|
||||
if appId != nil {
|
||||
filter["appId"] = appId
|
||||
}
|
||||
|
||||
sort := bson.D{{Key: "createdAt", Value: -1}}
|
||||
|
||||
opts := options.Find().SetSort(sort).SetSkip(skip).SetLimit(limit)
|
||||
|
||||
if err = db.Find(ctx, &data, filter, opts); err != nil {
|
||||
log.Error("advertise location FindAdvertiseLocationList Find err", log.E(err))
|
||||
return
|
||||
}
|
||||
|
||||
if count, err = db.Count(ctx, filter); err != nil {
|
||||
log.Error("advertise location FindAdvertiseLocationList Count err", log.E(err))
|
||||
return
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
// 根据应用id和位置id获取广告位置
|
||||
func FindLocationByAppIdAndLocId(ctx context.Context, appId, locId int32) (data AdvertiseLocation, err error) {
|
||||
filter := bson.M{"appId": appId, "locId": locId}
|
||||
|
||||
if err = db.FindOne(ctx, &data, filter); err != nil {
|
||||
log.Error("advertise location FindLocationByAppIdAndLocId FindOne err", log.E(err))
|
||||
return
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
// 根据id获取广告位置
|
||||
func FindLocationById(ctx context.Context, id int64) (data AdvertiseLocation, err error) {
|
||||
filter := bson.M{"_id": id}
|
||||
|
||||
if err = db.FindOne(ctx, &data, filter); err != nil {
|
||||
log.Error("advertise location FindLocationById FindOne err", log.E(err))
|
||||
return
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
// GetAllAdvertiseLocationMap 获取 map[appId]map[locId]*AdvertiseLocation
|
||||
func GetAllAdvertiseLocationMap(ctx context.Context, filter bson.M) (map[int32]map[int32]*AdvertiseLocation, error) {
|
||||
dict := make(map[int32]map[int32]*AdvertiseLocation)
|
||||
list := make([]*AdvertiseLocation, 0)
|
||||
if err := db.Find(ctx, &list, filter); err != nil {
|
||||
log.Error("AdvertiseLocation GetAllAdvertiseMap Find err", log.E(err))
|
||||
return dict, err
|
||||
}
|
||||
for _, loc := range list {
|
||||
m := dict[loc.AppId]
|
||||
if m == nil {
|
||||
m = make(map[int32]*AdvertiseLocation)
|
||||
m[loc.LocId] = loc
|
||||
dict[loc.AppId] = m
|
||||
} else {
|
||||
m[loc.LocId] = loc
|
||||
}
|
||||
}
|
||||
return dict, nil
|
||||
}
|
||||
Reference in New Issue
Block a user