argument('task_id'); $migrateAll = $this->option('all'); if (!$taskId && !$migrateAll) { $this->error('请指定任务ID或使用 --all 选项迁移所有任务'); return 1; } if ($taskId) { // 迁移单个任务 $this->migrateTask($taskId); } else { // 迁移所有任务 $this->migrateAllTasks(); } return 0; } /** * 迁移单个任务 * * @param int $taskId 任务ID * @return void */ private function migrateTask(int $taskId) { $task = Task::find($taskId); if (!$task) { $this->error("任务 {$taskId} 不存在"); return; } $this->info("开始迁移任务 {$taskId}: {$task->name}"); $result = TaskRewardGroupService::migrateTaskRewardsToRewardGroup($taskId); if ($result['success']) { $this->info("任务 {$taskId} 迁移成功: {$result['message']}"); if ($result['reward_group_id']) { $this->info("关联的奖励组ID: {$result['reward_group_id']}"); } } else { $this->error("任务 {$taskId} 迁移失败: {$result['message']}"); } } /** * 迁移所有任务 * * @return void */ private function migrateAllTasks() { $this->info('开始迁移所有任务奖励到奖励组'); // 获取所有有奖励但没有关联奖励组的任务 $tasks = Task::whereHas('rewards') ->whereNull('reward_group_id') ->get(); $total = $tasks->count(); $this->info("找到 {$total} 个需要迁移的任务"); $bar = $this->output->createProgressBar($total); $bar->start(); $success = 0; $failed = 0; foreach ($tasks as $task) { $result = TaskRewardGroupService::migrateTaskRewardsToRewardGroup($task->id); if ($result['success']) { $success++; } else { $failed++; $this->newLine(); $this->error("任务 {$task->id} 迁移失败: {$result['message']}"); } $bar->advance(); } $bar->finish(); $this->newLine(2); $this->info("迁移完成: 成功 {$success} 个, 失败 {$failed} 个"); } }