dongasai před 6 měsíci
rodič
revize
61601c69ba

+ 110 - 0
AiWork/2025年07月/05日0300-改进URS关系缓存重建命令支持指定用户处理.md

@@ -0,0 +1,110 @@
+# 改进URS关系缓存重建命令支持指定用户处理
+
+## 任务时间
+- 开始时间:2025年07月05日 03:00:04 CST
+- 完成时间:2025年07月05日 03:30:00 CST
+
+## 任务背景
+用户反馈URS推荐系统中的缓存生成存在逻辑bug,缓存不应该基于缓存生成,要从关系表中读取数据。同时需要改进UrsRebuildRelationCacheCommand命令,支持指定用户处理功能。
+
+## 问题分析
+1. **缓存生成逻辑错误**:原逻辑中存在基于现有缓存生成新缓存的问题,导致循环依赖
+2. **路径链不完整**:用户44578有多级推荐关系,但缓存只生成了部分级别
+3. **缺少指定用户处理功能**:命令只能全量重建,无法针对特定用户操作
+
+## 解决方案
+
+### 1. 新增指定用户处理功能
+在`UrsRebuildRelationCacheCommand`中添加以下参数:
+- `--user=` : 指定单个URS用户ID
+- `--users=` : 指定多个URS用户ID(逗号分隔)
+- `--clear` : 清除指定用户的缓存
+
+### 2. 修复缓存生成逻辑
+- 移除`generateIndirectRelationsFromExistingCache`方法
+- 修改`generateIndirectRelationsFromUrsChain`方法,完全基于URS推荐关系表生成缓存
+- 修复路径链构建逻辑,确保完整的路径链传递
+
+### 3. 完善命令功能
+- 添加用户ID验证
+- 改进错误处理和用户反馈
+- 添加进度条显示
+- 更新命令帮助和使用示例
+
+## 实现细节
+
+### 命令参数扩展
+```php
+protected $signature = 'urs:rebuild-relation-cache 
+                        {--batch-size=100 : 批处理大小}
+                        {--check : 仅检查完整性,不重建}
+                        {--fix : 修复发现的问题}
+                        {--user= : 指定URS用户ID,仅处理该用户}
+                        {--users= : 指定多个URS用户ID,用逗号分隔}
+                        {--clear : 清除指定用户的缓存(需配合--user或--users使用)}';
+```
+
+### 逻辑修复
+1. **移除错误的缓存依赖逻辑**:
+   ```php
+   // 原错误逻辑(已删除)
+   $upperRelations = UrsUserRelationCache::where('urs_user_id', $ursReferrerId)->get();
+   
+   // 修复后的逻辑
+   $this->generateIndirectRelationsFromUrsChain($ursUserId, $farmUserId, $ursReferrerId, 1);
+   ```
+
+2. **修复路径链构建**:
+   ```php
+   // 构建完整的路径链
+   $newUrsPath = empty($currentUrsPath) ? (string)$upperUrsReferrerId : $currentUrsPath . ',' . $upperUrsReferrerId;
+   $newFarmPath = empty($currentFarmPath) ? $upperFarmPart : $currentFarmPath . ',' . $upperFarmPart;
+   ```
+
+## 测试验证
+
+### 测试用例:用户44578
+**推荐关系链**:44578 -> 44572 -> 10387 -> 10002 -> 1
+
+**修复前**:只有1级缓存
+```
+Depth 1: related_urs_id=44572, path=44572
+```
+
+**修复后**:完整的4级缓存
+```
+Depth 1: related_urs_id=44572, urs_path=44572, farm_path=40124
+Depth 2: related_urs_id=10387, urs_path=44572,10387, farm_path=40124,39172
+Depth 3: related_urs_id=10002, urs_path=44572,10387,10002, farm_path=40124,39172,39186
+Depth 4: related_urs_id=1, urs_path=44572,10387,10002,1, farm_path=40124,39172,39186,0
+```
+
+### 命令测试
+```bash
+# 指定单个用户
+php artisan urs:rebuild-relation-cache --user=44578
+
+# 指定多个用户
+php artisan urs:rebuild-relation-cache --users=44578,10387
+
+# 清除指定用户缓存
+php artisan urs:rebuild-relation-cache --user=44578 --clear
+
+# 无效用户ID处理
+php artisan urs:rebuild-relation-cache --user=999999
+# 输出:所有指定的用户ID都无效
+```
+
+## 代码变更
+- 修改文件:`app/Module/UrsPromotion/Commands/UrsRebuildRelationCacheCommand.php`
+- 修改文件:`app/Module/UrsPromotion/Logics/UrsRelationCacheLogic.php`
+- 新增方法:`handleSpecificUsers()`, `getSpecifiedUserIds()`, `validateUserIds()`, `clearSpecificUsersCache()`, `rebuildSpecificUsersCache()`
+- 删除方法:`generateIndirectRelationsFromExistingCache()`, `buildCombinedFarmPath()`, `buildCombinedUrsPath()`, `buildFarmUserPath()`, `buildUrsPath()`
+
+## 总结
+1. **成功修复**了缓存生成的逻辑bug,改为完全基于URS推荐关系表生成
+2. **新增**了指定用户处理功能,提高了命令的灵活性
+3. **修复**了路径链构建问题,确保缓存数据的完整性和正确性
+4. **完善**了错误处理和用户体验
+
+该改进解决了用户反馈的核心问题,提升了URS推荐系统缓存的可靠性和维护效率。

+ 1 - 0
app/Module/UrsPromotion/Commands/TestRelationCacheFixCommand.php

@@ -14,6 +14,7 @@ use Illuminate\Support\Facades\DB;
  * 测试关系缓存断代修复命令
  * 
  * 用于测试和验证关系缓存断代问题的修复效果
+ * php artisan urs:test-relation-cache-fix --check
  */
 class TestRelationCacheFixCommand extends Command
 {

+ 15 - 5
app/Module/UrsPromotion/Services/UrsReferralService.php

@@ -138,20 +138,30 @@ class UrsReferralService
      * 获取团队成员人数/层级
      * @param int $ursUserId
      * @param int $maxLevels
-     * @return array
+     * @return array [代数=>人数]
      */
     private static function getTeamMNumberFromCache(int $ursUserId, int $maxLevels): array
     {
-        dump($maxLevels);
+
         // 使用关系缓存表查询所有下级关系
         $relations = \App\Module\UrsPromotion\Models\UrsUserRelationCache::where('urs_related_user_id', $ursUserId)
             ->where('depth', '<=', $maxLevels)
+            ->where('user_id','>',0)
             ->orderBy('depth')
             ->groupBy('depth')
-            ->get([DB::raw('count(*) as count'), 'depth']);
-
+            ->pluck(
+                DB::raw('count(*) as total'),
+                'depth'
+                    );
 
-        dd($relations);
+        $team = [];
+        $totle = 0;
+        foreach ($relations as $level=>$number) {
+            $team[$level] = $number;
+            $totle += $number;
+        }
+        $team['total'] = $totle;
+//        dd($relations);
         return $team;
     }
 

+ 8 - 8
app/Module/UrsPromotion/Services/UrsTalentService.php

@@ -34,8 +34,8 @@ class UrsTalentService
 
             // 计算应该的达人等级
             $newLevel = self::calculateTalentLevel(
-                $stats['direct_count'],
-                $stats['total_team_count'],
+                $stats[1],
+                $stats['total'],
                 $activeStats['active_direct_count'],
                 $activeStats['active_total_count']
             );
@@ -56,10 +56,10 @@ class UrsTalentService
 
             // 更新团队统计数据(使用模型方法确保逻辑一致性)
             $talent->updateTeamStats(
-                $stats['direct_count'],
-                $stats['indirect_count'],
-                $stats['third_count'],
-                $stats['total_team_count'] // 20代总人数
+                $stats[1],
+                $stats[2],
+                $stats[3],
+                $stats['total'] // 20代总人数
             );
 
             // 更新达人等级
@@ -76,8 +76,8 @@ class UrsTalentService
                     'user_id' => $ursUserId,
                     'old_level' => $oldLevel,
                     'new_level' => $newLevel,
-                    'direct_count' => $stats['direct_count'],
-                    'total_team_count' => $stats['total_team_count']
+                    'direct_count' => $stats[1],
+                    'total_team_count' => $stats['total'],
                 ]);
             }