TestRelationCacheFixCommand.php 9.7 KB

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