MigrateLandUpgradeMaterialsToConsumeGroupsCommand.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. <?php
  2. namespace App\Module\Farm\Commands;
  3. use App\Module\Farm\Models\FarmLandUpgradeConfig;
  4. use App\Module\Game\Enums\CONSUME_TYPE;
  5. use App\Module\Game\Models\GameConsumeGroup;
  6. use App\Module\Game\Models\GameConsumeItem;
  7. use Illuminate\Console\Command;
  8. use Illuminate\Support\Facades\DB;
  9. use Illuminate\Support\Str;
  10. /**
  11. * 将土地升级材料迁移到消耗组的命令
  12. */
  13. class MigrateLandUpgradeMaterialsToConsumeGroupsCommand extends Command
  14. {
  15. /**
  16. * 命令名称
  17. *
  18. * @var string
  19. */
  20. protected $signature = 'farm:migrate-land-upgrade-materials {--dry-run : 只显示将要执行的操作,不实际执行}';
  21. /**
  22. * 命令描述
  23. *
  24. * @var string
  25. */
  26. protected $description = '将土地升级材料迁移到消耗组';
  27. /**
  28. * 执行命令
  29. *
  30. * @return int
  31. */
  32. public function handle()
  33. {
  34. $this->info('开始迁移土地升级材料到消耗组...');
  35. // 获取所有土地升级配置
  36. $configs = FarmLandUpgradeConfig::all();
  37. $this->info("找到 {$configs->count()} 个土地升级配置");
  38. // 是否为干运行模式
  39. $dryRun = $this->option('dry-run');
  40. if ($dryRun) {
  41. $this->warn('干运行模式:不会实际执行数据库操作');
  42. }
  43. // 开始迁移
  44. $migratedCount = 0;
  45. $skippedCount = 0;
  46. $errorCount = 0;
  47. foreach ($configs as $config) {
  48. $this->line('');
  49. $this->line("处理配置 ID: {$config->id}, 从 {$config->from_type_id} 到 {$config->to_type_id}");
  50. // 如果已经有消耗组ID,则跳过
  51. if ($config->consume_group_id) {
  52. $this->warn(" 已有消耗组ID ({$config->consume_group_id}),跳过");
  53. $skippedCount++;
  54. continue;
  55. }
  56. // 获取材料
  57. $materials = [];
  58. if (is_string($config->materials)) {
  59. $materials = json_decode($config->materials, true)['materials'] ?? [];
  60. } else {
  61. $materials = $config->materials['materials'] ?? [];
  62. }
  63. if (empty($materials)) {
  64. $this->warn(" 没有材料,跳过");
  65. $skippedCount++;
  66. continue;
  67. }
  68. $this->info(" 找到 " . count($materials) . " 个材料项");
  69. foreach ($materials as $material) {
  70. $this->line(" 物品ID: {$material['item_id']}, 数量: {$material['amount']}");
  71. }
  72. try {
  73. // 如果不是干运行模式,则创建消耗组
  74. if (!$dryRun) {
  75. DB::beginTransaction();
  76. // 创建消耗组
  77. $consumeGroup = new GameConsumeGroup();
  78. $consumeGroup->name = "土地升级: {$config->from_type_id} -> {$config->to_type_id}";
  79. $consumeGroup->code = "land_upgrade_{$config->from_type_id}_{$config->to_type_id}_" . Str::random(6);
  80. $consumeGroup->description = "从土地类型 {$config->from_type_id} 升级到 {$config->to_type_id} 所需材料";
  81. $consumeGroup->save();
  82. $this->info(" 创建消耗组: ID={$consumeGroup->id}, 名称={$consumeGroup->name}");
  83. // 创建消耗项
  84. foreach ($materials as $material) {
  85. $consumeItem = new GameConsumeItem();
  86. $consumeItem->group_id = $consumeGroup->id;
  87. $consumeItem->consume_type = CONSUME_TYPE::ITEM->value;
  88. $consumeItem->target_id = $material['item_id'];
  89. $consumeItem->quantity = $material['amount'];
  90. $consumeItem->save();
  91. $this->info(" 创建消耗项: ID={$consumeItem->id}, 物品ID={$consumeItem->target_id}, 数量={$consumeItem->quantity}");
  92. }
  93. // 更新土地升级配置
  94. $config->consume_group_id = $consumeGroup->id;
  95. $config->save();
  96. $this->info(" 更新土地升级配置: consume_group_id={$config->consume_group_id}");
  97. DB::commit();
  98. } else {
  99. $this->info(" [干运行] 将创建消耗组和消耗项,并更新土地升级配置");
  100. }
  101. $migratedCount++;
  102. } catch (\Exception $e) {
  103. if (!$dryRun) {
  104. DB::rollBack();
  105. }
  106. $this->error(" 迁移失败: " . $e->getMessage());
  107. $errorCount++;
  108. }
  109. }
  110. $this->line('');
  111. $this->info("迁移完成: 成功 {$migratedCount}, 跳过 {$skippedCount}, 失败 {$errorCount}");
  112. return 0;
  113. }
  114. }