RelationCacheLogic.php 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. <?php
  2. namespace App\Module\Promotion\Logics;
  3. use App\Module\Promotion\Enums\REFERRAL_LEVEL;
  4. use App\Module\Promotion\Models\PromotionUserReferral;
  5. use App\Module\Promotion\Models\PromotionUserRelationCache;
  6. use Illuminate\Support\Facades\DB;
  7. use Illuminate\Support\Facades\Log;
  8. use Illuminate\Support\Facades\Redis;
  9. /**
  10. * 关系缓存逻辑类
  11. *
  12. * 处理用户关系缓存的核心业务逻辑,包括生成关系缓存、清理关系缓存、
  13. * 重建关系缓存等功能。该类仅供内部使用,不对外提供服务。
  14. */
  15. class RelationCacheLogic
  16. {
  17. /**
  18. * 生成用户的关系缓存
  19. *
  20. * @param int $userId 用户ID
  21. * @return bool
  22. */
  23. public function generateUserRelationCache(int $userId): bool
  24. {
  25. try {
  26. // 清除用户的现有关系缓存
  27. $this->clearUserRelationCache($userId);
  28. // 获取用户的直接推荐人
  29. $referral = PromotionUserReferral::where('user_id', $userId)->first();
  30. if (!$referral) {
  31. Log::info("用户 {$userId} 没有推荐人,无需生成关系缓存");
  32. return true;
  33. }
  34. $referrerId = $referral->referrer_id;
  35. // 创建直接关系缓存
  36. $directRelation = new PromotionUserRelationCache();
  37. $directRelation->user_id = $userId;
  38. $directRelation->related_user_id = $referrerId;
  39. $directRelation->level = REFERRAL_LEVEL::DIRECT;
  40. $directRelation->path = (string)$referrerId;
  41. $directRelation->depth = 1;
  42. $directRelation->save();
  43. // 获取推荐人的所有上级
  44. $upperRelations = PromotionUserRelationCache::where('user_id', $referrerId)->get();
  45. // 创建间接关系缓存
  46. foreach ($upperRelations as $upperRelation) {
  47. $indirectRelation = new PromotionUserRelationCache();
  48. $indirectRelation->user_id = $userId;
  49. $indirectRelation->related_user_id = $upperRelation->related_user_id;
  50. $indirectRelation->level = REFERRAL_LEVEL::INDIRECT;
  51. $indirectRelation->path = $referrerId . ',' . $upperRelation->path;
  52. $indirectRelation->depth = $upperRelation->depth + 1;
  53. $indirectRelation->save();
  54. }
  55. // 清除Redis缓存
  56. Redis::del("promotion:user:{$userId}:all_referrers");
  57. return true;
  58. } catch (\Exception $e) {
  59. Log::error("生成用户关系缓存失败: " . $e->getMessage());
  60. return false;
  61. }
  62. }
  63. /**
  64. * 清除用户的关系缓存
  65. *
  66. * @param int $userId 用户ID
  67. * @return bool
  68. */
  69. public function clearUserRelationCache(int $userId): bool
  70. {
  71. try {
  72. // 删除数据库中的关系缓存
  73. PromotionUserRelationCache::where('user_id', $userId)->delete();
  74. // 清除Redis缓存
  75. Redis::del("promotion:user:{$userId}:all_referrers");
  76. return true;
  77. } catch (\Exception $e) {
  78. Log::error("清除用户关系缓存失败: " . $e->getMessage());
  79. return false;
  80. }
  81. }
  82. /**
  83. * 重建所有用户的关系缓存
  84. *
  85. * @param int $batchSize 批处理大小
  86. * @return array 包含成功和失败的数量
  87. */
  88. public function rebuildAllRelationCache(int $batchSize = 100): array
  89. {
  90. try {
  91. // 清空关系缓存表
  92. PromotionUserRelationCache::truncate();
  93. // 清除所有Redis缓存
  94. $keys = Redis::keys("promotion:user:*:all_referrers");
  95. if (!empty($keys)) {
  96. Redis::del($keys);
  97. }
  98. // 获取所有用户ID
  99. $userIds = PromotionUserReferral::pluck('user_id')->toArray();
  100. $successCount = 0;
  101. $failCount = 0;
  102. // 分批处理
  103. $chunks = array_chunk($userIds, $batchSize);
  104. foreach ($chunks as $chunk) {
  105. foreach ($chunk as $userId) {
  106. if ($this->generateUserRelationCache($userId)) {
  107. $successCount++;
  108. } else {
  109. $failCount++;
  110. }
  111. }
  112. }
  113. return [
  114. 'success' => $successCount,
  115. 'fail' => $failCount,
  116. 'total' => count($userIds)
  117. ];
  118. } catch (\Exception $e) {
  119. Log::error("重建所有关系缓存失败: " . $e->getMessage());
  120. return [
  121. 'success' => 0,
  122. 'fail' => 0,
  123. 'total' => 0,
  124. 'error' => $e->getMessage()
  125. ];
  126. }
  127. }
  128. /**
  129. * 检查关系缓存的完整性
  130. *
  131. * @return array 包含检查结果
  132. */
  133. public function checkRelationCacheIntegrity(): array
  134. {
  135. try {
  136. $totalUsers = PromotionUserReferral::count();
  137. $usersWithCache = PromotionUserRelationCache::distinct('user_id')->count('user_id');
  138. $missingUsers = $totalUsers - $usersWithCache;
  139. $circularRelations = DB::select("
  140. SELECT r1.user_id, r1.referrer_id
  141. FROM promotion_user_referrals r1
  142. JOIN promotion_user_referrals r2 ON r1.referrer_id = r2.user_id
  143. WHERE r2.referrer_id = r1.user_id
  144. ");
  145. $orphanedCaches = DB::select("
  146. SELECT c.user_id
  147. FROM promotion_user_relation_cache c
  148. LEFT JOIN promotion_user_referrals r ON c.user_id = r.user_id
  149. WHERE r.user_id IS NULL
  150. ");
  151. return [
  152. 'total_users' => $totalUsers,
  153. 'users_with_cache' => $usersWithCache,
  154. 'missing_users' => $missingUsers,
  155. 'circular_relations' => count($circularRelations),
  156. 'orphaned_caches' => count($orphanedCaches)
  157. ];
  158. } catch (\Exception $e) {
  159. Log::error("检查关系缓存完整性失败: " . $e->getMessage());
  160. return [
  161. 'error' => $e->getMessage()
  162. ];
  163. }
  164. }
  165. /**
  166. * 修复关系缓存问题
  167. *
  168. * @return array 包含修复结果
  169. */
  170. public function fixRelationCacheIssues(): array
  171. {
  172. try {
  173. // 检查完整性
  174. $integrity = $this->checkRelationCacheIntegrity();
  175. $fixed = [
  176. 'missing_users' => 0,
  177. 'orphaned_caches' => 0
  178. ];
  179. // 修复缺失的用户缓存
  180. if ($integrity['missing_users'] > 0) {
  181. $usersWithCache = PromotionUserRelationCache::distinct('user_id')->pluck('user_id')->toArray();
  182. $allUsers = PromotionUserReferral::pluck('user_id')->toArray();
  183. $missingUsers = array_diff($allUsers, $usersWithCache);
  184. foreach ($missingUsers as $userId) {
  185. if ($this->generateUserRelationCache($userId)) {
  186. $fixed['missing_users']++;
  187. }
  188. }
  189. }
  190. // 清理孤立的缓存
  191. if ($integrity['orphaned_caches'] > 0) {
  192. $orphanedCaches = DB::select("
  193. SELECT c.user_id
  194. FROM promotion_user_relation_cache c
  195. LEFT JOIN promotion_user_referrals r ON c.user_id = r.user_id
  196. WHERE r.user_id IS NULL
  197. ");
  198. foreach ($orphanedCaches as $cache) {
  199. if ($this->clearUserRelationCache($cache->user_id)) {
  200. $fixed['orphaned_caches']++;
  201. }
  202. }
  203. }
  204. return [
  205. 'integrity' => $integrity,
  206. 'fixed' => $fixed
  207. ];
  208. } catch (\Exception $e) {
  209. Log::error("修复关系缓存问题失败: " . $e->getMessage());
  210. return [
  211. 'error' => $e->getMessage()
  212. ];
  213. }
  214. }
  215. }