| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130 |
- <?php
- namespace App\Module\Game\Commands;
- use App\Module\Game\Models\GameRewardGroup;
- use App\Module\Game\Models\GameRewardItem;
- use Illuminate\Console\Command;
- use Illuminate\Support\Facades\DB;
- use Illuminate\Support\Facades\File;
- /**
- * 导入奖励组配置命令
- */
- class ImportRewardGroupsCommand extends Command
- {
- /**
- * 命令名称
- *
- * @var string
- */
- protected $signature = 'game:import-reward-groups {file : JSON配置文件路径}';
- /**
- * 命令描述
- *
- * @var string
- */
- protected $description = '从JSON文件导入奖励组配置';
- /**
- * 执行命令
- *
- * @return int
- */
- public function handle()
- {
- $filePath = $this->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;
- }
- }
|