ImportRewardGroupsCommand.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. <?php
  2. namespace App\Module\Game\Commands;
  3. use App\Module\Game\Models\GameRewardGroup;
  4. use App\Module\Game\Models\GameRewardItem;
  5. use Illuminate\Console\Command;
  6. use Illuminate\Support\Facades\DB;
  7. use Illuminate\Support\Facades\File;
  8. /**
  9. * 导入奖励组配置命令
  10. */
  11. class ImportRewardGroupsCommand extends Command
  12. {
  13. /**
  14. * 命令名称
  15. *
  16. * @var string
  17. */
  18. protected $signature = 'game:import-reward-groups {file : JSON配置文件路径}';
  19. /**
  20. * 命令描述
  21. *
  22. * @var string
  23. */
  24. protected $description = '从JSON文件导入奖励组配置';
  25. /**
  26. * 执行命令
  27. *
  28. * @return int
  29. */
  30. public function handle()
  31. {
  32. $filePath = $this->argument('file');
  33. // 检查文件是否存在
  34. if (!File::exists($filePath)) {
  35. $this->error("文件不存在: {$filePath}");
  36. return 1;
  37. }
  38. // 读取JSON文件
  39. $content = File::get($filePath);
  40. $data = json_decode($content, true);
  41. if (json_last_error() !== JSON_ERROR_NONE) {
  42. $this->error("JSON解析错误: " . json_last_error_msg());
  43. return 1;
  44. }
  45. if (!isset($data['reward_groups']) || !is_array($data['reward_groups'])) {
  46. $this->error("无效的JSON格式,缺少reward_groups数组");
  47. return 1;
  48. }
  49. $groups = $data['reward_groups'];
  50. $this->info("找到 " . count($groups) . " 个奖励组配置");
  51. // 开始导入
  52. $importedCount = 0;
  53. $skippedCount = 0;
  54. $errorCount = 0;
  55. foreach ($groups as $groupData) {
  56. try {
  57. // 检查必要字段
  58. if (!isset($groupData['code']) || !isset($groupData['name'])) {
  59. $this->warn("跳过无效的奖励组配置: 缺少code或name字段");
  60. $skippedCount++;
  61. continue;
  62. }
  63. // 检查奖励组是否已存在
  64. $existingGroup = GameRewardGroup::where('code', $groupData['code'])->first();
  65. if ($existingGroup) {
  66. $this->warn("跳过已存在的奖励组: {$groupData['code']}");
  67. $skippedCount++;
  68. continue;
  69. }
  70. // 开始事务
  71. DB::beginTransaction();
  72. // 创建奖励组
  73. $group = new GameRewardGroup();
  74. $group->name = $groupData['name'];
  75. $group->code = $groupData['code'];
  76. $group->description = $groupData['description'] ?? '';
  77. $group->is_random = $groupData['is_random'] ?? false;
  78. $group->random_count = $groupData['random_count'] ?? 1;
  79. $group->save();
  80. // 创建奖励项
  81. if (isset($groupData['items']) && is_array($groupData['items'])) {
  82. foreach ($groupData['items'] as $itemData) {
  83. $item = new GameRewardItem();
  84. $item->group_id = $group->id;
  85. $item->reward_type = $itemData['reward_type'];
  86. $item->target_id = $itemData['target_id'];
  87. $item->param1 = $itemData['param1'] ?? 0;
  88. $item->param2 = $itemData['param2'] ?? 0;
  89. $item->quantity = $itemData['quantity'] ?? 1;
  90. $item->weight = $itemData['weight'] ?? 1.0;
  91. $item->is_guaranteed = $itemData['is_guaranteed'] ?? false;
  92. $item->extra_data = $itemData['extra_data'] ?? null;
  93. $item->save();
  94. }
  95. }
  96. // 提交事务
  97. DB::commit();
  98. $this->info("导入奖励组成功: {$groupData['code']}");
  99. $importedCount++;
  100. } catch (\Exception $e) {
  101. // 回滚事务
  102. DB::rollBack();
  103. $this->error("导入奖励组失败: {$e->getMessage()}");
  104. $errorCount++;
  105. }
  106. }
  107. $this->info("导入完成: 成功 {$importedCount}, 跳过 {$skippedCount}, 失败 {$errorCount}");
  108. return 0;
  109. }
  110. }