forked from all_user/stat_center
446 lines
12 KiB
Go
446 lines
12 KiB
Go
package channelCodeBlocStatMod
|
|
|
|
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"
|
|
)
|
|
|
|
func Init() {
|
|
db = mongos.New(mongos.MgoManager, CollChannelCodeBlocStat)
|
|
|
|
indexes := []mongo.IndexModel{
|
|
{
|
|
Keys: bson.D{{Key: "channelCode", Value: 1}, {Key: "appId", Value: 1}, {Key: "sumDate", Value: 1}},
|
|
Options: options.Index().SetUnique(true),
|
|
},
|
|
{
|
|
Keys: bson.D{{Key: "channel", Value: 1}},
|
|
},
|
|
{
|
|
Keys: bson.D{{Key: "appId", Value: 1}},
|
|
},
|
|
}
|
|
|
|
if _, err := db.CreateIndex(context.Background(), indexes); err != nil {
|
|
panic(fmt.Sprintf("Model %s InitIndex error %+v", CollChannelCodeBlocStat, err))
|
|
}
|
|
}
|
|
|
|
// UpsertOneUserRegisterStat 更新或插入一条用户注册信息统计信息
|
|
func UpsertOneUserRegisterStat(ctx context.Context, code string, appId int32, sumDate time.Time, direct, fission, deduction int64) (err error) {
|
|
filter := bson.M{"channelCode": code, "appId": appId, "sumDate": sumDate}
|
|
|
|
update := bson.M{}
|
|
if direct > 0 {
|
|
update["newDirectCount"] = direct
|
|
}
|
|
if fission > 0 {
|
|
update["newFissionCount"] = fission
|
|
}
|
|
if deduction > 0 {
|
|
update["newDeductionCount"] = deduction
|
|
}
|
|
|
|
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
|
|
insertDoc["channelCode"] = code
|
|
|
|
_, err = db.InsertOne(ctx, insertDoc)
|
|
}
|
|
|
|
return err
|
|
}
|
|
|
|
// UpsertOneUserRechargeStat 更新或插入一条用户消费统计信息
|
|
func UpsertOneUserRechargeStat(ctx context.Context, code string, appId int32, sumDate time.Time, rechargeAmount, deductionAmount, fissionAmount, newRechargeAmount int64) (err error) {
|
|
filter := bson.M{"channelCode": code, "appId": appId, "sumDate": sumDate}
|
|
|
|
update := bson.M{}
|
|
if rechargeAmount > 0 {
|
|
update["directAmount"] = rechargeAmount
|
|
}
|
|
if deductionAmount > 0 {
|
|
update["deductionAmount"] = deductionAmount
|
|
}
|
|
if fissionAmount > 0 {
|
|
update["fissionAmount"] = fissionAmount
|
|
}
|
|
|
|
if newRechargeAmount > 0 {
|
|
update["newRechargeAmount"] = newRechargeAmount
|
|
}
|
|
|
|
exists, err := db.Exists(ctx, filter)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
if exists {
|
|
_, err = db.UpdateOne(ctx, filter, bson.M{"$inc": update})
|
|
} else {
|
|
insertDoc := update
|
|
insertDoc["sumDate"] = sumDate
|
|
insertDoc["appId"] = appId
|
|
insertDoc["channelCode"] = code
|
|
|
|
_, err = db.InsertOne(ctx, insertDoc)
|
|
}
|
|
|
|
return err
|
|
}
|
|
|
|
// UpsertOneDistrictStat 更新渠道的留存
|
|
func UpsertOneDistrictStat(ctx context.Context, code string, appId int32, sumDate time.Time, day1Rate, day3Rate, day7Rate *float64) (err error) {
|
|
filter := bson.M{"channelCode": code, "appId": appId, "sumDate": sumDate}
|
|
|
|
update := bson.M{}
|
|
if day1Rate != nil {
|
|
update["day1RetainRate"] = *day1Rate
|
|
}
|
|
if day3Rate != nil {
|
|
update["day3RetainRate"] = *day3Rate
|
|
}
|
|
|
|
if day7Rate != nil {
|
|
update["day7RetainRate"] = *day7Rate
|
|
}
|
|
|
|
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
|
|
insertDoc["channelCode"] = code
|
|
|
|
_, err = db.InsertOne(ctx, insertDoc)
|
|
}
|
|
|
|
return err
|
|
}
|
|
|
|
// UpsertOneDistrictVisitStat 更新渠道的留存
|
|
func UpsertOneDistrictVisitStat(ctx context.Context, code string, appId int32, sumDate time.Time, directVisitCount, fissionVisitCount int64) (err error) {
|
|
filter := bson.M{"channelCode": code, "appId": appId, "sumDate": sumDate}
|
|
|
|
update := bson.M{"directVisitCount": directVisitCount, "fissionVisitCount": fissionVisitCount}
|
|
|
|
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
|
|
insertDoc["channelCode"] = code
|
|
|
|
_, err = db.InsertOne(ctx, insertDoc)
|
|
}
|
|
|
|
return err
|
|
}
|
|
|
|
// UpsertOneDistrictCpc 更新渠道的留存
|
|
func UpsertOneDistrictCpc(ctx context.Context, code string, appId int32, sumDate time.Time, visitCount int64) (err error) {
|
|
filter := bson.M{"channelCode": code, "appId": appId, "sumDate": sumDate}
|
|
|
|
update := bson.M{"landPageVisitCount": visitCount}
|
|
|
|
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
|
|
insertDoc["channelCode"] = code
|
|
|
|
_, err = db.InsertOne(ctx, insertDoc)
|
|
}
|
|
|
|
return err
|
|
}
|
|
|
|
// UpsertOneDistrictRechargeStat 更新渠道的新增充值
|
|
func UpsertOneDistrictRechargeStat(ctx context.Context, code string, appId int32, sumDate time.Time, directAmount, fissionAmount, deduction int64) (err error) {
|
|
filter := bson.M{"channelCode": code, "appId": appId, "sumDate": sumDate}
|
|
|
|
update := bson.M{"fissionAmount": fissionAmount, "directAmount": directAmount, "deductionAmount": deduction}
|
|
|
|
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
|
|
insertDoc["channelCode"] = code
|
|
|
|
_, err = db.InsertOne(ctx, insertDoc)
|
|
}
|
|
|
|
return err
|
|
}
|
|
|
|
// 后台获取统计信息
|
|
func GetChannelCodeBlocStatAgentList(ctx context.Context, channelCodes []string, appId int32, start, end *time.Time, skip, limit int64) (count int64, data ChannelCodeBlocStatList, err error) {
|
|
if len(channelCodes) == 0 {
|
|
return
|
|
}
|
|
filter := bson.M{"channelCode": bson.M{"$in": channelCodes}, "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}, {Key: "newDirectCount", Value: -1}}
|
|
|
|
opts := options.Find().SetSort(sort).SetSkip(skip).SetLimit(limit)
|
|
|
|
if err = db.Find(ctx, &data, filter, opts); err != nil {
|
|
log.Error("ChannelCodeBlocStat GetChannelCodeBlocStatList Find err", log.E(err))
|
|
return
|
|
}
|
|
if count, err = db.Count(ctx, filter); err != nil {
|
|
log.Error("ChannelCodeBlocStat GetChannelCodeBlocStatList Count err", log.E(err))
|
|
return
|
|
}
|
|
|
|
return
|
|
}
|
|
|
|
// 后台获取统计信息
|
|
func GetChannelCodeBlocStatAgentListTotal(ctx context.Context, channelCodes []string, appId int32, start, end *time.Time) (data ChannelCodeBlocStatList, err error) {
|
|
if len(channelCodes) == 0 {
|
|
return
|
|
}
|
|
filter := bson.M{"channelCode": bson.M{"$in": channelCodes}, "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}, {Key: "newDirectCount", Value: -1}}
|
|
|
|
opts := options.Find().SetSort(sort)
|
|
|
|
if err = db.Find(ctx, &data, filter, opts); err != nil {
|
|
log.Error("ChannelCodeBlocStat GetChannelCodeBlocStatList Find err", log.E(err))
|
|
return
|
|
}
|
|
return
|
|
}
|
|
|
|
// 后台获取统计信息总数
|
|
func GetBlocChannelCodeBlocStatListTotal(ctx context.Context, channel *string, appId *int32, start, end *time.Time) (data ChannelCodeBlocStatList, err error) {
|
|
filter := bson.M{}
|
|
|
|
if channel != nil {
|
|
filter["channelCode"] = channel
|
|
}
|
|
|
|
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}
|
|
}
|
|
|
|
if err = db.Find(ctx, &data, filter); err != nil {
|
|
log.Error("ChannelCodeBlocStat GetChannelCodeBlocStatList Find err", log.E(err))
|
|
return
|
|
}
|
|
|
|
return
|
|
}
|
|
|
|
// 后台获取统计信息
|
|
func GetChannelCodeBlocStatList(ctx context.Context, channelCodes []string, appId *int32, start, end *time.Time, skip, limit int64) (count int64, data ChannelCodeBlocStatList, err error) {
|
|
filter := bson.M{}
|
|
if len(channelCodes) > 0 {
|
|
filter["channelCode"] = bson.M{"$in": channelCodes}
|
|
}
|
|
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}, {Key: "newDirectCount", Value: -1}}
|
|
|
|
opts := options.Find().SetSort(sort).SetSkip(skip).SetLimit(limit)
|
|
|
|
if err = db.Find(ctx, &data, filter, opts); err != nil {
|
|
log.Error("ChannelCodeBlocStat GetChannelCodeBlocStatList Find err", log.E(err))
|
|
return
|
|
}
|
|
if count, err = db.Count(ctx, filter); err != nil {
|
|
log.Error("ChannelCodeBlocStat GetChannelCodeBlocStatList Count err", log.E(err))
|
|
return
|
|
}
|
|
|
|
return
|
|
}
|
|
|
|
// 后台获取统计信息
|
|
func GetBlocChannelCodeBlocStatList(ctx context.Context, channel *string, appId *int32, start, end *time.Time, skip, limit int64) (count int64, data ChannelCodeBlocStatList, err error) {
|
|
filter := bson.M{}
|
|
|
|
if channel != nil {
|
|
filter["channelCode"] = channel
|
|
}
|
|
|
|
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}, {Key: "newDirectCount", Value: -1}}
|
|
|
|
opts := options.Find().SetSort(sort).SetSkip(skip).SetLimit(limit)
|
|
|
|
if err = db.Find(ctx, &data, filter, opts); err != nil {
|
|
log.Error("ChannelCodeBlocStat GetChannelCodeBlocStatList Find err", log.E(err))
|
|
return
|
|
}
|
|
if count, err = db.Count(ctx, filter); err != nil {
|
|
log.Error("ChannelCodeBlocStat GetChannelCodeBlocStatList Count err", log.E(err))
|
|
return
|
|
}
|
|
|
|
return
|
|
}
|
|
|
|
// 后台获取统计信息
|
|
func GetBlocChannelCodeStatTotal(ctx context.Context, channels []string, appId *int32, start, end *time.Time) (count int64, data ChannelCodeBlocStatList, err error) {
|
|
filter := bson.M{}
|
|
if len(channels) > 0 {
|
|
filter["channelCode"] = bson.M{"$in": channels}
|
|
}
|
|
|
|
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}
|
|
}
|
|
|
|
if err = db.Find(ctx, &data, filter); err != nil {
|
|
log.Error("ChannelCodeBlocStat GetChannelCodeBlocStatList Find err", log.E(err))
|
|
return
|
|
}
|
|
if count, err = db.Count(ctx, filter); err != nil {
|
|
log.Error("ChannelCodeBlocStat GetChannelCodeBlocStatList Count err", log.E(err))
|
|
return
|
|
}
|
|
|
|
return
|
|
}
|
|
|
|
// GetAllChannelCodeBlocStatList 根据渠道码获取统计信息
|
|
func GetAllChannelCodeBlocStatList(ctx context.Context, appId int32, channelCode []string, sumDate time.Time) (data ChannelCodeBlocStatList, err error) {
|
|
filter := bson.M{"sumDate": sumDate, "appId": appId, "channelCode": bson.M{"$in": channelCode}}
|
|
|
|
if err = db.Find(ctx, &data, filter); err != nil {
|
|
log.Error("ChannelCodeBlocStat GetChannelCodeBlocStatList Find err", log.E(err))
|
|
return
|
|
}
|
|
|
|
return
|
|
}
|
|
|
|
// GetChannelCodeBlocStatListTool 根据渠道码获取统计信息
|
|
func GetChannelCodeBlocStatListTool(ctx context.Context, appId int32, channelCode string) (data ChannelCodeBlocStatList, err error) {
|
|
filter := bson.M{"appId": appId, "channelCode": channelCode}
|
|
|
|
if err = db.Find(ctx, &data, filter); err != nil {
|
|
log.Error("ChannelCodeBlocStat GetChannelCodeBlocStatList Find err", log.E(err))
|
|
return
|
|
}
|
|
|
|
return
|
|
}
|
|
|
|
// UpsertOneDistrictRechargeStat 更新渠道的新增充值
|
|
func UpsertOneDistrictFissionRechargeStat(ctx context.Context, code string, appId int32, sumDate time.Time, rechargeCount, rechargeAmount int64) (err error) {
|
|
filter := bson.M{"channelCode": code, "appId": appId, "sumDate": sumDate}
|
|
|
|
update := bson.M{"fissionAmount": rechargeAmount}
|
|
|
|
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
|
|
insertDoc["channelCode"] = code
|
|
|
|
_, err = db.InsertOne(ctx, insertDoc)
|
|
}
|
|
|
|
return err
|
|
}
|