RoleDataLogic.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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\RoleData;
  12. use support\think\Db;
  13. /**
  14. * 角色数据逻辑层
  15. */
  16. class RoleDataLogic extends BaseLogic
  17. {
  18. /**
  19. * 构造函数
  20. */
  21. public function __construct()
  22. {
  23. $this->model = new RoleData();
  24. }
  25. public function getList($query): 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. $regTime = request()->input('reg_time', '');
  33. $gameId = request()->input('game_id', '');
  34. $mediaId = request()->input('media_id', '');
  35. $siteId = request()->input('site_id', '');
  36. $authId = request()->input('auth_id', '');
  37. $userName = request()->input('user_name', '');
  38. $egTableName = 'sdk_reg_log_' . date('Ym', strtotime($regTime));
  39. $userData = Db::connect('db_game_log')->table($egTableName)->where('reg_time', '>=', strtotime($regTime . ' 00:00:00'))->where('reg_time', '<=', strtotime($regTime . ' 23:59:59'))->select()->toArray();
  40. print_r('$userdata===>');
  41. print_r($userData);
  42. $where_sql = "";
  43. if(!empty($gameId)){
  44. $where_sql .= is_array($gameId) ? " AND game_id in (".implode(',', $gameId).")" : " AND game_id = {$gameId}";
  45. }
  46. if(!empty($mediaId)){
  47. $where_sql .= " AND media_id = {$mediaId}";
  48. }
  49. if(!empty($siteId)){
  50. $where_sql .= " AND site_id = {$siteId}";
  51. }
  52. if(!empty($authId)){
  53. $where_sql .= " AND auth_id = {$authId}";
  54. }
  55. if(!empty($userName)){
  56. $where_sql .= " AND user_name = {$userName}";
  57. }
  58. $uids = array_column($userData, 'uid');
  59. if(empty($userData)){
  60. return [];
  61. }
  62. $where_sql = " AND uid in (".implode(',', $uids).") {$where_sql}";
  63. $sql_parts = [];
  64. $sql_parts[] = "SELECT * FROM role_data_and WHERE 1=1 {$where_sql}";
  65. $sql_parts[] = "SELECT * FROM role_data_ios WHERE 1=1 {$where_sql}";
  66. $unionSql = implode(" UNION ALL ", $sql_parts);
  67. // 分页
  68. $page = request()->input('page', 1);
  69. $limit = request()->input('limit', 10);
  70. $offset = ($page - 1) * $limit;
  71. $unionSql .= " LIMIT {$offset}, {$limit}";
  72. $roleData = Db::connect('db_game_log')->query($unionSql);
  73. $count = count($roleData);
  74. // 合并相同uid的数据,将角色信息放到数组中
  75. $mergedData = [];
  76. foreach ($roleData as $item) {
  77. $uid = $item['uid'];
  78. if (!isset($mergedData[$uid])) {
  79. // 创建用户基础信息,排除角色相关字段
  80. $baseInfo = array_diff_key($item, array_flip(['role_id', 'role_name', 'role_level']));
  81. $mergedData[$uid] = $baseInfo + ['roles' => []];
  82. }
  83. // 添加角色信息到数组
  84. $mergedData[$uid]['roles'][] = [
  85. 'server_name' => $item['server_name'],
  86. 'role_name' => $item['role_name'],
  87. 'role_level' => $item['role_level'],
  88. ];
  89. }
  90. // 转换回数组格式
  91. $roleData = array_values($mergedData);
  92. return [
  93. 'data' => $roleData,
  94. 'current_page' => $page,
  95. 'per_page' => $limit,
  96. 'last_page' => ceil($count / $limit),
  97. 'has_more' => $page < ceil($count / $limit),
  98. 'total' => $count
  99. ];
  100. }
  101. }