Retaine.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. <?php
  2. /**
  3. * 前台注册用户管理控制器
  4. */
  5. namespace app\guildapi\controller;
  6. use app\common\library\MakeReportGo;
  7. use think\Db;
  8. use think\JsonException;
  9. class Retaine extends Guild
  10. {
  11. public function retaineGameChannel()
  12. {
  13. // if (input('level')) {
  14. // $level = input('level');
  15. // } else {
  16. // $level = $this->_channelLevel;
  17. // }
  18. // if ($this->_channelLevel != $level && !input('channel_id')) {
  19. // return json(['data' => [], 'code' => 10020, 'msg' => '推广账号必填']);
  20. // }
  21. if (input('download') == 1) {
  22. return $this->retaineGameList(input(), 2);
  23. } else {
  24. list($list, $total) = $this->retaineGameList(input(), 2);
  25. $game_list = model('Common/Game')->getAllByCondition('id,name', ['game_kind' => 1, 'cooperation_status' => ['neq', 0], 'id' => ['in', model('game')->getTakingGame($this->_channelId)]], '', 'self');
  26. if ($this->_channelLevel == 1) {
  27. $channelLevel = [
  28. ['id' => 1, 'name' => '会长(B)'],
  29. ['id' => 2, 'name' => '子会长(B-)'],
  30. ['id' => 3, 'name' => '推广员(C)'],
  31. ];
  32. } else if ($this->_channelLevel == 2) {
  33. $channelLevel = [
  34. ['id' => 2, 'name' => '子会长(B-)'],
  35. ['id' => 3, 'name' => '推广员(C)'],
  36. ];
  37. } else if ($this->_channelLevel == 3) {
  38. $channelLevel = [
  39. ['id' => 3, 'name' => '推广员(C)'],
  40. ];
  41. }
  42. return json(['data' => $list, 'code' => 20000, 'msg' => '获取数据成功', 'total' => $total, 'gameList' => $game_list, 'channelLevel' => $channelLevel, 'level' => $this->_channelLevel]);
  43. }
  44. }
  45. private function retaineGameList($data, $type_id)
  46. {
  47. $tmpGameList = model('Common/Game')->getAllByCondition('name,id');
  48. $gameList = array();
  49. foreach ($tmpGameList as $game) {
  50. $gameList[$game['id']] = $game;
  51. }
  52. $channelC = model('common/Channel')->cache("Retaine:retaineGameList", 60)->field('id,name')->order('id desc')->select();
  53. foreach ($channelC as $v) {
  54. $channelList[$v['id']] = $v;
  55. }
  56. $where['type_id'] = ['=', $type_id];
  57. if (isset($data['game_id']) && $data['game_id']) {
  58. $where['game_id'] = ['in', $data['game_id']];
  59. }
  60. //开始时间和结束时间不为空时
  61. if (isset($data['start']) && isset($data['end']) && $data['start'] != '' && $data['end'] != '') {
  62. $where['day'] = [
  63. ['>=', $data['start']],
  64. ['<=', $data['end']],
  65. ];
  66. } //开始时间不为空时
  67. elseif (isset($data['start']) && $data['start'] != '') {
  68. $where['day'] = ['>=', $data['start']];
  69. } //结束时间不为空时
  70. elseif (isset($data['end']) && $data['end'] != '') {
  71. $where['day'] = ['<=', $data['end']];
  72. }
  73. if ($data['level']) {
  74. $level = $data['level'];
  75. } else {
  76. $level = $this->_channelLevel;
  77. }
  78. if ($data['channel_id']) {
  79. $channelId = $data['channel_id'];
  80. } else {
  81. $channelId = $this->_channelId;
  82. }
  83. $business = model('common/channel')->getChannelIds($channelId, $level);
  84. // var_dump($business);
  85. $_channelLevel = $this->_channelLevel;
  86. $_channelId = $this->_channelId;
  87. if ($type_id == 2 && $level != 3) {
  88. if ($level == 0) {
  89. if ($business && $business != -2) {
  90. if (isset($data['channel_id']) && $data['channel_id']) {
  91. if (in_array($data['channel_id'], $business)) {
  92. $where['s_channel_id'] = ['in', $data['channel_id']];
  93. } else {
  94. $where['s_channel_id'] = ['in', [-1]];
  95. }
  96. } else {
  97. $where['s_channel_id'] = ['in', $business];
  98. }
  99. } else {
  100. if (isset($data['channel_id']) && $data['channel_id']) {
  101. $where['s_channel_id'] = ['in', [$data['channel_id']]];
  102. }
  103. }
  104. $field = ',s_channel_id as channel_id';
  105. $group = 's_channel_id';
  106. } else if ($level == 1) {
  107. // if (isset($data['channel_id']) && $data['channel_id']) {
  108. // $where['b_channel_id'] = ['in', $data['channel_id']];
  109. // }
  110. if ($business && $business != -2) {
  111. if (isset($data['channel_id']) && $data['channel_id']) {
  112. if (in_array($data['channel_id'], $business)) {
  113. $where['b_channel_id'] = ['in', $data['channel_id']];
  114. } else {
  115. $where['b_channel_id'] = ['in', [-1]];
  116. }
  117. } else {
  118. $where['b_channel_id'] = ['in', $business];
  119. }
  120. } else {
  121. if (isset($data['channel_id']) && $data['channel_id']) {
  122. $where['b_channel_id'] = ['in', [$data['channel_id']]];
  123. }
  124. }
  125. $field = ',b_channel_id as channel_id';
  126. $group = 'b_channel_id';
  127. } else if ($level == 2) {
  128. if ($business && $business != -2) {
  129. if (isset($data['channel_id']) && $data['channel_id']) {
  130. if (in_array($data['channel_id'], $business)) {
  131. $where['bz_channel_id'] = ['in', $data['channel_id']];
  132. } else {
  133. $where['bz_channel_id'] = ['in', [-1]];
  134. }
  135. } else {
  136. $where['bz_channel_id'] = ['in', $business];
  137. }
  138. } else {
  139. if (isset($data['channel_id']) && $data['channel_id']) {
  140. $where['bz_channel_id'] = ['in', [$data['channel_id']]];
  141. }
  142. }
  143. if (!(isset($data['channel_id']) && $data['channel_id'])) {
  144. $where['bz_channel_id'] = ['>', 0];
  145. }
  146. $field = ',bz_channel_id as channel_id';
  147. $group = 'bz_channel_id';
  148. }
  149. if ($data['download'] == 1) {
  150. $sql = model('common/RetaineGame')->field('type_id,day,game_id,bz_channel_id,b_channel_id,s_channel_id,sum(reg_num) as reg_num,sum(role_num) as role_num,sum(act_num) as act_num,sum(recharge_num) as recharge_num,sum(pay_num) as pay_num,sum(one_stay) as one_stay,sum(three_stay) as three_stay,sum(four_stay) as four_stay,sum(five_stay) as five_stay,sum(six_stay) as six_stay,sum(seven_stay) as seven_stay,sum(fifteen_stay) as fifteen_stay,sum(thirty_stay) as thirty_stay' . $field)->where($where)->where(function ($query) use ($_channelLevel, $_channelId) {
  151. if ($_channelLevel == 1) {
  152. $query->where('b_channel_id', $_channelId);
  153. } else if ($_channelLevel == 2) {
  154. $query->where('bz_channel_id', $_channelId);
  155. } else if ($_channelLevel == 3) {
  156. $query->where('channel_id', $_channelId);
  157. }
  158. })->group($group . ',game_id,day')->order('day desc,game_id desc')->fetchSql(true)->select();
  159. if ((new MakeReportGo())->addTask('guild.retaineGame', $sql, 'cps' . $this->_adminId, ['Level' => $level, 'TypeId' => $type_id])) {
  160. return json(['data' => '', 'code' => 20000, 'msg' => '报表生成的任务已经提交, 报表生成完成后,会及时通知您,请耐心稍等']);
  161. } else {
  162. return json(['data' => '', 'code' => 20013, 'msg' => '报表生成任务不可重复提交,如遇到无法导出情况,建议修改查询条件解除当前状态,提交重新生成报表任务!']);
  163. }
  164. }
  165. $total = model('common/RetaineGame')->where($where)->where(function ($query) use ($_channelLevel, $_channelId) {
  166. if ($_channelLevel == 1) {
  167. $query->where('b_channel_id', $_channelId);
  168. } else if ($_channelLevel == 2) {
  169. $query->where('bz_channel_id', $_channelId);
  170. } else if ($_channelLevel == 3) {
  171. $query->where('channel_id', $_channelId);
  172. }
  173. })->field('sum(reg_num) as reg_num,sum(role_num) as role_num,sum(recharge_num) as recharge_num,sum(act_num) as act_num')->find();
  174. $sql = model('common/RetaineGame')->field('type_id,day,game_id,bz_channel_id,b_channel_id,s_channel_id,sum(reg_num) as reg_num,sum(role_num) as role_num,sum(act_num) as act_num,sum(recharge_num) as recharge_num,sum(pay_num) as pay_num,sum(one_stay) as one_stay,sum(three_stay) as three_stay,sum(four_stay) as four_stay,sum(five_stay) as five_stay,sum(six_stay) as six_stay,sum(seven_stay) as seven_stay,sum(fifteen_stay) as fifteen_stay,sum(thirty_stay) as thirty_stay' . $field)->where($where)->where(function ($query) use ($_channelLevel, $_channelId) {
  175. if ($_channelLevel == 1) {
  176. $query->where('b_channel_id', $_channelId);
  177. } else if ($_channelLevel == 2) {
  178. $query->where('bz_channel_id', $_channelId);
  179. } else if ($_channelLevel == 3) {
  180. $query->where('channel_id', $_channelId);
  181. }
  182. })->group($group . ',game_id,day')->where($where)->buildSql();
  183. $list = Db::table($sql . ' a')->order('day desc,game_id desc')->paginate(['list_rows' => $data['pageSize'], 'page' => $data['page']])->each(function ($item, $key) use ($gameList, $channelList) {
  184. $item['rate'] = $item['act_num'] > 0 ? priceFormat(100 * $item['pay_num'] / $item['act_num']) . '%' : '0.00%';//付费人数/活跃人数
  185. $item['arpu'] = $item['act_num'] > 0 ? priceFormat($item['recharge_num'] / $item['act_num']) : '0.00';//充值金额/活跃玩家
  186. $item['arppu'] = $item['pay_num'] > 0 ? priceFormat($item['recharge_num'] / $item['pay_num']) : '0.00';// 充值金额/付费人数
  187. $item['one_stay'] = $item['one_stay'] . ($item['role_num'] > 0 ? '(' . priceFormat(100 * $item['one_stay'] / $item['role_num']) . '%)' : '(0.00%)'); //次留
  188. $item['three_stay'] = $item['three_stay'] . ($item['role_num'] > 0 ? '(' . priceFormat(100 * $item['three_stay'] / $item['role_num']) . '%)' : '(0.00%)'); //三留
  189. $item['four_stay'] = $item['four_stay'] . ($item['role_num'] > 0 ? '(' . priceFormat(100 * $item['four_stay'] / $item['role_num']) . '%)' : '(0.00%)'); //四留
  190. $item['five_stay'] = $item['five_stay'] . ($item['role_num'] > 0 ? '(' . priceFormat(100 * $item['five_stay'] / $item['role_num']) . '%)' : '(0.00%)'); //五留
  191. $item['six_stay'] = $item['six_stay'] . ($item['role_num'] > 0 ? '(' . priceFormat(100 * $item['six_stay'] / $item['role_num']) . '%)' : '(0.00%)'); //六留
  192. $item['seven_stay'] = $item['seven_stay'] . ($item['role_num'] > 0 ? '(' . priceFormat(100 * $item['seven_stay'] / $item['role_num']) . '%)' : '(0.00%)'); //七留
  193. $item['fifteen_stay'] = $item['fifteen_stay'] . ($item['role_num'] > 0 ? '(' . priceFormat(100 * $item['fifteen_stay'] / $item['role_num']) . '%)' : '(0.00%)'); //十五留
  194. $item['thirty_stay'] = $item['thirty_stay'] . ($item['role_num'] > 0 ? '(' . priceFormat(100 * $item['thirty_stay'] / $item['role_num']) . '%)' : '(0.00%)'); //三十留
  195. $item['game_name'] = isset($gameList[$item['game_id']]) ? $gameList[$item['game_id']]['name'] : '';
  196. $item['channel_name'] = isset($channelList[$item['channel_id']]) ? $channelList[$item['channel_id']]['name'] : '';
  197. return $item;
  198. })->toArray();
  199. } else {
  200. if ($level = 3) {
  201. if ($business && $business != -2) {
  202. if (isset($data['channel_id']) && $data['channel_id']) {
  203. if (in_array($data['channel_id'], $business)) {
  204. $where['channel_id'] = ['in', $data['channel_id']];
  205. } else {
  206. $where['channel_id'] = ['in', [-1]];
  207. }
  208. } else {
  209. $where['channel_id'] = ['in', $business];
  210. }
  211. } else {
  212. if (isset($data['channel_id']) && $data['channel_id']) {
  213. $where['channel_id'] = ['in', $data['channel_id']];
  214. }
  215. }
  216. }
  217. if ($data['download'] == 1) {
  218. $sql = model('common/RetaineGame')->where($where)->order('day desc,game_id desc')->where(function ($query) use ($_channelLevel, $_channelId) {
  219. if ($_channelLevel == 1) {
  220. $query->where('b_channel_id', $_channelId);
  221. } else if ($_channelLevel == 2) {
  222. $query->where('bz_channel_id', $_channelId);
  223. } else if ($_channelLevel == 3) {
  224. $query->where('channel_id', $_channelId);
  225. }
  226. })->fetchSql(true)->select();
  227. if ((new MakeReportGo())->addTask('guild.retaineGame', $sql, 'cps' . $this->_adminId, ['Level' => $level, 'TypeId' => $type_id])) {
  228. return json(['data' => '', 'code' => 20000, 'msg' => '报表生成的任务已经提交, 报表生成完成后,会及时通知您,请耐心稍等']);
  229. } else {
  230. return json(['data' => '', 'code' => 20013, 'msg' => '报表生成任务不可重复提交,如遇到无法导出情况,建议修改查询条件解除当前状态,提交重新生成报表任务!']);
  231. }
  232. }
  233. $total = model('common/RetaineGame')->where($where)->where(function ($query) use ($_channelLevel, $_channelId) {
  234. if ($_channelLevel == 1) {
  235. $query->where('b_channel_id', $_channelId);
  236. } else if ($_channelLevel == 2) {
  237. $query->where('bz_channel_id', $_channelId);
  238. } else if ($_channelLevel == 3) {
  239. $query->where('channel_id', $_channelId);
  240. }
  241. })->field('sum(reg_num) as reg_num,sum(role_num) as role_num,sum(recharge_num) as recharge_num,sum(act_num) as act_num')->find();
  242. $list = model('common/RetaineGame')->where($where)->where(function ($query) use ($_channelLevel, $_channelId) {
  243. if ($_channelLevel == 1) {
  244. $query->where('b_channel_id', $_channelId);
  245. } else if ($_channelLevel == 2) {
  246. $query->where('bz_channel_id', $_channelId);
  247. } else if ($_channelLevel == 3) {
  248. $query->where('channel_id', $_channelId);
  249. }
  250. })->order('day desc,game_id desc')->paginate(['list_rows' => $data['pageSize'], 'page' => $data['page']])->each(function ($item, $key) use ($gameList, $channelList) {
  251. $item['rate'] = $item['act_num'] > 0 ? priceFormat(100 * $item['pay_num'] / $item['act_num']) . '%' : '0.00%';//付费人数/活跃人数
  252. $item['arpu'] = $item['act_num'] > 0 ? priceFormat($item['recharge_num'] / $item['act_num']) : '0.00';//充值金额/活跃玩家
  253. $item['arppu'] = $item['pay_num'] > 0 ? priceFormat($item['recharge_num'] / $item['pay_num']) : '0.00';// 充值金额/付费人数
  254. $item['one_stay'] = $item['one_stay'] . ($item['role_num'] > 0 ? '(' . priceFormat(100 * $item['one_stay'] / $item['role_num']) . '%)' : '(0.00%)'); //次留
  255. $item['three_stay'] = $item['three_stay'] . ($item['role_num'] > 0 ? '(' . priceFormat(100 * $item['three_stay'] / $item['role_num']) . '%)' : '(0.00%)'); //三留
  256. $item['four_stay'] = $item['four_stay'] . ($item['role_num'] > 0 ? '(' . priceFormat(100 * $item['four_stay'] / $item['role_num']) . '%)' : '(0.00%)'); //四留
  257. $item['five_stay'] = $item['five_stay'] . ($item['role_num'] > 0 ? '(' . priceFormat(100 * $item['five_stay'] / $item['role_num']) . '%)' : '(0.00%)'); //五留
  258. $item['six_stay'] = $item['six_stay'] . ($item['role_num'] > 0 ? '(' . priceFormat(100 * $item['six_stay'] / $item['role_num']) . '%)' : '(0.00%)'); //六留
  259. $item['seven_stay'] = $item['seven_stay'] . ($item['role_num'] > 0 ? '(' . priceFormat(100 * $item['seven_stay'] / $item['role_num']) . '%)' : '(0.00%)'); //七留
  260. $item['fifteen_stay'] = $item['fifteen_stay'] . ($item['role_num'] > 0 ? '(' . priceFormat(100 * $item['fifteen_stay'] / $item['role_num']) . '%)' : '(0.00%)'); //十五留
  261. $item['thirty_stay'] = $item['thirty_stay'] . ($item['role_num'] > 0 ? '(' . priceFormat(100 * $item['thirty_stay'] / $item['role_num']) . '%)' : '(0.00%)'); //三十留
  262. $item['game_name'] = isset($gameList[$item['game_id']]) ? $gameList[$item['game_id']]['name'] : '';
  263. $item['channel_name'] = isset($channelList[$item['channel_id']]) ? $channelList[$item['channel_id']]['name'] : '';
  264. return $item;
  265. })->toArray();
  266. }
  267. return [$list, $total];
  268. }
  269. public function getChannel()
  270. {
  271. $business = model('common/channel')->getChannelIds($this->_channelId, input('level', 3));
  272. $channelWhere = [];
  273. if ($business != -1) {
  274. if ($business) {
  275. $channelWhere['id'] = ['in', $business];
  276. } else {
  277. $channelWhere['id'] = -1;
  278. }
  279. }
  280. $channel = model('common/Channel')->where(['level' => input('level', 3)])->where($channelWhere)->field('id,name')->order('id desc')->select();
  281. $this->jsonResult($channel, 20000, '成功');
  282. }
  283. }