diff --git a/app/service/AdsInsightService.php b/app/service/AdsInsightService.php index 91b9384..9744136 100644 --- a/app/service/AdsInsightService.php +++ b/app/service/AdsInsightService.php @@ -69,9 +69,9 @@ class AdsInsightService COALESCE(SUM(d.adds_to_cart), 0) as adds_to_cart, COALESCE(SUM(d.cost_per_atc), 0) as cost_per_atc, COALESCE(SUM(d.purchases), 0) as purchases, - COALESCE(SUM(d.purchases_value), 0) as purchases_value, - COALESCE(SUM(d.revenue), 0) as revenue, - COALESCE(SUM(d.total_cost), 0) as total_cost, + COALESCE(SUM(d.purchases_value / 1000000), 0) as purchases_value, + COALESCE(SUM(d.revenue / 1000000), 0) as revenue, + COALESCE(SUM(d.total_cost / 1000000), 0) as total_cost, -1 as conversion_rate, -1 as roas, -1 as ctr,-1 as net_profit,-1 as net_profit_margin,-1 as net_profit_on_ad_spend') ->group('c.campaign_id, c.status, c.account_id, c.name,c.platform_type') ->where('c.account_id', 'in', $customerIds); // 添加 customerIds 条件 @@ -116,9 +116,9 @@ class AdsInsightService 'revenue' => '$' . number_format(array_sum(array_column($allCampaigns, '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($total_revenue - $total_cost, 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), // 广告支出净利润 + 'net_profit_on_ad_spend' => $total_spend == 0 ? '-' : round(($total_revenue - $total_cost) / $total_spend, 2) * 100 . '%', // 广告支出净利润 // 计算总的 CTR 'ctr' => ($total_impressions > 0) ? number_format(($total_clicks / $total_impressions) * 100, 2) . '%' : '-', // 格式化为百分比 ]; @@ -133,11 +133,11 @@ class AdsInsightService // Conversion Rate 的计算:转换率 = 购买数 / 点击数 $conversion_rate = $item['clicks'] > 0 ? round(($item['purchases'] / $item['clicks']) * 100, 2) . '%' : '-'; // Net Profit 的计算:净利润 = 收入 - 总成本 - $net_profit = ($item['revenue'] - $item['total_cost']) >= 0 ? '+$' . number_format($item['revenue'] - $item['total_cost'], 2) : '-$' . number_format($item['revenue'] - $item['total_cost'], 2); + $net_profit = ($item['revenue'] - $item['total_cost']) >= 0 ? '+$' . number_format($item['revenue'] - $item['total_cost'], 2) : '-$' . number_format(abs($item['revenue'] - $item['total_cost']), 2); // Net Profit Margin 的计算:净利润率 = 净利润 / 收入 $net_profit_margin = $item['revenue'] > 0 ? round(($item['revenue'] - $item['total_cost']) / $item['revenue'], 2) * 100 . '%' : '-'; // Net Profit on Ad Spend 的计算:广告支出净利润 = (收入 - 总成本) / 广告支出 - $net_profit_on_ad_spend = $item['spend'] > 0 ? round(($item['revenue'] - $item['total_cost']) / $item['spend'], 2) : '-'; + $net_profit_on_ad_spend = $item['spend'] > 0 ? round(($item['revenue'] - $item['total_cost']) / $item['spend'], 2) * 100 . '%' : '-'; return [ 'id' => $item['campaign_id'], 'platform_type' => $item['platform_type'], @@ -224,9 +224,9 @@ class AdsInsightService COALESCE(SUM(d.adds_to_cart), 0) as adds_to_cart, COALESCE(SUM(d.cost_per_atc), 0) as cost_per_atc, COALESCE(SUM(d.purchases), 0) as purchases, - COALESCE(SUM(d.purchases_value), 0) as purchases_value, - COALESCE(SUM(d.revenue), 0) as revenue, - COALESCE(SUM(d.total_cost), 0) as total_cost, + COALESCE(SUM(d.purchases_value / 1000000), 0) as purchases_value, + COALESCE(SUM(d.revenue / 1000000), 0) as revenue, + COALESCE(SUM(d.total_cost / 1000000), 0) as total_cost, -1 as conversion_rate, -1 as roas, -1 as ctr,-1 as net_profit,-1 as net_profit_margin,-1 as net_profit_on_ad_spend') ->group('s.ad_set_id, s.status, s.account_id, s.name,s.platform_type,c.name') ->where('s.account_id', 'in', $customerIds); // 添加 customerIds 条件 @@ -273,9 +273,9 @@ class AdsInsightService 'revenue' => '$' . number_format(array_sum(array_column($allAdsets, '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($total_revenue - $total_cost, 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), // 广告支出净利润 + 'net_profit_on_ad_spend' => $total_spend == 0 ? '-' : round(($total_revenue - $total_cost) / $total_spend, 2) * 100 . '%', // 广告支出净利润 // 计算总的 CTR 'ctr' => ($total_impressions > 0) ? number_format(($total_clicks / $total_impressions) * 100, 2) . '%' : '-', // 格式化为百分比 ]; @@ -290,11 +290,11 @@ class AdsInsightService // Conversion Rate 的计算:转换率 = 购买数 / 点击数 $conversion_rate = $item['clicks'] > 0 ? round(($item['purchases'] / $item['clicks']) * 100, 2) . '%' : '-'; // Net Profit 的计算:净利润 = 收入 - 总成本 - $net_profit = ($item['revenue'] - $item['total_cost']) >= 0 ? '+$' . number_format($item['revenue'] - $item['total_cost'], 2) : '-$' . number_format($item['revenue'] - $item['total_cost'], 2); + $net_profit = ($item['revenue'] - $item['total_cost']) >= 0 ? '+$' . number_format($item['revenue'] - $item['total_cost'], 2) : '-$' . number_format(abs($item['revenue'] - $item['total_cost']), 2); // Net Profit Margin 的计算:净利润率 = 净利润 / 收入 $net_profit_margin = $item['revenue'] > 0 ? round(($item['revenue'] - $item['total_cost']) / $item['revenue'], 2) * 100 . '%' : '-'; // Net Profit on Ad Spend 的计算:广告支出净利润 = (收入 - 总成本) / 广告支出 - $net_profit_on_ad_spend = $item['spend'] > 0 ? round(($item['revenue'] - $item['total_cost']) / $item['spend'], 2) : '-'; + $net_profit_on_ad_spend = $item['spend'] > 0 ? round(($item['revenue'] - $item['total_cost']) / $item['spend'], 2) * 100 . '%' : '-'; return [ 'id' => $item['ad_set_id'], 'platform_type' => $item['platform_type'], @@ -378,9 +378,9 @@ class AdsInsightService COALESCE(SUM(d.adds_to_cart), 0) as adds_to_cart, COALESCE(SUM(d.cost_per_atc), 0) as cost_per_atc, COALESCE(SUM(d.purchases), 0) as purchases, - COALESCE(SUM(d.purchases_value), 0) as purchases_value, - COALESCE(SUM(d.revenue), 0) as revenue, - COALESCE(SUM(d.total_cost), 0) as total_cost, + COALESCE(SUM(d.purchases_value) / 1000000, 0) as purchases_value, + COALESCE(SUM(d.revenue) / 1000000, 0) as revenue, + COALESCE(SUM(d.total_cost) / 1000000, 0) as total_cost, -1 as conversion_rate, -1 as roas, -1 as ctr, -1 as net_profit, -1 as net_profit_margin, -1 as net_profit_on_ad_spend') ->group('a.ad_id, a.status, a.account_id, a.name,a.platform_type, s.name') ->where('a.account_id', 'in', $customerIds); // 添加 customerIds 条件 @@ -427,9 +427,9 @@ class AdsInsightService '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($total_revenue - $total_cost, 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), + '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) . '%' : '-', // 格式化为百分比 ]; @@ -438,7 +438,17 @@ class AdsInsightService // 确保数据格式统一 $result = array_map(function ($item) { - $ctr = $item['impressions'] > 0 ? number_format(($item['clicks'] / $item['impressions']) * 100, 2) . '%' : '-'; // 格式化为百分比 + // CTR 的计算:点击率 = 点击数 / 展示数 + $ctr = $item['impressions'] > 0 ? number_format(($item['clicks'] / $item['impressions']) * 100, 2) . '%' : '-'; + // Conversion Rate 的计算:转换率 = 购买数 / 点击数 + $conversion_rate = $item['clicks'] > 0 ? round(($item['purchases'] / $item['clicks']) * 100, 2) . '%' : '-'; + // Net Profit 的计算:净利润 = 收入 - 总成本 + $net_profit = ($item['revenue'] - $item['total_cost']) >= 0 ? '+$' . number_format($item['revenue'] - $item['total_cost'], 2) : '-$' . number_format(abs($item['revenue'] - $item['total_cost']), 2); + // Net Profit Margin 的计算:净利润率 = 净利润 / 收入 + $net_profit_margin = $item['revenue'] > 0 ? round(($item['revenue'] - $item['total_cost']) / $item['revenue'], 2) * 100 . '%' : '-'; + // Net Profit on Ad Spend 的计算:广告支出净利润 = (收入 - 总成本) / 广告支出 + $net_profit_on_ad_spend = $item['spend'] > 0 ? round(($item['revenue'] - $item['total_cost']) / $item['spend'], 2) * 100 . '%' : '-'; + return [ 'id' => $item['ad_id'], 'platform_type' => $item['platform_type'], @@ -460,10 +470,10 @@ class AdsInsightService 'cost_per_purchase' => $item['purchases'] > 0 ? '$' . number_format(($item['spend'] / $item['purchases']), 2) : '$0.00', 'revenue' => '$' . number_format($item['revenue'], 2), // 格式化收入 'total_cost' => '$' . number_format($item['total_cost'], 2), // 格式化总成本 - 'conversion_rate' => '-', // 没有提供有效的计算,保持为 '-' - 'net_profit' => '-', // 没有提供 net_profit 计算,保持为 '-' - 'net_profit_margin' => '-', // 没有提供 net_profit_margin 计算,保持为 '-' - 'net_profit_on_ad_spend' => '-', // 没有提供 net_profit_on_ad_spend 计算,保持为 '-' + 'conversion_rate' => $conversion_rate, + 'net_profit' => $net_profit, + 'net_profit_margin' => $net_profit_margin, + 'net_profit_on_ad_spend' => $net_profit_on_ad_spend, ]; }, $ads->items()); @@ -523,9 +533,9 @@ class AdsInsightService COALESCE(SUM(d.adds_to_cart), 0) as adds_to_cart, COALESCE(SUM(d.cost_per_atc), 0) as cost_per_atc, COALESCE(SUM(d.purchases), 0) as purchases, - COALESCE(SUM(d.purchases_value), 0) as purchases_value, - COALESCE(SUM(d.revenue), 0) as revenue, - COALESCE(SUM(d.total_cost), 0) as total_cost') + COALESCE(SUM(d.purchases_value/1000000), 0) as purchases_value, + COALESCE(SUM(d.revenue/1000000), 0) as revenue, + COALESCE(SUM(d.total_cost)/1000000, 0) as total_cost') ->leftJoin('bps.bps_ads_insights d', "a.advertiser_id = d.account_id AND {$dateCondition}") ->where('u.id', 'in', $userIds); // 添加 userIds 条件 @@ -586,9 +596,9 @@ class AdsInsightService 'revenue' => '$' . number_format($total_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($total_revenue - $total_cost, 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), // 广告支出净利润 + 'net_profit_on_ad_spend' => $total_spend == 0 ? '-' : round(($total_revenue - $total_cost) / $total_spend, 2) * 100 . '%', // 广告支出净利润 'ctr' => $ctr, // CTR 字段 ]; @@ -655,11 +665,11 @@ class AdsInsightService // Conversion Rate 的计算:转换率 = 购买数 / 点击数 $conversion_rate = $data['clicks'] > 0 ? round(($data['purchases'] / $data['clicks']) * 100, 2) . '%' : '-'; // Net Profit 的计算:净利润 = 收入 - 总成本 - $net_profit = ($data['revenue'] - $data['total_cost']) >= 0 ? '+$' . number_format($data['revenue'] - $data['total_cost'], 2) : '-$' . number_format($data['revenue'] - $data['total_cost'], 2); + $net_profit = ($data['revenue'] - $data['total_cost']) >= 0 ? '+$' . number_format($data['revenue'] - $data['total_cost'], 2) : '-$' . number_format(abs($data['revenue'] - $data['total_cost']), 2); // Net Profit Margin 的计算:净利润率 = 净利润 / 收入 $net_profit_margin = $data['revenue'] > 0 ? round(($data['revenue'] - $data['total_cost']) / $data['revenue'], 2) * 100 . '%' : '-'; // Net Profit on Ad Spend 的计算:广告支出净利润 = (收入 - 总成本) / 广告支出 - $net_profit_on_ad_spend = $data['spend'] > 0 ? round(($data['revenue'] - $data['total_cost']) / $data['spend'], 2) : '-'; + $net_profit_on_ad_spend = $data['spend'] > 0 ? round(($data['revenue'] - $data['total_cost']) / $data['spend'], 2) * 100 . '%' : '-'; // 计算 ROAS, CTR 和其他需要的字段 $aggregatedUsers[$userId]['roas'] = $total_spend == 0 ? '-' : round($total_purchases_value / $total_spend, 2); @@ -674,30 +684,30 @@ class AdsInsightService $aggregatedUsers[$userId]['net_profit_margin'] = $net_profit_margin; $aggregatedUsers[$userId]['net_profit_on_ad_spend'] = $net_profit_on_ad_spend; } - // 如果 aggregatedUsers 为空,返回默认的数据结构 + // 如果 aggregatedUsers 为空,返回默认的数据结构 if (empty($aggregatedUsers)) { $emptyUser = []; - $query = ThirdUser::alias('u') - ->cache(false) // 强制不使用缓存 - ->leftJoin('bps.bps_third_user_advertiser a', "u.id = a.doc_") - ->field('u.id as user_id,u.third_type,a.advertiser_name'); - // 添加关键字过滤条件 + $query = ThirdUser::alias('u') + ->cache(false) // 强制不使用缓存 + ->leftJoin('bps.bps_third_user_advertiser a', "u.id = a.doc_") + ->field('u.id as user_id,u.third_type,a.advertiser_name'); + // 添加关键字过滤条件 $query->where(function ($query) use ($keyword, $platformType) { // if ($keyword) { // $query->where('a.advertiser_name', 'like', '%' . $keyword . '%'); // } if ($platformType) { - $a = (int)$platformType; - $platformTypeNames = [1=>'facebook',2=>'google',3=>'tiktok']; + $a = (int)$platformType; + $platformTypeNames = [1 => 'facebook', 2 => 'google', 3 => 'tiktok']; $query->where('u.third_type', '=', $platformTypeNames[$a]); } }); - // 获取所有符合条件的数据(不分页) - $allUsers = $query->select()->toArray(); + // 获取所有符合条件的数据(不分页) + $allUsers = $query->select()->toArray(); foreach ($allUsers as $user) { - if (in_array($user['user_id'], $userIds)) { - $emptyUser[] = [ + if (in_array($user['user_id'], $userIds)) { + $emptyUser[] = [ 'user_id' => $user['user_id'], 'platform_type' => (int)$platformType, 'advertiser_name' => $user['advertiser_name'], @@ -718,7 +728,7 @@ class AdsInsightService 'net_profit_on_ad_spend' => '-', 'ctr' => '-', ]; - } + } } // dump($allUsers,'222');