Initial commit
This commit is contained in:
@@ -0,0 +1,149 @@
|
||||
package advertisestat
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"stat/common/log"
|
||||
"stat/common/mongos"
|
||||
"stat/common/stdLog"
|
||||
|
||||
"go.mongodb.org/mongo-driver/bson"
|
||||
"go.mongodb.org/mongo-driver/mongo"
|
||||
"go.mongodb.org/mongo-driver/mongo/options"
|
||||
)
|
||||
|
||||
// 表名
|
||||
|
||||
var db *mongos.MongoTool
|
||||
|
||||
func Init() {
|
||||
// 获取数据库表
|
||||
db = mongos.New(mongos.MgoManager, TN.AdvertiseStat)
|
||||
// 获取数据库表
|
||||
indexes := []mongo.IndexModel{
|
||||
{
|
||||
Keys: bson.D{
|
||||
{Key: FN.AdvertiseStat.AdsId, Value: 1},
|
||||
{Key: FN.AdvertiseStat.SumData, Value: -1},
|
||||
},
|
||||
Options: options.Index().SetUnique(true),
|
||||
},
|
||||
{
|
||||
Keys: bson.D{
|
||||
{Key: FN.AdvertiseStat.Id, Value: 1},
|
||||
},
|
||||
},
|
||||
{
|
||||
Keys: bson.D{
|
||||
{Key: FN.AdvertiseStat.AppId, Value: 1},
|
||||
},
|
||||
},
|
||||
{
|
||||
Keys: bson.D{
|
||||
{Key: FN.AdvertiseStat.SumData, Value: -1},
|
||||
},
|
||||
},
|
||||
{
|
||||
Keys: bson.D{
|
||||
{Key: FN.AdvertiseStat.Total, Value: -1},
|
||||
},
|
||||
},
|
||||
}
|
||||
if _, err := db.CreateIndex(context.Background(), indexes); err != nil {
|
||||
panic(fmt.Sprintf("(media:initAdvertiseStatIndex) Create AdvertiseStat Index failed! err:%+v:", err))
|
||||
}
|
||||
}
|
||||
|
||||
// InsertOneAdvertiseStat 插入一个广告统计
|
||||
func InsertOneAdvertiseStat(ctx context.Context, applicationStat *AdvertiseStat) error {
|
||||
if _, err := db.InsertOne(ctx, applicationStat); err != nil {
|
||||
log.Warn("media:InsertOneAdvertiseStat db.InsertOne failed", log.E(err))
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// DelOneAdvertiseStat 删除一个广告统计tag
|
||||
func DelOneAdvertiseStat(ctx context.Context, filter bson.M) (int64, error) {
|
||||
count, err := db.DeleteOne(ctx, filter)
|
||||
if err != nil {
|
||||
log.Warn("media:DelOneAdvertiseStat db.DeleteOne failed", log.E(err), log.Any("filter", filter))
|
||||
return 0, err
|
||||
}
|
||||
return count, nil
|
||||
}
|
||||
|
||||
// FindOneAdvertiseStat 查找一个广告统计
|
||||
func FindOneAdvertiseStat(ctx context.Context, filter bson.M) (*AdvertiseStat, error) {
|
||||
var applicationStat AdvertiseStat
|
||||
if err := db.FindOneEx(ctx, &applicationStat, filter); err != nil { //mongo.ErrNoDocuments
|
||||
return nil, err
|
||||
}
|
||||
return &applicationStat, nil
|
||||
}
|
||||
|
||||
// CountAdvertiseStats 统计广告统计数量
|
||||
func CountAdvertiseStats(ctx context.Context, filter bson.M) (int64, error) {
|
||||
count, err := db.Count(ctx, filter)
|
||||
if err != nil {
|
||||
log.Warn("media:CountAdvertiseStats db.Count failed",
|
||||
log.E(err), log.Any("filter", filter))
|
||||
return 0, err
|
||||
}
|
||||
return count, nil
|
||||
}
|
||||
|
||||
// UpdateOneAdvertiseStat 更新一个广告统计
|
||||
func UpdateOneAdvertiseStat(ctx context.Context, filter bson.M, update bson.M) error {
|
||||
result, err := db.UpdateOne(ctx, filter, update)
|
||||
if err != nil {
|
||||
log.Warn("media:UpdateOneAdvertiseStat db.UpdateOne failed",
|
||||
log.E(err), log.Any("filter", filter))
|
||||
return err
|
||||
}
|
||||
if result.MatchedCount != 1 {
|
||||
log.Warn("media:UpdateOneAdvertiseStat db.UpdateOne failed",
|
||||
log.E(err), log.Any("filter", filter))
|
||||
return mongo.ErrNoDocuments
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// UpdateManyAdvertiseStat 更新一批广告统计
|
||||
func UpdateManyAdvertiseStat(ctx context.Context, filter bson.M, update bson.M) error {
|
||||
result, err := db.UpdateMMany(ctx, filter, update)
|
||||
if err != nil {
|
||||
log.Warn("media:UpdateManyAdvertiseStat db.UpdateMMany failed",
|
||||
log.E(err), log.Any("filter", filter))
|
||||
return err
|
||||
}
|
||||
if result.MatchedCount <= 0 {
|
||||
log.Warn("media:UpdateManyAdvertiseStat db.UpdateMMany failed",
|
||||
log.E(err), log.Any("filter", filter))
|
||||
//return srvdef.New(srvdef.ErrApplicationNotExsit, "", "")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// AggregateTopicInfos 聚合查找一批用户信息
|
||||
func AggregateAdvertiseStat(ctx context.Context,
|
||||
bind interface{}, pipeline []bson.M) *stdLog.SrvErr {
|
||||
if err := db.Aggregate(ctx, bind, pipeline); err != nil {
|
||||
log.Warn("feedback:AggregateAdvertiseStat db.Aggregate failed",
|
||||
log.E(err), log.Any("pipeline", pipeline))
|
||||
return stdLog.New(stdLog.QueryDataError, "", err.Error())
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// FindManyAdvertiseStat 查找一批广告统计
|
||||
func FindManyAdvertiseStat(ctx context.Context, filter bson.M,
|
||||
opts ...*options.FindOptions) (AdvertiseStatList, error) {
|
||||
data := make(AdvertiseStatList, 0)
|
||||
if err := db.Find(ctx, &data, filter, opts...); err != nil {
|
||||
log.Warn("media:FindManyAdvertiseStat db.Find failed",
|
||||
log.E(err), log.Any("filter", filter))
|
||||
return nil, err
|
||||
}
|
||||
return data, nil
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
package advertisestat
|
||||
|
||||
type TagAdvertiseStat struct {
|
||||
Id string
|
||||
AdsId string
|
||||
AppId string
|
||||
SumData string
|
||||
Total string
|
||||
HourData string
|
||||
CreatedAt string
|
||||
UpdatedAt string
|
||||
}
|
||||
|
||||
var FN struct {
|
||||
AdvertiseStat TagAdvertiseStat
|
||||
}
|
||||
|
||||
var TN struct {
|
||||
AdvertiseStat string
|
||||
}
|
||||
|
||||
func init() {
|
||||
FN.AdvertiseStat.Id = "_id"
|
||||
FN.AdvertiseStat.AdsId = "adsId"
|
||||
FN.AdvertiseStat.AppId = "appId"
|
||||
FN.AdvertiseStat.SumData = "sumDate"
|
||||
FN.AdvertiseStat.Total = "total"
|
||||
FN.AdvertiseStat.HourData = "hourData"
|
||||
FN.AdvertiseStat.CreatedAt = "createdAt"
|
||||
FN.AdvertiseStat.UpdatedAt = "updatedAt"
|
||||
TN.AdvertiseStat = "advertisestat"
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
package advertisestat
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"go.mongodb.org/mongo-driver/bson/primitive"
|
||||
)
|
||||
|
||||
const ColladvertiseStat = "advertisestat"
|
||||
|
||||
type THourStat map[int]int64
|
||||
|
||||
func (m THourStat) Total() int64 {
|
||||
var sum int64
|
||||
for _, v := range m {
|
||||
sum += v
|
||||
}
|
||||
return sum
|
||||
}
|
||||
|
||||
type Update struct {
|
||||
Total *int64 `json:"total" bson:"total,omitempty"` //今日点击
|
||||
HourData *THourStat `json:"hourData" bson:"hourData,omitempty"` //小时数据
|
||||
UpdatedAt *time.Time `json:"updatedAt" bson:"updatedAt,omitempty"` //更新时间
|
||||
}
|
||||
|
||||
// 应用统计
|
||||
// @table:advertisejob
|
||||
type AdvertiseStat struct {
|
||||
Id primitive.ObjectID `json:"id" bson:"_id,omitempty"`
|
||||
AdsId int64 `json:"adsId" bson:"adsId"` //广告ID
|
||||
AppId int32 `json:"appId" bson:"appId"` //应用ID
|
||||
SumDate time.Time `json:"sumDate" bson:"sumDate"` //记录时间
|
||||
Total int64 `json:"total" bson:"total"` //今日点击
|
||||
HourData THourStat `json:"hourData" bson:"hourData"` //小时数据
|
||||
CreatedAt time.Time `json:"createdAt" bson:"createdAt"` //创建时间
|
||||
UpdatedAt time.Time `json:"updatedAt" bson:"updatedAt"` //更新时间
|
||||
}
|
||||
|
||||
type AdvertiseStatList []*AdvertiseStat
|
||||
Reference in New Issue
Block a user