164 lines
4.4 KiB
Go
164 lines
4.4 KiB
Go
package applicationLocMod
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"stat/common/log"
|
|
"stat/common/mongos"
|
|
"stat/common/stdLog"
|
|
"stat/common/utils"
|
|
|
|
"go.mongodb.org/mongo-driver/bson"
|
|
"go.mongodb.org/mongo-driver/mongo"
|
|
"go.mongodb.org/mongo-driver/mongo/options"
|
|
)
|
|
|
|
func Init(ctx context.Context) {
|
|
// 获取数据库表
|
|
db = mongos.New(mongos.MgoManager, CollApplicationLocation)
|
|
|
|
// 初始化索引
|
|
indexes := []mongo.IndexModel{
|
|
{
|
|
Keys: bson.D{{Key: "appId", Value: 1}, {Key: "locId", Value: 1}},
|
|
Options: options.Index().SetUnique(true),
|
|
},
|
|
}
|
|
|
|
if _, err := db.CreateIndex(ctx, indexes); err != nil {
|
|
panic(fmt.Sprintf("Model %s InitIndex error %+v", CollApplicationLocation, err))
|
|
}
|
|
}
|
|
|
|
// 新增广告位置信息
|
|
func AddApplicationLocation(ctx context.Context, doc *ApplicationLocation) (err error) {
|
|
if _, err = db.InsertOne(ctx, doc); err != nil {
|
|
log.Error("application location AddApplicationLocation InsertOne err", log.E(err))
|
|
return
|
|
}
|
|
return
|
|
}
|
|
|
|
// 修改广告位置信息就
|
|
func UpdateApplicationLocation(ctx context.Context, id int64, doc UpdateAppLoc) (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("application location UpdateApplicationLocation UpdateOne err", log.E(err))
|
|
return
|
|
}
|
|
|
|
return
|
|
}
|
|
|
|
// 删除广告位置信息就
|
|
func DelApplicationLocation(ctx context.Context, id int64) (err error) {
|
|
filter := bson.M{"_id": id}
|
|
|
|
if _, err = db.DeleteOne(ctx, filter); err != nil {
|
|
log.Error("application location DelApplicationLocation UpdateOne err", log.E(err))
|
|
return
|
|
}
|
|
|
|
return
|
|
}
|
|
|
|
// 删除广告位置信息就
|
|
func FindApplicationLocationList(ctx context.Context, appId *int32, skip, limit int64) (count int64, data []ApplicationLocation, 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("application location FindApplicationLocationList Find err", log.E(err))
|
|
return
|
|
}
|
|
|
|
if count, err = db.Count(ctx, filter); err != nil {
|
|
log.Error("application location FindApplicationLocationList Count err", log.E(err))
|
|
return
|
|
}
|
|
|
|
return
|
|
}
|
|
|
|
// 根据应用id和位置id获取广告位置
|
|
func FindLocationByAppIdAndLocId(ctx context.Context, appId, locId int32) (data ApplicationLocation, err error) {
|
|
filter := bson.M{"appId": appId, "locId": locId}
|
|
|
|
if err = db.FindOne(ctx, &data, filter); err != nil {
|
|
log.Error("application location FindLocationByAppIdAndLocId FindOne err", log.E(err))
|
|
return
|
|
}
|
|
|
|
return
|
|
}
|
|
|
|
// 根据id获取广告位置
|
|
func FindLocationById(ctx context.Context, id int64) (data ApplicationLocation, err error) {
|
|
filter := bson.M{"_id": id}
|
|
|
|
if err = db.FindOne(ctx, &data, filter); err != nil {
|
|
log.Error("application location FindLocationById FindOne err", log.E(err))
|
|
return
|
|
}
|
|
|
|
return
|
|
}
|
|
|
|
// GetAllApplicationLocationMap 获取 map[appId]map[locId]*ApplicationLocation
|
|
func GetAllApplicationLocationMap(ctx context.Context, filter bson.M) (map[int32]map[int32]*ApplicationLocation, error) {
|
|
dict := make(map[int32]map[int32]*ApplicationLocation)
|
|
list := make([]*ApplicationLocation, 0)
|
|
if err := db.Find(ctx, &list, filter); err != nil {
|
|
log.Error("ApplicationLocation GetAllApplicationMap Find err", log.E(err))
|
|
return dict, err
|
|
}
|
|
for _, loc := range list {
|
|
m := dict[loc.AppId]
|
|
if m == nil {
|
|
m = make(map[int32]*ApplicationLocation)
|
|
m[loc.LocId] = loc
|
|
dict[loc.AppId] = m
|
|
} else {
|
|
m[loc.LocId] = loc
|
|
}
|
|
}
|
|
return dict, nil
|
|
}
|
|
|
|
func GetAppLocIdNameMap(ctx context.Context) (map[int64]ApplicationLocation, *stdLog.SrvErr) {
|
|
list := make([]ApplicationLocation, 0)
|
|
if err := db.Find(ctx, &list, bson.M{}); err != nil {
|
|
log.Warn("media:GetAppLocIdNameMap db.Find failed", log.E(err))
|
|
return nil, stdLog.New(stdLog.QueryDataError, "", "")
|
|
}
|
|
dict := make(map[int64]ApplicationLocation)
|
|
for _, item := range list {
|
|
dict[int64(item.LocId)] = item
|
|
}
|
|
return dict, nil
|
|
}
|
|
|
|
func FindApplicationLocationDict(ctx context.Context, appId int64) map[int64]ApplicationLocation {
|
|
resp := make(map[int64]ApplicationLocation)
|
|
list := make([]ApplicationLocation, 0)
|
|
if err := db.Find(ctx, &list, bson.M{
|
|
"appId": appId,
|
|
}); err != nil {
|
|
return resp
|
|
}
|
|
for _, item := range list {
|
|
resp[int64(item.LocId)] = item
|
|
}
|
|
return resp
|
|
}
|