Răsfoiți Sursa

feat(points): 积分功能大升级

- 重构 UserPointEntity 类,增加积分变更方法,支持多种变更类型
- 新增 UserPointTypeEnum 枚举,定义积分变更类型
- 创建 UserPointLogModel,用于记录积分变更日志
- 更新 UserPointModel,调整积分数据结构
- 编写单元测试,验证积分变更功能- 添加积分业务类型配置
runphp 6 luni în urmă
părinte
comite
5621749542

+ 53 - 3
config.php

@@ -1,6 +1,56 @@
 <?php
 declare(strict_types=1);
-
 return [
-
-];
+    [
+        'type' => 'group',
+        'field' => 'biz_type',
+        'value' => [
+            [
+                'label' => '订单',
+                'value' => 1
+            ],
+            [
+                'label' => '对冲',
+                'value' => 2
+            ],
+        ],
+        'title' => '积分的业务类型',
+        'info' => '',
+        '$required' => false,
+        'display' => true,
+        'hidden' => false,
+        'props' => [
+            'rule' => [
+                [
+                    'type' => 'input',
+                    'field' => 'label',
+                    'title' => '名称',
+                    'info' => '',
+                    '$required' => false,
+                    '_fc_id' => 'id_F599mdzkoghraic',
+                    'name' => 'ref_Fuy7mdzkoghrajc',
+                    'display' => true,
+                    'hidden' => false,
+                    '_fc_drag_tag' => 'input'
+                ],
+                [
+                    'type' => 'inputNumber',
+                    'field' => 'value',
+                    'title' => '值',
+                    'info' => '',
+                    '$required' => false,
+                    '_fc_id' => 'id_Fdewmdzkonjkalc',
+                    'name' => 'ref_Fj6nmdzkonjkamc',
+                    'display' => true,
+                    'hidden' => false,
+                    '_fc_drag_tag' => 'inputNumber'
+                ]
+            ]
+        ],
+        '_fc_id' => 'id_Fitzmdzknngkafc',
+        'name' => 'ref_Fug8mdzknngkagc',
+        'display' => true,
+        'hidden' => false,
+        '_fc_drag_tag' => 'group'
+    ]
+];

+ 26 - 5
src/Entity/UserPointEntity.php

@@ -3,22 +3,43 @@ declare(strict_types=1);
 namespace SixShop\Points\Entity;
 
 use SixShop\Core\Entity\BaseEntity;
-use SixShop\Points\Model\UserPointModel;
+use SixShop\Points\Enum\UserPointTypeEnum;
+use SixShop\Points\Model\UserPointLogModel;
 
 class UserPointEntity extends BaseEntity
 {
-    public function add(int $userID, int $point): UserPointModel
+    public function change(
+        int $userID,
+        int $point,
+        UserPointTypeEnum $type,
+        int $bizType,
+        int $bizID,
+        string $remark,
+        int $freezePoint = 0,
+    ): self
     {
         $userPoint = $this->where('user_id', $userID)->findOrEmpty();
         if ($userPoint->isEmpty()) {
-            $userPoint = $this->create([
+            $userPoint->save([
                 'user_id' => $userID,
                 'point' => $point,
+                'freeze_point' => $freezePoint,
             ]);
         } else {
-            $userPoint->inc('point', $point)->save();
+            $userPoint->inc('point', $point)
+                ->inc('freeze_point', $freezePoint)->save();
         }
-        // tood log
+        UserPointLogModel::create( [
+            'user_id' => $userID,
+            'point' => $point,
+            'type' => $type,
+            'biz_type' => $bizType,
+            'biz_id' => $bizID,
+            'remark' => $remark,
+            'freeze_point' => $freezePoint,
+            'after_point' => $userPoint->point,
+            'after_freeze_point' => $userPoint->freeze_point,
+        ]);
         return $userPoint;
     }
 }

+ 14 - 0
src/Enum/UserPointTypeEnum.php

@@ -0,0 +1,14 @@
+<?php
+declare(strict_types=1);
+namespace SixShop\Points\Enum;
+enum UserPointTypeEnum: int
+{
+    // 1 增加
+    case INCREASE = 1;
+    // 2 减少
+    case DECREASE = 2;
+
+    // 3 对冲
+    case OFFSET = 3;
+
+}

+ 4 - 1
src/Model/UserPointLogModel.php

@@ -2,6 +2,7 @@
 declare(strict_types=1);
 namespace SixShop\Points\Model;
 
+use SixShop\Points\Enum\UserPointTypeEnum;
 use think\Model;
 
 class UserPointLogModel extends Model
@@ -10,7 +11,9 @@ class UserPointLogModel extends Model
     {
         return [
             'name' => 'extension_user_point_log',
+            'type' => [
+                'type' => UserPointTypeEnum::class,
+            ]
         ];
-        
     }
 }

+ 1 - 1
src/Model/UserPointModel.php

@@ -10,7 +10,7 @@ class UserPointModel extends Model
     {
         return [
             'name' => 'extension_user_point',
-            'pk' => 'user_id',
+            'pk' => 'user_id'
         ];
     }
 }

+ 11 - 1
test/Entity/UserPointEntityTest.php

@@ -3,6 +3,8 @@ declare(strict_types=1);
 namespace SixShop\Points\Entity;
 
 use PHPUnit\Framework\TestCase;
+use SixShop\Points\Enum\UserPointTypeEnum;
+use SixShop\Points\Model\UserPointModel;
 
 class UserPointEntityTest extends TestCase
 {
@@ -17,6 +19,14 @@ class UserPointEntityTest extends TestCase
      */
     public  function testAdd()
     {
-        $this->userPointEntity->add(1, 100);
+        UserPointModel::where('user_id', 1)->delete();
+        $result = $this->userPointEntity->change(1, 100, UserPointTypeEnum::INCREASE, 1, 1, '测试添加积分');
+        $this->assertInstanceOf(UserPointEntity::class, $result);
+        $this->assertEquals(100, $result->point);
+        $this->assertEquals(1, $result->user_id);
+        $result = $this->userPointEntity->change(1, 100, UserPointTypeEnum::INCREASE, 1, 1, '测试添加积分');
+        $this->assertInstanceOf(UserPointEntity::class, $result);
+        $this->assertEquals(200, $result->point);
+        $this->assertEquals(1, $result->user_id);
     }
 }