GameGroupLogic.php 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | saiadmin [ saiadmin快速开发框架 ]
  4. // +----------------------------------------------------------------------
  5. // | Author: your name
  6. // +----------------------------------------------------------------------
  7. namespace app\v1\logic\center;
  8. use plugin\saiadmin\basic\BaseLogic;
  9. use plugin\saiadmin\exception\ApiException;
  10. use plugin\saiadmin\utils\Helper;
  11. use app\v1\model\center\GameGroup;
  12. use support\think\Db;
  13. /**
  14. * 游戏分组逻辑层
  15. */
  16. class GameGroupLogic extends BaseLogic
  17. {
  18. private $gameMainLogic;
  19. /**
  20. * 构造函数
  21. */
  22. public function __construct()
  23. {
  24. $this->model = new GameGroup();
  25. $this->gameMainLogic = new GameMainLogic();
  26. }
  27. /**
  28. * 读取数据
  29. * @param $id
  30. * @return mixed
  31. */
  32. public function read($id): mixed
  33. {
  34. $data = $this->model->where('id', $id)->find();
  35. $game_list = explode(',', $data['game_list']);
  36. $allGameList = Db::connect('db_center')->table('pf_game')->field('id,name')->select()->toArray();
  37. $allGameList = array_column($allGameList, 'name', 'id');
  38. $gameList = [];
  39. foreach ($game_list as $game_id) {
  40. $gameList[] = '[' . $game_id . ']' . $allGameList[$game_id] ?? '';
  41. }
  42. $data['game_list_str'] = $gameList;
  43. return $data;
  44. }
  45. /**
  46. * 给游戏分组用的游戏列表
  47. */
  48. public function getGameListOptionsByGroup()
  49. {
  50. $data = $this->model->where('1=1')->order('sort', 'desc')->order('id', 'desc')->select()->toArray();
  51. // 已经存在分组的game_list
  52. $gameIdList = [];
  53. foreach ($data as $item) {
  54. $temGameListArray = explode(',', $item['game_list']);
  55. $gameIdList = array_merge($gameIdList, $temGameListArray);
  56. }
  57. $where = [];
  58. $where[] = ['status', 1];
  59. if (!empty($gameIdList)) {
  60. $where[] = ['id', 'notin', implode(',', $gameIdList)];
  61. }
  62. $game = Db::connect('db_center')->table('pf_game')->where($where)->select()->toArray();
  63. $mainGameMap = $this->gameMainLogic->getMainGameMap();
  64. $groupedGames = [];
  65. foreach ($game as $item) {
  66. $mainId = $item['main_id'];
  67. $groupedGames[$mainId]['id'] = 'main_id_'.$mainId;
  68. $groupedGames[$mainId]['name'] = $mainGameMap[$mainId] ?? $mainId;
  69. $groupedGames[$mainId]['children'][] = [
  70. 'id' => $item['id'],
  71. 'name' => '['.$item['id'].']'.$item['name']
  72. ];
  73. }
  74. return array_values($groupedGames);
  75. }
  76. }