AnalyseLogic.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. <?php
  2. // 玩家日志逻
  3. namespace app\v1\logic\gameLog;
  4. use app\v1\logic\tool\ToolLogic;
  5. use plugin\saiadmin\basic\BaseLogic;
  6. use support\think\Db;
  7. class AnalyseLogic extends BaseLogic
  8. {
  9. // 注册按日
  10. public function getRegDayDataList($where){
  11. $params = $this->searchByAuth($where);
  12. $whereSql = $this->generateWhereSql($params);
  13. $field = "SUM(reg_total) AS reg_total, tdate,game_id";
  14. $baseData = $this->generateYearUnionList('base_total_day_',$where['reg_date'],$whereSql,$field,'game_id,tdate');
  15. $data=[];
  16. foreach($baseData as $row){
  17. $tdate = $row['tdate'];
  18. $game_id = $row['game_id'];
  19. $data[$game_id][$tdate] = $row['reg_total'];
  20. $data[$game_id]['total'] = !empty($data[$game_id]['total']) ? $data[$game_id]['total'] + $row['reg_total'] : $row['reg_total'];
  21. $data[$game_id]['game_id'] = $game_id;
  22. }
  23. $list = array_values($data);
  24. $totalRow = ['game_id'=>'合计'];
  25. foreach($list as &$row){
  26. $totalRow['total'] = !empty($totalRow['total']) ? $totalRow['total'] + $row['total'] : $row['total'];
  27. foreach ($row as $key => $value) {
  28. if($key == 'total' || $key == 'game_id'){
  29. continue;
  30. }
  31. $totalRow[$key] = !empty($totalRow[$key]) ? $totalRow[$key] + $value : $value;
  32. }
  33. }
  34. // 获取两个注册日期之间的日期列表
  35. $dateList = ToolLogic::getDatesBetween($where['reg_date'][0],$where['reg_date'][1]);
  36. $columnsData = [
  37. [
  38. 'title' => '游戏ID',
  39. 'dataIndex' => 'game_id',
  40. 'width' => 120,
  41. ],
  42. [
  43. 'title' => '游戏',
  44. 'dataIndex' => 'game_name',
  45. 'width' => 120,
  46. ],
  47. [
  48. 'title' => '合计',
  49. 'dataIndex' => 'total',
  50. 'width' => 120,
  51. ]
  52. ];
  53. foreach($dateList as $date){
  54. $columnsData[] = [
  55. 'title' => $date,
  56. 'dataIndex' => $date,
  57. 'width' => 120,
  58. ];
  59. }
  60. $result['data'] = $list;
  61. $result['totalRow'] = $totalRow;
  62. $result['columns'] = $columnsData;
  63. return $result;
  64. }
  65. // 注册按时
  66. public function getRegHourDataList($where){
  67. $params = $this->searchByAuth($where);
  68. $field = "SUM(role_create_user) AS role_create_user, tdate,game_id,thour";
  69. $regDate = $where['reg_date'];
  70. // 将$regDate转化为年月格式,如202509
  71. $ym = date('Ym', strtotime($regDate));
  72. $tableName = 'base_total_hour_'.$ym;
  73. $whereSql = 'WHERE 1=1';
  74. if(!empty($params['game_id'])){
  75. $whereSql .= " AND game_id IN(" . implode(',', $params['game_id']) . ")";
  76. }
  77. if(!empty($params['reg_date'])){
  78. $whereSql .= " AND tdate = '" .$params['reg_date']. "'";
  79. }
  80. $sql = "SELECT {$field} FROM {$tableName} {$whereSql} GROUP BY game_id,thour,tdate";
  81. $baseData = Db::connect('db_data_report')->query($sql);
  82. $data = [];
  83. foreach($baseData as &$row){
  84. $game_id = $row['game_id'];
  85. $thour = $row['thour'];
  86. $data[$game_id]['h'.$thour] = $row['role_create_user'];
  87. $data[$game_id]['total'] = !empty($data[$game_id]['total']) ? $data[$game_id]['total'] + $row['role_create_user'] : $row['role_create_user'];
  88. $data[$game_id]['game_id'] = $game_id;
  89. }
  90. $list = array_values($data);
  91. $totalRow = ['game_id'=>'合计'];
  92. foreach($list as &$row){
  93. $totalRow['total'] = !empty($totalRow['total']) ? $totalRow['total'] + $row['total'] : $row['total'];
  94. foreach ($row as $key => $value) {
  95. if($key == 'total' || $key == 'game_id'){
  96. continue;
  97. }
  98. $totalRow[$key] = !empty($totalRow[$key]) ? $totalRow[$key] + $value : $value;
  99. }
  100. }
  101. $result['data'] = $list;
  102. $result['totalRow'] = $totalRow;
  103. return $result;
  104. }
  105. // 留存按日
  106. public function getRetentionDayDataList($where){
  107. $params = $this->searchByAuth($where);
  108. // 构建whereSql
  109. $whereSql = '';
  110. if(!empty($params['game_id'])){
  111. $whereSql .= " AND game_id IN(" . implode(',', $params['game_id']) . ")";
  112. }
  113. if(!empty($params['reg_date'])){
  114. $whereSql .= " AND tdate BETWEEN '{$params['reg_date'][0]}' AND '{$params['reg_date'][1]}'";
  115. }
  116. $data = [];
  117. $totalRow = ['game_id'=>'合计'];
  118. $baseField = "SUM(reg_total) AS reg_total, tdate,game_id";
  119. $activeField = "SUM(active_total) AS active_total, reg_date,game_id,days";
  120. // 1. 查询每天的注册数,根据注册时间,游戏
  121. $baseData = $this->generateYearUnionList('base_total_day_',$params['reg_date'],$whereSql,$baseField,'game_id,tdate');
  122. $baseData = array_column($baseData,null,'tdate');
  123. // 2. 查询每天的活跃数,根据注册时间,游戏,留存天数
  124. $activeWhereSql = str_replace("AND tdate", "AND reg_date", $whereSql);
  125. if(!empty($params['days'])){
  126. $activeWhereSql .= " AND days = {$params['days']}";
  127. }
  128. $activeData = $this->generateYearUnionList('game_active_day_',$params['reg_date'],$activeWhereSql,$activeField,'game_id,reg_date');
  129. $activeData = array_column($activeData,null,'reg_date');
  130. foreach($baseData as $row){
  131. $game_id = $row['game_id'];
  132. $tdate = $row['tdate'];
  133. $data[$game_id][$tdate]['reg_total'] = $row['reg_total'];
  134. $data[$game_id][$tdate]['active_total'] = !empty($activeData[$tdate]['active_total']) ? $activeData[$tdate]['active_total'] : 0;
  135. $data[$game_id][$tdate]['retention_total'] = ToolLogic::getPercent($data[$game_id][$tdate]['active_total'],$data[$game_id][$tdate]['reg_total']);
  136. // 每日合计
  137. $data[$game_id]['total_reg_total'] = !empty($data[$game_id]['total_reg_total']) ? $data[$game_id]['total_reg_total'] + $data[$game_id][$tdate]['reg_total'] : $data[$game_id][$tdate]['reg_total'];
  138. $data[$game_id]['total_active_total'] = !empty($data[$game_id]['total_active_total']) ? $data[$game_id]['total_active_total'] + $data[$game_id][$tdate]['active_total'] : $data[$game_id][$tdate]['active_total'];
  139. // 底部合计
  140. $totalRow['total_reg_total'] = !empty($totalRow['total_reg_total']) ? $totalRow['total_reg_total'] + $data[$game_id][$tdate]['reg_total'] : $data[$game_id][$tdate]['reg_total'];
  141. $totalRow['total_active_total'] = !empty($totalRow['total_active_total']) ? $totalRow['total_active_total'] + $data[$game_id][$tdate]['active_total'] : $data[$game_id][$tdate]['active_total'];
  142. $totalRow[$tdate]['reg_total'] = !empty($totalRow[$tdate]['reg_total']) ? $totalRow[$tdate]['reg_total'] + $data[$game_id][$tdate]['reg_total'] : $data[$game_id][$tdate]['reg_total'];
  143. $totalRow[$tdate]['active_total'] = !empty($totalRow[$tdate]['active_total']) ? $totalRow[$tdate]['active_total'] + $data[$game_id][$tdate]['active_total'] : $data[$game_id][$tdate]['active_total'];
  144. }
  145. $data = array_values($data);
  146. foreach($data as &$row){
  147. foreach($row as $key => $value){
  148. if($key == 'total_reg_total' || $key == 'game_id'){
  149. continue;
  150. }
  151. if($key == 'total_active_total'){
  152. $row['total'] = ToolLogic::getPercent($row['total_active_total'],$row['total_reg_total']);
  153. }else{
  154. $row[$key] = ToolLogic::getPercent($value['active_total'],$value['reg_total']);
  155. }
  156. }
  157. }
  158. foreach($totalRow as $totalKey => &$row){
  159. if($totalKey == 'game_id' || $totalKey == 'total_active_total'||$totalKey == 'total_reg_total'){
  160. continue;
  161. }
  162. $totalRow[$totalKey] = ToolLogic::getPercent($row['active_total'],$row['reg_total']);
  163. }
  164. $totalRow['total'] = ToolLogic::getPercent($totalRow['total_active_total'],$totalRow['total_reg_total']);
  165. $result['data'] = $data;
  166. $result['totalRow'] = $totalRow;
  167. return $result;
  168. }
  169. // 生成基础日统计表的联合查询列表
  170. public function generateYearUnionList($namePrefix, $regDate,$whereSql='',$field='*',$group=null){
  171. $tableNames = ToolLogic::getYearlyTableNames($namePrefix, $regDate[0],$regDate[1]);
  172. $sqlParts = [];
  173. foreach ($tableNames as $tableName){
  174. $sqlParts[] = "SELECT * FROM {$tableName} WHERE 1=1 {$whereSql}";
  175. }
  176. $unionSql = implode(" UNION ALL ", $sqlParts);
  177. $finalSql = "
  178. SELECT {$field} FROM ( {$unionSql} ) AS all_total_day
  179. ";
  180. if (!empty($group)) {
  181. $finalSql .= " GROUP BY {$group}";
  182. }
  183. $baseData = Db::connect('db_data_report')->query($finalSql);
  184. return $baseData;
  185. }
  186. // 生成wheresql
  187. public function generateWhereSql($params){
  188. $whereSql = "";
  189. // 游戏id
  190. if(!empty($params['game_id'])){
  191. if (is_array($params['game_id'])) {
  192. $whereSql .= " AND game_id IN(" . implode(',', $params['game_id']) . ")";
  193. } else {
  194. $whereSql .= " AND game_id = {$params['game_id']}";
  195. }
  196. }
  197. // 媒体id
  198. if(!empty($params['media_id'])){
  199. $whereSql .= " AND media_id = {$params['media_id']}";
  200. }
  201. // 渠道id
  202. if(!empty($params['agent_id'])){
  203. $whereSql .= " AND agent_id = {$params['agent_id']}";
  204. }
  205. // 广告位id
  206. if(!empty($params['site_id'])){
  207. if (is_array($params['site_id'])) {
  208. $whereSql .= " AND site_id IN(" . implode(',', $params['site_id']) . ")";
  209. } else {
  210. $whereSql .= " AND site_id = {$params['site_id']}";
  211. }
  212. }
  213. // 负责人
  214. if(!empty($params['auth_id'])){
  215. if (is_array($params['auth_id'])) {
  216. $whereSql .= " AND auth_id IN(" . implode(',', $params['auth_id']) . ")";
  217. } else {
  218. $whereSql .= " AND auth_id = {$params['auth_id']}";
  219. }
  220. }
  221. // 注册日期
  222. if(!empty($params['reg_date'])??null){
  223. $whereSql .= " AND tdate BETWEEN '{$params['reg_date'][0]}' AND '{$params['reg_date'][1]}'";
  224. }
  225. return $whereSql;
  226. }
  227. }