浏览代码

完善URS推广模块三代推广系统

新增内容:
- 创建测试命令TestUrsProfitCommand和TestUrsTalentCommand
- 完善任务记录文档和WORK.md更新
- 数据库表创建和初始化数据完成
- 验证三代推广关系和收益分成功能正常
notfff 7 月之前
父节点
当前提交
572cfb18

+ 184 - 0
AiWork/202506/15-0024-升级URS推广模块为三代推广系统.md

@@ -0,0 +1,184 @@
+# 升级URS推广模块为三代推广系统
+
+**任务时间**: 2025年06月15日 00:24  
+**任务类型**: 功能升级  
+**模块**: UrsPromotion  
+
+## 任务概述
+
+根据用户需求,将原有的URS推广模块从二代推广升级为三代推广系统,并新增两种收益类型:推广收益和种植收益。
+
+## 主要变更
+
+### 1. 推广关系扩展
+- **原有**: 支持直推和间推(二代)
+- **升级**: 支持直推、间推、三推(三代)
+- **影响**: 扩大了推广收益的覆盖范围
+
+### 2. 收益类型分类
+- **推广收益 (promotion_reward)**: 下级用户进入农场时发放奖励
+- **种植收益 (planting_reward)**: 下级用户收获作物时发放奖励
+- **分成比例**: 不同达人等级享有不同的分成比例
+
+### 3. 达人等级配置优化
+- **配置结构**: 支持两种收益类型的独立分成比例配置
+- **JSON格式**: `{"1": 直推比例, "2": 间推比例, "3": 三推比例}`
+- **等级要求**: 调整了各等级的团队人数要求
+
+## 技术实现
+
+### 1. 数据库结构调整
+
+#### 新增字段
+- `urs_promotion_user_talents.indirect_count`: 间推人数
+- `urs_promotion_user_talents.third_count`: 三推人数
+- `urs_promotion_profits.profit_type`: 收益类型
+- `urs_promotion_profits.relation_level`: 推荐层级
+
+#### 配置表优化
+- `urs_promotion_talent_configs.promotion_reward_rates`: 推广收益分成比例
+- `urs_promotion_talent_configs.planting_reward_rates`: 种植收益分成比例
+
+### 2. 枚举类型定义
+
+#### UrsPromotionRelationLevel
+```php
+enum UrsPromotionRelationLevel: int
+{
+    case DIRECT = 1;    // 直推
+    case INDIRECT = 2;  // 间推
+    case THIRD = 3;     // 三推
+}
+```
+
+#### UrsProfitType
+```php
+enum UrsProfitType: string
+{
+    case PROMOTION_REWARD = 'promotion_reward';  // 推广收益
+    case PLANTING_REWARD = 'planting_reward';    // 种植收益
+}
+```
+
+#### UrsTalentLevel
+```php
+enum UrsTalentLevel: int
+{
+    case NONE = 0;          // 非达人
+    case JUNIOR = 1;        // 初级达人
+    case INTERMEDIATE = 2;  // 中级达人
+    case SENIOR = 3;        // 高级达人
+    case EXPERT = 4;        // 资深达人
+    case MASTER = 5;        // 顶级达人
+}
+```
+
+### 3. 核心业务逻辑
+
+#### 收益分成逻辑 (UrsProfitLogic)
+- 支持三代推广关系的收益分成计算
+- 根据达人等级和收益类型确定分成比例
+- 自动记录详细的分成明细
+
+#### 达人等级逻辑 (UrsTalentLogic)
+- 计算三代团队统计数据
+- 根据团队规模自动升级达人等级
+- 支持批量更新和关系树查询
+
+### 4. 服务接口
+
+#### UrsProfitService
+- `distributePromotionReward()`: 分发推广收益
+- `distributePlantingReward()`: 分发种植收益
+- `getUserProfitStats()`: 获取用户收益统计
+
+#### UrsTalentService
+- `updateUserTalent()`: 更新用户达人等级
+- `getUserReferralTree()`: 获取用户推荐关系树
+- `getUserTeamStats()`: 获取用户团队统计
+
+## 配置数据
+
+### 达人等级配置
+
+| 等级 | 名称 | 直推要求 | 团队要求 | 推广收益分成 | 种植收益分成 |
+|------|------|----------|----------|--------------|--------------|
+| 0 | 非达人 | 0 | 0 | 0%/0%/0% | 0%/0%/0% |
+| 1 | 初级达人 | 3 | 5 | 5%/2%/1% | 3%/1%/0.5% |
+| 2 | 中级达人 | 8 | 15 | 8%/4%/2% | 5%/2%/1% |
+| 3 | 高级达人 | 15 | 30 | 12%/6%/3% | 8%/4%/2% |
+| 4 | 资深达人 | 30 | 80 | 15%/8%/4% | 10%/5%/2.5% |
+| 5 | 顶级达人 | 50 | 150 | 20%/10%/5% | 15%/8%/4% |
+
+*注: 分成比例格式为 直推%/间推%/三推%*
+
+## 测试验证
+
+### 1. 数据库测试
+- ✅ 创建所有数据库表
+- ✅ 插入初始化配置数据
+- ✅ 创建测试推荐关系数据
+- ✅ 验证收益分成记录
+
+### 2. 功能测试
+- 创建了测试命令 `TestUrsProfitCommand` 和 `TestUrsTalentCommand`
+- 可以通过命令行测试收益分成和达人等级功能
+
+### 3. 测试数据
+```sql
+-- 测试推荐关系: 1001 -> 1002 -> 1003 -> 1004
+-- 用户1001: 初级达人,3直推+1间推+1三推=5人团队
+-- 收益分成测试: 用户1004收获100金币,1001获得0.5金币三推种植收益
+```
+
+## 文件变更清单
+
+### 新增文件
+- `Enums/UrsPromotionRelationLevel.php` - 推广关系层级枚举
+- `Enums/UrsProfitType.php` - 收益类型枚举  
+- `Enums/UrsTalentLevel.php` - 达人等级枚举
+- `Models/UrsUserReferral.php` - 用户推荐关系模型
+- `Models/UrsUserTalent.php` - 用户达人等级模型
+- `Models/UrsProfit.php` - 团队收益记录模型
+- `Models/UrsTalentConfig.php` - 达人等级配置模型
+- `Logics/UrsProfitLogic.php` - 收益分成逻辑
+- `Logics/UrsTalentLogic.php` - 达人等级逻辑
+- `Services/UrsProfitService.php` - 收益分成服务
+- `Services/UrsTalentService.php` - 达人等级服务
+- `Repositorys/` - 各种Repository类
+- `Commands/TestUrsProfitCommand.php` - 收益分成测试命令
+- `Commands/TestUrsTalentCommand.php` - 达人等级测试命令
+- `Databases/createsql/create_urs_promotion_tables.sql` - 数据库创建SQL
+- `Databases/createsql/init_urs_promotion_data.sql` - 初始化数据SQL
+
+### 修改文件
+- `README.md` - 更新模块说明
+- `Docs/设计概述.md` - 更新设计文档
+- `Docs/数据库设计.md` - 更新数据库设计文档
+
+## 后续工作
+
+### 1. 集成开发
+- [ ] 与Farm模块集成,监听收获事件
+- [ ] 与User模块集成,处理用户注册推荐关系
+- [ ] 创建后台管理界面
+- [ ] 添加API接口
+
+### 2. 性能优化
+- [ ] 添加缓存机制
+- [ ] 优化查询性能
+- [ ] 考虑分表策略
+
+### 3. 监控告警
+- [ ] 添加业务监控
+- [ ] 异常情况告警
+- [ ] 数据一致性检查
+
+## 总结
+
+本次升级成功将URS推广模块从二代推广扩展为三代推广系统,新增了推广收益和种植收益两种类型,完善了达人等级配置,为后续的业务发展奠定了坚实的技术基础。
+
+系统设计遵循了模块化、可扩展的原则,通过枚举类型、服务层、逻辑层的分离,确保了代码的可维护性和可测试性。
+
+**任务状态**: ✅ 已完成  
+**提交记录**: 0587e93c - 升级URS推广模块为三代推广系统

+ 13 - 9
AiWork/WORK.md

@@ -8,6 +8,18 @@
 
 ## 已完成任务(保留最新的10条,多余的删除)
 
+**2025-06-15 00:24** - 升级URS推广模块为三代推广系统 - 支持三代推广关系和两种收益类型
+- 任务:将原有的URS推广模块从二代推广升级为三代推广系统,新增推广收益和种植收益两种类型
+- 推广:扩展推广关系从直推+间推升级为直推+间推+三推,支持三代推广收益分成
+- 收益:新增推广收益(下级进入农场)和种植收益(下级收获作物)两种收益类型
+- 枚举:创建UrsPromotionRelationLevel、UrsProfitType、UrsTalentLevel三个核心枚举类
+- 模型:实现UrsUserReferral、UrsUserTalent、UrsProfit、UrsTalentConfig四个核心模型
+- 逻辑:开发UrsProfitLogic和UrsTalentLogic两个业务逻辑类,支持三代分成计算和达人等级升级
+- 服务:提供UrsProfitService和UrsTalentService两个对外服务接口
+- 数据库:创建5个核心表,插入6个达人等级配置,支持不同收益类型的分成比例配置
+- 测试:创建测试命令和测试数据,验证三代推广关系和收益分成功能正常
+- 文件:./AiWork/202506/15-0024-升级URS推广模块为三代推广系统.md
+
 **2025-06-14 22:42** - 实现ThirdParty模块基础架构 - 实现标准化第三方对接的基础架构
 - 任务:根据ThirdParty模块文档中描述的规范,实现标准化第三方对接的基础架构
 - 基类:创建BaseRequest请求基类和BaseWebhook基类,支持抽象化请求处理和Webhook分发
@@ -151,15 +163,7 @@
 - 工具:创建InsertOpenAPIAdminMenu管理命令,支持自动化菜单配置和重复检查
 - 文件:./AiWork/2025年06月/131912-OpenAPI模块后台菜单配置完成.md
 
-**2025-06-13 18:25** - 创建OpenAPI模块,专门处理对外开放API的需求
-- 任务:创建OpenAPI模块,为第三方应用提供安全、稳定的API接入服务
-- 架构:完整的模块化设计,包含服务、控制器、中间件、事件、枚举等20个核心文件
-- 功能:应用管理(注册审核密钥)、认证授权(6种认证方式)、权限控制(19种权限范围)、访问控制(限流白名单)、监控统计(日志分析)、开发支持(文档SDK)
-- 枚举:APP_STATUS(7种状态)、AUTH_TYPE(6种认证)、SCOPE_TYPE(19种权限),支持状态转换、安全级别、依赖关系
-- 数据库:6个表(应用、日志、权限、统计、回调、限流),完整索引设计和19条权限默认数据
-- 特点:事件驱动架构、多认证支持、细粒度权限、高性能设计、安全保障、完整API管理生态
-- 集成:更新模块目录文档,模块总数33→34个,基础服务模块8→9个
-- 文件:./AiWork/202506/131825-创建OpenAPI模块.md
+
 
 
 

+ 112 - 0
app/Module/UrsPromotion/Commands/TestUrsProfitCommand.php

@@ -0,0 +1,112 @@
+<?php
+
+namespace App\Module\UrsPromotion\Commands;
+
+use Illuminate\Console\Command;
+use App\Module\UrsPromotion\Services\UrsProfitService;
+use App\Module\UrsPromotion\Services\UrsTalentService;
+use App\Module\UrsPromotion\Enums\UrsProfitType;
+
+/**
+ * URS收益分成测试命令
+ * 
+ * 用于测试URS推广系统的收益分成功能
+ */
+class TestUrsProfitCommand extends Command
+{
+    /**
+     * 命令签名
+     */
+    protected $signature = 'urs:test-profit {user_id} {amount} {type=planting_reward}';
+
+    /**
+     * 命令描述
+     */
+    protected $description = '测试URS收益分成功能';
+
+    /**
+     * 执行命令
+     */
+    public function handle()
+    {
+        $userId = (int)$this->argument('user_id');
+        $amount = $this->argument('amount');
+        $type = $this->argument('type');
+        
+        $this->info("开始测试URS收益分成功能");
+        $this->info("用户ID: {$userId}");
+        $this->info("收益金额: {$amount}");
+        $this->info("收益类型: {$type}");
+        $this->line('');
+        
+        // 显示用户的推荐关系树
+        $this->info("=== 用户推荐关系树 ===");
+        $tree = UrsTalentService::getUserReferralTree($userId);
+        $this->displayReferralTree($tree);
+        $this->line('');
+        
+        // 执行收益分成
+        $this->info("=== 执行收益分成 ===");
+        
+        if ($type === 'promotion_reward') {
+            $profits = UrsProfitService::distributePromotionReward(
+                $userId, 
+                'test_command', 
+                time(), 
+                $amount
+            );
+        } else {
+            $profits = UrsProfitService::distributePlantingReward(
+                $userId, 
+                'test_command', 
+                time(), 
+                $amount
+            );
+        }
+        
+        if (empty($profits)) {
+            $this->warn("没有产生任何收益分成");
+        } else {
+            $this->info("成功分成给 " . count($profits) . " 个用户:");
+            foreach ($profits as $profit) {
+                $this->line("- 用户{$profit->user_id}: {$profit->profit_amount} ({$profit->profit_rate * 100}%, {$profit->getRelationLevelName()})");
+            }
+        }
+        
+        $this->line('');
+        $this->info("测试完成!");
+    }
+    
+    /**
+     * 显示推荐关系树
+     */
+    private function displayReferralTree(array $tree): void
+    {
+        $this->line("用户 {$tree['user_id']} 的团队结构:");
+        
+        if (!empty($tree['direct'])) {
+            $this->line("  直推用户 (" . count($tree['direct']) . "人):");
+            foreach ($tree['direct'] as $user) {
+                $this->line("    - 用户{$user['user_id']}");
+            }
+        }
+        
+        if (!empty($tree['indirect'])) {
+            $this->line("  间推用户 (" . count($tree['indirect']) . "人):");
+            foreach ($tree['indirect'] as $user) {
+                $this->line("    - 用户{$user['user_id']} (通过用户{$user['referrer_id']})");
+            }
+        }
+        
+        if (!empty($tree['third'])) {
+            $this->line("  三推用户 (" . count($tree['third']) . "人):");
+            foreach ($tree['third'] as $user) {
+                $this->line("    - 用户{$user['user_id']} (通过用户{$user['referrer_id']})");
+            }
+        }
+        
+        if (empty($tree['direct']) && empty($tree['indirect']) && empty($tree['third'])) {
+            $this->line("  无团队成员");
+        }
+    }
+}

+ 116 - 0
app/Module/UrsPromotion/Commands/TestUrsTalentCommand.php

@@ -0,0 +1,116 @@
+<?php
+
+namespace App\Module\UrsPromotion\Commands;
+
+use Illuminate\Console\Command;
+use App\Module\UrsPromotion\Services\UrsTalentService;
+
+/**
+ * URS达人等级测试命令
+ * 
+ * 用于测试URS推广系统的达人等级功能
+ */
+class TestUrsTalentCommand extends Command
+{
+    /**
+     * 命令签名
+     */
+    protected $signature = 'urs:test-talent {user_id}';
+
+    /**
+     * 命令描述
+     */
+    protected $description = '测试URS达人等级功能';
+
+    /**
+     * 执行命令
+     */
+    public function handle()
+    {
+        $userId = (int)$this->argument('user_id');
+        
+        $this->info("开始测试URS达人等级功能");
+        $this->info("用户ID: {$userId}");
+        $this->line('');
+        
+        // 显示当前达人信息
+        $this->info("=== 当前达人信息 ===");
+        $talent = UrsTalentService::getUserTalent($userId);
+        if ($talent) {
+            $this->line("达人等级: {$talent->getTalentLevelName()} (等级{$talent->talent_level})");
+            $this->line("直推人数: {$talent->direct_count}");
+            $this->line("间推人数: {$talent->indirect_count}");
+            $this->line("三推人数: {$talent->third_count}");
+            $this->line("团队总人数: {$talent->promotion_count}");
+        } else {
+            $this->line("用户暂无达人信息");
+        }
+        $this->line('');
+        
+        // 显示团队统计
+        $this->info("=== 团队统计信息 ===");
+        $stats = UrsTalentService::getUserTeamStats($userId);
+        $this->line("直推: {$stats['direct_count']} 人");
+        $this->line("间推: {$stats['indirect_count']} 人");
+        $this->line("三推: {$stats['third_count']} 人");
+        $this->line("总计: {$stats['promotion_count']} 人");
+        $this->line("当前等级: {$stats['talent_level_name']}");
+        $this->line('');
+        
+        // 更新达人等级
+        $this->info("=== 更新达人等级 ===");
+        $updatedTalent = UrsTalentService::updateUserTalent($userId);
+        if ($updatedTalent) {
+            $this->info("达人等级更新成功");
+            $this->line("新等级: {$updatedTalent->getTalentLevelName()} (等级{$updatedTalent->talent_level})");
+            $this->line("直推人数: {$updatedTalent->direct_count}");
+            $this->line("间推人数: {$updatedTalent->indirect_count}");
+            $this->line("三推人数: {$updatedTalent->third_count}");
+            $this->line("团队总人数: {$updatedTalent->promotion_count}");
+        } else {
+            $this->error("达人等级更新失败");
+        }
+        $this->line('');
+        
+        // 显示推荐关系树
+        $this->info("=== 推荐关系树 ===");
+        $tree = UrsTalentService::getUserReferralTree($userId);
+        $this->displayReferralTree($tree);
+        
+        $this->line('');
+        $this->info("测试完成!");
+    }
+    
+    /**
+     * 显示推荐关系树
+     */
+    private function displayReferralTree(array $tree): void
+    {
+        $this->line("用户 {$tree['user_id']} 的团队结构:");
+        
+        if (!empty($tree['direct'])) {
+            $this->line("  直推用户 (" . count($tree['direct']) . "人):");
+            foreach ($tree['direct'] as $user) {
+                $this->line("    - 用户{$user['user_id']} (推荐时间: {$user['referral_time']})");
+            }
+        }
+        
+        if (!empty($tree['indirect'])) {
+            $this->line("  间推用户 (" . count($tree['indirect']) . "人):");
+            foreach ($tree['indirect'] as $user) {
+                $this->line("    - 用户{$user['user_id']} (通过用户{$user['referrer_id']}, 推荐时间: {$user['referral_time']})");
+            }
+        }
+        
+        if (!empty($tree['third'])) {
+            $this->line("  三推用户 (" . count($tree['third']) . "人):");
+            foreach ($tree['third'] as $user) {
+                $this->line("    - 用户{$user['user_id']} (通过用户{$user['referrer_id']}, 推荐时间: {$user['referral_time']})");
+            }
+        }
+        
+        if (empty($tree['direct']) && empty($tree['indirect']) && empty($tree['third'])) {
+            $this->line("  无团队成员");
+        }
+    }
+}