getAdList方法中实现对$statistics的缓存10分钟
This commit is contained in:
parent
ec69bc1d0a
commit
2744512839
@ -71,7 +71,7 @@ class AdsInsightService
|
||||
return json_decode($cachedData, true);
|
||||
}
|
||||
// 没有缓存时重新计算
|
||||
$countData = self::calculateCountData($merchantId,$customerIds, $startDate, $endDate);
|
||||
$countData = self::calculateCountData($merchantId, $customerIds, $startDate, $endDate);
|
||||
|
||||
// 缓存到 Redis,有效期 10 分钟
|
||||
Redis::setex($redisKey, 600, json_encode($countData));
|
||||
@ -89,7 +89,7 @@ class AdsInsightService
|
||||
[$startDate, $endDate] = self::getLastWeekDateRange();
|
||||
}
|
||||
return [
|
||||
'account_list_count' => self::getAccountListCount($merchantId,$customerIds, $startDate, $endDate),
|
||||
'account_list_count' => self::getAccountListCount($merchantId, $customerIds, $startDate, $endDate),
|
||||
'campaign_list_count' => self::getCampaignListCount($customerIds, $startDate, $endDate),
|
||||
'adset_list_count' => self::getAdsetListCount($customerIds, $startDate, $endDate),
|
||||
'ad_list_count' => self::getAdListCount($customerIds, $startDate, $endDate),
|
||||
@ -133,9 +133,9 @@ class AdsInsightService
|
||||
/**
|
||||
* 获取账户列表的 count
|
||||
*/
|
||||
protected static function getAccountListCount($merchantId,$customerIds, $startDate, $endDate)
|
||||
protected static function getAccountListCount($merchantId, $customerIds, $startDate, $endDate)
|
||||
{
|
||||
return self::getAccountList(0, $merchantId,$customerIds, 1, 1, '', $startDate, $endDate, false, true);
|
||||
return self::getAccountList(0, $merchantId, $customerIds, 1, 1, '', $startDate, $endDate, false, true);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -143,7 +143,7 @@ class AdsInsightService
|
||||
*/
|
||||
protected static function getCreativeListCount(array $customerIds, $startDate, $endDate)
|
||||
{
|
||||
return self::getCreativeInsightData(0, $customerIds, 1, 1, '', $startDate, $endDate, false,true);
|
||||
return self::getCreativeInsightData(0, $customerIds, 1, 1, '', $startDate, $endDate, false, true);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -721,7 +721,7 @@ class AdsInsightService
|
||||
}
|
||||
}
|
||||
|
||||
public static function exportCreativesToExcel($platformType, $customerIds, $page, $pageSize, $keyword, $startDate = null, $endDate = null,$requireSpend = false)
|
||||
public static function exportCreativesToExcel($platformType, $customerIds, $page, $pageSize, $keyword, $startDate = null, $endDate = null, $requireSpend = false)
|
||||
{
|
||||
// 获取广告创意数据
|
||||
$creativeList = self::getCreativeInsightData($platformType, $customerIds, $page, $pageSize, $keyword, $startDate, $endDate, $requireSpend, false);
|
||||
@ -1428,26 +1428,47 @@ class AdsInsightService
|
||||
$adds_to_cart = array_sum(array_column($allAds, 'adds_to_cart'));
|
||||
$cost_per_purchase = $total_purchases == 0 ? 0 : round($total_spend / $total_purchases, 2);
|
||||
|
||||
$statistics = [
|
||||
'platform_purchase' => number_format(array_sum(array_column($allAds, 'platform_purchase'))),
|
||||
'pixel_purchase' => number_format(array_sum(array_column($allAds, 'pixel_purchase'))),
|
||||
'roas' => $total_spend == 0 ? '-' : round($total_purchases_value / $total_spend, 2) . 'x',
|
||||
'amount_spend' => '$' . number_format($total_spend, 2) ?: '$0.00', // 格式化支出
|
||||
'clicks' => number_format($total_clicks),
|
||||
'impressions' => number_format($total_impressions),
|
||||
'adds_to_cart' => number_format($adds_to_cart),
|
||||
'cost_per_atc' => $adds_to_cart == 0 ? '-' : '$' . number_format(($total_spend / $adds_to_cart), 2),
|
||||
'purchases' => number_format($total_purchases),
|
||||
'purchases_value' => array_sum(array_column($allAds, 'purchases_value')),
|
||||
'cost_per_purchase' => '$' . number_format($cost_per_purchase, 2) ?: '$0.00',
|
||||
'revenue' => '$' . number_format(array_sum(array_column($allAds, 'revenue')), 2) ?: '$0.00', // 格式化收入
|
||||
'total_cost' => '$' . number_format($total_cost, 2) ?: '$0.00', // 格式化总成本
|
||||
'conversion_rate' => $total_clicks == 0 ? '-' : round(($total_purchases / $total_clicks) * 100, 2) . '%', // 转换率
|
||||
'net_profit' => ($total_revenue - $total_cost) >= 0 ? '+$' . number_format($total_revenue - $total_cost, 2) : '-$' . number_format(abs($total_revenue - $total_cost), 2), // 净利润
|
||||
'net_profit_margin' => $total_revenue == 0 ? '-' : round(($total_revenue - $total_cost) / $total_revenue, 2) * 100 . '%', // 净利润率
|
||||
'net_profit_on_ad_spend' => $total_spend == 0 ? '-' : round(($total_revenue - $total_cost) / $total_spend, 2) * 100 . '%', // 平均广告花费的净利润
|
||||
'ctr' => ($total_impressions > 0) ? number_format(($total_clicks / $total_impressions) * 100, 2) . '%' : '-', // 格式化为百分比
|
||||
];
|
||||
// 生成唯一缓存键
|
||||
$cacheKey = 'adlist_stats:' . md5(serialize([
|
||||
$platformType,
|
||||
$customerIds,
|
||||
$page,
|
||||
$pageSize,
|
||||
$keyword,
|
||||
$startDate,
|
||||
$endDate,
|
||||
$status,
|
||||
$requireSpend
|
||||
]));
|
||||
// 尝试从缓存读取统计数据
|
||||
if (!$countOnly && $cached = Redis::get($cacheKey)) {
|
||||
$statistics = json_decode($cached, true);
|
||||
// 跳过后续统计计算直接使用缓存数据...
|
||||
} else {
|
||||
$statistics = [
|
||||
'platform_purchase' => number_format(array_sum(array_column($allAds, 'platform_purchase'))),
|
||||
'pixel_purchase' => number_format(array_sum(array_column($allAds, 'pixel_purchase'))),
|
||||
'roas' => $total_spend == 0 ? '-' : round($total_purchases_value / $total_spend, 2) . 'x',
|
||||
'amount_spend' => '$' . number_format($total_spend, 2) ?: '$0.00', // 格式化支出
|
||||
'clicks' => number_format($total_clicks),
|
||||
'impressions' => number_format($total_impressions),
|
||||
'adds_to_cart' => number_format($adds_to_cart),
|
||||
'cost_per_atc' => $adds_to_cart == 0 ? '-' : '$' . number_format(($total_spend / $adds_to_cart), 2),
|
||||
'purchases' => number_format($total_purchases),
|
||||
'purchases_value' => array_sum(array_column($allAds, 'purchases_value')),
|
||||
'cost_per_purchase' => '$' . number_format($cost_per_purchase, 2) ?: '$0.00',
|
||||
'revenue' => '$' . number_format(array_sum(array_column($allAds, 'revenue')), 2) ?: '$0.00', // 格式化收入
|
||||
'total_cost' => '$' . number_format($total_cost, 2) ?: '$0.00', // 格式化总成本
|
||||
'conversion_rate' => $total_clicks == 0 ? '-' : round(($total_purchases / $total_clicks) * 100, 2) . '%', // 转换率
|
||||
'net_profit' => ($total_revenue - $total_cost) >= 0 ? '+$' . number_format($total_revenue - $total_cost, 2) : '-$' . number_format(abs($total_revenue - $total_cost), 2), // 净利润
|
||||
'net_profit_margin' => $total_revenue == 0 ? '-' : round(($total_revenue - $total_cost) / $total_revenue, 2) * 100 . '%', // 净利润率
|
||||
'net_profit_on_ad_spend' => $total_spend == 0 ? '-' : round(($total_revenue - $total_cost) / $total_spend, 2) * 100 . '%', // 平均广告花费的净利润
|
||||
'ctr' => ($total_impressions > 0) ? number_format(($total_clicks / $total_impressions) * 100, 2) . '%' : '-', // 格式化为百分比
|
||||
];
|
||||
|
||||
// 将新生成的统计数据存入缓存(10分钟有效期)
|
||||
Redis::setex($cacheKey, 600, json_encode($statistics));
|
||||
}
|
||||
|
||||
|
||||
// 获取分页数据
|
||||
|
Loading…
Reference in New Issue
Block a user