123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211 |
- <?php
- namespace App\Services;
- use App\Models\Account;
- use App\Models\AdsDailyReport;
- use App\Models\AdsReport;
- use App\Models\conf\AccountConf;
- use App\Models\conf\NovelConf;
- use App\Models\conf\PromoterConf;
- use App\Models\Order;
- class PromoterService
- {
- /**
- * 获取推手列表
- * */
- public static function getPromoterList()
- {
- $list = PromoterConf::promoterList();
- return $list;
- }
- /*
- * 获取投手大盘数据
- * */
- public static function promoteCondition($promoterId, $startDate, $endDate, $page, $pageSize)
- {
- if(!$promoterId) return [[], 0, []];
- # 获取对应账号
- list($accountList, $count) = AccountConf::accountList($promoterId, $startDate, $endDate, $page, $pageSize);
- # 获取需查询的账号ID
- $appIds = $accountList->pluck('app_id');
- $appIds = array_unique($appIds->toArray());
- # 查询账号信息
- $accountData = Account::query()->selectRaw("channel_id, platform_id, nickname, app_id")
- ->whereIn('app_id', $appIds)->orderByDesc('created_at')->get();
- $promoters = PromoterConf::promoterList();
- $novels = NovelConf::novelList();
- foreach($accountList as $item) {
- $accountInfo = $accountData->where('app_id', $item->app_id)->first();
- if(empty($accountInfo)) continue;
- // 由于一个公众号可能会对应多个平台,渠道id会多于1个
- $channelList = $accountData->where('app_id', $item->app_id)->pluck('channel_id')->toArray();
- $item->channel_id = $channelList;
- $item->platform_id = $accountInfo->platform_id;
- $item->nickname = $accountInfo->nickname;
- $appId = isset($item->app_id) ? $item->app_id : '';
- $start = $item->start_date < $startDate ? $startDate : $item->start_date;
- $end = $item->end_date > $endDate ? $endDate : $item->end_date;
- # 获取账号对应的充值数据
- $chargeData = Order::chargeDataOfPromoter($appId, $start, $end);
- # 累计充值
- $item->charge_total = isset($chargeData->charge_total) ? $chargeData->charge_total / 100 : 0;
- # 推手
- $promoterInfo = $promoters->where('id', $item->promoter_id)->first();
- $item->promoter = isset($promoterInfo->name) ? $promoterInfo->name : '--';
- # 小说
- $novelInfo = $novels->where('id', $item->novel_id)->first();
- $item->novel = isset($novelInfo->name) ? $novelInfo->name : '--';
- # 累计MP消耗
- $paidData = AdsReport::paidDataOfPromoter($item->app_id, $start, $end);
- $item->paid_total = isset($paidData->paid_total) ? $paidData->paid_total / 100 : 0;
- # 累计ADQ消耗
- list($adqPaid, $adqFollowUv, $stDate, $etDate) = AdsReport::getAdqPaid($item->app_id, $start, $end);
- $item->paid_total += $adqPaid / 100;
- # 总毛利额
- $item->margin_rate = $item->charge_total - $item->paid_total;
- # 回本率
- $item->cost_recovery_rate = $item->paid_total != 0 ? ($item->charge_total / $item->paid_total) : 0;
- # 首日roi = 首日充值/首日消耗
- // $firstDayCharge = Order::chargeDataOfPromoter($item->channel_id, $item->platform_id, $start, $start);
- $date = $start;
- $item->first_day_charge = 0;
- $item->first_day_paid = 0;
- while($date <= $end) {
- $firstDayCharge = Order::getDeadLineChargeData($item->channel_id, $date, $date, $date);
- $firstDayPaid = AdsReport::paidDataOfPromoter($item->app_id, $date, $date);
- list($adqPaid, $adqFollowUv, $stDate, $etDate) = AdsReport::getAdqPaid($item->app_id, $date, $date);
- $item->first_day_charge = isset($firstDayCharge->charge_total) ?
- ($firstDayCharge->charge_total / 100) + $item->first_day_charge : $item->first_day_charge;
- $item->first_day_paid = isset($firstDayPaid->paid_total) ?
- ($firstDayPaid->paid_total / 100) + $item->first_day_paid + ($adqPaid / 100) : $item->first_day_paid + ($adqPaid / 100);
- $date = date('Y-m-d', strtotime($date.' +1 day'));
- }
- // $item->roi = $item->paid_total != 0 ? $item->first_day_charge / $item->paid_total : 0;
- $item->roi = $item->first_day_paid != 0 ? $item->first_day_charge / $item->first_day_paid : 0;
- }
- if(empty($accountList))
- return [[], 0, []];
- # 计算推手总概数据
- $chargeTotal = $accountList->sum('charge_total'); // 累计充值
- $paidTotal = $accountList->sum('paid_total'); // 累计消耗
- $marginRate = $chargeTotal - $paidTotal; // 总毛利额
- $costRecoveryRate = $paidTotal != 0 ? $chargeTotal / $paidTotal : 0; // 回本率
- $firstDayCharge = $accountList->sum('first_day_charge');
- $firstDayPaid = $accountList->sum('first_day_paid');
- $roi = $firstDayPaid !=0 ? $firstDayCharge / $firstDayPaid : 0;
- $condition = [
- 'charge_total' => $chargeTotal,
- 'paid_total' => $paidTotal,
- 'margin_rate' => $marginRate,
- 'cost_cover_rate' => $costRecoveryRate,
- 'roi' => $roi,
- ];
- return [$accountList, $count, $condition];
- }
- /**
- * 获取推手大盘数据
- * */
- public static function getPromoteCondition($promoterId, $startDate, $endDate, $page, $pageSize)
- {
- # 获取对应账号
- list($accountList, $count) = AccountConf::getAccountList($promoterId, $startDate, $endDate, $page, $pageSize);
- $promoters = PromoterConf::promoterList();
- $novels = NovelConf::novelList();
- # 获取需查询的账号ID
- $appIds = $accountList->pluck('app_id');
- $appIds = array_unique($appIds->toArray());
- # 查询账号信息
- $accountData = Account::query()->selectRaw("channel_id, platform_id, nickname, app_id")
- ->whereIn('app_id', $appIds)->orderByDesc('updated_at')->get();
- foreach($accountList as $item) {
- $accountInfo = $accountData->where('app_id', $item->app_id)->first();
- if(empty($accountInfo)) continue;
- // 由于一个公众号可能会对应多个平台,渠道id会多于1个
- $channelList = $accountData->where('app_id', $item->app_id)->pluck('channel_id')->toArray();
- $item->channel_id = $channelList;
- $start = $item->start_date < $startDate ? $startDate : $item->start_date;
- $end = $item->end_date > $endDate ? $endDate : $item->end_date;
- # 获取账号对应的充值数据
- $chargeData = Order::chargeDataOfPromoter($item->app_id, $start, $end);
- # 累计充值
- $item->charge_total = isset($chargeData->charge_total) ? $chargeData->charge_total / 100 : 0;
- # 推手
- $promoterInfo = $promoters->where('id', $item->promoter_id)->first();
- $item->promoter = isset($promoterInfo->name) ? $promoterInfo->name : '--';
- # 小说
- $novelInfo = $novels->where('id', $item->novel_id)->first();
- $item->novel = isset($novelInfo->name) ? $novelInfo->name : '--';
- # 累计消耗
- $paidData = AdsReport::paidDataOfPromoter($item->app_id, $start, $end);
- $item->paid_total = isset($paidData->paid_total) ? $paidData->paid_total / 100 : 0;
- # 总毛利额
- $item->margin_rate = $item->charge_total - $item->paid_total;
- # 回本率
- $item->cost_recovery_rate = $item->paid_total != 0 ? ($item->charge_total / $item->paid_total) : 0;
- # 首日roi = 首日充值/首日消耗
- // $firstDayCharge = Order::chargeDataOfPromoter($item->channel_id, $item->platform_id, $start, $start);
- $date = $start;
- $item->first_day_charge = 0;
- $item->first_day_paid = 0;
- while($date <= $end) {
- $firstDayCharge = Order::getDeadLineChargeData([$item->channel_id], $date, $date, $date);
- $firstDayPaid = AdsReport::paidDataOfPromoter($item->app_id, $date, $date);
- $item->first_day_charge = isset($firstDayCharge->charge_total) ?
- ($firstDayCharge->charge_total / 100) + $item->first_day_charge : $item->first_day_charge;
- $item->first_day_paid = isset($firstDayPaid->paid_total) ?
- ($firstDayPaid->paid_total / 100) + $item->first_day_paid : $item->first_day_paid;
- $date = date('Y-m-d', strtotime($date.' +1 day'));
- }
- $item->roi = $item->first_day_paid != 0 ? $item->first_day_charge / $item->first_day_paid : 0;
- }
- if(empty($accountList))
- return [[], 0, []];
- # 计算推手总概数据
- $chargeTotal = $accountList->sum('charge_total'); // 累计充值
- $paidTotal = $accountList->sum('paid_total'); // 累计消耗
- $marginRate = $chargeTotal - $paidTotal; // 总毛利额
- $costRecoveryRate = $paidTotal != 0 ? $chargeTotal / $paidTotal : 0; // 回本率
- $firstDayCharge = $accountList->sum('first_day_charge');
- $firstDayPaid = $accountList->sum('first_day_paid');
- $roi = $firstDayPaid !=0 ? $firstDayCharge / $firstDayPaid : 0;
- $condition = [
- 'charge_total' => $chargeTotal,
- 'paid_total' => $paidTotal,
- 'margin_rate' => $marginRate,
- 'cost_cover_rate' => $costRecoveryRate,
- 'roi' => $roi,
- ];
- return [$accountList, $count, $condition];
- }
- }
|