| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329 |
- <?php
- namespace App\Http\Controllers\Admin;
- use Illuminate\Http\Request;
- use Illuminate\Support\Facades\DB;
- class GameSomeConfigController
- {
- const CONFIG_TABLE = 'QPPlatformDB.dbo.GameSomeConfig';
- // 游戏卡片来源表(与 /admin/game_site/builder 游戏卡片编辑保持一致)
- const GAME_TABLE = 'webgame.games';
- /**
- * 品牌厂商分组:展示名 => webgame.games.brand 中对应的取值。
- * 取值与 webgame.games.brand 实际存储一致;如后续新增厂商在此追加即可。
- */
- private static $brandGroups = [
- 'PGSoft' => ['PGSoft'],
- 'JILI' => ['JILI'],
- 'PragmaticPlay' => ['PP'],
- 'IGT' => ['IGT'],
- 'Hacksaw' => ['Hacksaw'],
- ];
- /**
- * 个控回报配置列表(可编辑)
- */
- public function index(Request $request)
- {
- // 获取游戏ID,默认为0(通用配置)
- $gameId = (int)$request->input('game_id', 0);
- // 获取库存模式,默认为1(普通模式)
- $stockMode = (int)$request->input('stock_mode', 1);
- if (!in_array($stockMode, [1, 2])) {
- $stockMode = 1;
- }
- // 按品牌分组的开放游戏列表
- $groupedGames = $this->getOpenGamesGroupedByBrand();
- $games = [0 => '通用配置'];
- foreach ($groupedGames as $brandGames) {
- foreach ($brandGames as $game) {
- $games[$game['id']] = $game['name'];
- }
- }
- if (!array_key_exists($gameId, $games)) {
- $gameId = 0;
- }
- // 查询指定GameID和StockMode的配置
- $configs = DB::connection('write')
- ->table(self::CONFIG_TABLE)
- ->where('GameID', $gameId)
- ->where('StockMode', $stockMode)
- ->orderBy('ZMin', 'asc')
- ->orderBy('ZMax', 'asc')
- ->orderBy('MultiMin', 'asc')
- ->get();
- return view('admin.game_some_config.index', compact('configs', 'games', 'groupedGames', 'gameId', 'stockMode'));
- }
- /**
- * 批量更新配置(只更新有变动的数据)
- */
- public function update(Request $request)
- {
- $db = DB::connection('write');
- try {
- $configs = $request->input('configs', []);
- if (empty($configs)) {
- return apiReturnFail('没有需要更新的数据');
- }
- $db->beginTransaction();
- $updatedIds = [];
- $updatedCount = 0;
- foreach ($configs as $configId => $data) {
- // 构建更新数据(只包含提交的字段)
- $updateData = [];
- if (isset($data['ZMin'])) {
- $updateData['ZMin'] = (int)$data['ZMin'];
- }
- if (isset($data['ZMax'])) {
- $updateData['ZMax'] = (int)$data['ZMax'];
- }
- if (isset($data['MultiMin'])) {
- $updateData['MultiMin'] = round((float)$data['MultiMin'], 2);
- }
- if (isset($data['MultiMax'])) {
- $updateData['MultiMax'] = round((float)$data['MultiMax'], 2);
- }
- // if (isset($data['MultiAvg'])) {
- // $updateData['MultiAvg'] = round((float)$data['MultiAvg'], 2);
- // }
- if (isset($data['Weight'])) {
- $updateData['Weight'] = (int)$data['Weight'];
- }
- // 个控调节支持配置为空(使用 array_key_exists 以支持空字符串)
- if (array_key_exists('WeightAdjust', $data)) {
- $updateData['WeightAdjust'] = (string)($data['WeightAdjust'] ?? '');
- }
- // Status字段不允许修改,已移除
- // 只有有数据时才更新
- if (!empty($updateData)) {
- $db->table(self::CONFIG_TABLE)
- ->where('ConfigID', $configId)
- ->update($updateData);
- $updatedIds[] = $configId;
- $updatedCount++;
- }
- }
- $db->commit();
- \Log::info('个控回报配置更新成功', [
- 'admin_id' => $request->session()->get('admin')->id ?? 0,
- 'updated_count' => $updatedCount,
- 'config_ids' => $updatedIds
- ]);
- return apiReturnSuc("配置更新成功,共更新 {$updatedCount} 条数据");
- } catch (\Exception $e) {
- try {
- $db->rollBack();
- } catch (\Exception $rollbackException) {
- }
- \Log::error('个控回报配置更新失败', [
- 'error' => $e->getMessage(),
- 'trace' => $e->getTraceAsString()
- ]);
- return apiReturnFail('更新失败: ' . $e->getMessage());
- }
- }
- /**
- * 将通用配置复制到选中的开放游戏。
- */
- public function copyCommon(Request $request)
- {
- $db = DB::connection('write');
- try {
- $stockMode = (int)$request->input('stock_mode', 1);
- if (!in_array($stockMode, [1, 2])) {
- return apiReturnFail('库存模式参数错误');
- }
- $gameIds = $request->input('game_ids', []);
- if (!is_array($gameIds)) {
- $gameIds = explode(',', (string)$gameIds);
- }
- $openGameIds = $this->getOpenGameIds();
- $targetGameIds = [];
- foreach ($gameIds as $gameId) {
- $gameId = (int)$gameId;
- if ($gameId > 0 && in_array($gameId, $openGameIds)) {
- $targetGameIds[$gameId] = $gameId;
- }
- }
- $targetGameIds = array_values($targetGameIds);
- if (empty($targetGameIds)) {
- return apiReturnFail('请先选择要复制到的游戏');
- }
- $commonConfigs = $db->table(self::CONFIG_TABLE)
- ->where('GameID', 0)
- ->where('StockMode', $stockMode)
- ->orderBy('ZMin', 'asc')
- ->orderBy('ZMax', 'asc')
- ->orderBy('MultiMin', 'asc')
- ->get();
- if ($commonConfigs->isEmpty()) {
- return apiReturnFail('通用配置暂无数据,无法复制');
- }
- $db->beginTransaction();
- $insertedCount = 0;
- foreach ($targetGameIds as $targetGameId) {
- $db->table(self::CONFIG_TABLE)
- ->where('GameID', $targetGameId)
- ->where('StockMode', $stockMode)
- ->delete();
- $rows = [];
- foreach ($commonConfigs as $config) {
- $row = (array)$config;
- unset($row['ConfigID']);
- $row['GameID'] = $targetGameId;
- $row['StockMode'] = $stockMode;
- $rows[] = $row;
- }
- if (!empty($rows)) {
- foreach (array_chunk($rows, 100) as $chunk) {
- $db->table(self::CONFIG_TABLE)->insert($chunk);
- }
- $insertedCount += count($rows);
- }
- }
- $db->commit();
- \Log::info('通用个控回报配置复制成功', [
- 'admin_id' => $request->session()->get('admin')->id ?? 0,
- 'stock_mode' => $stockMode,
- 'target_game_ids' => $targetGameIds,
- 'inserted_count' => $insertedCount,
- ]);
- return apiReturnSuc([
- 'affected_games' => count($targetGameIds),
- 'inserted_count' => $insertedCount,
- ], '', '复制成功');
- } catch (\Exception $e) {
- try {
- $db->rollBack();
- } catch (\Exception $rollbackException) {
- }
- \Log::error('通用个控回报配置复制失败', [
- 'error' => $e->getMessage(),
- 'trace' => $e->getTraceAsString(),
- ]);
- return apiReturnFail('复制失败: ' . $e->getMessage());
- }
- }
- /**
- * 获取开放游戏并按品牌分类。
- *
- * 数据来源与 /admin/game_site/builder 游戏卡片编辑一致:直接读取 webgame.games,
- * 以 state>0 判定为开放游戏,按真实 brand 字段归类。
- * GameSomeConfig.GameID 对应 webgame.games.com_gameId(统一内部gameid),
- * 因此仅纳入 com_gameId>0 的游戏。
- */
- private function getOpenGamesGroupedByBrand()
- {
- // 收集目标品牌的所有 brand 取值,并建立 brand 取值 => 展示名 的映射
- $brandValues = [];
- $brandLabelMap = [];
- foreach (self::$brandGroups as $label => $values) {
- foreach ($values as $value) {
- $brandValues[] = $value;
- $brandLabelMap[strtolower($value)] = $label;
- }
- }
- $rows = DB::connection('mysql')
- ->table(self::GAME_TABLE)
- ->select('com_gameId', 'title', 'brand')
- ->where('state', '>', 0)
- ->where('com_gameId', '>', 0)
- ->whereIn('brand', $brandValues)
- ->orderBy('brand', 'asc')
- ->orderBy('title', 'asc')
- ->get();
- $groups = [];
- $seen = [];
- foreach ($rows as $row) {
- $gameId = (int)$row->com_gameId;
- if ($gameId <= 0 || isset($seen[$gameId])) {
- continue;
- }
- $seen[$gameId] = true;
- $label = $brandLabelMap[strtolower((string)$row->brand)] ?? (string)$row->brand;
- if (!isset($groups[$label])) {
- $groups[$label] = [];
- }
- $groups[$label][] = [
- 'id' => $gameId,
- 'name' => ($row->title !== null && $row->title !== '') ? $row->title : ('Game ' . $gameId),
- ];
- }
- // 按 $brandGroups 定义的厂商顺序输出
- $orderedGroups = [];
- foreach (array_keys(self::$brandGroups) as $label) {
- if (!empty($groups[$label])) {
- $orderedGroups[$label] = $groups[$label];
- unset($groups[$label]);
- }
- }
- foreach ($groups as $label => $games) {
- $orderedGroups[$label] = $games;
- }
- return $orderedGroups;
- }
- /**
- * 获取所有开放游戏的 GameID 列表(用于复制通用配置时校验目标游戏)。
- */
- private function getOpenGameIds()
- {
- $ids = [];
- foreach ($this->getOpenGamesGroupedByBrand() as $games) {
- foreach ($games as $game) {
- $gameId = (int)$game['id'];
- if ($gameId > 0) {
- $ids[$gameId] = $gameId;
- }
- }
- }
- return array_values($ids);
- }
- }
|