TestRelationCacheFixCommand.php 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. <?php
  2. namespace App\Module\UrsPromotion\Commands;
  3. use App\Module\UrsPromotion\Logics\UrsRelationCacheLogic;
  4. use App\Module\UrsPromotion\Models\UrsUserMapping;
  5. use App\Module\UrsPromotion\Models\UrsUserReferral;
  6. use App\Module\UrsPromotion\Models\UrsUserRelationCache;
  7. use App\Module\UrsPromotion\Services\UrsUserMappingService;
  8. use Illuminate\Console\Command;
  9. use Illuminate\Support\Facades\DB;
  10. /**
  11. * 测试关系缓存断代修复命令
  12. *
  13. * 用于测试和验证关系缓存断代问题的修复效果
  14. */
  15. class TestRelationCacheFixCommand extends Command
  16. {
  17. /**
  18. * 命令签名
  19. */
  20. protected $signature = 'urs:test-relation-cache-fix
  21. {--user-id= : 指定测试的URS用户ID}
  22. {--check : 检查断代问题}
  23. {--fix : 修复断代问题}
  24. {--stats : 显示统计信息}';
  25. /**
  26. * 命令描述
  27. */
  28. protected $description = '测试URS关系缓存断代修复功能';
  29. /**
  30. * 执行命令
  31. */
  32. public function handle()
  33. {
  34. $this->info('=== URS关系缓存断代修复测试 ===');
  35. if ($this->option('check')) {
  36. $this->checkBreakageIssues();
  37. } elseif ($this->option('fix')) {
  38. $this->fixBreakageIssues();
  39. } elseif ($this->option('stats')) {
  40. $this->showStatistics();
  41. } elseif ($this->option('user-id')) {
  42. $this->testSpecificUser((int)$this->option('user-id'));
  43. } else {
  44. $this->showUsage();
  45. }
  46. }
  47. /**
  48. * 检查断代问题
  49. */
  50. private function checkBreakageIssues()
  51. {
  52. $this->info('检查关系缓存断代问题...');
  53. // 1. 查找有推荐关系但没有缓存的用户
  54. $usersWithReferralButNoCache = DB::select("
  55. SELECT r.urs_user_id, r.urs_referrer_id, m.user_id as farm_user_id
  56. FROM kku_urs_promotion_user_referrals r
  57. LEFT JOIN kku_urs_promotion_user_relation_cache c ON r.urs_user_id = c.urs_user_id
  58. LEFT JOIN kku_urs_promotion_user_mappings m ON r.urs_user_id = m.urs_user_id AND m.status = 1
  59. WHERE r.status = 1
  60. AND c.urs_user_id IS NULL
  61. AND m.user_id > 0
  62. LIMIT 10
  63. ");
  64. $this->table(
  65. ['URS用户ID', '推荐人ID', '农场用户ID', '问题'],
  66. array_map(function ($user) {
  67. return [
  68. $user->urs_user_id,
  69. $user->urs_referrer_id,
  70. $user->farm_user_id,
  71. '有推荐关系但无缓存'
  72. ];
  73. }, $usersWithReferralButNoCache)
  74. );
  75. // 2. 查找占位缓存记录
  76. $placeholderCaches = UrsUserRelationCache::where('related_user_id', 0)->limit(10)->get();
  77. if ($placeholderCaches->count() > 0) {
  78. $this->info("\n占位缓存记录:");
  79. $this->table(
  80. ['用户ID', 'URS用户ID', 'URS推荐人ID', '深度'],
  81. $placeholderCaches->map(function ($cache) {
  82. return [
  83. $cache->user_id,
  84. $cache->urs_user_id,
  85. $cache->urs_related_user_id,
  86. $cache->depth
  87. ];
  88. })->toArray()
  89. );
  90. }
  91. $this->info("\n断代问题检查完成");
  92. }
  93. /**
  94. * 修复断代问题
  95. */
  96. private function fixBreakageIssues()
  97. {
  98. $this->info('开始修复关系缓存断代问题...');
  99. $logic = new UrsRelationCacheLogic();
  100. // 获取所有有推荐关系但没有缓存的已进入农场用户
  101. $problematicUsers = DB::select("
  102. SELECT r.urs_user_id, m.user_id as farm_user_id
  103. FROM kku_urs_promotion_user_referrals r
  104. LEFT JOIN kku_urs_promotion_user_relation_cache c ON r.urs_user_id = c.urs_user_id
  105. INNER JOIN kku_urs_promotion_user_mappings m ON r.urs_user_id = m.urs_user_id AND m.status = 1
  106. WHERE r.status = 1
  107. AND c.urs_user_id IS NULL
  108. AND m.user_id > 0
  109. ");
  110. $successCount = 0;
  111. $failCount = 0;
  112. $progressBar = $this->output->createProgressBar(count($problematicUsers));
  113. $progressBar->start();
  114. foreach ($problematicUsers as $user) {
  115. if ($logic->generateUserRelationCache($user->urs_user_id)) {
  116. $successCount++;
  117. } else {
  118. $failCount++;
  119. }
  120. $progressBar->advance();
  121. }
  122. $progressBar->finish();
  123. $this->info("\n修复完成:");
  124. $this->info("成功: {$successCount}");
  125. $this->info("失败: {$failCount}");
  126. $this->info("总计: " . count($problematicUsers));
  127. }
  128. /**
  129. * 显示统计信息
  130. */
  131. private function showStatistics()
  132. {
  133. $this->info('=== 关系缓存统计信息 ===');
  134. // 基础统计
  135. $totalReferrals = UrsUserReferral::where('status', UrsUserReferral::STATUS_VALID)->count();
  136. $totalMappings = UrsUserMapping::where('status', UrsUserMapping::STATUS_VALID)->count();
  137. $totalCaches = UrsUserRelationCache::count();
  138. $placeholderCaches = UrsUserRelationCache::where('related_user_id', 0)->count();
  139. $this->table(
  140. ['指标', '数量'],
  141. [
  142. ['有效推荐关系', $totalReferrals],
  143. ['用户映射关系', $totalMappings],
  144. ['关系缓存记录', $totalCaches],
  145. ['占位缓存记录', $placeholderCaches],
  146. ]
  147. );
  148. // 断代问题统计
  149. $usersWithReferralButNoCache = DB::selectOne("
  150. SELECT COUNT(*) as count
  151. FROM kku_urs_promotion_user_referrals r
  152. LEFT JOIN kku_urs_promotion_user_relation_cache c ON r.urs_user_id = c.urs_user_id
  153. INNER JOIN kku_urs_promotion_user_mappings m ON r.urs_user_id = m.urs_user_id AND m.status = 1
  154. WHERE r.status = 1
  155. AND c.urs_user_id IS NULL
  156. AND m.user_id > 0
  157. ");
  158. $this->info("\n断代问题统计:");
  159. $this->info("有推荐关系但无缓存的已进入农场用户: " . $usersWithReferralButNoCache->count);
  160. if ($placeholderCaches > 0) {
  161. $this->warn("发现 {$placeholderCaches} 条占位缓存记录,表示存在推荐人未进入农场的情况");
  162. }
  163. }
  164. /**
  165. * 测试特定用户
  166. */
  167. private function testSpecificUser(int $ursUserId)
  168. {
  169. $this->info("测试URS用户 {$ursUserId} 的关系缓存...");
  170. // 显示用户基本信息
  171. $referral = UrsUserReferral::where('urs_user_id', $ursUserId)->first();
  172. $mapping = UrsUserMapping::where('urs_user_id', $ursUserId)->first();
  173. $this->table(
  174. ['属性', '值'],
  175. [
  176. ['URS用户ID', $ursUserId],
  177. ['推荐人ID', $referral ? $referral->urs_referrer_id : '无'],
  178. ['农场用户ID', $mapping ? $mapping->user_id : '未进入农场'],
  179. ['映射状态', $mapping ? ($mapping->status == 1 ? '有效' : '无效') : '不存在'],
  180. ]
  181. );
  182. // 显示现有缓存
  183. $caches = UrsUserRelationCache::where('urs_user_id', $ursUserId)->get();
  184. if ($caches->count() > 0) {
  185. $this->info("\n现有关系缓存:");
  186. $this->table(
  187. ['深度', 'URS推荐人ID', '农场推荐人ID', 'URS路径'],
  188. $caches->map(function ($cache) {
  189. return [
  190. $cache->depth,
  191. $cache->urs_related_user_id,
  192. $cache->related_user_id ?: '占位',
  193. $cache->urs_path
  194. ];
  195. })->toArray()
  196. );
  197. } else {
  198. $this->warn("该用户没有关系缓存记录");
  199. }
  200. // 重新生成缓存
  201. if ($this->confirm('是否重新生成该用户的关系缓存?')) {
  202. $logic = new UrsRelationCacheLogic();
  203. if ($logic->generateUserRelationCache($ursUserId)) {
  204. $this->info("关系缓存重新生成成功");
  205. // 显示新的缓存
  206. $newCaches = UrsUserRelationCache::where('urs_user_id', $ursUserId)->get();
  207. $this->info("\n新的关系缓存:");
  208. $this->table(
  209. ['深度', 'URS推荐人ID', '农场推荐人ID', 'URS路径'],
  210. $newCaches->map(function ($cache) {
  211. return [
  212. $cache->depth,
  213. $cache->urs_related_user_id,
  214. $cache->related_user_id ?: '占位',
  215. $cache->urs_path
  216. ];
  217. })->toArray()
  218. );
  219. } else {
  220. $this->error("关系缓存重新生成失败");
  221. }
  222. }
  223. }
  224. /**
  225. * 显示使用说明
  226. */
  227. private function showUsage()
  228. {
  229. $this->info('使用说明:');
  230. $this->info(' --check 检查断代问题');
  231. $this->info(' --fix 修复断代问题');
  232. $this->info(' --stats 显示统计信息');
  233. $this->info(' --user-id=ID 测试特定用户');
  234. $this->info('');
  235. $this->info('示例:');
  236. $this->info(' php artisan urs:test-relation-cache-fix --check');
  237. $this->info(' php artisan urs:test-relation-cache-fix --fix');
  238. $this->info(' php artisan urs:test-relation-cache-fix --user-id=24127');
  239. }
  240. }