gomod引用
This commit is contained in:
@@ -30,12 +30,6 @@ func (s *advertisement) Add(ctx context.Context, req *dto.AddAdvertisementReq) (
|
||||
// 设置初始状态
|
||||
advertisement.Status = "待审核"
|
||||
|
||||
// 初始化统计字段
|
||||
advertisement.Impressions = 0
|
||||
advertisement.Clicks = 0
|
||||
advertisement.Conversions = 0
|
||||
advertisement.Cost = 0
|
||||
|
||||
if err = dao.Advertisement.Insert(ctx, advertisement); err != nil {
|
||||
return
|
||||
}
|
||||
@@ -89,75 +83,5 @@ func (s *advertisement) List(ctx context.Context, req *dto.ListAdvertisementReq)
|
||||
|
||||
// UpdateAdStatistics 更新广告统计
|
||||
func (s *advertisement) UpdateAdStatistics(ctx context.Context, id string, impressions, clicks, conversions int64, cost int64) (err error) {
|
||||
// 获取广告信息
|
||||
ad, err := dao.Advertisement.GetOne(ctx, id)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
// 计算统计数据
|
||||
totalImpressions := ad.Impressions + impressions
|
||||
totalClicks := ad.Clicks + clicks
|
||||
totalConversions := ad.Conversions + conversions
|
||||
totalCost := ad.Cost + cost
|
||||
|
||||
// 计算比率
|
||||
ctr := 0.0
|
||||
if totalImpressions > 0 {
|
||||
ctr = float64(totalClicks) / float64(totalImpressions)
|
||||
}
|
||||
|
||||
cvr := 0.0
|
||||
if totalClicks > 0 {
|
||||
cvr = float64(totalConversions) / float64(totalClicks)
|
||||
}
|
||||
|
||||
cpm := int64(0)
|
||||
if totalImpressions > 0 {
|
||||
cpm = totalCost * 1000 / totalImpressions
|
||||
}
|
||||
|
||||
cpc := int64(0)
|
||||
if totalClicks > 0 {
|
||||
cpc = totalCost / totalClicks
|
||||
}
|
||||
|
||||
// 构建更新数据
|
||||
stats := map[string]interface{}{
|
||||
"impressions": totalImpressions,
|
||||
"clicks": totalClicks,
|
||||
"conversions": totalConversions,
|
||||
"cost": totalCost,
|
||||
"updatedAt": time.Now(),
|
||||
}
|
||||
|
||||
// 更新广告统计
|
||||
err = dao.Advertisement.UpdateStatistics(ctx, id, stats)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
// 插入统计记录
|
||||
today := time.Now()
|
||||
todayStart := time.Date(today.Year(), today.Month(), today.Day(), 0, 0, 0, 0, today.Location()).Unix()
|
||||
|
||||
statistics := &entity.AdStatistics{
|
||||
StatType: "advertisement",
|
||||
StatDimension: "day",
|
||||
TargetId: id,
|
||||
TargetName: ad.Title,
|
||||
StatDate: todayStart,
|
||||
Impressions: impressions,
|
||||
Clicks: clicks,
|
||||
Conversions: conversions,
|
||||
Cost: cost,
|
||||
CTR: ctr,
|
||||
CVR: cvr,
|
||||
CPM: cpm,
|
||||
CPC: cpc,
|
||||
// CreatedAt、UpdatedAt、IsDeleted字段是嵌入字段,无需单独设置
|
||||
}
|
||||
|
||||
err = dao.AdStatistics.Upsert(ctx, statistics)
|
||||
return
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user