SdkOrderRankLogic.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | saiadmin [ saiadmin快速开发框架 ]
  4. // +----------------------------------------------------------------------
  5. // | Author: your name
  6. // +----------------------------------------------------------------------
  7. namespace app\v1\logic\gameLog;
  8. use plugin\saiadmin\basic\BaseLogic;
  9. use plugin\saiadmin\exception\ApiException;
  10. use plugin\saiadmin\utils\Helper;
  11. use app\v1\model\gameLog\SdkOrderRank;
  12. use support\think\Db;
  13. /**
  14. * 充值排行逻辑层
  15. */
  16. class SdkOrderRankLogic extends BaseLogic
  17. {
  18. /**
  19. * 构造函数
  20. */
  21. public function __construct()
  22. {
  23. $this->model = new SdkOrderRank();
  24. }
  25. public function getList($where): mixed
  26. {
  27. $saiType = request()->input('saiType', 'list');
  28. $page = request()->input('page', 1);
  29. $limit = request()->input('limit', 10);
  30. $orderBy = request()->input('orderBy', '');
  31. $orderType = request()->input('orderType', $this->orderType);
  32. if(empty($orderBy)) {
  33. $orderBy = 'searchTotalMoney';
  34. }
  35. $params = $this->searchByAuth($where);
  36. $where = [];
  37. if(!empty($params['game_id'])){
  38. $where[] = ['game_id', 'in', $params['game_id']];
  39. }
  40. if(!empty($params['auth_id'])){
  41. $where[] = ['auth_id', 'in', $params['auth_id']];
  42. }
  43. if(!empty($params['agent_id'])){
  44. $where['agent_id'] = $params['agent_id'];
  45. }
  46. if(!empty($params['site_id'])){
  47. $where['site_id'] = $params['site_id'];
  48. }
  49. if(!empty($params['server_id'])){
  50. $where['server_id'] = $params['server_id'];
  51. }
  52. if(!empty($params['user_name'])){
  53. $where['user_name'] = $params['user_name'];
  54. }
  55. if(!empty($params['server_name'])){
  56. $where['server_name'] = $params['server_name'];
  57. }
  58. if(!empty($params['reg_date'])){
  59. $where['reg_date'] = $params['reg_date'];
  60. }
  61. if(!empty($params['pay_date'])){
  62. $sTime = $params['pay_date'][0] . " 00:00:00";
  63. $eTime = $params['pay_date'][1] . " 23:59:59";
  64. $where[] = ['pay_date', 'between', [$sTime, $eTime]];
  65. }
  66. $rankQuery = Db::connect('db_game_log')->table('sdk_order_success')->where($where);
  67. $rankQuery->order($orderBy, $orderType);
  68. // 显示指定日期范围内的所有充值记录,不按uid和game_id分组
  69. $rankQuery->field('user_name,sum(money) as searchTotalMoney,
  70. orderid,
  71. game_id,
  72. media_id,
  73. agent_id,
  74. site_id,
  75. uid,
  76. pay_channel_id,
  77. auth_id,
  78. server_id,
  79. server_name,
  80. pay_date,
  81. reg_date,
  82. role_name,
  83. role_id');
  84. if ($saiType === 'all') {
  85. return $rankQuery->select()->toArray();
  86. }
  87. // 先分组,再分页
  88. $rankQuery->group('uid, game_id,site_id,server_id,media_id,auth_id,agent_id,server_id,auth_id,role_id');
  89. $data = $rankQuery->paginate($limit, false, ['page' => $page])->toArray();
  90. print_r($data);
  91. $data['data'] = $this->trandformListColumn($data['data'], ['game','auth', 'agent', 'pay_channel']);
  92. // 告警提示,查询最近登录时间,查询最近充值时间,和现在时间对比,如果超过3天,则告警,并提示
  93. foreach($data['data'] as &$item){
  94. // 最后登录时间
  95. $item['login_time'] = Db::connect('db_origin')->table('user_'.$item['uid']%10)->where('uid', $item['uid'])->value('login_time');
  96. // 最近注册时间
  97. $item['pay_time'] = Db::connect('db_origin')->table('user_'.$item['uid']%10)->where('uid', $item['uid'])->value('pay_time');
  98. // 现在时间
  99. $item['now_time'] = time();
  100. // 最近登录时间与现在时间对比,如果超过3天,则告警,并提示
  101. if($item['login_time'] < $item['now_time'] - 3 * 24 * 60 * 60){
  102. $item['login_alert'] = 1;
  103. }else{
  104. $item['login_alert'] = 0;
  105. }
  106. // 最近充值时间与现在时间对比,如果超过3天,则告警,并提示
  107. if($item['pay_time'] < $item['now_time'] - 3 * 24 * 60 * 60){
  108. $item['pay_alert'] = 1;
  109. }else{
  110. $item['pay_alert'] = 0;
  111. }
  112. }
  113. $totalWhere = array_filter($where, function($item) {
  114. return $item[0] !== 'pay_date';
  115. });
  116. $totalList = Db::connect('db_game_log')->table('sdk_order_success')
  117. ->where($totalWhere)
  118. ->field('game_id,uid,sum(money) as totalMoney')
  119. ->group('uid,game_id,site_id,server_id,media_id,auth_id,agent_id,server_id,auth_id,role_id')
  120. ->select()->toArray();
  121. // 查询累计充值金额(对每一行数据单独查询历史充值金额)
  122. $totalMoneyMap = [];
  123. if (!empty($data['data']) && !empty($totalList)) {
  124. foreach ($totalList as $totalItem) {
  125. $key = $totalItem['uid'] . '_' . $totalItem['game_id'];
  126. $totalMoneyMap[$key] = $totalItem['totalMoney'] ?? 0;
  127. }
  128. }
  129. // 为搜索结果添加累计充值金额
  130. foreach($data['data'] as &$item){
  131. $key = $item['uid'] . '_' . $item['game_id'];
  132. $item['totalMoney'] = $totalMoneyMap[$key] ?? 0;
  133. }
  134. return $data;
  135. }
  136. }