TestActiveUserCommand.php 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. <?php
  2. namespace App\Module\UrsPromotion\Commands;
  3. use App\Module\UrsPromotion\Services\UrsActiveUserService;
  4. use App\Module\UrsPromotion\Services\UrsUserMappingService;
  5. use App\Module\UrsPromotion\Services\UrsTalentService;
  6. use App\Module\UrsPromotion\Models\UrsUserMapping;
  7. use App\Module\UrsPromotion\Models\UrsTalentConfig;
  8. use Illuminate\Console\Command;
  9. /**
  10. * URS活跃用户功能测试命令
  11. */
  12. class TestActiveUserCommand extends Command
  13. {
  14. /**
  15. * 命令签名
  16. */
  17. protected $signature = 'urs:test-active-user
  18. {--user-id= : 测试指定的URS用户ID}
  19. {--stats : 显示活跃用户统计}
  20. {--talent : 测试达人等级计算}
  21. {--update : 测试活跃状态更新}';
  22. /**
  23. * 命令描述
  24. */
  25. protected $description = 'Test URS active user functionality';
  26. /**
  27. * 执行命令
  28. */
  29. public function handle()
  30. {
  31. $this->info('🧪 URS活跃用户功能测试');
  32. $this->line('');
  33. // 显示活跃用户统计
  34. if ($this->option('stats')) {
  35. $this->testActiveUserStats();
  36. }
  37. // 测试活跃状态更新
  38. if ($this->option('update')) {
  39. $this->testActiveStatusUpdate();
  40. }
  41. // 测试达人等级计算
  42. if ($this->option('talent')) {
  43. $this->testTalentCalculation();
  44. }
  45. // 测试指定用户
  46. if ($userId = $this->option('user-id')) {
  47. $this->testSpecificUser((int) $userId);
  48. }
  49. // 如果没有指定选项,运行完整测试
  50. if (!$this->option('stats') && !$this->option('update') &&
  51. !$this->option('talent') && !$this->option('user-id')) {
  52. $this->runFullTest();
  53. }
  54. $this->info('✅ 测试完成');
  55. }
  56. /**
  57. * 测试活跃用户统计
  58. */
  59. protected function testActiveUserStats(): void
  60. {
  61. $this->info('📊 活跃用户统计测试');
  62. $stats = UrsActiveUserService::getActiveUserStats();
  63. $detailedStats = UrsActiveUserService::getDetailedActiveStats();
  64. $this->table(['指标', '数值'], [
  65. ['总用户数', $stats['total_users']],
  66. ['活跃用户数', $stats['active_users']],
  67. ['不活跃用户数', $stats['inactive_users']],
  68. ['活跃比例', $stats['active_percentage'] . '%'],
  69. ['最近24小时更新', $detailedStats['recent_updates']],
  70. ['需要检查的用户', $detailedStats['need_check_count']],
  71. ['最后更新时间', $detailedStats['last_update_time'] ?? '从未更新'],
  72. ]);
  73. $this->line('');
  74. }
  75. /**
  76. * 测试活跃状态更新
  77. */
  78. protected function testActiveStatusUpdate(): void
  79. {
  80. $this->info('🔄 活跃状态更新测试');
  81. // 获取需要检查的用户
  82. $needCheckUsers = UrsUserMapping::getUsersNeedActivityCheck(5);
  83. if ($needCheckUsers->isEmpty()) {
  84. $this->warn('没有需要检查的用户');
  85. return;
  86. }
  87. $this->info("找到 {$needCheckUsers->count()} 个需要检查的用户");
  88. foreach ($needCheckUsers as $mapping) {
  89. $this->line("测试用户 URS:{$mapping->urs_user_id} Farm:{$mapping->user_id}");
  90. $before = $mapping->is_active;
  91. $result = UrsActiveUserService::updateUserActiveStatus($mapping->urs_user_id);
  92. $mapping->refresh();
  93. $after = $mapping->is_active;
  94. $status = $result ? '✅' : '❌';
  95. $change = $before !== $after ? " ({$before} -> {$after})" : '';
  96. $this->line(" {$status} 更新结果: " . ($result ? '成功' : '失败') . $change);
  97. }
  98. $this->line('');
  99. }
  100. /**
  101. * 测试达人等级计算
  102. */
  103. protected function testTalentCalculation(): void
  104. {
  105. $this->info('🏆 达人等级计算测试');
  106. // 获取等级配置
  107. $configs = UrsTalentConfig::where('status', 1)->orderBy('level')->get();
  108. $this->info('达人等级配置:');
  109. $headers = ['等级', '名称', '直推要求', '团队要求', '活跃直推要求', '活跃团队要求'];
  110. $rows = [];
  111. foreach ($configs as $config) {
  112. $rows[] = [
  113. $config->level,
  114. $config->name,
  115. $config->direct_count_required,
  116. $config->promotion_count_required,
  117. $config->active_direct_required,
  118. $config->active_count_required,
  119. ];
  120. }
  121. $this->table($headers, $rows);
  122. // 测试几个用户的等级计算
  123. $testUsers = UrsUserMapping::where('status', UrsUserMapping::STATUS_VALID)
  124. ->limit(3)
  125. ->get();
  126. if ($testUsers->isNotEmpty()) {
  127. $this->info('用户等级检查示例:');
  128. foreach ($testUsers as $mapping) {
  129. $this->line("用户 URS:{$mapping->urs_user_id}");
  130. try {
  131. $eligibility = UrsTalentService::checkUpgradeEligibility($mapping->urs_user_id);
  132. $activeStats = UrsActiveUserService::getActiveTeamMembers($mapping->urs_user_id);
  133. $this->line(" 活跃直推: {$activeStats['active_direct_count']}");
  134. $this->line(" 活跃团队: {$activeStats['active_total_count']}");
  135. $this->line(" 升级资格: " . ($eligibility['eligible'] ? '✅ 符合' : '❌ 不符合'));
  136. if (!$eligibility['eligible'] && isset($eligibility['requirements'])) {
  137. foreach ($eligibility['requirements'] as $type => $req) {
  138. if (!$req['met']) {
  139. $this->line(" 缺少 {$type}: {$req['gap']}");
  140. }
  141. }
  142. }
  143. } catch (\Exception $e) {
  144. $this->error(" 检查失败: " . $e->getMessage());
  145. }
  146. $this->line('');
  147. }
  148. }
  149. }
  150. /**
  151. * 测试指定用户
  152. */
  153. protected function testSpecificUser(int $ursUserId): void
  154. {
  155. $this->info("👤 测试用户 URS:{$ursUserId}");
  156. // 检查映射关系
  157. $mapping = UrsUserMapping::where('urs_user_id', $ursUserId)
  158. ->where('status', UrsUserMapping::STATUS_VALID)
  159. ->first();
  160. if (!$mapping) {
  161. $this->error('用户映射关系不存在');
  162. return;
  163. }
  164. $this->line("农场用户ID: {$mapping->user_id}");
  165. $this->line("当前活跃状态: " . ($mapping->isActive() ? '活跃' : '不活跃'));
  166. $this->line("活跃天数: {$mapping->active_days_count}");
  167. $this->line("最后检查时间: " . ($mapping->last_activity_check ? $mapping->last_activity_check->format('Y-m-d H:i:s') : '从未检查'));
  168. // 更新活跃状态
  169. $this->line('');
  170. $this->info('更新活跃状态...');
  171. $result = UrsActiveUserService::updateUserActiveStatus($ursUserId);
  172. $this->line('更新结果: ' . ($result ? '成功' : '失败'));
  173. // 获取活跃团队统计
  174. $this->line('');
  175. $this->info('活跃团队统计:');
  176. $activeStats = UrsActiveUserService::getActiveTeamMembers($ursUserId);
  177. $this->line("活跃直推: {$activeStats['active_direct_count']}");
  178. $this->line("活跃团队: {$activeStats['active_total_count']}");
  179. // 检查升级条件
  180. $this->line('');
  181. $this->info('升级条件检查:');
  182. try {
  183. $eligibility = UrsTalentService::checkUpgradeEligibility($ursUserId);
  184. $this->line("升级资格: " . ($eligibility['eligible'] ? '✅ 符合' : '❌ 不符合'));
  185. if (isset($eligibility['next_level_name'])) {
  186. $this->line("下一等级: {$eligibility['next_level_name']}");
  187. }
  188. if (isset($eligibility['requirements'])) {
  189. foreach ($eligibility['requirements'] as $type => $req) {
  190. $status = $req['met'] ? '✅' : '❌';
  191. $this->line(" {$status} {$type}: {$req['current']}/{$req['required']}");
  192. }
  193. }
  194. } catch (\Exception $e) {
  195. $this->error('检查失败: ' . $e->getMessage());
  196. }
  197. }
  198. /**
  199. * 运行完整测试
  200. */
  201. protected function runFullTest(): void
  202. {
  203. $this->info('🚀 运行完整测试');
  204. $this->line('');
  205. $this->testActiveUserStats();
  206. $this->testActiveStatusUpdate();
  207. $this->testTalentCalculation();
  208. $this->info('完整测试完成');
  209. }
  210. }