RemainController.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. <?php
  2. /**
  3. * PayDataController.php UTF-8
  4. *
  5. *
  6. * @date : 2017/12/18 11:16
  7. *
  8. * @license 这不是一个自由软件,未经授权不许任何使用和传播。
  9. * @author : liguanglong <lgl@huosdk.com>
  10. * @version : HUOSDK 8.0
  11. */
  12. namespace admin\admin\controller\data;
  13. use cmf\controller\AdminBaseController;
  14. use huo\controller\agent\AgentGame;
  15. use huo\logic\agent\AgentLogic;
  16. use huo\logic\data\GenDataLogic;
  17. use huo\logic\data\RemainDataLogic;
  18. use huolib\constant\CommonConst;
  19. use huolib\constant\GameConst;
  20. use huolib\status\CommonStatus;
  21. use huolib\tool\Export;
  22. use think\Lang;
  23. use think\Request;
  24. class RemainController extends AdminbaseController {
  25. protected $pub_where = [];
  26. protected $data_games = [];
  27. protected $data_agents = [];
  28. function _initialize() {
  29. parent::_initialize();
  30. Lang::load(APP_PATH.'admin/lang'.DS.$this->lang.DS.'remain'.EXT);
  31. }
  32. /**
  33. * 留存数据导出
  34. *
  35. * @param $param
  36. *
  37. * @return mixed
  38. */
  39. public function exportIndex($param) {
  40. $_p = 1;
  41. $_offset = Export::MAX_ROWS;
  42. Request::instance()->get(['page' => 1]);
  43. Request::instance()->get(['list_rows' => $_offset]);
  44. if (!empty($param['app_id']) && !empty($param['agent_id'])) {
  45. $_data = (new RemainDataLogic())->gameAgent($param)->toArray();
  46. } else if (empty($param['app_id']) && !empty($param['agent_id'])) {
  47. $_data = (new RemainDataLogic())->agent($param)->toArray();
  48. } else if (!empty($param['app_id']) && empty($param['agent_id'])) {
  49. $_data = (new RemainDataLogic())->game($param)->toArray();
  50. } else if (!empty($_param['cp_app_id'])) {
  51. $_data = (new RemainDataLogic())->cpgame($_param)->toArray();
  52. } else {
  53. $_data = (new RemainDataLogic())->main($param)->toArray();
  54. }
  55. $_total_cnt = $_data['total'];
  56. if ($_total_cnt <= 0) {
  57. $this->adminError(CommonStatus::getMsg(CommonStatus::CNT_IS_ZERO));
  58. }
  59. $_for_cnt = ceil($_total_cnt / $_offset);
  60. $_file_name_arr = [];
  61. while ($_p <= $_for_cnt) {
  62. $_head = ['时间'];
  63. if (!empty($param['app_id'])) {
  64. array_push($_head, '游戏');
  65. }
  66. if (!empty($param['agent_id'])) {
  67. array_push($_head, '渠道');
  68. }
  69. $_head2 = [
  70. '注册用户',
  71. '2日',
  72. '3日',
  73. '4日',
  74. '5日',
  75. '6日',
  76. '7日',
  77. '15日',
  78. '30日',
  79. ];
  80. $_head = array_merge($_head, $_head2);
  81. $_export_datas = [];
  82. foreach ($_data['data'] as $_key => $_val) {
  83. if (!empty($_val['reg_cnt'])) {
  84. $_day2 = '('.round($_val['day2'] / $_val['reg_cnt'], 4) * 100 .'%)';
  85. $_day3 = '('.round($_val['day3'] / $_val['reg_cnt'], 4) * 100 .'%)';
  86. $_day4 = '('.round($_val['day4'] / $_val['reg_cnt'], 4) * 100 .'%)';
  87. $_day5 = '('.round($_val['day5'] / $_val['reg_cnt'], 4) * 100 .'%)';
  88. $_day6 = '('.round($_val['day6'] / $_val['reg_cnt'], 4) * 100 .'%)';
  89. $_day7 = '('.round($_val['day7'] / $_val['reg_cnt'], 4) * 100 .'%)';
  90. $_day15 = '('.round($_val['day15'] / $_val['reg_cnt'], 4) * 100 .'%)';
  91. $_day30 = '('.round($_val['day30'] / $_val['reg_cnt'], 4) * 100 .'%)';
  92. } else {
  93. $_day2 = '(0%)';
  94. $_day3 = '(0%)';
  95. $_day4 = '(0%)';
  96. $_day5 = '(0%)';
  97. $_day6 = '(0%)';
  98. $_day7 = '(0%)';
  99. $_day15 = '(0%)';
  100. $_day30 = '(0%)';
  101. }
  102. $_export_data['date'] = $_val['date'];
  103. if (!empty($param['app_id'])) {
  104. $_export_data['game'] = $this->data_games[$_val['app_id']];
  105. }
  106. if (!empty($_GET['agent_id'])) {
  107. if (!empty($_GET['data_summary']) && 2 == $_GET['data_summary']) {
  108. $_export_data['agent'] = $this->data_agents[$_val['agent_id']];
  109. } else {
  110. $_export_data['agent'] = $this->data_agents[$_val['agent_id']];
  111. }
  112. }
  113. $_export_data['reg_cnt'] = $_val['reg_cnt'];
  114. $_export_data['day2'] = $_val['day2'].$_day2;
  115. $_export_data['day3'] = $_val['day3'].$_day3;
  116. $_export_data['day4'] = $_val['day4'].$_day4;
  117. $_export_data['day5'] = $_val['day5'].$_day5;
  118. $_export_data['day6'] = $_val['day6'].$_day6;
  119. $_export_data['day7'] = $_val['day7'].$_day7;
  120. $_export_data['day15'] = $_val['day15'].$_day15;
  121. $_export_data['day30'] = $_val['day30'].$_day30;
  122. $_export_datas[] = $_export_data;
  123. }
  124. if (1 == $_for_cnt) {
  125. Export::exportCsv($_head, $_export_datas, $path = $this->admin_id, '留存分析', '.csv', true);
  126. break;
  127. } else {
  128. $_file_name = '留存分析'.$_p;
  129. Export::exportCsv($_head, $_export_datas, $path = $this->admin_id, $_file_name);
  130. $_file_name_arr[] = $_file_name.'.csv';
  131. $_p++;
  132. if ($_p > 1) {
  133. $_data = null;
  134. Request::instance()->get(['page' => $_p]);
  135. if (!empty($param['app_id']) && !empty($param['agent_id'])) {
  136. $_data = (new RemainDataLogic())->gameAgent($param)->toArray();
  137. } else if (empty($param['app_id']) && !empty($param['agent_id'])) {
  138. $_data = (new RemainDataLogic())->agent($param)->toArray();
  139. } else if (!empty($param['app_id']) && empty($param['agent_id'])) {
  140. $_data = (new RemainDataLogic())->game($param)->toArray();
  141. } else if (!empty($_param['cp_app_id'])) {
  142. $_data = (new RemainDataLogic())->cpgame($_param)->toArray();
  143. } else {
  144. $_data = (new RemainDataLogic())->main($param)->toArray();
  145. }
  146. }
  147. }
  148. }
  149. Export::exportZip($_file_name_arr, $path = $this->admin_id, '留存分析');
  150. }
  151. /**
  152. * 留存数据
  153. * admin/data.remain/index
  154. */
  155. public function index() {
  156. $_param = $this->_getSearchParam();
  157. if ('1' == $this->request->param('export/d', 0)) {
  158. return $this->exportIndex($_param);
  159. }
  160. if (!empty($_param['app_id']) && !empty($_param['agent_id'])) {
  161. return $this->gameAgent($_param);
  162. } else if (empty($_param['app_id']) && !empty($_param['agent_id'])) {
  163. return $this->agent($_param);
  164. } else if (!empty($_param['app_id']) && empty($_param['agent_id'])) {
  165. return $this->game($_param);
  166. } else if (!empty($_param['cp_app_id'])) {
  167. return $this->cpgame($_param);
  168. }
  169. return $this->main($_param);
  170. }
  171. public function update() {
  172. /* 执行定时任务更新当天数据 */
  173. $_date = date('Y-m-d');
  174. (new GenDataLogic())->archiveDaily($_date);
  175. return $this->index();
  176. }
  177. /**
  178. * 总体数据
  179. *
  180. * @param $_param
  181. *
  182. * @return mixed
  183. */
  184. public function main($_param) {
  185. list($_data, $sum) = (new RemainDataLogic())->main($_param, '-date', true);
  186. $this->assign('sum', $sum);
  187. $this->assign('items', $_data);
  188. $this->assign('page', $_data->render());
  189. return $this->fetch('data/remain/index');
  190. }
  191. /**
  192. * 游戏渠道
  193. *
  194. * @param $_param
  195. *
  196. * @return mixed
  197. */
  198. public function gameAgent($_param) {
  199. list($_data, $sum) = (new RemainDataLogic())->gameAgent($_param, '-date', true);
  200. $this->assign('sum', $sum);
  201. $this->assign('items', $_data);
  202. $this->assign('page', $_data->render());
  203. return $this->fetch('data/remain/index');
  204. }
  205. /**
  206. * 游戏
  207. *
  208. * @param $_param
  209. *
  210. * @return mixed
  211. */
  212. public function game($_param) {
  213. list($_data, $sum) = (new RemainDataLogic())->game($_param, '-date', true);
  214. $this->assign('sum', $sum);
  215. $this->assign('items', $_data);
  216. $this->assign('page', $_data->render());
  217. return $this->fetch('data/remain/index');
  218. }
  219. /**
  220. * cp游戏
  221. *
  222. * @param $_param
  223. *
  224. * @return mixed
  225. */
  226. public function cpgame($_param) {
  227. $_data = (new RemainDataLogic())->cpgame($_param);
  228. $this->assign('items', $_data);
  229. $this->assign('page', $_data->render());
  230. return $this->fetch('data/remain/index');
  231. }
  232. /**
  233. * 渠道
  234. *
  235. * @param $_param
  236. *
  237. * @return mixed
  238. */
  239. public function agent($_param) {
  240. list($_data, $sum) = (new RemainDataLogic())->agent($_param, '-date', true);
  241. $this->assign('sum', $sum);
  242. $this->assign('items', $_data);
  243. $this->assign('page', $_data->render());
  244. return $this->fetch('data/remain/index');
  245. }
  246. /***
  247. * 获取筛选数据
  248. */
  249. public function _getSearchParam() {
  250. $_rq_data = $this->request->param();
  251. $this->assign('rq_data', $_rq_data);
  252. if (!input('?get.page')) {
  253. Request::instance()->get(['page' => 1]);
  254. }
  255. if (!input('?get.list_rows')) {
  256. Request::instance()->get(['list_rows' => 10]);
  257. }
  258. $_param['app_id'] = $this->request->param('app_id/d', 0);
  259. $_param['data_summary'] = $this->request->param('data_summary/d', 0);
  260. list($_param['start_time'], $_param['end_time']) = $this->_time();
  261. $_param['agent_id'] = $this->request->param('agent_id/d', CommonConst::CONST_ZERO);
  262. $this->data_agents = $this->_agents($_param['agent_id']);
  263. $_include_agent = $this->request->param('include_agent/d', 2);
  264. $this->includeAgent($_include_agent);
  265. /* 游戏筛选 */
  266. $_game_where = [];
  267. if (!empty($this->map['agent_id'])) {
  268. if (empty($_param['agent_id'])) {
  269. $_param['agent_id'] = $this->map['agent_id'];
  270. }
  271. /* 获取渠道游戏 */
  272. $_app_ids = (new AgentGame())->getGameIds($this->map);
  273. $_game_where['id'] = 0;
  274. if (!empty($_app_ids)) {
  275. $_game_where['id'] = ['in', $_app_ids];
  276. }
  277. }
  278. if (!empty($_param['agent_id']) && empty($this->map['agent_id']) && -1 != $_param['agent_id']
  279. && 2 == $_include_agent) {
  280. $_agent_ids = (new AgentLogic())->getAgentIds($_param['agent_id'], true);
  281. if (!empty($_agent_ids)) {
  282. $_param['agent_id'] = ['in', $_agent_ids];
  283. }
  284. }
  285. /* cp游戏 */
  286. if (!empty($this->cp_app_id)) {
  287. $_game_where['id'] = ['in', $this->cp_app_id];
  288. $_param['cp_app_id'] = $this->cp_app_id;
  289. }
  290. $this->data_games = $this->_games(
  291. $_param['app_id'], GameConst::GAME_STATUS_ON, CommonConst::CONST_NOT_DELETE,
  292. GameConst::GAME_IS_SDK, 0, false, true, $_game_where
  293. );
  294. $_percent = $this->request->param('percent', null);
  295. if (is_null($_percent)) {
  296. $_GET['percent'] = 1;
  297. }
  298. return $_param;
  299. }
  300. }