| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120 |
- <?php
- namespace App\Module\Promotion\Commands;
- use App\Module\Promotion\Logics\TalentLogic;
- use App\Module\Promotion\Models\PromotionUserTalent;
- use Illuminate\Console\Command;
- /**
- * 更新达人等级命令
- *
- * 用于更新所有用户的达人等级,确保等级数据的准确性。
- */
- class UpdateTalentLevelsCommand extends Command
- {
- /**
- * 命令名称
- *
- * @var string
- */
- protected $signature = 'promotion:update-talent-levels {--user-id= : 指定用户ID} {--debug-info : 显示详细信息}';
- /**
- * 命令描述
- *
- * @var string
- */
- protected $description = '更新用户达人等级';
- /**
- * 达人等级逻辑类
- *
- * @var TalentLogic
- */
- protected $talentLogic;
- /**
- * 创建命令实例
- *
- * @param TalentLogic $talentLogic 达人等级逻辑类
- * @return void
- */
- public function __construct(TalentLogic $talentLogic)
- {
- parent::__construct();
- $this->talentLogic = $talentLogic;
- }
- /**
- * 执行命令
- *
- * @return int
- */
- public function handle()
- {
- $this->info('开始更新达人等级...');
- $userId = $this->option('user-id');
- $showDebugInfo = $this->option('debug-info');
- try {
- if ($userId) {
- // 更新指定用户的达人等级
- $result = $this->talentLogic->checkAndUpdateTalentLevel($userId);
- if ($showDebugInfo) {
- if ($result) {
- $talent = PromotionUserTalent::where('user_id', $userId)->first();
- $this->info("用户 {$userId} 的达人等级更新为 {$talent->talent_level}");
- } else {
- $this->info("用户 {$userId} 的达人等级未变更");
- }
- } else {
- $this->info("更新完成");
- }
- } else {
- // 更新所有用户的达人等级
- $users = PromotionUserTalent::all();
- $total = $users->count();
- $updated = 0;
- if ($showDebugInfo) {
- $this->info("共有 {$total} 个用户需要更新达人等级");
- $bar = $this->output->createProgressBar($total);
- $bar->start();
- }
- foreach ($users as $user) {
- $result = $this->talentLogic->checkAndUpdateTalentLevel($user->user_id);
- if ($result) {
- $updated++;
- }
- if ($showDebugInfo) {
- $bar->advance();
- }
- }
- if ($showDebugInfo) {
- $bar->finish();
- $this->newLine();
- $this->info("共更新了 {$updated} 个用户的达人等级");
- } else {
- $this->info("更新完成");
- }
- }
- return 0;
- } catch (\Exception $e) {
- $this->error("更新达人等级失败: " . $e->getMessage());
- if ($showDebugInfo) {
- $this->error($e->getTraceAsString());
- }
- return 1;
- }
- }
- }
|