Commit 8531d67b by 彭芳

[feat]: add creative report api

parent 061fb63a
......@@ -35,7 +35,8 @@ func wireApp(confServer *conf.Server, confData *conf.Data, logger log.Logger) (*
}
campaignRepo := data.NewCampaignRepo(dataData, logger)
adRepo := data.NewAdRepo(dataData, logger)
reportUsecase := biz.NewReportUsecase(campaignRepo, adRepo, logger)
creativeRepo := data.NewCreativeRepo(dataData, logger)
reportUsecase := biz.NewReportUsecase(campaignRepo, adRepo, creativeRepo, logger)
reportService := service.NewReportService(reportUsecase, logger)
web := &router.Web{
Common: common,
......
......@@ -27,6 +27,24 @@ type ListReportRequest struct {
EndDateLong util.Second `json:"-"` // 结束时间
}
func (p *ListReportRequest) Validate() error {
if p.StartDate != "" {
parse, err := time.Parse("2006-01-02", p.StartDate)
if err != nil {
return errs.ErrorArgs.WithMessage("开始时间格式错误")
}
p.StartDateLong = util.NewSecond(parse)
}
if p.EndDate != "" {
parse, err := time.Parse("2006-01-02", p.EndDate)
if err != nil {
return errs.ErrorArgs.WithMessage("结束时间格式错误")
}
p.EndDateLong = util.NewSecond(parse)
}
return nil
}
type ListCampaignResponse struct {
List CampaignList `json:"list"` // 数据信息
PageInfo *common.PageInfo `json:"page_info"` // 页码信息
......@@ -287,8 +305,8 @@ func (p AdList) PrepareRateAndCost() {
}
type Ad struct {
AdId string `json:"ad_id"`
AdName string `json:"ad_name"`
AdId string `json:"ad_id"` // 广告计划id
AdName string `json:"ad_name"` // 广告计划名称
Campaign
}
......@@ -296,36 +314,44 @@ type AdRepo interface {
List(ctx context.Context, customerId int64, req *ListReportRequest) (int64, AdList, error)
}
type ListCreativeResponse struct {
List CreativeList `json:"list"` // 数据信息
PageInfo *common.PageInfo `json:"page_info"` // 页码信息
}
type CreativeList []*Creative
func (p CreativeList) PrepareRateAndCost() {
for _, ad := range p {
ad.PrepareRateAndCost()
}
}
type Creative struct {
CreativeId string `json:"creative_id"` // 创意id
Ad
}
type CreativeRepo interface {
List(ctx context.Context, customerId int64, req *ListReportRequest) (int64, CreativeList, error)
}
// ReportUsecase is a Report usecase.
type ReportUsecase struct {
camRepo CampaignRepo
adRepo AdRepo
log *log.Helper
camRepo CampaignRepo
adRepo AdRepo
creativeRepo CreativeRepo
log *log.Helper
}
// NewReportUsecase new a Report usecase.
func NewReportUsecase(repo CampaignRepo, adRepo AdRepo, logger log.Logger) *ReportUsecase {
return &ReportUsecase{camRepo: repo, adRepo: adRepo, log: log.NewHelper(logger)}
func NewReportUsecase(repo CampaignRepo, adRepo AdRepo, creativeRepo CreativeRepo, logger log.Logger) *ReportUsecase {
return &ReportUsecase{camRepo: repo, adRepo: adRepo, creativeRepo: creativeRepo, log: log.NewHelper(logger)}
}
// ListCampaignData list campaign data.
func (p *ReportUsecase) ListCampaignData(ctx context.Context, customerId int64, req *ListReportRequest) (*ListCampaignResponse, error) {
p.log.WithContext(ctx).Infof("req: %v", req)
if req.StartDate != "" {
parse, err := time.Parse("2006-01-02", req.StartDate)
if err != nil {
return nil, errs.ErrorArgs.WithMessage("开始时间格式错误")
}
req.StartDateLong = util.NewSecond(parse)
}
if req.EndDate != "" {
parse, err := time.Parse("2006-01-02", req.EndDate)
if err != nil {
return nil, errs.ErrorArgs.WithMessage("结束时间格式错误")
}
req.EndDateLong = util.NewSecond(parse)
}
total, list, err := p.camRepo.List(ctx, customerId, req)
if err != nil {
return nil, err
......@@ -340,21 +366,6 @@ func (p *ReportUsecase) ListCampaignData(ctx context.Context, customerId int64,
// ListAdData list ad data.
func (p *ReportUsecase) ListAdData(ctx context.Context, customerId int64, req *ListReportRequest) (*ListAdResponse, error) {
p.log.WithContext(ctx).Infof("req: %v", req)
if req.StartDate != "" {
parse, err := time.Parse("2006-01-02", req.StartDate)
if err != nil {
return nil, errs.ErrorArgs.WithMessage("开始时间格式错误")
}
req.StartDateLong = util.NewSecond(parse)
}
if req.EndDate != "" {
parse, err := time.Parse("2006-01-02", req.EndDate)
if err != nil {
return nil, errs.ErrorArgs.WithMessage("结束时间格式错误")
}
req.EndDateLong = util.NewSecond(parse)
}
total, list, err := p.adRepo.List(ctx, customerId, req)
if err != nil {
return nil, err
......@@ -365,3 +376,17 @@ func (p *ReportUsecase) ListAdData(ctx context.Context, customerId int64, req *L
PageInfo: req.PageInfo(total),
}, nil
}
// ListCreativeData list creative data.
func (p *ReportUsecase) ListCreativeData(ctx context.Context, customerId int64, req *ListReportRequest) (*ListCreativeResponse, error) {
p.log.WithContext(ctx).Infof("req: %v", req)
total, list, err := p.creativeRepo.List(ctx, customerId, req)
if err != nil {
return nil, err
}
list.PrepareRateAndCost()
return &ListCreativeResponse{
List: list,
PageInfo: req.PageInfo(total),
}, nil
}
package data
import (
"context"
"github.com/go-kratos/kratos/v2/log"
"demo/internal/biz"
)
type creativeRepo struct {
data *Data
log *log.Helper
}
func NewCreativeRepo(data *Data, logger log.Logger) biz.CreativeRepo {
return &creativeRepo{
data: data,
log: log.NewHelper(logger),
}
}
func (p *creativeRepo) List(ctx context.Context, customerId int64, req *biz.ListReportRequest) (int64, biz.CreativeList, error) {
tableName := "ads_report_creative_day"
if req.TimeGranularity == biz.StatTimeGranularityHourly {
tableName = "ads_report_creative_hour"
}
db := p.data.db.Table(tableName).
Select("creative_id, advertise_id as ad_id, advertise_name as ad_name, campaign_id, campaign_name, advertiser_id, date_long AS stat_datetime, cost, `show`, click, `convert`, "+
"deep_convert, attribution_convert, attribution_deep_convert, download_start, download_finish, click_install, "+
"install_finish, active, register, next_day_open, next_day_open_cost, next_day_open_rate, attribution_next_day_open_cnt,"+
" game_addiction, pay_count, in_app_uv, in_app_detail_uv, in_app_cart, in_app_pay, in_app_order, attribution_game_pay_7d_count,"+
" game_pay_count, total_play, valid_play, play_25_feed_break, play_50_feed_break, play_75_feed_break, play_100_feed_break, "+
"play_duration_sum, attribution_active_pay_intra_one_day_count, attribution_active_pay_intra_one_day_amount, gmt_modified AS timestamp").
Where("customer_id = ?", customerId).
Where("is_delete = ?", 0)
// add start_date and end_date condition
if req.StartDate != "" {
db = db.Where("date_long >= ?", req.StartDateLong)
}
if req.EndDate != "" {
db = db.Where("date_long <= ?", req.EndDateLong)
}
var total int64
if err := db.Count(&total).Error; err != nil {
return 0, nil, err
}
var list biz.CreativeList
if err := db.Order(req.GetOrderBy("id") + " " + req.GetOrderByMode()).
Limit(req.Limit()).Offset(req.Offset()).Scan(&list).Error; err != nil {
return total, nil, err
}
return total, list, nil
}
......@@ -15,7 +15,7 @@ import (
)
// ProviderSet is data providers.
var ProviderSet = wire.NewSet(NewData, NewCampaignRepo, NewAdRepo)
var ProviderSet = wire.NewSet(NewData, NewCampaignRepo, NewAdRepo, NewCreativeRepo)
// Data .
type Data struct {
......
......@@ -33,6 +33,7 @@ func (p *Web) RouterInit(engine *gin.Engine) {
{
report.POST("/campaign", p.Report.ListCampaignData)
report.POST("/ad", p.Report.ListAdData)
report.POST("/creative", p.Report.ListCreativeData)
}
}
}
......@@ -29,6 +29,12 @@ func (p *ReportService) ListCampaignData(ctx *gin.Context) {
ResponseError(ctx, errs.ErrorArgs)
return
}
if err := req.Validate(); err != nil {
p.log.Error("Validate err:", err)
ResponseError(ctx, err)
return
}
customerIdStr := ctx.GetString(util.CustomerId)
customerId, err := strconv.Atoi(customerIdStr)
if err != nil {
......@@ -53,6 +59,12 @@ func (p *ReportService) ListAdData(ctx *gin.Context) {
ResponseError(ctx, errs.ErrorArgs)
return
}
if err := req.Validate(); err != nil {
p.log.Error("Validate err:", err)
ResponseError(ctx, err)
return
}
customerIdStr := ctx.GetString(util.CustomerId)
customerId, err := strconv.Atoi(customerIdStr)
if err != nil {
......@@ -68,3 +80,34 @@ func (p *ReportService) ListAdData(ctx *gin.Context) {
}
ResponseSuccess(ctx, data)
}
// ListCreativeData 查询广告创意数据报表
func (p *ReportService) ListCreativeData(ctx *gin.Context) {
var req biz.ListReportRequest
if err := ctx.ShouldBind(&req); err != nil {
p.log.Error("ShouldBind err:", err)
ResponseError(ctx, errs.ErrorArgs)
return
}
if err := req.Validate(); err != nil {
p.log.Error("Validate err:", err)
ResponseError(ctx, err)
return
}
customerIdStr := ctx.GetString(util.CustomerId)
customerId, err := strconv.Atoi(customerIdStr)
if err != nil {
p.log.Error("Atoi err:", err)
ResponseError(ctx, err)
return
}
data, err := p.uc.ListCreativeData(ctx, int64(customerId), &req)
if err != nil {
p.log.Error("ListCreativeData err:", err)
ResponseError(ctx, err)
return
}
ResponseSuccess(ctx, data)
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment