| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138 |
- <?php
- namespace App\Module\Farm\Commands;
- use App\Module\Farm\Models\FarmLandUpgradeConfig;
- use App\Module\Game\Enums\CONSUME_TYPE;
- use App\Module\Game\Models\GameConsumeGroup;
- use App\Module\Game\Models\GameConsumeItem;
- use Illuminate\Console\Command;
- use Illuminate\Support\Facades\DB;
- use Illuminate\Support\Str;
- /**
- * 将土地升级材料迁移到消耗组的命令
- */
- class MigrateLandUpgradeMaterialsToConsumeGroupsCommand extends Command
- {
- /**
- * 命令名称
- *
- * @var string
- */
- protected $signature = 'farm:migrate-land-upgrade-materials {--dry-run : 只显示将要执行的操作,不实际执行}';
- /**
- * 命令描述
- *
- * @var string
- */
- protected $description = '将土地升级材料迁移到消耗组';
- /**
- * 执行命令
- *
- * @return int
- */
- public function handle()
- {
- $this->info('开始迁移土地升级材料到消耗组...');
- // 获取所有土地升级配置
- $configs = FarmLandUpgradeConfig::all();
- $this->info("找到 {$configs->count()} 个土地升级配置");
- // 是否为干运行模式
- $dryRun = $this->option('dry-run');
- if ($dryRun) {
- $this->warn('干运行模式:不会实际执行数据库操作');
- }
- // 开始迁移
- $migratedCount = 0;
- $skippedCount = 0;
- $errorCount = 0;
- foreach ($configs as $config) {
- $this->line('');
- $this->line("处理配置 ID: {$config->id}, 从 {$config->from_type_id} 到 {$config->to_type_id}");
- // 如果已经有消耗组ID,则跳过
- if ($config->consume_group_id) {
- $this->warn(" 已有消耗组ID ({$config->consume_group_id}),跳过");
- $skippedCount++;
- continue;
- }
- // 获取材料
- $materials = [];
- if (is_string($config->materials)) {
- $materials = json_decode($config->materials, true)['materials'] ?? [];
- } else {
- $materials = $config->materials['materials'] ?? [];
- }
- if (empty($materials)) {
- $this->warn(" 没有材料,跳过");
- $skippedCount++;
- continue;
- }
- $this->info(" 找到 " . count($materials) . " 个材料项");
- foreach ($materials as $material) {
- $this->line(" 物品ID: {$material['item_id']}, 数量: {$material['amount']}");
- }
- try {
- // 如果不是干运行模式,则创建消耗组
- if (!$dryRun) {
- DB::beginTransaction();
- // 创建消耗组
- $consumeGroup = new GameConsumeGroup();
- $consumeGroup->name = "土地升级: {$config->from_type_id} -> {$config->to_type_id}";
- $consumeGroup->code = "land_upgrade_{$config->from_type_id}_{$config->to_type_id}_" . Str::random(6);
- $consumeGroup->description = "从土地类型 {$config->from_type_id} 升级到 {$config->to_type_id} 所需材料";
- $consumeGroup->save();
- $this->info(" 创建消耗组: ID={$consumeGroup->id}, 名称={$consumeGroup->name}");
- // 创建消耗项
- foreach ($materials as $material) {
- $consumeItem = new GameConsumeItem();
- $consumeItem->group_id = $consumeGroup->id;
- $consumeItem->consume_type = CONSUME_TYPE::ITEM->value;
- $consumeItem->target_id = $material['item_id'];
- $consumeItem->quantity = $material['amount'];
- $consumeItem->save();
- $this->info(" 创建消耗项: ID={$consumeItem->id}, 物品ID={$consumeItem->target_id}, 数量={$consumeItem->quantity}");
- }
- // 更新土地升级配置
- $config->consume_group_id = $consumeGroup->id;
- $config->save();
- $this->info(" 更新土地升级配置: consume_group_id={$config->consume_group_id}");
- DB::commit();
- } else {
- $this->info(" [干运行] 将创建消耗组和消耗项,并更新土地升级配置");
- }
- $migratedCount++;
- } catch (\Exception $e) {
- if (!$dryRun) {
- DB::rollBack();
- }
- $this->error(" 迁移失败: " . $e->getMessage());
- $errorCount++;
- }
- }
- $this->line('');
- $this->info("迁移完成: 成功 {$migratedCount}, 跳过 {$skippedCount}, 失败 {$errorCount}");
- return 0;
- }
- }
|