Egame.php 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628
  1. <?php
  2. namespace App\Http\Controllers\admin;
  3. use App\Constants\HttpStatus;
  4. use App\Http\Controllers\Controller;
  5. use App\Models\EgameItem;
  6. use Exception;
  7. use Illuminate\Http\JsonResponse;
  8. use Illuminate\Support\Facades\DB;
  9. use Illuminate\Validation\Rule;
  10. use Illuminate\Validation\ValidationException;
  11. class Egame extends Controller
  12. {
  13. private const FALLBACK_GAME_TYPE_NAMES = [
  14. 1 => '视讯',
  15. 2 => '电子',
  16. 3 => '彩票',
  17. 4 => '体育',
  18. 5 => '电竞',
  19. 6 => '捕鱼',
  20. 7 => '棋牌',
  21. ];
  22. public function items(): JsonResponse
  23. {
  24. try {
  25. request()->validate([
  26. 'item_type' => ['nullable', Rule::in([
  27. EgameItem::TYPE_CATEGORY,
  28. EgameItem::TYPE_PLATFORM,
  29. EgameItem::TYPE_GAME,
  30. ])],
  31. 'platform_scope' => ['nullable', Rule::in([
  32. EgameItem::PLATFORM_SCOPE_GLOBAL,
  33. EgameItem::PLATFORM_SCOPE_CATEGORY,
  34. ])],
  35. 'plat_type' => ['nullable', 'string', 'max:32'],
  36. 'game_type' => ['nullable', 'integer', 'min:0', 'max:255'],
  37. 'game_code' => ['nullable', 'string', 'max:128'],
  38. 'name' => ['nullable', 'string', 'max:128'],
  39. 'keyword' => ['nullable', 'string', 'max:128'],
  40. 'ingress' => ['nullable', Rule::in(EgameItem::ingressValues())],
  41. 'network' => ['nullable', Rule::in(EgameItem::networkValues())],
  42. 'status' => ['nullable', 'integer', Rule::in([
  43. EgameItem::STATUS_DISABLED,
  44. EgameItem::STATUS_ENABLED,
  45. ])],
  46. 'page' => ['nullable', 'integer', 'min:1'],
  47. 'limit' => ['nullable', 'integer', 'min:1', 'max:200'],
  48. ]);
  49. $query = EgameItem::query();
  50. foreach (['item_type', 'plat_type', 'game_type', 'game_code', 'status', 'ingress', 'network'] as $field) {
  51. $value = request()->input($field);
  52. if ($value !== null && $value !== '') {
  53. $query->where($field, $value);
  54. }
  55. }
  56. $name = trim((string) request()->input('name', ''));
  57. if ($name !== '') {
  58. $query->where('name', 'like', "%{$name}%");
  59. }
  60. $platformScope = (string) request()->input('platform_scope', '');
  61. if ($platformScope !== '') {
  62. $itemType = (string) request()->input('item_type', '');
  63. if ($itemType !== '' && $itemType !== EgameItem::TYPE_PLATFORM) {
  64. throw new Exception('platform_scope 只适用于平台配置', HttpStatus::CUSTOM_ERROR);
  65. }
  66. $query->where('item_type', EgameItem::TYPE_PLATFORM);
  67. if ($platformScope === EgameItem::PLATFORM_SCOPE_GLOBAL) {
  68. $query->where('game_type', 0);
  69. } else {
  70. $query->where('game_type', '>', 0);
  71. }
  72. }
  73. $keyword = trim((string) request()->input('keyword', ''));
  74. if ($keyword !== '') {
  75. $query->where(function ($query) use ($keyword) {
  76. $query->where('name', 'like', "%{$keyword}%")
  77. ->orWhere('plat_type', 'like', "%{$keyword}%")
  78. ->orWhere('game_code', 'like', "%{$keyword}%");
  79. });
  80. }
  81. $limit = (int) request()->input('limit', 15);
  82. $page = (int) request()->input('page', 1);
  83. $total = (clone $query)->count();
  84. $states = $this->catalogStates();
  85. $gameCountsByPlat = $this->gameCountsByPlat();
  86. $list = $query->orderByDesc('sort')
  87. ->orderByDesc('id')
  88. ->forPage($page, $limit)
  89. ->get()
  90. ->map(fn (EgameItem $item) => $this->decorateItem($item, $states, $gameCountsByPlat))
  91. ->values();
  92. } catch (ValidationException $e) {
  93. return $this->error(HttpStatus::VALIDATION_FAILED, $e->validator->errors()->first());
  94. } catch (Exception $e) {
  95. return $this->error(HttpStatus::CUSTOM_ERROR, $e->getMessage());
  96. }
  97. return $this->success(['total' => $total, 'data' => $list]);
  98. }
  99. public function options(): JsonResponse
  100. {
  101. try {
  102. $states = $this->catalogStates();
  103. $categoryPlatformCounts = $this->groupedCounts(
  104. EgameItem::query()
  105. ->where('item_type', EgameItem::TYPE_PLATFORM)
  106. ->where('game_type', '>', 0),
  107. 'game_type',
  108. 'plat_type'
  109. );
  110. $categoryGameCounts = $this->groupedCounts(
  111. EgameItem::query()->where('item_type', EgameItem::TYPE_GAME),
  112. 'game_type',
  113. 'id'
  114. );
  115. $platformCategoryCounts = $this->groupedCounts(
  116. EgameItem::query()
  117. ->where('item_type', EgameItem::TYPE_PLATFORM)
  118. ->where('game_type', '>', 0),
  119. 'plat_type',
  120. 'game_type'
  121. );
  122. $platformGameCounts = $this->gameCountsByPlat();
  123. $categories = EgameItem::query()
  124. ->where('item_type', EgameItem::TYPE_CATEGORY)
  125. ->orderByDesc('sort')
  126. ->orderBy('game_type')
  127. ->get()
  128. ->map(function (EgameItem $item) use ($states, $categoryPlatformCounts, $categoryGameCounts) {
  129. $this->decorateItem($item, $states);
  130. $key = (string) $item->game_type;
  131. $item->setAttribute('platform_count', $categoryPlatformCounts[$key] ?? 0);
  132. $item->setAttribute('game_count', $categoryGameCounts[$key] ?? 0);
  133. return $item;
  134. })
  135. ->values();
  136. $platforms = EgameItem::query()
  137. ->where('item_type', EgameItem::TYPE_PLATFORM)
  138. ->where('game_type', 0)
  139. ->orderByDesc('sort')
  140. ->orderBy('name')
  141. ->orderBy('plat_type')
  142. ->get()
  143. ->map(function (EgameItem $item) use ($states, $platformCategoryCounts, $platformGameCounts) {
  144. $this->decorateItem($item, $states, $platformGameCounts);
  145. $key = strtolower((string) $item->plat_type);
  146. $item->setAttribute('category_count', $platformCategoryCounts[$key] ?? 0);
  147. $item->setAttribute('game_count', $platformGameCounts[$key] ?? 0);
  148. return $item;
  149. })
  150. ->values();
  151. } catch (Exception $e) {
  152. return $this->error(HttpStatus::CUSTOM_ERROR, $e->getMessage());
  153. }
  154. return $this->success([
  155. 'categories' => $categories,
  156. 'platforms' => $platforms,
  157. 'statuses' => EgameItem::optionList(EgameItem::STATUS_LABELS),
  158. 'ingress' => EgameItem::optionList(EgameItem::INGRESS_LABELS),
  159. 'networks' => EgameItem::optionList(EgameItem::NETWORK_LABELS),
  160. ]);
  161. }
  162. public function update(): JsonResponse
  163. {
  164. try {
  165. request()->validate([
  166. 'id' => ['nullable', 'integer', 'min:0'],
  167. 'item_type' => ['nullable', Rule::in([
  168. EgameItem::TYPE_CATEGORY,
  169. EgameItem::TYPE_PLATFORM,
  170. EgameItem::TYPE_GAME,
  171. ])],
  172. 'platform_scope' => ['nullable', Rule::in([
  173. EgameItem::PLATFORM_SCOPE_GLOBAL,
  174. EgameItem::PLATFORM_SCOPE_CATEGORY,
  175. ])],
  176. 'plat_type' => ['nullable', 'string', 'max:32'],
  177. 'game_type' => ['nullable', 'integer', 'min:0', 'max:255'],
  178. 'game_code' => ['nullable', 'string', 'max:128'],
  179. 'name' => ['nullable', 'string', 'max:128'],
  180. 'description' => ['nullable', 'string', 'max:255'],
  181. 'ingress' => ['nullable', Rule::in(EgameItem::ingressValues())],
  182. 'network' => ['nullable', Rule::in(array_merge([''], EgameItem::networkValues()))],
  183. 'lobby_enabled' => ['nullable', 'boolean'],
  184. 'logo' => ['nullable', 'string', 'max:500'],
  185. 'logo_pc' => ['nullable', 'string', 'max:500'],
  186. 'logo_h5' => ['nullable', 'string', 'max:500'],
  187. 'logo_langs' => ['nullable', 'array'],
  188. 'status' => ['nullable', 'integer', Rule::in([
  189. EgameItem::STATUS_DISABLED,
  190. EgameItem::STATUS_ENABLED,
  191. ])],
  192. 'sort' => ['nullable', 'integer', 'min:-999999', 'max:999999'],
  193. ]);
  194. $id = (int) request()->input('id', 0);
  195. $item = DB::transaction(function () use ($id) {
  196. if ($id > 0) {
  197. $item = EgameItem::query()->find($id);
  198. if (!$item) {
  199. throw new Exception('配置不存在', HttpStatus::CUSTOM_ERROR);
  200. }
  201. $this->assertIdentityUnchanged($item);
  202. } else {
  203. $identity = $this->normalizedIdentityForCreate();
  204. $item = EgameItem::query()->firstOrNew($identity);
  205. }
  206. $data = $this->mutableItemData($item, !$item->exists);
  207. $name = array_key_exists('name', $data) ? $data['name'] : (string) $item->name;
  208. if ($item->isCategory() && trim($name) === '') {
  209. throw new Exception('分类名称不能为空', HttpStatus::CUSTOM_ERROR);
  210. }
  211. if (!empty($data) || !$item->exists) {
  212. $item->fill($data)->save();
  213. }
  214. return $item->fresh();
  215. });
  216. } catch (ValidationException $e) {
  217. return $this->error(HttpStatus::VALIDATION_FAILED, $e->validator->errors()->first());
  218. } catch (Exception $e) {
  219. return $this->error((int) $e->getCode(), $e->getMessage());
  220. }
  221. return $this->success($this->decorateItem($item, $this->catalogStates(), $this->gameCountsByPlat()));
  222. }
  223. public function setStatus(): JsonResponse
  224. {
  225. try {
  226. request()->validate([
  227. 'id' => ['required', 'integer', 'min:1'],
  228. 'status' => ['required', 'integer', Rule::in([
  229. EgameItem::STATUS_DISABLED,
  230. EgameItem::STATUS_ENABLED,
  231. ])],
  232. ]);
  233. $item = EgameItem::query()->find((int) request()->input('id'));
  234. if (!$item) {
  235. throw new Exception('配置不存在', HttpStatus::CUSTOM_ERROR);
  236. }
  237. $item->status = (int) request()->input('status');
  238. $item->save();
  239. $item = $item->fresh();
  240. } catch (ValidationException $e) {
  241. return $this->error(HttpStatus::VALIDATION_FAILED, $e->validator->errors()->first());
  242. } catch (Exception $e) {
  243. return $this->error((int) $e->getCode(), $e->getMessage());
  244. }
  245. return $this->success($this->decorateItem($item, $this->catalogStates(), $this->gameCountsByPlat()));
  246. }
  247. public function delete(): JsonResponse
  248. {
  249. try {
  250. request()->validate([
  251. 'id' => ['required', 'integer', 'min:1'],
  252. ]);
  253. if (!EgameItem::query()->whereKey((int) request()->input('id'))->exists()) {
  254. throw new Exception('配置不存在', HttpStatus::CUSTOM_ERROR);
  255. }
  256. } catch (ValidationException $e) {
  257. return $this->error(HttpStatus::VALIDATION_FAILED, $e->validator->errors()->first());
  258. } catch (Exception $e) {
  259. return $this->error((int) $e->getCode(), $e->getMessage());
  260. }
  261. return $this->error(
  262. HttpStatus::CUSTOM_ERROR,
  263. '第三方游戏目录由同步任务维护,不能删除,请将状态设为关闭'
  264. );
  265. }
  266. private function normalizedIdentityForCreate(): array
  267. {
  268. $input = request()->all();
  269. if (!array_key_exists('item_type', $input) || !array_key_exists('game_type', $input)) {
  270. throw new Exception('新增配置必须填写 item_type 和 game_type', HttpStatus::CUSTOM_ERROR);
  271. }
  272. $itemType = (string) request()->input('item_type', '');
  273. $platformScope = (string) request()->input('platform_scope', '');
  274. if (!in_array($itemType, [
  275. EgameItem::TYPE_CATEGORY,
  276. EgameItem::TYPE_PLATFORM,
  277. EgameItem::TYPE_GAME,
  278. ], true)) {
  279. throw new Exception('item_type 参数错误', HttpStatus::CUSTOM_ERROR);
  280. }
  281. if ($platformScope !== '' && $itemType !== EgameItem::TYPE_PLATFORM) {
  282. throw new Exception('platform_scope 只适用于平台配置', HttpStatus::CUSTOM_ERROR);
  283. }
  284. $identity = [
  285. 'item_type' => $itemType,
  286. 'plat_type' => strtolower(trim((string) request()->input('plat_type', ''))),
  287. 'game_type' => (int) request()->input('game_type'),
  288. 'game_code' => trim((string) request()->input('game_code', '')),
  289. ];
  290. if ($itemType === EgameItem::TYPE_CATEGORY) {
  291. if ($identity['game_type'] <= 0) {
  292. throw new Exception('分类 game_type 必须大于 0', HttpStatus::CUSTOM_ERROR);
  293. }
  294. $identity['plat_type'] = '';
  295. $identity['game_code'] = '';
  296. return $identity;
  297. }
  298. if ($identity['plat_type'] === '') {
  299. throw new Exception('平台代码不能为空', HttpStatus::CUSTOM_ERROR);
  300. }
  301. if ($itemType === EgameItem::TYPE_PLATFORM) {
  302. if ($platformScope === EgameItem::PLATFORM_SCOPE_GLOBAL) {
  303. $identity['game_type'] = 0;
  304. }
  305. if ($platformScope === EgameItem::PLATFORM_SCOPE_CATEGORY && $identity['game_type'] <= 0) {
  306. throw new Exception('平台分类关联的 game_type 必须大于 0', HttpStatus::CUSTOM_ERROR);
  307. }
  308. $identity['game_code'] = '';
  309. return $identity;
  310. }
  311. if ($identity['game_type'] <= 0) {
  312. throw new Exception('游戏 game_type 必须大于 0', HttpStatus::CUSTOM_ERROR);
  313. }
  314. if ($identity['game_code'] === '') {
  315. throw new Exception('游戏配置必须填写 game_code', HttpStatus::CUSTOM_ERROR);
  316. }
  317. return $identity;
  318. }
  319. private function assertIdentityUnchanged(EgameItem $item): void
  320. {
  321. $input = request()->all();
  322. $checks = [
  323. 'item_type' => (string) $item->item_type,
  324. 'plat_type' => strtolower((string) $item->plat_type),
  325. 'game_type' => (int) $item->game_type,
  326. 'game_code' => (string) $item->game_code,
  327. ];
  328. foreach ($checks as $field => $expected) {
  329. if (!array_key_exists($field, $input) || $input[$field] === null) {
  330. continue;
  331. }
  332. $actual = $input[$field];
  333. if ($field === 'plat_type') {
  334. $actual = strtolower(trim((string) $actual));
  335. } elseif ($field === 'game_type') {
  336. $actual = (int) $actual;
  337. } else {
  338. $actual = trim((string) $actual);
  339. }
  340. if ($actual !== $expected) {
  341. throw new Exception('目录标识不允许修改:' . $field, HttpStatus::CUSTOM_ERROR);
  342. }
  343. }
  344. if (
  345. array_key_exists('platform_scope', $input)
  346. && $input['platform_scope'] !== null
  347. && $input['platform_scope'] !== ''
  348. ) {
  349. $expectedScope = '';
  350. if ($item->isGlobalPlatform()) {
  351. $expectedScope = EgameItem::PLATFORM_SCOPE_GLOBAL;
  352. } elseif ($item->isPlatformRelation()) {
  353. $expectedScope = EgameItem::PLATFORM_SCOPE_CATEGORY;
  354. }
  355. if ((string) $input['platform_scope'] !== $expectedScope) {
  356. throw new Exception('目录标识不允许修改:platform_scope', HttpStatus::CUSTOM_ERROR);
  357. }
  358. }
  359. }
  360. private function mutableItemData(EgameItem $item, bool $isNew): array
  361. {
  362. $input = request()->all();
  363. $data = $isNew ? [
  364. 'name' => '',
  365. 'description' => '',
  366. 'ingress' => EgameItem::INGRESS_BOTH,
  367. 'network' => '',
  368. 'lobby_enabled' => 0,
  369. 'logo' => '',
  370. 'logo_h5' => '',
  371. 'logo_langs' => null,
  372. 'status' => EgameItem::STATUS_ENABLED,
  373. 'sort' => 0,
  374. ] : [];
  375. foreach (['name', 'description', 'ingress', 'network', 'logo', 'logo_h5'] as $field) {
  376. if (array_key_exists($field, $input)) {
  377. $data[$field] = trim((string) $input[$field]);
  378. }
  379. }
  380. if (array_key_exists('logo_pc', $input) && !array_key_exists('logo', $data)) {
  381. $data['logo'] = trim((string) $input['logo_pc']);
  382. }
  383. if (array_key_exists('logo_langs', $input)) {
  384. $data['logo_langs'] = $item->item_type === EgameItem::TYPE_GAME
  385. ? $input['logo_langs']
  386. : null;
  387. }
  388. if (array_key_exists('status', $input)) {
  389. $data['status'] = (int) $input['status'];
  390. }
  391. if (array_key_exists('sort', $input)) {
  392. $data['sort'] = (int) $input['sort'];
  393. }
  394. if (array_key_exists('lobby_enabled', $input)) {
  395. $data['lobby_enabled'] = $item->isPlatformRelation()
  396. ? (int) request()->boolean('lobby_enabled')
  397. : 0;
  398. }
  399. if ($item->isCategory() && array_key_exists('ingress', $data)) {
  400. $data['ingress'] = EgameItem::INGRESS_BOTH;
  401. }
  402. if ($item->isCategory() && array_key_exists('network', $data)) {
  403. $data['network'] = '';
  404. }
  405. if ($item->item_type !== EgameItem::TYPE_GAME && array_key_exists('logo_langs', $data)) {
  406. $data['logo_langs'] = null;
  407. }
  408. if (!$item->isPlatformRelation() && array_key_exists('lobby_enabled', $data)) {
  409. $data['lobby_enabled'] = 0;
  410. }
  411. return $data;
  412. }
  413. private function catalogStates(): array
  414. {
  415. $states = [
  416. 'categories' => [],
  417. 'global_platforms' => [],
  418. 'platform_relations' => [],
  419. ];
  420. $rows = EgameItem::query()
  421. ->whereIn('item_type', [EgameItem::TYPE_CATEGORY, EgameItem::TYPE_PLATFORM])
  422. ->get();
  423. foreach ($rows as $row) {
  424. if ($row->isCategory()) {
  425. $states['categories'][(string) $row->game_type] = $row;
  426. continue;
  427. }
  428. $platType = strtolower((string) $row->plat_type);
  429. if ($row->isGlobalPlatform()) {
  430. $states['global_platforms'][$platType] = $row;
  431. continue;
  432. }
  433. if ($row->isPlatformRelation()) {
  434. $states['platform_relations'][$platType][(string) $row->game_type] = $row;
  435. }
  436. }
  437. return $states;
  438. }
  439. private function decorateItem(EgameItem $item, array $states, array $gameCountsByPlat = []): EgameItem
  440. {
  441. $platType = strtolower((string) $item->plat_type);
  442. $gameType = (string) $item->game_type;
  443. $disabledBy = null;
  444. $category = $states['categories'][$gameType] ?? null;
  445. $globalPlatform = $states['global_platforms'][$platType] ?? null;
  446. $platformRelation = $states['platform_relations'][$platType][$gameType] ?? null;
  447. if ($item->isCategory()) {
  448. if (!$this->enabled($item)) {
  449. $disabledBy = 'category';
  450. }
  451. } elseif ($item->isGlobalPlatform()) {
  452. if (!$this->enabled($item)) {
  453. $disabledBy = 'platform';
  454. }
  455. } elseif ($item->isPlatformRelation()) {
  456. if (!$this->enabled($category)) {
  457. $disabledBy = 'category';
  458. } elseif (!$this->enabled($globalPlatform)) {
  459. $disabledBy = 'platform';
  460. } elseif (!$this->enabled($item)) {
  461. $disabledBy = 'platform_category';
  462. }
  463. } else {
  464. if (!$this->enabled($category)) {
  465. $disabledBy = 'category';
  466. } elseif (!$this->enabled($globalPlatform)) {
  467. $disabledBy = 'platform';
  468. } elseif (!$this->enabled($platformRelation)) {
  469. $disabledBy = 'platform_category';
  470. } elseif (!$this->enabled($item)) {
  471. $disabledBy = 'game';
  472. }
  473. }
  474. $item->setAttribute('game_type_name', $this->gameTypeName((int) $item->game_type, $states));
  475. $item->setAttribute(
  476. 'platform_name',
  477. $this->platformName($item, $globalPlatform, $platformRelation)
  478. );
  479. if ($item->item_type === EgameItem::TYPE_PLATFORM) {
  480. $item->setAttribute(
  481. 'platform_scope',
  482. $item->isGlobalPlatform()
  483. ? EgameItem::PLATFORM_SCOPE_GLOBAL
  484. : EgameItem::PLATFORM_SCOPE_CATEGORY
  485. );
  486. } else {
  487. $item->setAttribute('platform_scope', null);
  488. }
  489. $item->setAttribute('effective_status', $disabledBy === null ? 1 : 0);
  490. $item->setAttribute('disabled_by', $disabledBy);
  491. $item->setAttribute('description', (string) ($item->description ?? ''));
  492. $item->setAttribute('logo_pc', (string) ($item->logo ?? ''));
  493. $item->setAttribute('logo_h5', (string) ($item->logo_h5 ?? ''));
  494. $item->setAttribute('network', (string) ($item->network ?? ''));
  495. $item->setAttribute('network_name', EgameItem::networkName($item->network ?? ''));
  496. $item->setAttribute('ingress_name', EgameItem::ingressName($item->ingress ?? ''));
  497. $item->setAttribute('status_name', EgameItem::STATUS_LABELS[(int) $item->status] ?? '');
  498. $gameCount = $gameCountsByPlat[$platType] ?? 0;
  499. $item->setAttribute(
  500. 'has_game_manage',
  501. $item->item_type === EgameItem::TYPE_PLATFORM && $gameCount > 0
  502. );
  503. if ($item->item_type === EgameItem::TYPE_PLATFORM && !$item->hasAttribute('game_count')) {
  504. $item->setAttribute('game_count', $gameCount);
  505. }
  506. return $item;
  507. }
  508. private function gameCountsByPlat(): array
  509. {
  510. $counts = [];
  511. foreach ($this->groupedCounts(
  512. EgameItem::query()->where('item_type', EgameItem::TYPE_GAME),
  513. 'plat_type',
  514. 'id'
  515. ) as $platType => $count) {
  516. $counts[strtolower((string) $platType)] = (int) $count;
  517. }
  518. return $counts;
  519. }
  520. private function enabled(?EgameItem $item): bool
  521. {
  522. return $item !== null && $item->status === EgameItem::STATUS_ENABLED;
  523. }
  524. private function platformName(
  525. EgameItem $item,
  526. ?EgameItem $globalPlatform,
  527. ?EgameItem $platformRelation
  528. ): string {
  529. if ($item->isCategory()) {
  530. return '';
  531. }
  532. if ($item->isGlobalPlatform() && trim((string) $item->name) !== '') {
  533. return (string) $item->name;
  534. }
  535. if ($globalPlatform && trim((string) $globalPlatform->name) !== '') {
  536. return (string) $globalPlatform->name;
  537. }
  538. if ($platformRelation && trim((string) $platformRelation->name) !== '') {
  539. return (string) $platformRelation->name;
  540. }
  541. if ($item->item_type === EgameItem::TYPE_PLATFORM && trim((string) $item->name) !== '') {
  542. return (string) $item->name;
  543. }
  544. return (string) $item->plat_type;
  545. }
  546. private function gameTypeName(int $gameType, array $states): string
  547. {
  548. if ($gameType <= 0) {
  549. return '';
  550. }
  551. $category = $states['categories'][(string) $gameType] ?? null;
  552. if ($category && trim((string) $category->name) !== '') {
  553. return (string) $category->name;
  554. }
  555. return self::FALLBACK_GAME_TYPE_NAMES[$gameType] ?? ('分类' . $gameType);
  556. }
  557. private function groupedCounts($query, string $groupColumn, string $countColumn): array
  558. {
  559. return $query
  560. ->select($groupColumn)
  561. ->selectRaw("COUNT(DISTINCT {$countColumn}) AS aggregate")
  562. ->groupBy($groupColumn)
  563. ->pluck('aggregate', $groupColumn)
  564. ->map(fn ($count) => (int) $count)
  565. ->all();
  566. }
  567. }