| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122 |
- <?php
- namespace App\Module\Task\Commands;
- use App\Module\Task\Models\Task;
- use App\Module\Task\Services\TaskRewardGroupService;
- use Illuminate\Console\Command;
- /**
- * 迁移任务奖励到奖励组命令
- */
- class MigrateTaskRewardsToRewardGroups extends Command
- {
- /**
- * 命令名称
- *
- * @var string
- */
- protected $signature = 'task:migrate-rewards {task_id?} {--all : 迁移所有任务}';
- /**
- * 命令描述
- *
- * @var string
- */
- protected $description = '将任务奖励迁移到奖励组';
- /**
- * 执行命令
- *
- * @return int
- */
- public function handle()
- {
- $taskId = $this->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} 个");
- }
- }
|