UserLogLogic.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. <?php
  2. // 玩家日志逻
  3. namespace app\v1\logic\dataReport;
  4. use plugin\saiadmin\exception\ApiException;
  5. use plugin\saiadmin\basic\BaseLogic;
  6. use plugin\saiadmin\service\OpenSpoutWriter;
  7. use support\think\Db;
  8. class UserLogLogic extends BaseLogic
  9. {
  10. // 注册日志
  11. public function getRegLogList($params): mixed
  12. {
  13. $orderBy = request()->input('orderBy', 'reg_time');
  14. $orderType = request()->input('orderType', 'desc');
  15. $params = $this->searchByAuth($params);
  16. $limit = request()->input('limit', 10);
  17. // 检查注册时间只能查询3个月内
  18. if (empty($params['reg_time']) || count($params['reg_time']) != 2) {
  19. throw new ApiException('请选择注册时间范围');
  20. }
  21. $startTime = strtotime($params['reg_time'][0]);
  22. $endTime = strtotime($params['reg_time'][1]);
  23. if ($endTime < $startTime) {
  24. throw new ApiException('注册时间范围不正确');
  25. }
  26. // 计算时间差,限制为3个月(90天)内
  27. $maxDays = 90;
  28. if (($endTime - $startTime) > ($maxDays * 86400)) {
  29. throw new ApiException('注册时间只能查询3个月内');
  30. }
  31. // 公共处理完的where, 自然量ID, auth_id=0为自然量
  32. $whereRaw = $this->getCommonWhereRaw($params);
  33. $monthRange = getMonthRange($params['reg_time'][0], $params['reg_time'][1]);
  34. $db = Db::connect('db_game_log');
  35. $unionQuery = [];
  36. foreach ($monthRange as $month){
  37. $tableName = 'sdk_reg_log_' . $month;
  38. $unionQuery[] = $db->table($tableName)->whereRaw(where: $whereRaw)->buildSql();
  39. }
  40. $fullSql = "(" . implode(' UNION ALL ', $unionQuery) . ") as unTable";
  41. return $db->table($fullSql)->order($orderBy, $orderType)->paginate($limit)->toArray();
  42. }
  43. // 登录日志
  44. public function getLoginLogList($params): mixed
  45. {
  46. $orderBy = request()->input('orderBy', 'login_time');
  47. $orderType = request()->input('orderType', 'desc');
  48. $page = request()->input('page', 1);
  49. $limit = request()->input('limit', 10);
  50. $params = $this->searchByAuth($params);
  51. // 检查注册时间只能查询3个月内
  52. if (empty($params['login_time']) || count($params['login_time']) != 2) {
  53. throw new ApiException('请选择登录时间范围');
  54. }
  55. $startTime = strtotime($params['login_time'][0]);
  56. $endTime = strtotime($params['login_time'][1]);
  57. if ($endTime < $startTime) {
  58. throw new ApiException('登录时间范围不正确');
  59. }
  60. // 计算时间差,限制为3个月(90天)内
  61. $maxDays = 90;
  62. if (($endTime - $startTime) > ($maxDays * 86400)) {
  63. throw new ApiException('登录时间只能查询3个月内');
  64. }
  65. // 检查登录时间只能查询3个月内
  66. if(!empty($params['reg_time'])){
  67. $startTime = strtotime($params['reg_time'][0]);
  68. $endTime = strtotime($params['reg_time'][1]);
  69. if ($endTime < $startTime) {
  70. throw new ApiException('注册时间范围不正确');
  71. }
  72. // 计算时间差,限制为3个月(90天)内
  73. $maxDays = 90;
  74. if (($endTime - $startTime) > ($maxDays * 86400)) {
  75. throw new ApiException('注册时间只能查询3个月内');
  76. }
  77. }
  78. // 公共处理完的where, 自然量ID, auth_id=0为自然量
  79. $whereRaw = $this->getCommonWhereRaw($params);
  80. $monthRange = getMonthRange($params['login_time'][0], $params['login_time'][1]);
  81. $db = Db::connect('db_game_log');
  82. $unionQuery = [];
  83. foreach ($monthRange as $month){
  84. $tableName = 'sdk_login_log_' . $month;
  85. $unionQuery[] = $db->table($tableName)->whereRaw(where: $whereRaw)->buildSql();
  86. }
  87. $fullSql = "(" . implode(' UNION ALL ', $unionQuery) . ") as unTable";
  88. return $db->table($fullSql)->order($orderBy, $orderType)->paginate($limit)->toArray();
  89. }
  90. // 充值明细
  91. public function getRechargeDetailList($where, $type = "list"): mixed
  92. {
  93. $orderBy = request()->input('orderBy', 'pay_date');
  94. $orderType = request()->input('orderType', 'desc');
  95. $page = request()->input('page', 1);
  96. $limit = request()->input('limit', 10);
  97. $params = $this->searchByAuth($where);
  98. $tableName = 'sdk_order_success';
  99. $rechargeDetailQuery = Db::connect('db_game_log')->table($tableName);
  100. // 公共处理完的where, 自然量ID, auth_id=0为自然量
  101. $whereRaw = $this->getCommonWhereRaw($params);
  102. $rechargeDetailQuery = $rechargeDetailQuery->whereRaw($whereRaw);
  103. $rechargeDetailQuery->order($orderBy, $orderType);
  104. if ($type == 'all') {
  105. $data = $rechargeDetailQuery->select()->toArray();
  106. } else {
  107. $data = $rechargeDetailQuery->paginate($limit)->toArray();
  108. }
  109. return $data;
  110. }
  111. // 充值明细导出
  112. public function exportRechargeDetailList($where): mixed
  113. {
  114. $data = $this->getRechargeDetailList($where, 'all');
  115. $data = $this->trandformListColumn($data, ['game', 'ip', 'agent', 'auth', 'pay_channel']);
  116. $data = array_map(function ($item) {
  117. return [
  118. 'order_id' => $item['order_id'],
  119. 'user_name' => $item['user_name'],
  120. 'agent_id' => $item['agent_id'],
  121. 'site_id' => $item['site_id'],
  122. 'game_name' => $item['game_name'],
  123. 'server_id' => $item['server_id'],
  124. 'server_name' => $item['server_name'],
  125. 'pay_channel_name' => $item['pay_channel_name'],
  126. 'money' => $item['money'],
  127. 'pay_date' => $item['pay_date'],
  128. 'reg_date' => $item['reg_date'],
  129. 'first_payment' => $item['first_payment'],
  130. 'agent_name' => $item['agent_name'],
  131. 'auth_name' => $item['auth_name'],
  132. ];
  133. }, $data);
  134. // 是否首次付费
  135. $filter = [
  136. 'first_payment' => [
  137. ['value' => 1, 'label' => '是'],
  138. ['value' => 0, 'label' => '否']
  139. ]
  140. ];
  141. $file_name = '充值明细_' . date('YmdHis') . '.xlsx';
  142. $header = ['订单号', '用户名', '渠道ID', '广告位ID', '游戏名', '服务器ID', '服务器名', '支付方式', '充值金额', '充值时间', '注册时间', '是否首次付费', '渠道名', '负责人'];
  143. $writer = new OpenSpoutWriter($file_name);
  144. $writer->setWidth([15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15]);
  145. $writer->setHeader($header);
  146. $writer->setData($data, null, $filter);
  147. $file_path = $writer->returnFile();
  148. return response()->download($file_path, urlencode($file_name));
  149. }
  150. // 充值排行
  151. public function getRechargeRankList($params): mixed
  152. {
  153. $orderBy = request()->input('orderBy', 'searchTotalMoney');
  154. $orderType = request()->input('orderType', 'desc');
  155. $page = request()->input('page', 1);
  156. $limit = request()->input('limit', 10);
  157. $params = $this->searchByAuth($params);
  158. $tableName = 'sdk_order_success';
  159. $rechargeRankQuery = Db::connect('db_game_log')->table($tableName);
  160. // 公共处理完的where, 自然量ID, auth_id=0为自然量
  161. $whereRaw = $this->getCommonWhereRaw($params);
  162. $rechargeRankQuery = $rechargeRankQuery->whereRaw($whereRaw);
  163. // 先选择字段,包括计算字段
  164. $rechargeRankQuery->field('user_name,sum(money) as searchTotalMoney,
  165. order_id,
  166. game_id,
  167. media_id,
  168. agent_id,
  169. site_id,
  170. uid,
  171. pay_channel_id,
  172. auth_id,
  173. server_id,
  174. server_name,
  175. pay_date,
  176. reg_date,
  177. role_name,
  178. role_id');
  179. $rechargeRankQuery->group('uid, game_id,site_id,server_id,media_id,auth_id,agent_id,server_id,auth_id,role_id');
  180. $rechargeRankQuery->order($orderBy, $orderType);
  181. $data = $rechargeRankQuery->paginate($limit)->toArray();
  182. $data['data'] = $this->trandformListColumn($data['data'], ['game', 'auth', 'agent', 'pay_channel']);
  183. // 告警提示,查询最近登录时间,查询最近充值时间,和现在时间对比,如果超过3天,则告警,并提示
  184. foreach ($data['data'] as &$item) {
  185. // 最后登录时间
  186. $item['login_time'] = Db::connect('db_origin')->table('user_' . $item['uid'] % 10)->where('uid', $item['uid'])->value('login_time');
  187. // 最近注册时间
  188. $item['pay_time'] = Db::connect('db_origin')->table('user_' . $item['uid'] % 10)->where('uid', $item['uid'])->value('pay_time');
  189. // 现在时间
  190. $item['now_time'] = time();
  191. // 最近登录时间与现在时间对比,如果超过3天,则告警,并提示
  192. if ($item['login_time'] < $item['now_time'] - 3 * 86400) {
  193. $item['login_alert'] = 1;
  194. } else {
  195. $item['login_alert'] = 0;
  196. }
  197. // 最近充值时间与现在时间对比,如果超过3天,则告警,并提示
  198. if ($item['pay_time'] < $item['now_time'] - 3 * 86400) {
  199. $item['pay_alert'] = 1;
  200. } else {
  201. $item['pay_alert'] = 0;
  202. }
  203. }
  204. $totalList = Db::connect('db_game_log')->table('sdk_order_success')
  205. ->whereRaw($whereRaw)
  206. ->field('game_id,uid,sum(money) as totalMoney')
  207. ->group('uid,game_id,site_id,server_id,media_id,auth_id,agent_id,server_id,auth_id,role_id')
  208. ->select()->toArray();
  209. // 查询累计充值金额(对每一行数据单独查询历史充值金额)
  210. $totalMoneyMap = [];
  211. if (!empty($data['data']) && !empty($totalList)) {
  212. foreach ($totalList as $totalItem) {
  213. $key = $totalItem['uid'] . '_' . $totalItem['game_id'];
  214. $totalMoneyMap[$key] = $totalItem['totalMoney'] ?? 0;
  215. }
  216. }
  217. // 为搜索结果添加累计充值金额
  218. foreach ($data['data'] as &$item) {
  219. $key = $item['uid'] . '_' . $item['game_id'];
  220. $item['totalMoney'] = $totalMoneyMap[$key] ?? 0;
  221. }
  222. return $data;
  223. }
  224. // 角色数据
  225. public function getRoleDataList($params): mixed
  226. {
  227. $page = request()->input('page', 1);
  228. $limit = request()->input('limit', 10);
  229. $orderBy = request()->input('orderBy', 'create_time');
  230. $orderType = request()->input('orderType', 'desc');
  231. $params = $this->searchByAuth($params);
  232. // 公共处理完的where, 自然量ID, auth_id=0为自然量
  233. $whereRaw = $this->getCommonWhereRaw($params);
  234. $tables = ['role_data_and', 'role_data_ios'];
  235. $db = Db::connect('db_game_log');
  236. $unionQuery = [];
  237. foreach ($tables as $tableName){
  238. $unionQuery[] = $db->table($tableName)->whereRaw(where: $whereRaw)->buildSql();
  239. }
  240. $fullSql = "(" . implode(' UNION ALL ', $unionQuery) . ") as unTable";
  241. return $db->table($fullSql)->order($orderBy, $orderType)->paginate($limit)->toArray();
  242. }
  243. }