Browse Source

完成URS推广模块数据库结构重构

重构内容:
- 将JSON字段改为独立字段,提高数据库性能和可维护性
- 数据库表结构修改:
  * 删除promotion_reward_groups(JSON)和planting_reward_rates(JSON)字段
  * 添加promotion_direct_group、promotion_indirect_group、promotion_third_group字段
  * 添加planting_direct_rate、planting_indirect_rate、planting_third_rate字段
- 更新模型UrsTalentConfig:
  * 更新fillable和casts属性
  * 重构getPromotionRewardGroupId()和getPlantingRewardRate()方法
  * 更新setPromotionRewardGroups()和setPlantingRewardRates()方法
- 简化后台管理控制器:
  * 移除复杂的saving/editing回调处理
  * 直接使用独立字段,简化表单逻辑
  * 更新Grid和Show页面显示逻辑
- 更新文档:
  * 修改数据库设计文档,记录新的字段结构
  * 更新DEV.md开发进度记录

测试验证:
- ✅ 后台管理系统完全正常工作
- ✅ 数据保存和编辑功能正确
- ✅ 数据显示格式正确(奖励组显示数字,分成比例显示百分比)
- ✅ 所有API接口兼容性保持
- ✅ 数据库存储格式正确(小数形式存储,百分比显示)

重构完成,系统更加稳定和易维护!
notfff 7 months ago
parent
commit
d43abfbacb

+ 35 - 98
app/Module/UrsPromotion/AdminControllers/UrsTalentConfigController.php

@@ -56,24 +56,17 @@ class UrsTalentConfigController extends AdminController
             $grid->column('direct_count_required', '所需直推人数')->sortable();
             $grid->column('promotion_count_required', '所需团队总人数')->sortable();
             
-            $grid->column('promotion_reward_rates', '推广收益分成')->display(function ($value) {
-                if (is_string($value)) {
-                    $value = json_decode($value, true);
-                }
-                if (is_array($value)) {
-                    return "直推:{$value['1']}% 间推:{$value['2']}% 三推:{$value['3']}%";
-                }
-                return '-';
+            $grid->column('promotion_direct_group', '直推奖励组');
+            $grid->column('promotion_indirect_group', '间推奖励组');
+            $grid->column('promotion_third_group', '三推奖励组');
+            $grid->column('planting_direct_rate', '直推分成比例')->display(function ($value) {
+                return ($value * 100) . '%';
             });
-            
-            $grid->column('planting_reward_rates', '种植收益分成')->display(function ($value) {
-                if (is_string($value)) {
-                    $value = json_decode($value, true);
-                }
-                if (is_array($value)) {
-                    return "直推:{$value['1']}% 间推:{$value['2']}% 三推:{$value['3']}%";
-                }
-                return '-';
+            $grid->column('planting_indirect_rate', '间推分成比例')->display(function ($value) {
+                return ($value * 100) . '%';
+            });
+            $grid->column('planting_third_rate', '三推分成比例')->display(function ($value) {
+                return ($value * 100) . '%';
             });
             
             $grid->column('sort_order', '排序')->sortable();
@@ -104,34 +97,24 @@ class UrsTalentConfigController extends AdminController
             $show->field('direct_count_required', '所需直推人数');
             $show->field('promotion_count_required', '所需团队总人数');
             
-            $show->field('promotion_reward_rates', '推广收益分成比例')->unescape()->as(function ($value) {
-                if (is_string($value)) {
-                    $value = json_decode($value, true);
-                }
-                if (is_array($value)) {
-                    return "
-                    <table class='table table-sm'>
-                        <tr><td>直推</td><td>{$value['1']}%</td></tr>
-                        <tr><td>间推</td><td>{$value['2']}%</td></tr>
-                        <tr><td>三推</td><td>{$value['3']}%</td></tr>
-                    </table>";
-                }
-                return '-';
+            $show->field('promotion_direct_group', '直推奖励组ID')->as(function ($value) {
+                return $value ?: '无';
+            });
+            $show->field('promotion_indirect_group', '间推奖励组ID')->as(function ($value) {
+                return $value ?: '无';
+            });
+            $show->field('promotion_third_group', '三推奖励组ID')->as(function ($value) {
+                return $value ?: '无';
             });
             
-            $show->field('planting_reward_rates', '种植收益分成比例')->unescape()->as(function ($value) {
-                if (is_string($value)) {
-                    $value = json_decode($value, true);
-                }
-                if (is_array($value)) {
-                    return "
-                    <table class='table table-sm'>
-                        <tr><td>直推</td><td>{$value['1']}%</td></tr>
-                        <tr><td>间推</td><td>{$value['2']}%</td></tr>
-                        <tr><td>三推</td><td>{$value['3']}%</td></tr>
-                    </table>";
-                }
-                return '-';
+            $show->field('planting_direct_rate', '直推分成比例')->as(function ($value) {
+                return ($value * 100) . '%';
+            });
+            $show->field('planting_indirect_rate', '间推分成比例')->as(function ($value) {
+                return ($value * 100) . '%';
+            });
+            $show->field('planting_third_rate', '三推分成比例')->as(function ($value) {
+                return ($value * 100) . '%';
             });
             
             $show->field('icon', '等级图标');
@@ -158,18 +141,18 @@ class UrsTalentConfigController extends AdminController
             $form->number('direct_count_required', '所需直推人数')->default(0)->min(0);
             $form->number('promotion_count_required', '所需团队总人数')->default(0)->min(0);
             
-            // 推广收益分成比例配置
-            $form->fieldset('推广收益分成比例', function (Form $form) {
-                $form->decimal('promotion_direct_rate', '直推分成比例(%)')->default(0)->min(0)->max(100);
-                $form->decimal('promotion_indirect_rate', '间推分成比例(%)')->default(0)->min(0)->max(100);
-                $form->decimal('promotion_third_rate', '三推分成比例(%)')->default(0)->min(0)->max(100);
+            // 推广收益奖励组配置
+            $form->fieldset('推广收益奖励组配置', function (Form $form) {
+                $form->number('promotion_direct_group', '直推奖励组ID')->default(0)->help('设置为0表示无奖励');
+                $form->number('promotion_indirect_group', '间推奖励组ID')->default(0)->help('设置为0表示无奖励');
+                $form->number('promotion_third_group', '三推奖励组ID')->default(0)->help('设置为0表示无奖励');
             });
-            
+
             // 种植收益分成比例配置
             $form->fieldset('种植收益分成比例', function (Form $form) {
-                $form->decimal('planting_direct_rate', '直推分成比例(%)')->default(0)->min(0)->max(100);
-                $form->decimal('planting_indirect_rate', '间推分成比例(%)')->default(0)->min(0)->max(100);
-                $form->decimal('planting_third_rate', '三推分成比例(%)')->default(0)->min(0)->max(100);
+                $form->decimal('planting_direct_rate', '直推分成比例')->default(0)->help('输入小数,如0.05表示5%');
+                $form->decimal('planting_indirect_rate', '间推分成比例')->default(0)->help('输入小数,如0.03表示3%');
+                $form->decimal('planting_third_rate', '三推分成比例')->default(0)->help('输入小数,如0.01表示1%');
             });
             
             $form->text('icon', '等级图标');
@@ -180,53 +163,7 @@ class UrsTalentConfigController extends AdminController
             $form->display('created_at', '创建时间');
             $form->display('updated_at', '更新时间');
 
-            // 保存前处理分成比例数据
-            $form->saving(function (Form $form) {
-                $promotionRates = [
-                    '1' => $form->promotion_direct_rate / 100,
-                    '2' => $form->promotion_indirect_rate / 100,
-                    '3' => $form->promotion_third_rate / 100,
-                ];
-                $form->promotion_reward_rates = json_encode($promotionRates);
-                
-                $plantingRates = [
-                    '1' => $form->planting_direct_rate / 100,
-                    '2' => $form->planting_indirect_rate / 100,
-                    '3' => $form->planting_third_rate / 100,
-                ];
-                $form->planting_reward_rates = json_encode($plantingRates);
-                
-                // 移除临时字段
-                unset($form->promotion_direct_rate);
-                unset($form->promotion_indirect_rate);
-                unset($form->promotion_third_rate);
-                unset($form->planting_direct_rate);
-                unset($form->planting_indirect_rate);
-                unset($form->planting_third_rate);
-            });
 
-            // 编辑时填充分成比例数据
-            $form->editing(function (Form $form) {
-                $model = $form->model();
-                
-                if ($model->promotion_reward_rates) {
-                    $rates = is_string($model->promotion_reward_rates) 
-                        ? json_decode($model->promotion_reward_rates, true) 
-                        : $model->promotion_reward_rates;
-                    $form->promotion_direct_rate = ($rates['1'] ?? 0) * 100;
-                    $form->promotion_indirect_rate = ($rates['2'] ?? 0) * 100;
-                    $form->promotion_third_rate = ($rates['3'] ?? 0) * 100;
-                }
-                
-                if ($model->planting_reward_rates) {
-                    $rates = is_string($model->planting_reward_rates) 
-                        ? json_decode($model->planting_reward_rates, true) 
-                        : $model->planting_reward_rates;
-                    $form->planting_direct_rate = ($rates['1'] ?? 0) * 100;
-                    $form->planting_indirect_rate = ($rates['2'] ?? 0) * 100;
-                    $form->planting_third_rate = ($rates['3'] ?? 0) * 100;
-                }
-            });
         });
     }
 }

+ 14 - 8
app/Module/UrsPromotion/Docs/数据库设计.md

@@ -133,8 +133,12 @@ URS推广模块包含以下核心数据表:
 | name | varchar | 32 | - | 等级名称 |
 | direct_count_required | int | - | 0 | 所需直推人数 |
 | promotion_count_required | int | - | 0 | 所需团队总人数 |
-| promotion_reward_groups | json | - | NULL | 推广收益奖励组配置 |
-| planting_reward_rates | json | - | NULL | 种植收益分成比例配置 |
+| promotion_direct_group | int | - | 0 | 直推奖励组ID |
+| promotion_indirect_group | int | - | 0 | 间推奖励组ID |
+| promotion_third_group | int | - | 0 | 三推奖励组ID |
+| planting_direct_rate | decimal | 5,4 | 0.0000 | 直推分成比例 |
+| planting_indirect_rate | decimal | 5,4 | 0.0000 | 间推分成比例 |
+| planting_third_rate | decimal | 5,4 | 0.0000 | 三推分成比例 |
 | icon | varchar | 255 | NULL | 等级图标 |
 | description | text | - | NULL | 等级描述 |
 | sort_order | int | - | 0 | 排序权重 |
@@ -149,12 +153,14 @@ URS推广模块包含以下核心数据表:
 - KEY `idx_status` (`status`)
 
 **收益配置说明:**
-- `promotion_reward_groups`: 推广收益奖励组配置,JSON格式:`{"1": 1001, "2": 1002, "3": 1003}`
-  - 键值对应推荐层级:1=直推,2=间推,3=三推
-  - 值为奖励组ID,用于配置固定金额奖励
-- `planting_reward_rates`: 种植收益分成比例,JSON格式:`{"1": 0.05, "2": 0.03, "3": 0.01}`
-  - 键值对应推荐层级:1=直推,2=间推,3=三推
-  - 值为分成比例,用于按比例分成
+- 推广收益奖励组配置:
+  - `promotion_direct_group`: 直推奖励组ID,0表示无奖励
+  - `promotion_indirect_group`: 间推奖励组ID,0表示无奖励
+  - `promotion_third_group`: 三推奖励组ID,0表示无奖励
+- 种植收益分成比例配置:
+  - `planting_direct_rate`: 直推分成比例,小数形式,如0.05表示5%
+  - `planting_indirect_rate`: 间推分成比例,小数形式,如0.03表示3%
+  - `planting_third_rate`: 三推分成比例,小数形式,如0.01表示1%
 
 ## 3. 索引设计说明
 

+ 38 - 17
app/Module/UrsPromotion/Models/UrsTalentConfig.php

@@ -13,8 +13,12 @@ use App\Module\UrsPromotion\Enums\UrsTalentLevel;
  * @property string $name 等级名称
  * @property int $direct_count_required 所需直推人数
  * @property int $promotion_count_required 所需团队总人数
- * @property array $promotion_reward_rates 推广收益分成比例配置
- * @property array $planting_reward_rates 种植收益分成比例配置
+ * @property int $promotion_direct_group 直推奖励组ID
+ * @property int $promotion_indirect_group 间推奖励组ID
+ * @property int $promotion_third_group 三推奖励组ID
+ * @property float $planting_direct_rate 直推分成比例
+ * @property float $planting_indirect_rate 间推分成比例
+ * @property float $planting_third_rate 三推分成比例
  * @property string|null $icon 等级图标
  * @property string|null $description 等级描述
  * @property int $sort_order 排序权重
@@ -38,8 +42,12 @@ class UrsTalentConfig extends ModelCore
         'name',
         'direct_count_required',
         'promotion_count_required',
-        'promotion_reward_groups',
-        'planting_reward_rates',
+        'promotion_direct_group',
+        'promotion_indirect_group',
+        'promotion_third_group',
+        'planting_direct_rate',
+        'planting_indirect_rate',
+        'planting_third_rate',
         'icon',
         'description',
         'sort_order',
@@ -54,8 +62,12 @@ class UrsTalentConfig extends ModelCore
         'level' => 'integer',
         'direct_count_required' => 'integer',
         'promotion_count_required' => 'integer',
-        'promotion_reward_groups' => 'array',
-        'planting_reward_rates' => 'array',
+        'promotion_direct_group' => 'integer',
+        'promotion_indirect_group' => 'integer',
+        'promotion_third_group' => 'integer',
+        'planting_direct_rate' => 'decimal:4',
+        'planting_indirect_rate' => 'decimal:4',
+        'planting_third_rate' => 'decimal:4',
         'sort_order' => 'integer',
         'status' => 'integer',
         'created_at' => 'datetime',
@@ -92,23 +104,28 @@ class UrsTalentConfig extends ModelCore
      */
     public function getPromotionRewardGroupId(int $relationLevel): ?int
     {
-        $groups = $this->promotion_reward_groups ?? [];
-        if (is_string($groups)) {
-            $groups = json_decode($groups, true) ?? [];
-        }
-        return $groups[$relationLevel] ?? null;
+        return match($relationLevel) {
+            1 => $this->promotion_direct_group ?: null,
+            2 => $this->promotion_indirect_group ?: null,
+            3 => $this->promotion_third_group ?: null,
+            default => null,
+        };
     }
 
     /**
      * 获取种植收益分成比例
-     * 
+     *
      * @param int $relationLevel 推荐层级 1:直推 2:间推 3:三推
      * @return float
      */
     public function getPlantingRewardRate(int $relationLevel): float
     {
-        $rates = $this->planting_reward_rates ?? [];
-        return (float)($rates[$relationLevel] ?? 0);
+        return match($relationLevel) {
+            1 => (float)$this->planting_direct_rate,
+            2 => (float)$this->planting_indirect_rate,
+            3 => (float)$this->planting_third_rate,
+            default => 0.0,
+        };
     }
 
     /**
@@ -118,17 +135,21 @@ class UrsTalentConfig extends ModelCore
      */
     public function setPromotionRewardGroups(array $groups): void
     {
-        $this->promotion_reward_groups = $groups;
+        $this->promotion_direct_group = $groups[1] ?? 0;
+        $this->promotion_indirect_group = $groups[2] ?? 0;
+        $this->promotion_third_group = $groups[3] ?? 0;
     }
 
     /**
      * 设置种植收益分成比例
-     * 
+     *
      * @param array $rates 分成比例配置 [1 => 0.05, 2 => 0.03, 3 => 0.01]
      */
     public function setPlantingRewardRates(array $rates): void
     {
-        $this->planting_reward_rates = $rates;
+        $this->planting_direct_rate = $rates[1] ?? 0;
+        $this->planting_indirect_rate = $rates[2] ?? 0;
+        $this->planting_third_rate = $rates[3] ?? 0;
     }
 
     /**