argument('file'); // 检查文件是否存在 if (!File::exists($filePath)) { $this->error("文件不存在: {$filePath}"); return 1; } // 读取JSON文件 $content = File::get($filePath); $data = json_decode($content, true); if (json_last_error() !== JSON_ERROR_NONE) { $this->error("JSON解析错误: " . json_last_error_msg()); return 1; } if (!isset($data['reward_groups']) || !is_array($data['reward_groups'])) { $this->error("无效的JSON格式,缺少reward_groups数组"); return 1; } $groups = $data['reward_groups']; $this->info("找到 " . count($groups) . " 个奖励组配置"); // 开始导入 $importedCount = 0; $skippedCount = 0; $errorCount = 0; foreach ($groups as $groupData) { try { // 检查必要字段 if (!isset($groupData['code']) || !isset($groupData['name'])) { $this->warn("跳过无效的奖励组配置: 缺少code或name字段"); $skippedCount++; continue; } // 检查奖励组是否已存在 $existingGroup = GameRewardGroup::where('code', $groupData['code'])->first(); if ($existingGroup) { $this->warn("跳过已存在的奖励组: {$groupData['code']}"); $skippedCount++; continue; } // 开始事务 DB::beginTransaction(); // 创建奖励组 $group = new GameRewardGroup(); $group->name = $groupData['name']; $group->code = $groupData['code']; $group->description = $groupData['description'] ?? ''; $group->is_random = $groupData['is_random'] ?? false; $group->random_count = $groupData['random_count'] ?? 1; $group->save(); // 创建奖励项 if (isset($groupData['items']) && is_array($groupData['items'])) { foreach ($groupData['items'] as $itemData) { $item = new GameRewardItem(); $item->group_id = $group->id; $item->reward_type = $itemData['reward_type']; $item->target_id = $itemData['target_id']; $item->param1 = $itemData['param1'] ?? 0; $item->param2 = $itemData['param2'] ?? 0; $item->quantity = $itemData['quantity'] ?? 1; $item->weight = $itemData['weight'] ?? 1.0; $item->is_guaranteed = $itemData['is_guaranteed'] ?? false; $item->extra_data = $itemData['extra_data'] ?? null; $item->save(); } } // 提交事务 DB::commit(); $this->info("导入奖励组成功: {$groupData['code']}"); $importedCount++; } catch (\Exception $e) { // 回滚事务 DB::rollBack(); $this->error("导入奖励组失败: {$e->getMessage()}"); $errorCount++; } } $this->info("导入完成: 成功 {$importedCount}, 跳过 {$skippedCount}, 失败 {$errorCount}"); return 0; } }