| 12345678910111213141516171819202122232425262728293031 |
- <?php
- namespace App\Console\Commands;
- use App\Services\PromotionService;
- use Illuminate\Console\Command;
- use Illuminate\Support\Facades\DB;
- use Illuminate\Support\Facades\Schema;
- use Illuminate\Support\Facades\Log;
- class SyncPromotions extends Command
- {
- protected $signature = 'promotions:sync';
- protected $description = '补记转盘任务次数并解冻满足流水的新活动奖金';
- public function handle(PromotionService $service): int
- {
- if (!Schema::hasTable('promotion_accounts')) return self::SUCCESS;
- $errors = 0;
- DB::table('promotion_accounts as p')->join('promotion_activities as a', 'a.id', '=', 'p.activity_id')->where('a.type', 9)->where('a.status', 1)
- ->where('a.start_time', '<=', now()->timestamp)->where('a.end_time', '>=', now()->timestamp)->select('p.*')->orderBy('p.id')
- ->chunkById(100, function ($rows) use ($service, &$errors) {
- foreach ($rows as $row) try { $service->progress($row->activity_id, $row->user_id); }
- catch (\Throwable $e) { ++$errors; Log::error('promotion_sync_failed', ['account_id' => $row->id, 'exception' => $e]); }
- }, 'p.id', 'id');
- foreach (DB::table('promotion_rewards')->where('status', 'locked')->distinct()->pluck('user_id') as $userId) {
- try { $service->unlock((int)$userId); } catch (\Throwable $e) { ++$errors; Log::error('promotion_unlock_failed', ['user_id' => $userId, 'exception' => $e]); }
- }
- $this->info('活动任务和奖金同步完成,失败数:' . $errors);
- return $errors ? self::FAILURE : self::SUCCESS;
- }
- }
|