|
|
@@ -79,7 +79,7 @@ class UrsRelationCacheLogic
|
|
|
}
|
|
|
|
|
|
// 尝试获取推荐人的上级关系(基于URS关系)
|
|
|
- $this->generateIndirectRelationsFromUrsChain($ursUserId, $farmUserId, $ursReferrerId, 1);
|
|
|
+ $this->generateIndirectRelationsFromUrsChain($ursUserId, $farmUserId, $ursReferrerId, 1, (string)$ursReferrerId, '0');
|
|
|
|
|
|
Log::info("URS用户 {$ursUserId} 占位缓存记录创建完成");
|
|
|
return true;
|
|
|
@@ -97,31 +97,8 @@ class UrsRelationCacheLogic
|
|
|
$directRelation->depth = 1;
|
|
|
$directRelation->save();
|
|
|
|
|
|
- // 获取推荐人的所有上级(限制在20代以内)
|
|
|
- $upperRelations = UrsUserRelationCache::where('urs_user_id', $ursReferrerId)
|
|
|
- ->where('depth', '<', UrsPromotionRelationLevel::getMaxLevel())
|
|
|
- ->get();
|
|
|
-
|
|
|
- // 创建间接关系缓存(最多20代)
|
|
|
- foreach ($upperRelations as $upperRelation) {
|
|
|
- $newDepth = $upperRelation->depth + 1;
|
|
|
-
|
|
|
- // 限制最大深度为20代
|
|
|
- if ($newDepth > UrsPromotionRelationLevel::getMaxLevel()) {
|
|
|
- continue;
|
|
|
- }
|
|
|
-
|
|
|
- $indirectRelation = new UrsUserRelationCache();
|
|
|
- $indirectRelation->user_id = $farmUserId;
|
|
|
- $indirectRelation->related_user_id = $upperRelation->related_user_id;
|
|
|
- $indirectRelation->urs_user_id = $ursUserId;
|
|
|
- $indirectRelation->urs_related_user_id = $upperRelation->urs_related_user_id;
|
|
|
- $indirectRelation->level = UrsPromotionRelationLevel::getLevelByDepth($newDepth);
|
|
|
- $indirectRelation->path = $farmReferrerId . ',' . $upperRelation->path;
|
|
|
- $indirectRelation->urs_path = $ursReferrerId . ',' . $upperRelation->urs_path;
|
|
|
- $indirectRelation->depth = $newDepth;
|
|
|
- $indirectRelation->save();
|
|
|
- }
|
|
|
+ // 基于URS推荐关系链直接生成所有间接关系(不依赖缓存)
|
|
|
+ $this->generateIndirectRelationsFromUrsChain($ursUserId, $farmUserId, $ursReferrerId, 1, (string)$ursReferrerId, (string)$farmReferrerId);
|
|
|
|
|
|
// 清除Redis缓存
|
|
|
Redis::del("urs_promotion:user:{$ursUserId}:all_referrers");
|
|
|
@@ -147,9 +124,11 @@ class UrsRelationCacheLogic
|
|
|
* @param int $farmUserId 当前用户的农场用户ID
|
|
|
* @param int $currentUrsReferrerId 当前层级的URS推荐人ID
|
|
|
* @param int $currentDepth 当前深度
|
|
|
+ * @param string $currentUrsPath 当前URS路径链
|
|
|
+ * @param string $currentFarmPath 当前农场路径链
|
|
|
* @return void
|
|
|
*/
|
|
|
- private function generateIndirectRelationsFromUrsChain(int $ursUserId, int $farmUserId, int $currentUrsReferrerId, int $currentDepth): void
|
|
|
+ private function generateIndirectRelationsFromUrsChain(int $ursUserId, int $farmUserId, int $currentUrsReferrerId, int $currentDepth, string $currentUrsPath = '', string $currentFarmPath = ''): void
|
|
|
{
|
|
|
// 限制最大深度为20代
|
|
|
if ($currentDepth >= UrsPromotionRelationLevel::getMaxLevel()) {
|
|
|
@@ -170,6 +149,13 @@ class UrsRelationCacheLogic
|
|
|
$upperFarmReferrerId = UrsUserMappingService::getFarmUserId($upperUrsReferrerId);
|
|
|
$newDepth = $currentDepth + 1;
|
|
|
|
|
|
+ // 构建新的路径链
|
|
|
+ $newUrsPath = empty($currentUrsPath) ? (string)$upperUrsReferrerId : $currentUrsPath . ',' . $upperUrsReferrerId;
|
|
|
+ $currentFarmReferrerId = UrsUserMappingService::getFarmUserId($currentUrsReferrerId);
|
|
|
+ $currentFarmPart = $currentFarmReferrerId > 0 ? (string)$currentFarmReferrerId : '0';
|
|
|
+ $upperFarmPart = $upperFarmReferrerId > 0 ? (string)$upperFarmReferrerId : '0';
|
|
|
+ $newFarmPath = empty($currentFarmPath) ? $upperFarmPart : $currentFarmPath . ',' . $upperFarmPart;
|
|
|
+
|
|
|
if ($upperFarmReferrerId > 0) {
|
|
|
// 检查是否已存在该深度的关系缓存
|
|
|
$existingCache = UrsUserRelationCache::where('urs_user_id', $ursUserId)
|
|
|
@@ -185,8 +171,8 @@ class UrsRelationCacheLogic
|
|
|
$indirectRelation->urs_user_id = $ursUserId;
|
|
|
$indirectRelation->urs_related_user_id = $upperUrsReferrerId;
|
|
|
$indirectRelation->level = UrsPromotionRelationLevel::getLevelByDepth($newDepth);
|
|
|
- $indirectRelation->path = $this->buildFarmUserPath($currentUrsReferrerId, $upperFarmReferrerId);
|
|
|
- $indirectRelation->urs_path = $this->buildUrsPath($currentUrsReferrerId, $upperUrsReferrerId);
|
|
|
+ $indirectRelation->path = $newFarmPath;
|
|
|
+ $indirectRelation->urs_path = $newUrsPath;
|
|
|
$indirectRelation->depth = $newDepth;
|
|
|
$indirectRelation->save();
|
|
|
}
|
|
|
@@ -197,8 +183,8 @@ class UrsRelationCacheLogic
|
|
|
'depth' => $newDepth
|
|
|
]);
|
|
|
|
|
|
- // 继续向上查找,基于已有的关系缓存
|
|
|
- $this->generateIndirectRelationsFromExistingCache($ursUserId, $farmUserId, $upperUrsReferrerId, $newDepth);
|
|
|
+ // 继续向上递归查找,基于URS推荐关系链
|
|
|
+ $this->generateIndirectRelationsFromUrsChain($ursUserId, $farmUserId, $upperUrsReferrerId, $newDepth, $newUrsPath, $newFarmPath);
|
|
|
} else {
|
|
|
// 检查是否已存在该深度的占位缓存
|
|
|
$existingCache = UrsUserRelationCache::where('urs_user_id', $ursUserId)
|
|
|
@@ -214,101 +200,22 @@ class UrsRelationCacheLogic
|
|
|
$indirectRelation->urs_user_id = $ursUserId;
|
|
|
$indirectRelation->urs_related_user_id = $upperUrsReferrerId;
|
|
|
$indirectRelation->level = UrsPromotionRelationLevel::getLevelByDepth($newDepth);
|
|
|
- $indirectRelation->path = $this->buildFarmUserPath($currentUrsReferrerId, 0);
|
|
|
- $indirectRelation->urs_path = $this->buildUrsPath($currentUrsReferrerId, $upperUrsReferrerId);
|
|
|
+ $indirectRelation->path = $newFarmPath;
|
|
|
+ $indirectRelation->urs_path = $newUrsPath;
|
|
|
$indirectRelation->depth = $newDepth;
|
|
|
$indirectRelation->save();
|
|
|
}
|
|
|
|
|
|
// 继续向上递归查找
|
|
|
- $this->generateIndirectRelationsFromUrsChain($ursUserId, $farmUserId, $upperUrsReferrerId, $newDepth);
|
|
|
+ $this->generateIndirectRelationsFromUrsChain($ursUserId, $farmUserId, $upperUrsReferrerId, $newDepth, $newUrsPath, $newFarmPath);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- /**
|
|
|
- * 基于已有关系缓存生成间接关系
|
|
|
- *
|
|
|
- * @param int $ursUserId 当前用户的URS用户ID
|
|
|
- * @param int $farmUserId 当前用户的农场用户ID
|
|
|
- * @param int $baseUrsReferrerId 基准URS推荐人ID
|
|
|
- * @param int $baseDepth 基准深度
|
|
|
- * @return void
|
|
|
- */
|
|
|
- private function generateIndirectRelationsFromExistingCache(int $ursUserId, int $farmUserId, int $baseUrsReferrerId, int $baseDepth): void
|
|
|
- {
|
|
|
- // 获取基准推荐人的所有上级关系缓存
|
|
|
- $upperRelations = UrsUserRelationCache::where('urs_user_id', $baseUrsReferrerId)
|
|
|
- ->where('depth', '<', UrsPromotionRelationLevel::getMaxLevel() - $baseDepth)
|
|
|
- ->get();
|
|
|
|
|
|
- foreach ($upperRelations as $upperRelation) {
|
|
|
- $newDepth = $baseDepth + $upperRelation->depth;
|
|
|
|
|
|
- // 限制最大深度为20代
|
|
|
- if ($newDepth > UrsPromotionRelationLevel::getMaxLevel()) {
|
|
|
- continue;
|
|
|
- }
|
|
|
|
|
|
- // 检查是否已存在该深度的关系缓存
|
|
|
- $existingCache = UrsUserRelationCache::where('urs_user_id', $ursUserId)
|
|
|
- ->where('urs_related_user_id', $upperRelation->urs_related_user_id)
|
|
|
- ->where('depth', $newDepth)
|
|
|
- ->first();
|
|
|
|
|
|
- if (!$existingCache) {
|
|
|
- $indirectRelation = new UrsUserRelationCache();
|
|
|
- $indirectRelation->user_id = $farmUserId;
|
|
|
- $indirectRelation->related_user_id = $upperRelation->related_user_id;
|
|
|
- $indirectRelation->urs_user_id = $ursUserId;
|
|
|
- $indirectRelation->urs_related_user_id = $upperRelation->urs_related_user_id;
|
|
|
- $indirectRelation->level = UrsPromotionRelationLevel::getLevelByDepth($newDepth);
|
|
|
- $indirectRelation->path = $this->buildCombinedFarmPath($baseUrsReferrerId, $upperRelation->path);
|
|
|
- $indirectRelation->urs_path = $this->buildCombinedUrsPath($baseUrsReferrerId, $upperRelation->urs_path);
|
|
|
- $indirectRelation->depth = $newDepth;
|
|
|
- $indirectRelation->save();
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
|
|
|
- /**
|
|
|
- * 构建农场用户路径
|
|
|
- */
|
|
|
- private function buildFarmUserPath(int $currentUrsReferrerId, int $upperFarmReferrerId): string
|
|
|
- {
|
|
|
- $currentFarmReferrerId = UrsUserMappingService::getFarmUserId($currentUrsReferrerId);
|
|
|
- $currentPart = $currentFarmReferrerId > 0 ? (string)$currentFarmReferrerId : '0';
|
|
|
-
|
|
|
- if ($upperFarmReferrerId > 0) {
|
|
|
- return $currentPart . ',' . $upperFarmReferrerId;
|
|
|
- }
|
|
|
- return $currentPart;
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 构建URS用户路径
|
|
|
- */
|
|
|
- private function buildUrsPath(int $currentUrsReferrerId, int $upperUrsReferrerId): string
|
|
|
- {
|
|
|
- return $currentUrsReferrerId . ',' . $upperUrsReferrerId;
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 构建组合农场用户路径
|
|
|
- */
|
|
|
- private function buildCombinedFarmPath(int $baseUrsReferrerId, string $upperPath): string
|
|
|
- {
|
|
|
- $baseFarmReferrerId = UrsUserMappingService::getFarmUserId($baseUrsReferrerId);
|
|
|
- $basePart = $baseFarmReferrerId > 0 ? (string)$baseFarmReferrerId : '0';
|
|
|
- return $basePart . ',' . $upperPath;
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 构建组合URS用户路径
|
|
|
- */
|
|
|
- private function buildCombinedUrsPath(int $baseUrsReferrerId, string $upperUrsPath): string
|
|
|
- {
|
|
|
- return $baseUrsReferrerId . ',' . $upperUrsPath;
|
|
|
- }
|
|
|
|
|
|
/**
|
|
|
* 清除用户的关系缓存
|
|
|
@@ -345,9 +252,10 @@ class UrsRelationCacheLogic
|
|
|
* 重建所有用户的关系缓存
|
|
|
*
|
|
|
* @param int $batchSize 批处理大小
|
|
|
+ * @param callable|null $progressCallback 进度回调函数
|
|
|
* @return array 包含成功和失败的数量
|
|
|
*/
|
|
|
- public function rebuildAllRelationCache(int $batchSize = 100): array
|
|
|
+ public function rebuildAllRelationCache(int $batchSize = 100, ?callable $progressCallback = null): array
|
|
|
{
|
|
|
try {
|
|
|
// 清空关系缓存表
|
|
|
@@ -370,12 +278,21 @@ class UrsRelationCacheLogic
|
|
|
|
|
|
$successCount = 0;
|
|
|
$failCount = 0;
|
|
|
+ $totalUsers = count($ursUserIds);
|
|
|
+ $processedCount = 0;
|
|
|
|
|
|
// 分批处理
|
|
|
$chunks = array_chunk($ursUserIds, $batchSize);
|
|
|
|
|
|
foreach ($chunks as $chunk) {
|
|
|
foreach ($chunk as $ursUserId) {
|
|
|
+ $processedCount++;
|
|
|
+
|
|
|
+ // 调用进度回调函数
|
|
|
+ if ($progressCallback) {
|
|
|
+ $progressCallback($ursUserId, $processedCount, $totalUsers, $successCount, $failCount);
|
|
|
+ }
|
|
|
+
|
|
|
if ($this->generateUserRelationCache($ursUserId)) {
|
|
|
$successCount++;
|
|
|
} else {
|
|
|
@@ -453,9 +370,10 @@ class UrsRelationCacheLogic
|
|
|
/**
|
|
|
* 修复关系缓存问题
|
|
|
*
|
|
|
+ * @param callable|null $progressCallback 进度回调函数
|
|
|
* @return array 包含修复结果
|
|
|
*/
|
|
|
- public function fixRelationCacheIssues(): array
|
|
|
+ public function fixRelationCacheIssues(?callable $progressCallback = null): array
|
|
|
{
|
|
|
try {
|
|
|
// 检查完整性
|
|
|
@@ -473,8 +391,17 @@ class UrsRelationCacheLogic
|
|
|
->pluck('urs_user_id')
|
|
|
->toArray();
|
|
|
$missingUsers = array_diff($allUsers, $usersWithCache);
|
|
|
+ $totalMissing = count($missingUsers);
|
|
|
+ $processedMissing = 0;
|
|
|
|
|
|
foreach ($missingUsers as $ursUserId) {
|
|
|
+ $processedMissing++;
|
|
|
+
|
|
|
+ // 调用进度回调函数
|
|
|
+ if ($progressCallback) {
|
|
|
+ $progressCallback('missing', $ursUserId, $processedMissing, $totalMissing, $fixed['missing_users']);
|
|
|
+ }
|
|
|
+
|
|
|
if ($this->generateUserRelationCache($ursUserId)) {
|
|
|
$fixed['missing_users']++;
|
|
|
}
|
|
|
@@ -489,8 +416,17 @@ class UrsRelationCacheLogic
|
|
|
LEFT JOIN kku_urs_promotion_user_referrals r ON c.urs_user_id = r.urs_user_id AND r.status = 1
|
|
|
WHERE r.urs_user_id IS NULL
|
|
|
");
|
|
|
+ $totalOrphaned = count($orphanedCaches);
|
|
|
+ $processedOrphaned = 0;
|
|
|
|
|
|
foreach ($orphanedCaches as $cache) {
|
|
|
+ $processedOrphaned++;
|
|
|
+
|
|
|
+ // 调用进度回调函数
|
|
|
+ if ($progressCallback) {
|
|
|
+ $progressCallback('orphaned', $cache->urs_user_id, $processedOrphaned, $totalOrphaned, $fixed['orphaned_caches']);
|
|
|
+ }
|
|
|
+
|
|
|
if ($this->clearUserRelationCache($cache->urs_user_id)) {
|
|
|
$fixed['orphaned_caches']++;
|
|
|
}
|