| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295 |
- <?php
- namespace App\Module\UrsPromotion\Commands;
- use App\Module\UrsPromotion\Services\UrsTalentService;
- use App\Module\UrsPromotion\Services\UrsUserMappingService;
- use App\Module\UrsPromotion\Models\UrsUserMapping;
- use Illuminate\Console\Command;
- use Illuminate\Support\Facades\Log;
- /**
- * URS用户达人等级更新命令
- *
- * 用于更新指定用户或批量更新用户的达人等级
- * 支持单个用户更新和批量更新功能
- * php artisan urs:update-talent-level 39296
- *
- */
- class UrsUpdateTalentLevelCommand extends Command
- {
- /**
- * 命令签名
- */
- protected $signature = 'urs:update-talent-level
- {user_id? : 农场用户ID,不指定则更新所有用户}
- {--batch-size=100 : 批处理大小}
- {--force : 强制重新计算等级}
- {--dry-run : 仅模拟运行,不实际执行更新}';
- /**
- * 命令描述
- */
- protected $description = 'URS用户达人等级更新命令 - 计算并更新用户的达人等级';
- /**
- * 执行命令
- */
- public function handle()
- {
- $userId = $this->argument('user_id');
- $batchSize = (int) $this->option('batch-size');
- $force = $this->option('force');
- $dryRun = $this->option('dry-run');
- $this->info('=== URS用户达人等级更新命令 ===');
-
- if ($dryRun) {
- $this->warn('模拟运行模式 - 不会实际执行更新操作');
- }
- try {
- if ($userId) {
- // 更新指定用户
- return $this->updateSingleUser((int) $userId, $force, $dryRun);
- } else {
- // 批量更新所有用户
- return $this->updateAllUsers($batchSize, $force, $dryRun);
- }
- } catch (\Exception $e) {
- $this->error('命令执行失败: ' . $e->getMessage());
- Log::error('URS达人等级更新命令执行失败', [
- 'user_id' => $userId,
- 'batch_size' => $batchSize,
- 'force' => $force,
- 'dry_run' => $dryRun,
- 'error' => $e->getMessage(),
- 'trace' => $e->getTraceAsString()
- ]);
- return 1;
- }
- }
- /**
- * 更新指定用户的达人等级
- *
- * @param int $userId 农场用户ID
- * @param bool $force 是否强制重新计算
- * @param bool $dryRun 是否仅模拟运行
- * @return int 命令退出码
- */
- private function updateSingleUser(int $userId, bool $force, bool $dryRun): int
- {
- $this->info("开始更新用户 {$userId} 的达人等级...");
- // 检查用户是否有URS映射关系
- $ursUserId = UrsUserMappingService::getMappingUrsUserId($userId);
- if ($ursUserId === 0) {
- $this->error("用户 {$userId} 没有URS映射关系,无法更新达人等级");
- return 1;
- }
- $this->info("找到URS用户ID: {$ursUserId}");
- if ($dryRun) {
- $this->info("模拟运行:将调用 UrsTalentService::updateTalentLevel({$userId})");
- return 0;
- }
- try {
- // 调用达人等级更新服务
- $talentDto = UrsTalentService::updateTalentLevel($userId);
-
- if ($talentDto) {
- $this->info("✓ 成功更新用户 {$userId} 的达人等级");
- // 基础信息表格
- $this->table(['属性', '值'], [
- ['URS用户ID', $talentDto->ursUserId],
- ['达人等级', $talentDto->talentLevel],
- ['等级名称', $talentDto->talentName],
- ['最后更新时间', $talentDto->lastLevelUpdateTime ?? '未更新']
- ]);
- // 团队统计表格
- $this->line('');
- $this->info('📊 团队统计数据:');
- $this->table(['统计项', '总数', '活跃数', '活跃率'], [
- [
- '直推人数',
- $talentDto->directCount,
- $talentDto->activeDirectCount,
- $talentDto->directCount > 0 ? round($talentDto->activeDirectCount * 100 / $talentDto->directCount, 1) . '%' : '0%'
- ],
- ['间推人数', $talentDto->indirectCount, '-', '-'],
- ['三推人数', $talentDto->thirdCount, '-', '-'],
- [
- '团队总人数',
- $talentDto->promotionCount,
- $talentDto->activeTotalCount,
- $talentDto->promotionCount > 0 ? round($talentDto->activeTotalCount * 100 / $talentDto->promotionCount, 1) . '%' : '0%'
- ]
- ]);
- // 升级条件检查
- if ($talentDto->nextConfig) {
- $this->line('');
- $this->info('🎯 下一等级升级条件:');
- $nextLevel = $talentDto->nextConfig;
- $directMet = $talentDto->directCount >= $nextLevel['direct_count_required'];
- $teamMet = $talentDto->promotionCount >= $nextLevel['promotion_count_required'];
- $activeDirectMet = $talentDto->activeDirectCount >= ($nextLevel['active_direct_required'] ?? 0);
- $this->table(['条件', '要求', '当前', '状态'], [
- [
- '直推人数',
- $nextLevel['direct_count_required'],
- $talentDto->directCount,
- $directMet ? '✅ 已满足' : '❌ 未满足'
- ],
- [
- '团队总人数',
- $nextLevel['promotion_count_required'],
- $talentDto->promotionCount,
- $teamMet ? '✅ 已满足' : '❌ 未满足'
- ],
- [
- '活跃直推',
- $nextLevel['active_direct_required'] ?? 0,
- $talentDto->activeDirectCount,
- $activeDirectMet ? '✅ 已满足' : '❌ 未满足'
- ]
- ]);
- $allMet = $directMet && $teamMet && $activeDirectMet;
- if ($allMet) {
- $this->info("🎉 恭喜!所有升级条件已满足,可升级到 {$nextLevel['name']}");
- } else {
- $this->warn("⚠️ 还有条件未满足,无法升级到 {$nextLevel['name']}");
- }
- }
- } else {
- $this->error("✗ 用户 {$userId} 达人等级更新失败");
- return 1;
- }
- Log::info('URS达人等级更新成功', [
- 'user_id' => $userId,
- 'urs_user_id' => $ursUserId,
- 'talent_level' => $talentDto->talentLevel,
- 'talent_name' => $talentDto->talentName,
- 'force' => $force
- ]);
- return 0;
- } catch (\Exception $e) {
- $this->error("✗ 用户 {$userId} 达人等级更新失败: " . $e->getMessage());
-
- Log::error('URS达人等级更新失败', [
- 'user_id' => $userId,
- 'urs_user_id' => $ursUserId,
- 'error' => $e->getMessage(),
- 'trace' => $e->getTraceAsString()
- ]);
- return 1;
- }
- }
- /**
- * 批量更新所有用户的达人等级
- *
- * @param int $batchSize 批处理大小
- * @param bool $force 是否强制重新计算
- * @param bool $dryRun 是否仅模拟运行
- * @return int 命令退出码
- */
- private function updateAllUsers(int $batchSize, bool $force, bool $dryRun): int
- {
- $this->info("开始批量更新所有用户的达人等级...");
- $this->info("批处理大小: {$batchSize}");
- // 获取所有有效的用户映射
- $totalUsers = UrsUserMapping::where('status', UrsUserMapping::STATUS_VALID)->count();
-
- if ($totalUsers === 0) {
- $this->warn('没有找到需要更新的用户');
- return 0;
- }
- $this->info("找到 {$totalUsers} 个用户需要更新");
- if ($dryRun) {
- $this->info("模拟运行:将分批处理 {$totalUsers} 个用户");
- return 0;
- }
- $progressBar = $this->output->createProgressBar($totalUsers);
- $progressBar->start();
- $successCount = 0;
- $errorCount = 0;
- $processedCount = 0;
- // 分批处理用户
- UrsUserMapping::where('status', UrsUserMapping::STATUS_VALID)
- ->chunk($batchSize, function ($mappings) use (
- &$successCount, &$errorCount, &$processedCount,
- $progressBar, $force
- ) {
- foreach ($mappings as $mapping) {
- try {
- $talentDto = UrsTalentService::updateTalentLevel($mapping->user_id);
-
- if ($talentDto) {
- $successCount++;
- } else {
- $errorCount++;
- }
- } catch (\Exception $e) {
- $errorCount++;
-
- Log::error('批量更新用户达人等级失败', [
- 'user_id' => $mapping->user_id,
- 'urs_user_id' => $mapping->urs_user_id,
- 'error' => $e->getMessage()
- ]);
- }
- $processedCount++;
- $progressBar->advance();
- }
- });
- $progressBar->finish();
- $this->newLine();
- // 显示更新结果
- $this->info('=== 更新结果统计 ===');
- $this->table(['项目', '数量'], [
- ['总用户数', $totalUsers],
- ['处理用户数', $processedCount],
- ['成功更新数', $successCount],
- ['失败数量', $errorCount]
- ]);
- Log::info('URS达人等级批量更新完成', [
- 'total_users' => $totalUsers,
- 'processed_count' => $processedCount,
- 'success_count' => $successCount,
- 'error_count' => $errorCount,
- 'batch_size' => $batchSize,
- 'force' => $force
- ]);
- if ($errorCount > 0) {
- $this->warn("有 {$errorCount} 个用户更新失败,请检查日志");
- return 1;
- }
- $this->info('所有用户达人等级更新完成');
- return 0;
- }
- }
|