ChannelAnalysisLogic.php 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. <?php
  2. // 玩家日志逻
  3. namespace app\v1\logic\gameLog;
  4. use app\v1\logic\tool\ToolLogic;
  5. use plugin\saiadmin\basic\BaseLogic;
  6. use plugin\saiadmin\service\OpenSpoutWriter;
  7. use support\think\Db;
  8. use support\Request;
  9. class channelAnalysisLogic extends BaseLogic
  10. {
  11. // 分时数据
  12. public function getHourDataList($where){
  13. // 合并表 union all
  14. $params = $this->searchByAuth($where);
  15. $orderBy = $where['orderBy'] ?? 'id';
  16. $orderType = $where['orderType'] ?? 'DESC';
  17. $orderBy = $orderBy . ' ' . $orderType;
  18. // hour=>小时, reg_total=>注册数,cost=>消耗,pay_num=>付费总用户数,
  19. // pay_total=>付费总金额,reg_pay_num=>小时注册当天付费数,reg_pay_total=>小时注册当天付费金额
  20. // reg_pay_num_rg=>注册累计付费数,reg_pay_total_rg=>注册累计付费金额
  21. $field = "
  22. agent_id, thour as hour,
  23. SUM(reg_total) as reg_total,
  24. SUM(cost) as cost,
  25. SUM(pay_num) as pay_num,
  26. SUM(pay_total) as pay_total,
  27. SUM(reg_pay_num) as reg_pay_num,
  28. SUM(reg_pay_total) as reg_pay_total,
  29. SUM(reg_pay_num_rg) as reg_pay_num_rg,
  30. SUM(reg_pay_total_rg) as reg_pay_total_rg";
  31. $group = 'agent_id,thour';
  32. $whereSql = $this->generateWhereSql($params);
  33. // 根据日期,连表查询
  34. $tableNames = ToolLogic::getMonthlyTableNames('base_total_hour_', $where['reg_date'][0], $where['reg_date'][1]);
  35. $sqlParts = [];
  36. foreach($tableNames as $tableName){
  37. // 安全过滤,避免 SQL 注入
  38. if (!preg_match('/^base_total_hour_\\d{6}$/', $tableName)) {
  39. continue;
  40. }
  41. $sqlParts[] = "SELECT * FROM {$tableName} WHERE 1=1 {$whereSql}";
  42. }
  43. $unionSql = implode(" UNION ALL ", $sqlParts);
  44. // 外层包裹分页、排序
  45. $finalSql = "
  46. SELECT {$field} FROM ( {$unionSql} ) AS all_hour
  47. GROUP BY {$group}
  48. ORDER BY {$orderBy}
  49. ";
  50. // 按小时数据
  51. $hourData = Db::connect('db_data_report')->query($finalSql);
  52. // 按天消耗数据,查询消耗表,得出每天的消耗数据,agent_id分组
  53. // $finalDayCostSql = "SELECT SUM(money) as cost FROM media_cost WHERE 1=1 {$whereSql} group by agent_id";
  54. // $dayCostData = Db::connect('db_advert')->query($finalDayCostSql);
  55. $total=[
  56. 'agent_id'=>'合计'
  57. ];
  58. // 计算小时消耗数据的 注册成本、付费率、
  59. $hourResult = [];
  60. foreach($hourData as $hourRow){
  61. $agentId = $hourRow['agent_id'];
  62. $hourKey = 'h'.intval($hourRow['hour']);
  63. if (!isset($hourResult[$agentId])) {
  64. $hourResult[$agentId] = [
  65. 'agent_id' => $agentId
  66. ];
  67. }
  68. unset($hourRow['agent_id'],$hourRow['hour']);
  69. // 注册成本
  70. $hourRow['reg_cost'] = ToolLogic::getRound($hourRow['cost'],$hourRow['reg_total']);
  71. // 付费率
  72. $hourRow['pay_rate'] = ToolLogic::getPercent($hourRow['reg_pay_num'],$hourRow['reg_total']);
  73. // 回本率
  74. $hourRow['roi'] = ToolLogic::getPercent($hourRow['reg_pay_total'],$hourRow['cost']);
  75. // 统计数据,每小时的数据
  76. $total[$hourKey]['reg_total'] = !empty($total[$hourKey]['reg_total']) ? ($total[$hourKey]['reg_total'] + $hourRow['reg_total']) : $hourRow['reg_total'];
  77. $total[$hourKey]['cost'] = !empty($total[$hourKey]['cost']) ? ($total[$hourKey]['cost'] + $hourRow['cost']) : $hourRow['cost'];
  78. $total[$hourKey]['reg_pay_total'] = !empty($total[$hourKey]['reg_pay_total']) ? ($total[$hourKey]['reg_pay_total'] + $hourRow['reg_pay_total']) : $hourRow['reg_pay_total'];
  79. $total[$hourKey]['reg_pay_num'] = !empty($total[$hourKey]['reg_pay_num']) ? ($total[$hourKey]['reg_pay_num'] + $hourRow['reg_pay_num']) : $hourRow['reg_pay_num'];
  80. $total['total_raw']['reg_total'] = !empty($total['total_raw']['reg_total']) ? ($total['total_raw']['reg_total'] + $hourRow['reg_total']) : $hourRow['reg_total'];
  81. $total['total_raw']['cost'] = !empty($total['total_raw']['cost']) ? ($total['total_raw']['cost'] + $hourRow['cost']) : $hourRow['cost'];
  82. $total['total_raw']['reg_pay_total'] = !empty($total['total_raw']['reg_pay_total']) ? ($total['total_raw']['reg_pay_total'] + $hourRow['reg_pay_total']) : $hourRow['reg_pay_total'];
  83. $total['total_raw']['reg_pay_num'] = !empty($total['total_raw']['reg_pay_num']) ? ($total['total_raw']['reg_pay_num'] + $hourRow['reg_pay_num']) : $hourRow['reg_pay_num'];
  84. $hourResult[$agentId][$hourKey] = $hourRow;
  85. }
  86. // 计算统计的注册成本、付费绿、回本率
  87. foreach ($total as $key => &$v) {
  88. if (!is_array($v)) continue; // 跳过 agent_id => 合计 等非数组元素
  89. $v['reg_cost'] = ToolLogic::getRound($v['cost'], $v['reg_total']);
  90. $v['pay_rate'] = ToolLogic::getPercent($v['reg_pay_num'], $v['reg_total']);
  91. $v['roi'] = ToolLogic::getPercent($v['reg_pay_total'], $v['cost']);
  92. }
  93. // 计算行汇总
  94. foreach ($hourResult as &$item){
  95. $item['total_raw']['cost'] = round(array_sum(array_column($item, 'cost')), 2);
  96. $item['total_raw']['reg_total'] = round(array_sum(array_column($item, 'reg_total')), 2);
  97. $item['total_raw']['reg_pay_num'] = round(array_sum(array_column($item, 'reg_pay_num')), 2);
  98. $item['total_raw']['reg'] = round(array_sum(array_column($item, 'reg')), 2);
  99. $item['total_raw']['pay_num'] = round(array_sum(array_column($item, 'pay_num')), 2);
  100. $item['total_raw']['pay'] = round(array_sum(array_column($item, 'pay')), 2);
  101. $item['total_raw']['reg_cost'] = ToolLogic::getRound($item['total_raw']['cost'], $item['total_raw']['reg'], 1);
  102. $item['total_raw']['pay_rate'] = ToolLogic::getPercent($item['total_raw']['pay_num'], $item['total_raw']['reg']);
  103. $item['total_raw']['roi'] = ToolLogic::getPercent($item['total_raw']['pay'], $item['total_raw']['cost']);
  104. }
  105. $data = array_values($hourResult);
  106. array_unshift($data, $total);
  107. return $data;
  108. }
  109. // 生成wheresql
  110. public function generateWhereSql($params){
  111. $startDate = $params['reg_date'][0];
  112. $endDate = $params['reg_date'][1];
  113. $whereSql = "";
  114. if(!empty($params['game_id'])){
  115. if (is_array($params['game_id'])) {
  116. $whereSql .= " AND game_id IN(" . implode(',', $params['game_id']) . ")";
  117. } else {
  118. $whereSql .= " AND game_id = {$params['game_id']}";
  119. }
  120. }
  121. if(!empty($params['media_id'])){
  122. $whereSql .= " AND media_id = {$params['media_id']}";
  123. }
  124. if(!empty($params['agent_id'])){
  125. $whereSql .= " AND agent_id = {$params['agent_id']}";
  126. }
  127. if(!empty($params['site_id'])){
  128. if (is_array($params['site_id'])) {
  129. $whereSql .= " AND site_id IN(" . implode(',', $params['site_id']) . ")";
  130. } else {
  131. $whereSql .= " AND site_id = {$params['site_id']}";
  132. }
  133. }
  134. if(!empty($params['auth_id'])){
  135. if (is_array($params['auth_id'])) {
  136. $whereSql .= " AND auth_id IN(" . implode(',', $params['auth_id']) . ")";
  137. } else {
  138. $whereSql .= " AND auth_id = {$params['auth_id']}";
  139. }
  140. }
  141. if(!empty($params['red_date'])){
  142. $whereSql .= " AND tdate BETWEEN '{$startDate}' AND '{$endDate}'";
  143. }
  144. return $whereSql;
  145. }
  146. }