forked from all_user/stat_center
266 lines
6.4 KiB
Go
266 lines
6.4 KiB
Go
package advertiseSer
|
|
|
|
import (
|
|
"context"
|
|
"stat/common/log"
|
|
"stat/models/advertiseLocMod"
|
|
"stat/models/advertiseMod"
|
|
"stat/models/advertisestat"
|
|
"stat/models/appInfoMod"
|
|
|
|
"go.mongodb.org/mongo-driver/mongo/options"
|
|
|
|
"go.mongodb.org/mongo-driver/bson"
|
|
)
|
|
|
|
// GetAdvertiseStatList 获取广告统计列表
|
|
func GetAdvertiseStatList(ctx context.Context, param *GetAdvertiseStatReq) (*GetAdvertiseStatResp, error) {
|
|
resp := &GetAdvertiseStatResp{}
|
|
statFilter := bson.M{}
|
|
if param.AppId != nil {
|
|
statFilter[advertisestat.FN.AdvertiseStat.AppId] = bson.M{"$in": *param.AppId}
|
|
}
|
|
if param.AdsId != nil {
|
|
statFilter[advertisestat.FN.AdvertiseStat.AdsId] = *param.AdsId
|
|
}
|
|
if param.AdsName != nil {
|
|
adsInfos, err := advertiseMod.FindAdvertiselist(ctx, bson.M{"title": param.AdsName})
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
adsIds := make([]int64, 0)
|
|
for _, ad := range adsInfos {
|
|
adsIds = append(adsIds, ad.Id)
|
|
}
|
|
|
|
statFilter[advertisestat.FN.AdvertiseStat.AdsId] = bson.M{"$in": adsIds}
|
|
}
|
|
if param.Start != nil || param.End != nil {
|
|
statFilter[advertisestat.FN.AdvertiseStat.SumData] = bson.M{"$gte": *param.Start, "$lte": *param.End}
|
|
}
|
|
filter := bson.M{}
|
|
// app dict
|
|
appDict, err := appInfoMod.GetAllAppIdMap(ctx, filter)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
// ads dict
|
|
adsDict, err := advertiseMod.GetAllAdvertiseMap(ctx, filter)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
// ads location dict
|
|
locDict, err := advertiseLocMod.GetAllAdvertiseLocationMap(ctx, filter)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
opt := &options.FindOptions{}
|
|
opt.SetSort(bson.D{
|
|
{advertisestat.FN.AdvertiseStat.SumData, -1},
|
|
{advertisestat.FN.AdvertiseStat.Total, -1},
|
|
})
|
|
opt.SetLimit(param.PageSize)
|
|
opt.SetSkip((param.PageNum - 1) * param.PageSize)
|
|
totalStats, err := advertisestat.FindManyAdvertiseStat(ctx, statFilter)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
pageStats, err := advertisestat.FindManyAdvertiseStat(ctx, statFilter, opt)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
for _, stat := range totalStats {
|
|
resp.Summary.TotalClick += stat.Total
|
|
}
|
|
for _, stat := range pageStats {
|
|
appName := ""
|
|
if app := appDict[stat.AppId]; app != nil {
|
|
appName = app.Name
|
|
}
|
|
adsName := ""
|
|
locId := int32(0)
|
|
if ads := adsDict[stat.AdsId]; ads != nil {
|
|
adsName = ads.Title
|
|
locId = ads.LocId
|
|
}
|
|
locName := ""
|
|
if appLoc := locDict[stat.AppId]; appLoc != nil {
|
|
if loc := appLoc[locId]; loc != nil {
|
|
locName = loc.Name
|
|
}
|
|
}
|
|
hours := make([]int64, 24)
|
|
for hour := range hours {
|
|
hours[hour] = stat.HourData[hour]
|
|
}
|
|
resp.List = append(resp.List, &AdvertiseStatInfo{
|
|
Id: stat.Id,
|
|
AdsId: stat.AdsId,
|
|
AdsName: adsName,
|
|
AppId: stat.AppId,
|
|
AppName: appName,
|
|
LocId: locId,
|
|
LocName: locName,
|
|
SumDate: stat.SumDate,
|
|
Total: stat.Total,
|
|
HourData: hours,
|
|
CreatedAt: stat.CreatedAt,
|
|
UpdatedAt: stat.UpdatedAt,
|
|
})
|
|
}
|
|
resp.Total = int64(len(totalStats))
|
|
return resp, nil
|
|
}
|
|
|
|
// GetAdvertiseStatList 获取广告统计列表
|
|
func GetAdvertiseTotalStatList(ctx context.Context, param *GetAdvertiseTotalStatReq) (*GetAdvertiseTotalStatResp,
|
|
error) {
|
|
resp := &GetAdvertiseTotalStatResp{
|
|
List: make([]*AdvertiseTotalStatInfo, 0),
|
|
}
|
|
/*
|
|
statFilter := bson.M{}
|
|
if param.AppId != nil {
|
|
statFilter[advertisestat.FN.AdvertiseStat.AppId] = *param.AppId
|
|
}
|
|
if param.Start != nil || param.End != nil {
|
|
statFilter[advertisestat.FN.AdvertiseStat.SumData] = bson.M{"$gte": *param.Start, "$lte": *param.End}
|
|
}
|
|
*/
|
|
// app dict
|
|
appFilter := bson.M{}
|
|
if param.AppId != nil {
|
|
appFilter[appInfoMod.FN.AppInfo.AppId] = bson.M{"$in": *param.AppId}
|
|
}
|
|
apps, serr := appInfoMod.GetAllAppIdMap(ctx, appFilter)
|
|
if serr != nil {
|
|
return nil, serr
|
|
}
|
|
// ads dict
|
|
adsFilter := bson.M{}
|
|
if param.AppId != nil {
|
|
adsFilter["appId"] = bson.M{"$in": *param.AppId}
|
|
}
|
|
if param.AdsId != nil {
|
|
adsFilter["_id"] = *param.AdsId
|
|
}
|
|
if param.AdsName != nil {
|
|
adsFilter["title"] = *param.AdsName
|
|
}
|
|
adsDict, err := advertiseMod.GetAllAdvertiseMap(ctx, adsFilter)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
// ads location dict
|
|
locFilter := bson.M{}
|
|
if param.AppId != nil {
|
|
locFilter[appInfoMod.FN.AppInfo.AppId] = bson.M{"$in": *param.AppId}
|
|
}
|
|
locDict, err := advertiseLocMod.GetAllAdvertiseLocationMap(ctx, locFilter)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
filter := bson.M{}
|
|
if param.AppId != nil {
|
|
filter["appId"] = bson.M{"$in": *param.AppId}
|
|
}
|
|
if param.AdsId != nil {
|
|
filter["adsId"] = *param.AdsId
|
|
}
|
|
if param.AdsName != nil {
|
|
adsInfos, err := advertiseMod.FindAdvertiselist(ctx, bson.M{"title": param.AdsName})
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
adsIds := make([]int64, 0)
|
|
for _, ad := range adsInfos {
|
|
adsIds = append(adsIds, ad.Id)
|
|
}
|
|
|
|
filter["adsId"] = bson.M{"$in": adsIds}
|
|
}
|
|
if param.Start != nil && param.End != nil {
|
|
filter["sumDate"] = bson.M{"$gte": *param.Start, "$lte": *param.End}
|
|
}
|
|
pipeline := []bson.M{
|
|
{
|
|
"$match": filter,
|
|
},
|
|
{
|
|
"$group": bson.M{
|
|
"_id": "$adsId",
|
|
"total": bson.M{"$sum": "$total"},
|
|
},
|
|
},
|
|
{"$sort": bson.D{{Key: "_id", Value: -1}}},
|
|
}
|
|
|
|
list := make([]AdvertiseTempInfo, 0)
|
|
serr1 := advertisestat.AggregateAdvertiseStat(ctx, &list, pipeline)
|
|
if serr1 != nil {
|
|
log.Info("", log.Any("serr1", serr1))
|
|
return nil, serr1
|
|
}
|
|
|
|
pipeline = []bson.M{
|
|
{
|
|
"$match": filter,
|
|
},
|
|
{
|
|
"$group": bson.M{
|
|
"_id": "$adsId",
|
|
"total": bson.M{"$sum": "$total"},
|
|
},
|
|
},
|
|
{"$sort": bson.D{{Key: "_id", Value: -1}}},
|
|
{"$skip": (param.PageNum - 1) * param.PageSize},
|
|
{"$limit": param.PageSize},
|
|
}
|
|
|
|
info := make([]AdvertiseTempInfo, 0)
|
|
serr1 = advertisestat.AggregateAdvertiseStat(ctx, &info, pipeline)
|
|
if serr1 != nil {
|
|
log.Info("", log.Any("serr1", serr1))
|
|
return nil, serr1
|
|
}
|
|
|
|
for _, temp := range list {
|
|
resp.Summary.TotalClick += temp.Total
|
|
}
|
|
|
|
for _, stat := range info {
|
|
appName := ""
|
|
adsName := ""
|
|
locName := ""
|
|
locId := int32(0)
|
|
appId := int32(0)
|
|
if ads := adsDict[stat.AdsId]; ads != nil {
|
|
adsName = ads.Title
|
|
locId = ads.LocId
|
|
if app := apps[ads.AppId]; app != nil {
|
|
appName = app.Name
|
|
appId = app.AppId
|
|
if appLoc := locDict[app.AppId]; appLoc != nil {
|
|
if loc := appLoc[locId]; loc != nil {
|
|
locName = loc.Name
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
resp.List = append(resp.List, &AdvertiseTotalStatInfo{
|
|
AdsId: stat.AdsId,
|
|
AdsName: adsName,
|
|
AppId: appId,
|
|
AppName: appName,
|
|
LocId: int64(locId),
|
|
LocName: locName,
|
|
Total: stat.Total,
|
|
})
|
|
}
|
|
|
|
resp.Total = int64(len(list))
|
|
return resp, nil
|
|
}
|