小说推广数据系统

PromoterService.php 9.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. <?php
  2. namespace App\Services;
  3. use App\Models\Account;
  4. use App\Models\AdsDailyReport;
  5. use App\Models\AdsReport;
  6. use App\Models\conf\AccountConf;
  7. use App\Models\conf\NovelConf;
  8. use App\Models\conf\PromoterConf;
  9. use App\Models\Order;
  10. class PromoterService
  11. {
  12. /**
  13. * 获取推手列表
  14. * */
  15. public static function getPromoterList()
  16. {
  17. $list = PromoterConf::promoterList();
  18. return $list;
  19. }
  20. /*
  21. * 获取投手大盘数据
  22. * */
  23. public static function promoteCondition($promoterId, $startDate, $endDate, $page, $pageSize)
  24. {
  25. if(!$promoterId) return [[], 0, []];
  26. # 获取对应账号
  27. list($accountList, $count) = AccountConf::accountList($promoterId, $startDate, $endDate, $page, $pageSize);
  28. # 获取需查询的账号ID
  29. $appIds = $accountList->pluck('app_id');
  30. $appIds = array_unique($appIds->toArray());
  31. # 查询账号信息
  32. $accountData = Account::query()->selectRaw("channel_id, platform_id, nickname, app_id")
  33. ->whereIn('app_id', $appIds)->orderByDesc('created_at')->get();
  34. $promoters = PromoterConf::promoterList();
  35. $novels = NovelConf::novelList();
  36. foreach($accountList as $item) {
  37. $accountInfo = $accountData->where('app_id', $item->app_id)->first();
  38. if(empty($accountInfo)) continue;
  39. // 由于一个公众号可能会对应多个平台,渠道id会多于1个
  40. $channelList = $accountData->where('app_id', $item->app_id)->pluck('channel_id')->toArray();
  41. $item->channel_id = $channelList;
  42. $item->platform_id = $accountInfo->platform_id;
  43. $item->nickname = $accountInfo->nickname;
  44. $appId = isset($item->app_id) ? $item->app_id : '';
  45. $start = $item->start_date < $startDate ? $startDate : $item->start_date;
  46. $end = $item->end_date > $endDate ? $endDate : $item->end_date;
  47. # 获取账号对应的充值数据
  48. $chargeData = Order::chargeDataOfPromoter($appId, $start, $end);
  49. # 累计充值
  50. $item->charge_total = isset($chargeData->charge_total) ? $chargeData->charge_total / 100 : 0;
  51. # 推手
  52. $promoterInfo = $promoters->where('id', $item->promoter_id)->first();
  53. $item->promoter = isset($promoterInfo->name) ? $promoterInfo->name : '--';
  54. # 小说
  55. $novelInfo = $novels->where('id', $item->novel_id)->first();
  56. $item->novel = isset($novelInfo->name) ? $novelInfo->name : '--';
  57. # 累计MP消耗
  58. $paidData = AdsReport::paidDataOfPromoter($item->app_id, $start, $end);
  59. $item->paid_total = isset($paidData->paid_total) ? $paidData->paid_total / 100 : 0;
  60. # 累计ADQ消耗
  61. list($adqPaid, $adqFollowUv, $stDate, $etDate) = AdsReport::getAdqPaid($item->app_id, $start, $end);
  62. $item->paid_total += $adqPaid / 100;
  63. # 总毛利额
  64. $item->margin_rate = $item->charge_total - $item->paid_total;
  65. # 回本率
  66. $item->cost_recovery_rate = $item->paid_total != 0 ? ($item->charge_total / $item->paid_total) : 0;
  67. # 首日roi = 首日充值/首日消耗
  68. // $firstDayCharge = Order::chargeDataOfPromoter($item->channel_id, $item->platform_id, $start, $start);
  69. $date = $start;
  70. $item->first_day_charge = 0;
  71. $item->first_day_paid = 0;
  72. while($date <= $end) {
  73. $firstDayCharge = Order::getDeadLineChargeData($item->channel_id, $date, $date, $date);
  74. $firstDayPaid = AdsReport::paidDataOfPromoter($item->app_id, $date, $date);
  75. list($adqPaid, $adqFollowUv, $stDate, $etDate) = AdsReport::getAdqPaid($item->app_id, $date, $date);
  76. $item->first_day_charge = isset($firstDayCharge->charge_total) ?
  77. ($firstDayCharge->charge_total / 100) + $item->first_day_charge : $item->first_day_charge;
  78. $item->first_day_paid = isset($firstDayPaid->paid_total) ?
  79. ($firstDayPaid->paid_total / 100) + $item->first_day_paid + ($adqPaid / 100) : $item->first_day_paid + ($adqPaid / 100);
  80. $date = date('Y-m-d', strtotime($date.' +1 day'));
  81. }
  82. // $item->roi = $item->paid_total != 0 ? $item->first_day_charge / $item->paid_total : 0;
  83. $item->roi = $item->first_day_paid != 0 ? $item->first_day_charge / $item->first_day_paid : 0;
  84. }
  85. if(empty($accountList))
  86. return [[], 0, []];
  87. # 计算推手总概数据
  88. $chargeTotal = $accountList->sum('charge_total'); // 累计充值
  89. $paidTotal = $accountList->sum('paid_total'); // 累计消耗
  90. $marginRate = $chargeTotal - $paidTotal; // 总毛利额
  91. $costRecoveryRate = $paidTotal != 0 ? $chargeTotal / $paidTotal : 0; // 回本率
  92. $firstDayCharge = $accountList->sum('first_day_charge');
  93. $firstDayPaid = $accountList->sum('first_day_paid');
  94. $roi = $firstDayPaid !=0 ? $firstDayCharge / $firstDayPaid : 0;
  95. $condition = [
  96. 'charge_total' => $chargeTotal,
  97. 'paid_total' => $paidTotal,
  98. 'margin_rate' => $marginRate,
  99. 'cost_cover_rate' => $costRecoveryRate,
  100. 'roi' => $roi,
  101. ];
  102. return [$accountList, $count, $condition];
  103. }
  104. /**
  105. * 获取推手大盘数据
  106. * */
  107. public static function getPromoteCondition($promoterId, $startDate, $endDate, $page, $pageSize)
  108. {
  109. # 获取对应账号
  110. list($accountList, $count) = AccountConf::getAccountList($promoterId, $startDate, $endDate, $page, $pageSize);
  111. $promoters = PromoterConf::promoterList();
  112. $novels = NovelConf::novelList();
  113. # 获取需查询的账号ID
  114. $appIds = $accountList->pluck('app_id');
  115. $appIds = array_unique($appIds->toArray());
  116. # 查询账号信息
  117. $accountData = Account::query()->selectRaw("channel_id, platform_id, nickname, app_id")
  118. ->whereIn('app_id', $appIds)->orderByDesc('updated_at')->get();
  119. foreach($accountList as $item) {
  120. $accountInfo = $accountData->where('app_id', $item->app_id)->first();
  121. if(empty($accountInfo)) continue;
  122. // 由于一个公众号可能会对应多个平台,渠道id会多于1个
  123. $channelList = $accountData->where('app_id', $item->app_id)->pluck('channel_id')->toArray();
  124. $item->channel_id = $channelList;
  125. $start = $item->start_date < $startDate ? $startDate : $item->start_date;
  126. $end = $item->end_date > $endDate ? $endDate : $item->end_date;
  127. # 获取账号对应的充值数据
  128. $chargeData = Order::chargeDataOfPromoter($item->app_id, $start, $end);
  129. # 累计充值
  130. $item->charge_total = isset($chargeData->charge_total) ? $chargeData->charge_total / 100 : 0;
  131. # 推手
  132. $promoterInfo = $promoters->where('id', $item->promoter_id)->first();
  133. $item->promoter = isset($promoterInfo->name) ? $promoterInfo->name : '--';
  134. # 小说
  135. $novelInfo = $novels->where('id', $item->novel_id)->first();
  136. $item->novel = isset($novelInfo->name) ? $novelInfo->name : '--';
  137. # 累计消耗
  138. $paidData = AdsReport::paidDataOfPromoter($item->app_id, $start, $end);
  139. $item->paid_total = isset($paidData->paid_total) ? $paidData->paid_total / 100 : 0;
  140. # 总毛利额
  141. $item->margin_rate = $item->charge_total - $item->paid_total;
  142. # 回本率
  143. $item->cost_recovery_rate = $item->paid_total != 0 ? ($item->charge_total / $item->paid_total) : 0;
  144. # 首日roi = 首日充值/首日消耗
  145. // $firstDayCharge = Order::chargeDataOfPromoter($item->channel_id, $item->platform_id, $start, $start);
  146. $date = $start;
  147. $item->first_day_charge = 0;
  148. $item->first_day_paid = 0;
  149. while($date <= $end) {
  150. $firstDayCharge = Order::getDeadLineChargeData([$item->channel_id], $date, $date, $date);
  151. $firstDayPaid = AdsReport::paidDataOfPromoter($item->app_id, $date, $date);
  152. $item->first_day_charge = isset($firstDayCharge->charge_total) ?
  153. ($firstDayCharge->charge_total / 100) + $item->first_day_charge : $item->first_day_charge;
  154. $item->first_day_paid = isset($firstDayPaid->paid_total) ?
  155. ($firstDayPaid->paid_total / 100) + $item->first_day_paid : $item->first_day_paid;
  156. $date = date('Y-m-d', strtotime($date.' +1 day'));
  157. }
  158. $item->roi = $item->first_day_paid != 0 ? $item->first_day_charge / $item->first_day_paid : 0;
  159. }
  160. if(empty($accountList))
  161. return [[], 0, []];
  162. # 计算推手总概数据
  163. $chargeTotal = $accountList->sum('charge_total'); // 累计充值
  164. $paidTotal = $accountList->sum('paid_total'); // 累计消耗
  165. $marginRate = $chargeTotal - $paidTotal; // 总毛利额
  166. $costRecoveryRate = $paidTotal != 0 ? $chargeTotal / $paidTotal : 0; // 回本率
  167. $firstDayCharge = $accountList->sum('first_day_charge');
  168. $firstDayPaid = $accountList->sum('first_day_paid');
  169. $roi = $firstDayPaid !=0 ? $firstDayCharge / $firstDayPaid : 0;
  170. $condition = [
  171. 'charge_total' => $chargeTotal,
  172. 'paid_total' => $paidTotal,
  173. 'margin_rate' => $marginRate,
  174. 'cost_cover_rate' => $costRecoveryRate,
  175. 'roi' => $roi,
  176. ];
  177. return [$accountList, $count, $condition];
  178. }
  179. }