forked from all_user/stat_center
151 lines
4.4 KiB
Go
151 lines
4.4 KiB
Go
package blocDailystatmod
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"stat/common/log"
|
|
"stat/common/mongos"
|
|
"time"
|
|
|
|
"go.mongodb.org/mongo-driver/bson"
|
|
"go.mongodb.org/mongo-driver/mongo"
|
|
"go.mongodb.org/mongo-driver/mongo/options"
|
|
)
|
|
|
|
// 初始化model
|
|
func Init() {
|
|
// 获取数据库表
|
|
db = mongos.New(mongos.MgoManager, CollBlocDailyStat)
|
|
|
|
indexes := []mongo.IndexModel{
|
|
{
|
|
Keys: bson.D{{Key: "sumDate", Value: 1}},
|
|
},
|
|
}
|
|
|
|
if _, err := db.CreateIndex(context.Background(), indexes); err != nil {
|
|
panic(fmt.Sprintf("Model %s InitIndex error %+v", CollBlocDailyStat, err))
|
|
}
|
|
}
|
|
|
|
// UpsertDailyStat 更新或插入一条统计记录
|
|
func UpsertBlocDailyStat(ctx context.Context, sumDate time.Time, appId int32, doc BlocDailyStatDoc) (err error) {
|
|
var filter = bson.M{
|
|
"sumDate": sumDate,
|
|
"appId": appId,
|
|
}
|
|
var update = mongos.ToBsonM(doc)
|
|
|
|
exists, err := db.Exists(ctx, filter)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
if exists {
|
|
_, err = db.UpdateOne(ctx, filter, bson.M{"$set": update})
|
|
} else {
|
|
insertDoc := update
|
|
insertDoc["sumDate"] = sumDate
|
|
insertDoc["appId"] = appId
|
|
|
|
_, err = db.InsertOne(ctx, insertDoc)
|
|
}
|
|
|
|
return
|
|
}
|
|
|
|
// 获取日常统计列表
|
|
func GetBlocDailyStatList(ctx context.Context, appId *int32, start, end *time.Time, skip, limit int64) (count int64, data []BlocDailyStatMod, err error) {
|
|
filter := bson.M{}
|
|
if appId != nil {
|
|
filter["appId"] = appId
|
|
}
|
|
if start != nil && end != nil {
|
|
filter["sumDate"] = bson.M{"$gte": start, "$lt": end}
|
|
} else if start != nil {
|
|
filter["sumDate"] = bson.M{"$gte": start}
|
|
} else if end != nil {
|
|
filter["sumDate"] = bson.M{"$lt": end}
|
|
}
|
|
|
|
sort := bson.D{{Key: "sumDate", Value: -1}}
|
|
|
|
opts := options.Find().SetSort(sort).SetSkip(skip).SetLimit(limit)
|
|
|
|
if err = db.Find(ctx, &data, filter, opts); err != nil {
|
|
log.Error("channel GetDailyStatList Find err", log.E(err))
|
|
return
|
|
}
|
|
|
|
if count, err = db.Count(ctx, filter); err != nil {
|
|
log.Error("channel GetDailyStatList Count err", log.E(err))
|
|
return
|
|
}
|
|
|
|
return
|
|
}
|
|
|
|
// 获取日常统计列表
|
|
func GetBlocDailyStatListTotal(ctx context.Context, appId *int32, start, end *time.Time) (data []BlocDailyStatMod, err error) {
|
|
filter := bson.M{}
|
|
if appId != nil {
|
|
filter["appId"] = appId
|
|
}
|
|
if start != nil && end != nil {
|
|
filter["sumDate"] = bson.M{"$gte": start, "$lt": end}
|
|
} else if start != nil {
|
|
filter["sumDate"] = bson.M{"$gte": start}
|
|
} else if end != nil {
|
|
filter["sumDate"] = bson.M{"$lt": end}
|
|
}
|
|
|
|
sort := bson.D{{Key: "sumDate", Value: -1}}
|
|
|
|
opts := options.Find().SetSort(sort)
|
|
|
|
if err = db.Find(ctx, &data, filter, opts); err != nil {
|
|
log.Error("channel GetDailyStatList Find err", log.E(err))
|
|
return
|
|
}
|
|
|
|
return
|
|
}
|
|
|
|
func GetBlocDailyStatTotalList(ctx context.Context, start, end *time.Time) (data []BlocDailyStatMod, err error) {
|
|
|
|
match := bson.M{}
|
|
if start != nil && end != nil {
|
|
match["sumDate"] = bson.M{"$gte": start, "$lt": end}
|
|
}
|
|
|
|
pipelines := []bson.M{
|
|
{"$match": match},
|
|
{"$group": bson.M{"_id": "$sumDate",
|
|
"sumDate": bson.M{"$first": "$sumDate"},
|
|
"newDirectCount": bson.M{"$sum": "$newDirectCount"},
|
|
"newFissionCount": bson.M{"$sum": "$newFissionCount"},
|
|
"directVisitCount": bson.M{"$sum": "$directVisitCount"},
|
|
"fissionVisitCount": bson.M{"$sum": "$fissionVisitCount"},
|
|
"directAmount": bson.M{"$sum": "$directAmount"},
|
|
"fissionAmount": bson.M{"$sum": "$fissionAmount"},
|
|
"directAmountcount": bson.M{"$sum": "$directAmountcount"},
|
|
"fissionAmountcount": bson.M{"$sum": "$fissionAmountcount"},
|
|
"deductionAmountCount": bson.M{"$sum": "$deductionAmountCount"},
|
|
"newRechargeCount": bson.M{"$sum": "$newRechargeCount"},
|
|
"newRechargeAmount": bson.M{"$sum": "$newRechargeAmount"},
|
|
"daichongAmout": bson.M{"$sum": "$daichongAmout"},
|
|
"onlineAmount": bson.M{"$sum": "$onlineAmount"},
|
|
"userWithDrawAmount": bson.M{"$sum": "$userWithDrawAmount"},
|
|
"userWithDrawPaymoneyAmount": bson.M{"$sum": "$userWithDrawPaymoneyAmount"},
|
|
"gameRechageAmount": bson.M{"$sum": "$gameRechageAmount"},
|
|
"gameWithdrawAmount": bson.M{"$sum": "$gameWithdrawAmount"},
|
|
}},
|
|
{"$sort": bson.D{{Key: "sumDate", Value: -1}}},
|
|
}
|
|
if err = db.Aggregate(ctx, &data, pipelines); err != nil {
|
|
log.Error("channel GetDailyStatList Find err", log.E(err))
|
|
return
|
|
}
|
|
return
|
|
}
|