GameGroupLogic.php 5.8 KB

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