forked from all_user/stat_center
185 lines
5.3 KiB
Go
185 lines
5.3 KiB
Go
package applicationmod
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"stat/common/log"
|
|
"stat/common/mongos"
|
|
"stat/common/stdLog"
|
|
"time"
|
|
|
|
"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"
|
|
)
|
|
|
|
func Init(ctx context.Context) {
|
|
// 获取数据库表
|
|
db = mongos.New(mongos.MgoManager, Collapplication)
|
|
|
|
// 初始化索引
|
|
indexes := []mongo.IndexModel{
|
|
{
|
|
Keys: bson.D{{Key: "parentAppId", Value: 1}},
|
|
},
|
|
{
|
|
Keys: bson.D{{Key: "rank", Value: -1}},
|
|
},
|
|
{
|
|
Keys: bson.D{{Key: "locId", Value: -1}},
|
|
},
|
|
}
|
|
|
|
if _, err := db.CreateIndex(ctx, indexes); err != nil {
|
|
panic(fmt.Sprintf("Model %s InitIndex error %+v", Collapplication, err))
|
|
}
|
|
}
|
|
|
|
// InsertOneApplication 插入一个应用
|
|
func InsertOneApplication(ctx context.Context, Application *Application) *stdLog.SrvErr {
|
|
if _, err := db.InsertOne(ctx, Application); err != nil {
|
|
log.Warn("media:InsertOneApplication db.InsertOne failed", log.E(err))
|
|
return stdLog.New(stdLog.InsertDataError, "", "")
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// DelOneApplication 删除一个应用tag
|
|
func DelOneApplication(ctx context.Context, filter bson.M) (
|
|
int64, *stdLog.SrvErr) {
|
|
count, err := db.DeleteOne(ctx, filter)
|
|
if err != nil {
|
|
log.Warn("application:DelOneApplication db.DeleteOne failed",
|
|
log.E(err), log.Any("filter", filter))
|
|
return 0, stdLog.New(stdLog.DelDataError, "", "")
|
|
}
|
|
return count, nil
|
|
}
|
|
|
|
// UpdateOneApplication 更新一个应用
|
|
func UpdateOneApplication(ctx context.Context, filter bson.M, update bson.M) *stdLog.SrvErr {
|
|
result, err := db.UpdateOne(ctx, filter, update)
|
|
if err != nil {
|
|
log.Warn("application:UpdateOneApplication db.UpdateOne failed",
|
|
log.E(err), log.Any("filter", filter))
|
|
return stdLog.New(stdLog.UpdateDataError, "", "")
|
|
}
|
|
if result.MatchedCount != 1 {
|
|
log.Warn("application:UpdateOneApplication db.UpdateOne failed",
|
|
log.E(err), log.Any("filter", filter))
|
|
return stdLog.New(stdLog.ErrApplicationNotExist, "", "")
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// CountApplications 统计应用数量
|
|
func CountApplications(ctx context.Context, filter bson.M) (
|
|
int64, *stdLog.SrvErr) {
|
|
count, err := db.Count(ctx, filter)
|
|
if err != nil {
|
|
log.Warn("media:CountApplications db.Count failed",
|
|
log.E(err), log.Any("filter", filter))
|
|
return 0, stdLog.New(stdLog.QueryDataError, "", "")
|
|
}
|
|
return count, nil
|
|
}
|
|
|
|
// FindOneApplication 查找一个应用
|
|
func FindOneApplication(ctx context.Context, filter bson.M) (
|
|
*Application, *stdLog.SrvErr) {
|
|
var Application Application
|
|
if err := db.FindOneEx(
|
|
ctx, &Application, filter); err != nil {
|
|
if err != mongo.ErrNoDocuments {
|
|
log.Info("media:FindOneApplication db.FindOne failed",
|
|
log.E(err), log.Any("filter", filter))
|
|
return nil, stdLog.New(stdLog.QueryDataError, "", "")
|
|
}
|
|
return nil, stdLog.New(stdLog.ErrApplicationNotExist, "", "")
|
|
}
|
|
return &Application, nil
|
|
}
|
|
|
|
// FindManyApplication 查找一批应用
|
|
func FindManyApplication(ctx context.Context, filter bson.M,
|
|
opts ...*options.FindOptions) (ApplicationList, *stdLog.SrvErr) {
|
|
data := make(ApplicationList, 0)
|
|
if err := db.Find(ctx, &data, filter, opts...); err != nil {
|
|
log.Warn("application:FindManyApplication db.Find failed",
|
|
log.E(err), log.Any("filter", filter))
|
|
return nil, stdLog.New(stdLog.QueryDataError, "", "")
|
|
}
|
|
return data, nil
|
|
}
|
|
|
|
// 根据appId获取配置的app
|
|
func FindAppByAppId(ctx context.Context, appId int64) (data []Application, err error) {
|
|
now := time.Now().Local()
|
|
filter := bson.M{"parentAppId": appId, "status": true,
|
|
"startTime": bson.M{"$lt": now}, "endTime": bson.M{"$gte": now}}
|
|
|
|
sort := bson.D{{Key: "rank", Value: -1}}
|
|
|
|
opts := options.Find().SetSort(sort)
|
|
|
|
if err = db.Find(ctx, &data, filter, opts); err != nil {
|
|
log.Error("application FindAdsByAppId Find err", log.E(err))
|
|
return
|
|
}
|
|
return
|
|
}
|
|
|
|
// 增加广告点击量
|
|
func IncAppClickCount(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
|
|
}
|
|
|
|
// GetAllAdvertiseMap 获取ads id map
|
|
func GetAllAdvertiseMap(ctx context.Context) (map[primitive.ObjectID]*Application, error) {
|
|
filter := bson.M{}
|
|
dict := make(map[primitive.ObjectID]*Application)
|
|
list := make([]*Application, 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
|
|
}
|
|
|
|
// 根据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
|
|
}
|
|
|
|
// UpdateManyApplication 更新满足条件的应用
|
|
func UpdateManyApplication(ctx context.Context, filter bson.M, update bson.M) *stdLog.SrvErr {
|
|
_, err := db.UpdateMany(ctx, filter, update)
|
|
if err != nil {
|
|
log.Warn("application:UpdateManyApplication db.UpdateMany failed",
|
|
log.E(err), log.Any("filter", filter), log.Any("update", update))
|
|
return stdLog.New(stdLog.UpdateDataError, "", "")
|
|
}
|
|
return nil
|
|
}
|