UserLogLogic.php 11 KB

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