package landpageSer import ( "context" "fmt" "stat/api/proto" "stat/models/appInfoMod" "stat/models/downloadstat" "stat/models/landpageImg" "stat/service/cacheSer" "time" "go.mongodb.org/mongo-driver/bson" "go.mongodb.org/mongo-driver/bson/primitive" "go.mongodb.org/mongo-driver/mongo/options" ) func ListDailyStat(ctx context.Context, param *proto.ListDailyStatReq) (*proto.ListLandPageStatRes, error) { resp := &proto.ListLandPageStatRes{ Total: 0, List: make([]*proto.DownloadStatInfo, 0), } filter := bson.M{"type": downloadstat.StatTypeDaily} opt := &options.FindOptions{} opt.SetSort(bson.D{{Key: "sumDate", Value: -1}}) opt.SetSkip((param.PageNum - 1) * param.PageSize) opt.SetLimit(param.PageSize) if param.AppId != nil { filter["appId"] = bson.M{"$in": *param.AppId} } if param.StartTime != nil && param.EndTime != nil { filter["sumDate"] = bson.M{"$gte": *param.StartTime, "$lt": *param.EndTime} } total, _ := downloadstat.CountStats(ctx, filter) statList, err := downloadstat.FindManyStat(ctx, filter, opt) if err != nil { return nil, err } appMap, _ := appInfoMod.GetAllAppIdNameMap(ctx) for _, item := range statList { item.AppName = appMap[item.AppId] tmp := &proto.DownloadStatInfo{ Stat: *item, } proto.StatSummation(&resp.Summary, tmp) tmp.CalcConversionRate() resp.List = append(resp.List, tmp) } resp.Total = total resp.Summary.CalcConversionRate() return resp, nil } func ListDailyHourStat(ctx context.Context, param *proto.ListDailyHourStatReq) (*proto.ListLandPageStatRes, error) { resp := &proto.ListLandPageStatRes{ Total: 0, List: make([]*proto.DownloadStatInfo, 0), } filter := bson.M{"type": downloadstat.StatTypeHour} opt := &options.FindOptions{} opt.SetSort(bson.D{{Key: "sumDate", Value: -1}, {Key: "hour", Value: -1}}) opt.SetSkip((param.PageNum - 1) * param.PageSize) opt.SetLimit(param.PageSize) if param.AppId != nil { filter["appId"] = *param.AppId } if param.StartTime != nil && param.EndTime != nil { filter["sumDate"] = bson.M{"$gte": *param.StartTime, "$lt": *param.EndTime} } if param.SumHour != nil { filter["hour"] = *param.SumHour } total, _ := downloadstat.CountStats(ctx, filter) statList, err := downloadstat.FindManyStat(ctx, filter, opt) if err != nil { return nil, err } appMap, _ := appInfoMod.GetAllAppIdNameMap(ctx) for _, item := range statList { item.AppName = appMap[item.AppId] tmp := &proto.DownloadStatInfo{ Stat: *item, } proto.StatSummation(&resp.Summary, tmp) tmp.CalcConversionRate() resp.List = append(resp.List, tmp) } resp.Total = total resp.Summary.CalcConversionRate() return resp, nil } func LandpagePicture(ctx context.Context, req *proto.LandpagePictureSearch) (resp *proto.LandpagePictureSearchResp, err error) { resp = &proto.LandpagePictureSearchResp{} filter := bson.M{} opt := &options.FindOptions{} if req.Status != nil { filter[landpageImg.FN.LandpageImg.Status] = *req.Status } if req.Name != nil { filter[landpageImg.FN.LandpageImg.Name] = *req.Name } opt.SetSort(bson.M{landpageImg.FN.LandpageImg.CreatedAt: -1}) opt.SetSkip((req.PageNum - 1) * req.PageSize) opt.SetLimit(req.PageSize) resp.Total, err = landpageImg.CountLandpageImgs(ctx, filter) if err != nil { return nil, err } if resp.Total == 0 { return resp, nil } landpagePictureList, serr := landpageImg.FindManyLandpageImg(ctx, filter, opt) if serr != nil { return nil, serr } resp.List = landpagePictureList return } func AddLandpagePicture(ctx context.Context, req *proto.LandpagePictureAddReq) error { now := time.Now() req.CreatedAt = now req.UpdatedAt = now serr := landpageImg.InsertOneLandpageImg(ctx, &req.LandpageImg) if serr != nil { return serr } return nil } func DelLandpagePicture(ctx context.Context, id string) error { objectId, err := primitive.ObjectIDFromHex(id) if err != nil { return err } adFilter := bson.M{landpageImg.FN.LandpageImg.ID: objectId} //删除 _, serr := landpageImg.DelOneLandpageImg(ctx, adFilter) if serr != nil { return serr } return nil } func UpdateLandpagePicture(ctx context.Context, req *proto.LandpagePictureUpdateReq) error { now := time.Now() req.Param.UpdatedAt = &now objectId, err := primitive.ObjectIDFromHex(req.Id) if err != nil { return err } adFilter := bson.M{landpageImg.FN.LandpageImg.ID: objectId} updateDoc := bson.M{"$set": req.Param} serr := landpageImg.UpdateOneLandpageImg(ctx, adFilter, updateDoc) if serr != nil { return serr } return nil } func ListSummaryDownload(ctx context.Context) (*proto.ListDownloadSummaryRes, error) { resp := &proto.ListDownloadSummaryRes{ List: make([]proto.DownloadSummaryInfo, 0), } cacheKey := fmt.Sprintf("landPage#summary") if cache, exists := cacheSer.Cache.Get(cacheKey); exists { data, ok := cache.([]proto.DownloadSummaryInfo) if !ok { pipeline := []bson.M{ {"$group": bson.M{ "_id": "$appId", "total": bson.M{"$sum": "$downTotal"}, }}, } if err := downloadstat.AggregateStats(ctx, &resp.List, pipeline); err != nil { return nil, err } cacheSer.Cache.Set(cacheKey, resp.List, 15*time.Minute) } else { resp.List = data } } else { pipeline := []bson.M{ {"$group": bson.M{ "_id": "$appId", "total": bson.M{"$sum": "$downTotal"}, }}, } if err := downloadstat.AggregateStats(ctx, &resp.List, pipeline); err != nil { return nil, err } cacheSer.Cache.Set(cacheKey, resp.List, 15*time.Minute) } return resp, nil }