GameGroupLogic.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. <?php
  2. namespace app\v1\logic\center;
  3. use plugin\saiadmin\basic\BaseLogic;
  4. use app\v1\model\center\GameGroup;
  5. use support\think\Db;
  6. /**
  7. * 游戏分组逻辑层
  8. */
  9. class GameGroupLogic extends BaseLogic
  10. {
  11. /**
  12. * 构造函数
  13. */
  14. public function __construct()
  15. {
  16. $this->model = new GameGroup();
  17. }
  18. /**
  19. * 读取列表
  20. */
  21. public function getList($query): mixed
  22. {
  23. $page = request()->input('page') ? request()->input('page') : 1;
  24. $limit = request()->input('limit') ? request()->input('limit') : 10;
  25. $list = $this->model->order('sort', 'desc')->paginate($limit, false, ['page' => $page])->toArray();
  26. $gameList = Db::connect('db_center')->table('pf_game')->column('name', 'id');
  27. foreach ($gameList as $id => &$name) {
  28. $name = '[' . $id . ']' . $name;
  29. }
  30. $gameList = array_column($gameList, null, 'id');
  31. foreach ($list['data'] as $key => $value) {
  32. $gameIds = explode(',', $value['game_list']);
  33. foreach ($gameIds as $gameId) {
  34. $list['data'][$key]['children'][] = [
  35. 'id' => $gameId,
  36. 'name' => $gameList[$gameId],
  37. ];
  38. }
  39. }
  40. $total = $this->model->count();
  41. $last_page = ceil($total/$limit);
  42. $startIndex = ($page - 1) * $limit;
  43. $pageData = array_slice($list['data'], $startIndex, $limit);
  44. return [
  45. 'data' => $pageData,
  46. 'current_page' => $page,
  47. 'per_page' => $limit,
  48. 'last_page' => $last_page,
  49. 'total' => $total,
  50. ];
  51. // return $this->model->order('sort', 'desc')->paginate($limit, false, ['page' => $page])->toArray();
  52. }
  53. /**
  54. * 读取数据
  55. * @param $id
  56. * @return mixed
  57. */
  58. public function read($id): mixed
  59. {
  60. $data = $this->model->where('id', $id)->find();
  61. $gameIds = explode(',', $data['game_list']);
  62. $gameList = Db::connect('db_center')->table('pf_game')->where("id", 'in', $gameIds)->column('name', 'id');
  63. foreach ($gameList as $id => &$name) {
  64. $name = '[' . $id . ']' . $name;
  65. }
  66. $data['game_list_str'] = $gameList;
  67. return $data;
  68. }
  69. /**
  70. * 给游戏分组用的游戏列表,主要作用,返回可选或者可编辑的游戏列表
  71. * $where['game_group_id]
  72. */
  73. public function getGameListOptionsByGroup($where): array
  74. {
  75. // Todo 获取非自己的已选中
  76. $gameGroupId = $where['game_group_id'];
  77. $data = $this->model->where('id', '<>', $gameGroupId)->column("game_list");
  78. $gameIdList = [];
  79. foreach ($data as $gameIds) {
  80. $gameIds = explode(',', $gameIds);
  81. $gameIdList = array_merge($gameIdList, $gameIds);
  82. }
  83. // Todo 获取游戏分组
  84. $groupWhere = [
  85. 'status'=>1
  86. ];
  87. if (!empty($gameIdList)) {
  88. $groupWhere[] = [ 'id', 'notin', implode(',', $gameIdList)];
  89. }
  90. // 平台游戏
  91. $pfCame = Db::connect('db_center')->table('pf_game')->where($groupWhere)->select()->toArray();
  92. return (new GameLogic())->gameMainTree($pfCame);
  93. }
  94. /**
  95. * 获取游戏根据部门ID, 返回根据分组后的游戏列表
  96. */
  97. public function getGameListOptionsByDeptId($where): array
  98. {
  99. $deptId = $where['dept_id'];
  100. // 1. 根据部门ID获取游戏列表
  101. $deptGameList = Db::connect('db_system')
  102. ->table('sa_system_dept')
  103. ->field('game_list')
  104. ->where('id', $deptId)
  105. ->select()->toArray();
  106. $deptGameList = $deptGameList[0]['game_list'];
  107. // 2. 所有游戏信息列表
  108. $pfGameList = Db::connect('db_center')
  109. ->table('pf_game')
  110. ->field('id,name')
  111. ->select()->toArray();
  112. $pfGameList = array_column($pfGameList, null, 'id');
  113. // 3. 分组列表
  114. $groupList = Db::connect('db_center')
  115. ->table('game_group')
  116. ->select()->toArray();
  117. if(!empty($deptGameList)){
  118. // 返回分组所有游戏
  119. if($deptGameList == '*'){
  120. foreach($groupList as &$group){
  121. $group['id'] = 'group_'.$group['id'];
  122. $gameList = explode(',', $group['game_list']);
  123. foreach($gameList as $gameId){
  124. $group['children'][] = [
  125. 'id' => $pfGameList[$gameId]['id'],
  126. 'name' => '['.$pfGameList[$gameId]['id'].']'.$pfGameList[$gameId]['name']
  127. ];
  128. }
  129. }
  130. $gameList = $groupList;
  131. }else{
  132. // 根据权限 4,5,7
  133. // $deptGameList = 4,5,7
  134. // $group['game_list'] 取交集
  135. $deptGameList = explode(',', $deptGameList);
  136. foreach($groupList as &$group){
  137. $group['id'] = 'group_'.$group['id'];
  138. $gameList = explode(',', $group['game_list']);
  139. $group['game_list'] = array_intersect($deptGameList, $gameList);
  140. foreach($group['game_list'] as $gameId){
  141. $group['children'][] = [
  142. 'id' => $pfGameList[$gameId]['id'],
  143. 'name' => '['.$pfGameList[$gameId]['id'].']'.$pfGameList[$gameId]['name']
  144. ];
  145. }
  146. }
  147. $groupList = array_filter($groupList, function($group){
  148. return !empty($group['game_list']);
  149. });
  150. $gameList = $groupList;
  151. }
  152. }else{
  153. $gameList = [];
  154. }
  155. return $gameList;
  156. }
  157. }