SummaryV2.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. <?php
  2. namespace app\admin\controller;
  3. use think\Db;
  4. /**
  5. * 数据统计与展示控制器 V2
  6. */
  7. class SummaryV2 extends Admin
  8. {
  9. /**
  10. * 获取可用的单端游戏列表
  11. */
  12. private function getGameList()
  13. {
  14. return Db::table('cy_game')
  15. ->field('id, name, type')
  16. ->where('cooperation_status', '<>', 0)
  17. ->order('id desc')
  18. ->select();
  19. }
  20. /**
  21. * 获取游戏组列表,组 ID 和名称直接取自 nw_game_band
  22. */
  23. private function getGameBandList()
  24. {
  25. $bandList = Db::table('nw_game_band')
  26. ->field('id, game_name as name')
  27. ->order('id desc')
  28. ->select();
  29. foreach ($bandList as &$band) {
  30. $band['name'] = trim($band['name']);
  31. if ($band['name'] === '') {
  32. $band['name'] = '未命名游戏组#' . $band['id'];
  33. }
  34. }
  35. unset($band);
  36. return $bandList;
  37. }
  38. /**
  39. * 获取单端游戏与游戏组列表(用于前台合并下拉选项)
  40. */
  41. private function getGameAndBandList()
  42. {
  43. return [$this->getGameList(), $this->getGameBandList()];
  44. }
  45. /**
  46. * 解析前端下拉框传来的复合游戏筛选值 (例如 "band_5" 或 "game_10")
  47. */
  48. private function parseGameFilter($filterValue)
  49. {
  50. $gameId = 0;
  51. $gameBandId = 0;
  52. if (!empty($filterValue)) {
  53. if (strpos($filterValue, 'band_') === 0) {
  54. $gameBandId = (int)str_replace('band_', '', $filterValue);
  55. } elseif (strpos($filterValue, 'game_') === 0) {
  56. $gameId = (int)str_replace('game_', '', $filterValue);
  57. }
  58. }
  59. return [$gameId, $gameBandId];
  60. }
  61. /**
  62. * 校验统计页面的查询时间范围。
  63. */
  64. private function validateDateRange($start, $end)
  65. {
  66. $latestDate = date('Y-m-d', strtotime('yesterday'));
  67. if (($start !== '' && strtotime($start) > strtotime($latestDate))
  68. || ($end !== '' && strtotime($end) > strtotime($latestDate))) {
  69. $this->error('查询时间:最晚只能选择昨日');
  70. }
  71. if ($start !== '' && $end !== '' && strtotime($start) > strtotime($end)) {
  72. $this->error('查询时间:开始时间 不能大于 结束时间');
  73. }
  74. }
  75. /**
  76. * 获取统计页面的默认查询时间范围:昨日往前一个月至昨日。
  77. */
  78. private function getDefaultDateRange()
  79. {
  80. $defaultEndTimestamp = strtotime('yesterday');
  81. return [
  82. date('Y-m-d', strtotime('-1 month', $defaultEndTimestamp)),
  83. date('Y-m-d', $defaultEndTimestamp),
  84. ];
  85. }
  86. /**
  87. * 1. 数据汇总页面(仅展示游戏组;全部=所有游戏组)
  88. */
  89. public function summary()
  90. {
  91. $bandList = $this->getGameBandList();
  92. $bandMap = array_column($bandList, 'name', 'id');
  93. list($defaultStart, $defaultEnd) = $this->getDefaultDateRange();
  94. $start = input('get.start', $defaultStart) ?: $defaultStart;
  95. $end = input('get.end', $defaultEnd) ?: $defaultEnd;
  96. $gameFilter = input('get.game_filter', '');
  97. $this->validateDateRange($start, $end);
  98. // 拼接查询条件:只查游戏组数据,不混入单端游戏
  99. $where = [];
  100. $where['stat_date'] = [['>=', $start], ['<=', $end]];
  101. $where['game_id'] = 0;
  102. list(, $gameBandId) = $this->parseGameFilter($gameFilter);
  103. if ($gameBandId > 0) {
  104. $where['game_band_id'] = $gameBandId;
  105. } else {
  106. // 全部 = 所有游戏组
  107. $where['game_band_id'] = ['>', 0];
  108. }
  109. // 分页查询 cy_summary_game_daily 表(日期倒序,最新在前)
  110. $list = Db::table('cy_summary_game_daily')
  111. ->where($where)
  112. ->order(['stat_date' => 'desc', 'game_band_id' => 'asc', 'id' => 'desc'])
  113. ->paginate(20, false, ['query' => input('get.')])
  114. ->each(function ($item) use ($bandMap) {
  115. $item['display_name'] = '[组] ' . (isset($bandMap[$item['game_band_id']]) ? $bandMap[$item['game_band_id']] : '游戏组#' . $item['game_band_id']);
  116. // 付费率 = 付费玩家 / 活跃玩家
  117. $item['pay_rate'] = $item['active_user'] > 0 ? round(($item['pay_user'] / $item['active_user']) * 100, 2) : 0.00;
  118. // ARPU = 实付金额 / 活跃玩家
  119. $item['arpu'] = $item['active_user'] > 0 ? round($item['pay_amount'] / $item['active_user'], 2) : 0.00;
  120. // ARPPU = 实付金额 / 付费玩家
  121. $item['arppu'] = $item['pay_user'] > 0 ? round($item['pay_amount'] / $item['pay_user'], 2) : 0.00;
  122. return $item;
  123. });
  124. // 模板数据绑定
  125. $this->assign('bandList', $bandList);
  126. $this->assign('default_start', $defaultStart);
  127. $this->assign('default_end', $defaultEnd);
  128. $this->assign('list', $list);
  129. $this->assign('listTotal', $list->total());
  130. $this->assign('page', $list->render());
  131. return $this->fetch('summary');
  132. }
  133. /**
  134. * 2. 游戏留存页面(仅展示游戏组)
  135. */
  136. public function retention()
  137. {
  138. $bandList = $this->getGameBandList();
  139. $bandMap = array_column($bandList, 'name', 'id');
  140. list($defaultStart, $defaultEnd) = $this->getDefaultDateRange();
  141. $start = input('get.start', $defaultStart) ?: $defaultStart;
  142. $end = input('get.end', $defaultEnd) ?: $defaultEnd;
  143. $gameFilter = input('get.game_filter', '');
  144. $this->validateDateRange($start, $end);
  145. // 拼接查询条件
  146. $where = [];
  147. $where['stat_date'] = [['>=', $start], ['<=', $end]];
  148. list(, $gameBandId) = $this->parseGameFilter($gameFilter);
  149. $where['game_id'] = 0;
  150. if ($gameBandId > 0) {
  151. $where['game_band_id'] = $gameBandId;
  152. } else {
  153. // 默认查询全部游戏组,但不混入单端游戏数据
  154. $where['game_band_id'] = ['>', 0];
  155. }
  156. // 分页查询(日期倒序,最新在前)
  157. $list = Db::table('cy_summary_game_daily')
  158. ->where($where)
  159. ->order(['stat_date' => 'desc', 'game_band_id' => 'asc', 'id' => 'desc'])
  160. ->paginate(20, false, ['query' => input('get.')])
  161. ->each(function ($item) use ($bandMap) {
  162. $item['display_name'] = '[组] ' . (isset($bandMap[$item['game_band_id']]) ? $bandMap[$item['game_band_id']] : '游戏组#' . $item['game_band_id']);
  163. // N天留存率 = N天留存玩家数 / 新增玩家数
  164. $item['retention_d1_rate'] = $item['new_user'] > 0 ? round(($item['retention_d1'] / $item['new_user']) * 100, 2) : 0.00;
  165. $item['retention_d3_rate'] = $item['new_user'] > 0 ? round(($item['retention_d3'] / $item['new_user']) * 100, 2) : 0.00;
  166. $item['retention_d4_rate'] = $item['new_user'] > 0 ? round(($item['retention_d4'] / $item['new_user']) * 100, 2) : 0.00;
  167. $item['retention_d5_rate'] = $item['new_user'] > 0 ? round(($item['retention_d5'] / $item['new_user']) * 100, 2) : 0.00;
  168. $item['retention_d6_rate'] = $item['new_user'] > 0 ? round(($item['retention_d6'] / $item['new_user']) * 100, 2) : 0.00;
  169. $item['retention_d7_rate'] = $item['new_user'] > 0 ? round(($item['retention_d7'] / $item['new_user']) * 100, 2) : 0.00;
  170. $item['retention_d15_rate'] = $item['new_user'] > 0 ? round(($item['retention_d15'] / $item['new_user']) * 100, 2) : 0.00;
  171. $item['retention_d30_rate'] = $item['new_user'] > 0 ? round(($item['retention_d30'] / $item['new_user']) * 100, 2) : 0.00;
  172. return $item;
  173. });
  174. $this->assign('bandList', $bandList);
  175. $this->assign('default_start', $defaultStart);
  176. $this->assign('default_end', $defaultEnd);
  177. $this->assign('list', $list);
  178. $this->assign('listTotal', $list->total());
  179. $this->assign('page', $list->render());
  180. return $this->fetch('retention');
  181. }
  182. /**
  183. * 3. 区服数据页面(仅展示单端游戏筛选,默认全部)
  184. */
  185. public function serverSummary()
  186. {
  187. $games = $this->getGameList();
  188. $gameMap = array_column($games, 'name', 'id');
  189. list($defaultStart, $defaultEnd) = $this->getDefaultDateRange();
  190. $start = input('get.start', $defaultStart) ?: $defaultStart;
  191. $end = input('get.end', $defaultEnd) ?: $defaultEnd;
  192. $gameId = (int)input('get.game_id', 0);
  193. $this->validateDateRange($start, $end);
  194. // 获取区服 ID 对应的名称映射 (nw_game_server)
  195. $servers = Db::table('nw_game_server')->field('id, servername as server_name')->select();
  196. $serverMap = array_column($servers, 'server_name', 'id');
  197. // 查询条件
  198. $where = [];
  199. $where['stat_date'] = [['>=', $start], ['<=', $end]];
  200. if ($gameId > 0) {
  201. $where['game_id'] = $gameId;
  202. }
  203. $list = Db::table('cy_summary_server_daily')
  204. ->where($where)
  205. ->order(['stat_date' => 'desc', 'game_id' => 'asc', 'server_id' => 'asc'])
  206. ->paginate(20, false, ['query' => input('get.')])
  207. ->each(function ($item) use ($serverMap, $gameMap) {
  208. $item['game_name'] = isset($gameMap[$item['game_id']]) ? $gameMap[$item['game_id']] : '游戏#' . $item['game_id'];
  209. // 绑定区服名
  210. $item['server_name'] = isset($serverMap[$item['server_id']]) ? $serverMap[$item['server_id']] : '区服#' . $item['server_id'];
  211. // 付费率、ARPU、ARPPU 计算
  212. $item['pay_rate'] = $item['active_user'] > 0 ? round(($item['pay_user'] / $item['active_user']) * 100, 2) : 0.00;
  213. $item['arpu'] = $item['active_user'] > 0 ? round($item['pay_amount'] / $item['active_user'], 2) : 0.00;
  214. $item['arppu'] = $item['pay_user'] > 0 ? round($item['pay_amount'] / $item['pay_user'], 2) : 0.00;
  215. return $item;
  216. });
  217. $this->assign('games', $games);
  218. $this->assign('default_start', $defaultStart);
  219. $this->assign('default_end', $defaultEnd);
  220. $this->assign('list', $list);
  221. $this->assign('listTotal', $list->total());
  222. $this->assign('page', $list->render());
  223. return $this->fetch('server_summary');
  224. }
  225. /**
  226. * 4. 区服留存页面(仅展示单端游戏筛选,默认全部)
  227. */
  228. public function serverRetention()
  229. {
  230. $games = $this->getGameList();
  231. $gameMap = array_column($games, 'name', 'id');
  232. list($defaultStart, $defaultEnd) = $this->getDefaultDateRange();
  233. $start = input('get.start', $defaultStart) ?: $defaultStart;
  234. $end = input('get.end', $defaultEnd) ?: $defaultEnd;
  235. $gameId = (int)input('get.game_id', 0);
  236. $this->validateDateRange($start, $end);
  237. $servers = Db::table('nw_game_server')->field('id, servername as server_name')->select();
  238. $serverMap = array_column($servers, 'server_name', 'id');
  239. $where = [];
  240. $where['stat_date'] = [['>=', $start], ['<=', $end]];
  241. if ($gameId > 0) {
  242. $where['game_id'] = $gameId;
  243. }
  244. $list = Db::table('cy_summary_server_daily')
  245. ->where($where)
  246. ->order(['stat_date' => 'desc', 'game_id' => 'asc', 'server_id' => 'asc'])
  247. ->paginate(20, false, ['query' => input('get.')])
  248. ->each(function ($item) use ($serverMap, $gameMap) {
  249. $item['game_name'] = isset($gameMap[$item['game_id']]) ? $gameMap[$item['game_id']] : '游戏#' . $item['game_id'];
  250. $item['server_name'] = isset($serverMap[$item['server_id']]) ? $serverMap[$item['server_id']] : '区服#' . $item['server_id'];
  251. // 区服N天留存率
  252. $item['retention_d1_rate'] = $item['new_user'] > 0 ? round(($item['retention_d1'] / $item['new_user']) * 100, 2) : 0.00;
  253. $item['retention_d3_rate'] = $item['new_user'] > 0 ? round(($item['retention_d3'] / $item['new_user']) * 100, 2) : 0.00;
  254. $item['retention_d4_rate'] = $item['new_user'] > 0 ? round(($item['retention_d4'] / $item['new_user']) * 100, 2) : 0.00;
  255. $item['retention_d5_rate'] = $item['new_user'] > 0 ? round(($item['retention_d5'] / $item['new_user']) * 100, 2) : 0.00;
  256. $item['retention_d6_rate'] = $item['new_user'] > 0 ? round(($item['retention_d6'] / $item['new_user']) * 100, 2) : 0.00;
  257. $item['retention_d7_rate'] = $item['new_user'] > 0 ? round(($item['retention_d7'] / $item['new_user']) * 100, 2) : 0.00;
  258. $item['retention_d15_rate'] = $item['new_user'] > 0 ? round(($item['retention_d15'] / $item['new_user']) * 100, 2) : 0.00;
  259. $item['retention_d30_rate'] = $item['new_user'] > 0 ? round(($item['retention_d30'] / $item['new_user']) * 100, 2) : 0.00;
  260. return $item;
  261. });
  262. $this->assign('games', $games);
  263. $this->assign('default_start', $defaultStart);
  264. $this->assign('default_end', $defaultEnd);
  265. $this->assign('list', $list);
  266. $this->assign('listTotal', $list->total());
  267. $this->assign('page', $list->render());
  268. return $this->fetch('server_retention');
  269. }
  270. }