企微短剧业务系统

PublicAccountDataService.php 7.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: shensong
  5. * Date: 2023/3/24
  6. * Time: 14:00
  7. */
  8. namespace App\Service\Report;
  9. use App\Models\DramaUserRela;
  10. use App\Models\Es\OperateDataTrend;
  11. use App\Models\OfficialAccount;
  12. use App\Models\System\Users;
  13. use App\Service\Admin\AdminService;
  14. class PublicAccountDataService
  15. {
  16. public static function publicAccountDataList($params, $page, $pageSize, $sortField, $sortType)
  17. {
  18. # 表头处理
  19. $commonHeader = config('publicAccountData.basic');
  20. # 获取day1至day150表头
  21. $daysHeader = [];
  22. for($i = 0;$i < OperateDataTrend::ACCOUNT_TREND_DAYS;$i++) {
  23. $title = [
  24. 'name' => 'DAY'.($i+1),
  25. 'column' => 'day'.($i+1)
  26. ];
  27. $daysHeader[] = $title;
  28. }
  29. $header = array_merge($commonHeader, $daysHeader);
  30. # 数据权限
  31. $params['is_super_user'] = AdminService::isSuperUser($params['is_system_admin'], $params['sys_group_id'], $params['admin_id']);
  32. if($params['is_super_user']) {
  33. $params['user_list'] = Users::getCorpUserList($params['sys_group_id']);
  34. if(empty($params['user_list'])) return [0, [], $header];
  35. }
  36. # 查询数据
  37. list($list, $count) = OperateDataTrend::publicAccountDataList($params, $sortField, $sortType, $page, $pageSize);
  38. if(empty($list)) return [0, [], $header];
  39. # 公众号信息
  40. $appIdList = array_unique(array_column($list, 'app_id'));
  41. $officialAccountList = OfficialAccount::getAppInfoList($appIdList);
  42. # 查询所有配置关系
  43. // $dramaList = DramaUserRela::query()->where('enable', 1)->get();
  44. # 投手 运营人员信息
  45. $userList = Users::query()->select(['id', 'name'])->get();
  46. foreach ($list as &$item) {
  47. # 公众号信息处理
  48. $item['app_name'] = $officialAccountList[$item['app_id']] ?? '-';
  49. # 绑定运营人员处理
  50. $operateUser = $userList->whereIn('id', $item['operate_uid_list'])->all();
  51. $item['operator_list'] = array_column($operateUser, 'name');
  52. # 绑定投手信息
  53. $item['pitcher_id_list'] = explode(' ', $item['pitchers']);
  54. $pitcherUser = $userList->whereIn('id', $item['pitcher_id_list'])->all();
  55. $item['pitcher_list'] = array_column($pitcherUser, 'name');
  56. // $operatorCount = count($item['operator_list']);
  57. $operatorCount = 1;
  58. # 消耗金额单位处理
  59. $item['paid'] = ($item['paid'] / 100) / $operatorCount;
  60. # 首日roi
  61. $item['first_roi'] = round($item['first_roi'] * 100, 2) . '%';
  62. # 用户累计充值金额
  63. $item['pay_money_total'] = ($item['pay_money_total'] / 100) / $operatorCount;
  64. # 首日下单成本
  65. $item['first_charge_user_cost'] = round($item['first_charge_user_cost'] / 100, 2);
  66. # 首日新增用户充值金额
  67. $item['pay_money'] = ($item['pay_money'] / 100) / $operatorCount;
  68. # 总回本率
  69. $item['cost_cover_rate'] = round($item['cost_cover_rate'] * 100, 2) . '%';
  70. # 首日付费人数
  71. $item['pay_user'] = $item['pay_user'] / $operatorCount;
  72. # 累计付费人数
  73. $item['pay_user_total'] = $item['pay_user_total'] / $operatorCount;
  74. # 首日付费次数
  75. $item['pay_count'] = $item['pay_count'] / $operatorCount;
  76. # 累计付费次数
  77. $item['pay_count_total'] = $item['pay_count_total'] / $operatorCount;
  78. # 查询150天数据
  79. $dayInfo = [];
  80. $dayData = OperateDataTrend::getDataAfterExpense($params, $item['expense_date'], $item['app_id']);
  81. $paidDataMid = [];
  82. $payMoneyTotal = 0;
  83. if(!empty($dayData)){
  84. foreach($dayData as $val){
  85. $nd = (strtotime($val['ref_date']) - strtotime($val['expense_date'])) / 86400;
  86. $payMoneyTotal += $val['pay_money'];
  87. if(isset($paidDataMid[$nd])) {
  88. $paidDataMid[$nd]['active_fans'] += $val['active_fans'];
  89. $paidDataMid[$nd]['pay_user'] += $val['pay_user'];
  90. $paidDataMid[$nd]['pay_money'] += $val['pay_money'];
  91. $paidDataMid[$nd]['pay_money_total'] += $payMoneyTotal;
  92. } else {
  93. $paidDataMid[$nd]['active_fans'] = $val['active_fans'];
  94. $paidDataMid[$nd]['pay_user'] = $val['pay_user'];
  95. $paidDataMid[$nd]['pay_money'] = $val['pay_money'];
  96. $paidDataMid[$nd]['pay_money_total'] = $payMoneyTotal;
  97. }
  98. }
  99. foreach ($paidDataMid as $i=>$v) {
  100. $new_roi = $item['paid'] ? round($v['pay_money'] / 100 / $item['paid'], 4) * 100 . '%' : '0%';
  101. $total_roi = $item['paid'] ? round($v['pay_money_total'] / 100 / $item['paid'], 4) * 100 . '%' : '0%';
  102. $income_times = $item['pay_money'] ? round($v['pay_money_total'] / 100 / $item['pay_money'], 2) : 0;
  103. $dayInfo[$i] = [
  104. 'days' => 'day' . ($i+1),
  105. 'pay_money' => round($v['pay_money']/100, 2) / $operatorCount,
  106. 'new_roi' => $new_roi,
  107. 'total_roi' => $total_roi,
  108. 'income_times' => $income_times,
  109. 'charge_user' => $v['pay_user'] / $operatorCount
  110. ];
  111. }
  112. }
  113. $item['day_info'] = $dayInfo;
  114. }
  115. return [$count, $list, $header];
  116. }
  117. public static function publicAccountDataSummary($params)
  118. {
  119. # 表头处理
  120. $header = config('publicAccountData.summary');
  121. # 数据权限
  122. $params['is_super_user'] = AdminService::isSuperUser($params['is_system_admin'], $params['sys_group_id'], $params['admin_id']);
  123. if($params['is_super_user']) {
  124. $params['user_list'] = Users::getCorpUserList($params['sys_group_id']);
  125. if(empty($params['user_list'])) return [[], $header];
  126. }
  127. $data = OperateDataTrend::publicAccountDataSummary($params);
  128. # 消耗金额单位处理
  129. $data['paid'] = $data['paid'] / 100;
  130. # 首日roi
  131. $data['first_roi'] = round($data['first_roi'] * 100, 2) . '%';
  132. # 总回本率
  133. $data['cost_cover_rate'] = round($data['cost_cover_rate'] * 100, 2) . '%';
  134. # 首日新增用户充值金额
  135. $data['pay_money'] = $data['pay_money'] / 100;
  136. # 用户累计充值金额
  137. $data['pay_money_total'] = $data['pay_money_total'] / 100;
  138. # 首日下单成本
  139. $data['first_charge_user_cost'] = round($data['first_charge_user_cost'] / 100, 2);
  140. # 公众号
  141. $data['app_name'] = '汇总';
  142. # 运营
  143. $data['operator_list'] = '汇总';
  144. # 投手
  145. $data['pitcher_list'] = '汇总';
  146. # 日期
  147. $data['expense_date'] = '汇总';
  148. return [$data, $header];
  149. }
  150. }