| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261 |
- <?php
- namespace App\Module\UrsPromotion\Commands;
- use App\Module\UrsPromotion\Services\UrsActiveUserService;
- use App\Module\UrsPromotion\Services\UrsUserMappingService;
- use App\Module\UrsPromotion\Services\UrsTalentService;
- use App\Module\UrsPromotion\Models\UrsUserMapping;
- use App\Module\UrsPromotion\Models\UrsTalentConfig;
- use Illuminate\Console\Command;
- /**
- * URS活跃用户功能测试命令
- */
- class TestActiveUserCommand extends Command
- {
- /**
- * 命令签名
- */
- protected $signature = 'urs:test-active-user
- {--user-id= : 测试指定的URS用户ID}
- {--stats : 显示活跃用户统计}
- {--talent : 测试达人等级计算}
- {--update : 测试活跃状态更新}';
- /**
- * 命令描述
- */
- protected $description = 'Test URS active user functionality';
- /**
- * 执行命令
- */
- public function handle()
- {
- $this->info('🧪 URS活跃用户功能测试');
- $this->line('');
- // 显示活跃用户统计
- if ($this->option('stats')) {
- $this->testActiveUserStats();
- }
- // 测试活跃状态更新
- if ($this->option('update')) {
- $this->testActiveStatusUpdate();
- }
- // 测试达人等级计算
- if ($this->option('talent')) {
- $this->testTalentCalculation();
- }
- // 测试指定用户
- if ($userId = $this->option('user-id')) {
- $this->testSpecificUser((int) $userId);
- }
- // 如果没有指定选项,运行完整测试
- if (!$this->option('stats') && !$this->option('update') &&
- !$this->option('talent') && !$this->option('user-id')) {
- $this->runFullTest();
- }
- $this->info('✅ 测试完成');
- }
- /**
- * 测试活跃用户统计
- */
- protected function testActiveUserStats(): void
- {
- $this->info('📊 活跃用户统计测试');
-
- $stats = UrsActiveUserService::getActiveUserStats();
- $detailedStats = UrsActiveUserService::getDetailedActiveStats();
-
- $this->table(['指标', '数值'], [
- ['总用户数', $stats['total_users']],
- ['活跃用户数', $stats['active_users']],
- ['不活跃用户数', $stats['inactive_users']],
- ['活跃比例', $stats['active_percentage'] . '%'],
- ['最近24小时更新', $detailedStats['recent_updates']],
- ['需要检查的用户', $detailedStats['need_check_count']],
- ['最后更新时间', $detailedStats['last_update_time'] ?? '从未更新'],
- ]);
-
- $this->line('');
- }
- /**
- * 测试活跃状态更新
- */
- protected function testActiveStatusUpdate(): void
- {
- $this->info('🔄 活跃状态更新测试');
-
- // 获取需要检查的用户
- $needCheckUsers = UrsUserMapping::getUsersNeedActivityCheck(5);
-
- if ($needCheckUsers->isEmpty()) {
- $this->warn('没有需要检查的用户');
- return;
- }
-
- $this->info("找到 {$needCheckUsers->count()} 个需要检查的用户");
-
- foreach ($needCheckUsers as $mapping) {
- $this->line("测试用户 URS:{$mapping->urs_user_id} Farm:{$mapping->user_id}");
-
- $before = $mapping->is_active;
- $result = UrsActiveUserService::updateUserActiveStatus($mapping->urs_user_id);
-
- $mapping->refresh();
- $after = $mapping->is_active;
-
- $status = $result ? '✅' : '❌';
- $change = $before !== $after ? " ({$before} -> {$after})" : '';
-
- $this->line(" {$status} 更新结果: " . ($result ? '成功' : '失败') . $change);
- }
-
- $this->line('');
- }
- /**
- * 测试达人等级计算
- */
- protected function testTalentCalculation(): void
- {
- $this->info('🏆 达人等级计算测试');
-
- // 获取等级配置
- $configs = UrsTalentConfig::where('status', 1)->orderBy('level')->get();
-
- $this->info('达人等级配置:');
- $headers = ['等级', '名称', '直推要求', '团队要求', '活跃直推要求', '活跃团队要求'];
- $rows = [];
-
- foreach ($configs as $config) {
- $rows[] = [
- $config->level,
- $config->name,
- $config->direct_count_required,
- $config->promotion_count_required,
- $config->active_direct_required,
- $config->active_count_required,
- ];
- }
-
- $this->table($headers, $rows);
-
- // 测试几个用户的等级计算
- $testUsers = UrsUserMapping::where('status', UrsUserMapping::STATUS_VALID)
- ->limit(3)
- ->get();
-
- if ($testUsers->isNotEmpty()) {
- $this->info('用户等级检查示例:');
-
- foreach ($testUsers as $mapping) {
- $this->line("用户 URS:{$mapping->urs_user_id}");
-
- try {
- $eligibility = UrsTalentService::checkUpgradeEligibility($mapping->urs_user_id);
- $activeStats = UrsActiveUserService::getActiveTeamMembers($mapping->urs_user_id);
-
- $this->line(" 活跃直推: {$activeStats['active_direct_count']}");
- $this->line(" 活跃团队: {$activeStats['active_total_count']}");
- $this->line(" 升级资格: " . ($eligibility['eligible'] ? '✅ 符合' : '❌ 不符合'));
-
- if (!$eligibility['eligible'] && isset($eligibility['requirements'])) {
- foreach ($eligibility['requirements'] as $type => $req) {
- if (!$req['met']) {
- $this->line(" 缺少 {$type}: {$req['gap']}");
- }
- }
- }
-
- } catch (\Exception $e) {
- $this->error(" 检查失败: " . $e->getMessage());
- }
-
- $this->line('');
- }
- }
- }
- /**
- * 测试指定用户
- */
- protected function testSpecificUser(int $ursUserId): void
- {
- $this->info("👤 测试用户 URS:{$ursUserId}");
-
- // 检查映射关系
- $mapping = UrsUserMapping::where('urs_user_id', $ursUserId)
- ->where('status', UrsUserMapping::STATUS_VALID)
- ->first();
-
- if (!$mapping) {
- $this->error('用户映射关系不存在');
- return;
- }
-
- $this->line("农场用户ID: {$mapping->user_id}");
- $this->line("当前活跃状态: " . ($mapping->isActive() ? '活跃' : '不活跃'));
- $this->line("活跃天数: {$mapping->active_days_count}");
- $this->line("最后检查时间: " . ($mapping->last_activity_check ? $mapping->last_activity_check->format('Y-m-d H:i:s') : '从未检查'));
-
- // 更新活跃状态
- $this->line('');
- $this->info('更新活跃状态...');
- $result = UrsActiveUserService::updateUserActiveStatus($ursUserId);
- $this->line('更新结果: ' . ($result ? '成功' : '失败'));
-
- // 获取活跃团队统计
- $this->line('');
- $this->info('活跃团队统计:');
- $activeStats = UrsActiveUserService::getActiveTeamMembers($ursUserId);
- $this->line("活跃直推: {$activeStats['active_direct_count']}");
- $this->line("活跃团队: {$activeStats['active_total_count']}");
-
- // 检查升级条件
- $this->line('');
- $this->info('升级条件检查:');
- try {
- $eligibility = UrsTalentService::checkUpgradeEligibility($ursUserId);
- $this->line("升级资格: " . ($eligibility['eligible'] ? '✅ 符合' : '❌ 不符合'));
-
- if (isset($eligibility['next_level_name'])) {
- $this->line("下一等级: {$eligibility['next_level_name']}");
- }
-
- if (isset($eligibility['requirements'])) {
- foreach ($eligibility['requirements'] as $type => $req) {
- $status = $req['met'] ? '✅' : '❌';
- $this->line(" {$status} {$type}: {$req['current']}/{$req['required']}");
- }
- }
-
- } catch (\Exception $e) {
- $this->error('检查失败: ' . $e->getMessage());
- }
- }
- /**
- * 运行完整测试
- */
- protected function runFullTest(): void
- {
- $this->info('🚀 运行完整测试');
- $this->line('');
-
- $this->testActiveUserStats();
- $this->testActiveStatusUpdate();
- $this->testTalentCalculation();
-
- $this->info('完整测试完成');
- }
- }
|