Explorar el Código

feat(api): 添加共富感恩奖功能

- 新增共富感恩奖API路由接口
- 在CommonEnum中添加感恩奖分配比例配置
- 创建SharedProsperityPointEntity实体类用于积分数据管理
- 新增SharedProsperityPointLog模型和数据访问层
- 实现感恩奖计算和发放逻辑,按10%比例向上级用户发放积分
- 添加通过用户ID更新数据的方法
- 完善事务处理确保数据一致性
shichen hace 5 meses
padre
commit
d177e5277c

+ 18 - 0
app/common/dao/shared/SharedProsperityPointLogDao.php

@@ -0,0 +1,18 @@
1
+<?php
2
+
3
+namespace app\common\dao\shared;
4
+
5
+use app\common\dao\BaseDao;
6
+use app\common\model\BaseModel;
7
+use app\common\model\shared\SharedProsperityPointLog;
8
+
9
+class SharedProsperityPointLogDao extends BaseDao
10
+{
11
+    /**
12
+     * @return BaseModel
13
+     */
14
+    protected function getModel(): string
15
+    {
16
+        return SharedProsperityPointLog::class;
17
+    }
18
+}

+ 2 - 1
app/common/enum/CommonEnum.php

@@ -279,7 +279,8 @@ class CommonEnum
279 279
                 'ConsumerStocks' => [
280 280
                     'Ratio' => ['code' => 0.5, 'name' => '在共富区推广者获得购买商品赠送商品额50%的消费股']
281 281
                 ]
282
-            ]
282
+            ],
283
+            'Thank' => ['code' => 0.1, 'name' => '感恩奖分配比例,10%']
283 284
         ]
284 285
     ];
285 286
 

+ 17 - 0
app/common/model/shared/SharedProsperityPointLog.php

@@ -0,0 +1,17 @@
1
+<?php
2
+namespace app\common\model\shared;
3
+
4
+use app\common\model\BaseModel;
5
+
6
+class SharedProsperityPointLog extends BaseModel
7
+{
8
+    public static function tablePk(): ?string
9
+    {
10
+        return 'id';
11
+    }
12
+
13
+    public static function tableName(): string
14
+    {
15
+        return 'shared_prosperity_point_log';
16
+    }
17
+}

+ 90 - 1
app/common/repositories/shared/SharedProsperityTaskRepository.php

@@ -3,12 +3,14 @@
3 3
 namespace app\common\repositories\shared;
4 4
 
5 5
 use app\common\dao\shared\SharedProsperityBalanceLogDao;
6
+use app\common\dao\shared\SharedProsperityPointLogDao;
6 7
 use app\common\dao\shared\SharedProsperityRecommendConfigDao;
7 8
 use app\common\dao\shared\SharedProsperityUserDao;
8
-use app\common\dao\store\order\StoreOrderDao;
9 9
 use app\common\enum\CommonEnum;
10 10
 use app\common\repositories\BaseRepository;
11
+use think\Collection;
11 12
 use think\facade\Db;
13
+use think\facade\Log;
12 14
 
13 15
 class SharedProsperityTaskRepository extends BaseRepository
14 16
 {
@@ -123,4 +125,91 @@ class SharedProsperityTaskRepository extends BaseRepository
123 125
             throw new \Exception($e->getMessage());
124 126
         }
125 127
     }
128
+
129
+    /**
130
+     * @param $data
131
+     * @return true
132
+     * @throws \think\db\exception\DataNotFoundException
133
+     * @throws \think\db\exception\DbException
134
+     * @throws \think\db\exception\ModelNotFoundException
135
+     * @author 史晨
136
+     * @date 2026/2/10 15:22
137
+     * 感恩奖
138
+     */
139
+    public function thank($data)
140
+    {
141
+        // 获取时间范围内订单
142
+        $start = $data['startTime'];
143
+        $end = $data['endTime'];
144
+
145
+        // 获取pv
146
+        /** @var SharedProsperityRewardRecordRepository $rewardRecordRepository */
147
+        $rewardRecordRepository = app()->make(SharedProsperityRewardRecordRepository::class);
148
+        $rewardList = $rewardRecordRepository->getListByTime($start, $end);
149
+        if (empty($rewardList)) {
150
+            throw new \Exception('没有订单');
151
+        }
152
+
153
+        // user_id分区求和 [id]=>[number]
154
+        $groupedResult = array_reduce($rewardList, function ($carry, $item) {
155
+            $userId = $item['user_id'];
156
+            $number = floatval($item['number']);
157
+
158
+            if (!isset($carry[$userId])) {
159
+                $carry[$userId] = 0;
160
+            }
161
+
162
+            $carry[$userId] += $number;
163
+
164
+            return $carry;
165
+        }, []);
166
+
167
+        if (empty($groupedResult)) {
168
+            throw new \Exception('分组求和错误');
169
+        }
170
+
171
+        /** @var SharedProsperityUserRepository $userRepository */
172
+        $userRepository = app()->make(SharedProsperityUserRepository::class);
173
+
174
+        /** @var SharedProsperityPointLogDao $balanceDao */
175
+        $logDao = app()->make(SharedProsperityPointLogDao::class);
176
+
177
+        DB::startTrans();
178
+        try {
179
+            foreach ($groupedResult as $key => $value) {
180
+                // 获取感恩奖比例
181
+                $radio = CommonEnum::ORDER_EARNING_ALLOCATION['SharedProsperity']['Thank']['code'];
182
+                // 计算积分
183
+                $points = floor($value * $radio * 100) / 100;
184
+                // 获取上级
185
+                $userData = $userRepository->getSharedProsperityUserEntityByUserId($key);
186
+                $parentId = $userData->getParentId();
187
+                if ($parentId == 0) {
188
+                    Log::info('共富感恩奖发放:' . $key . '的上级是0,不发放感恩奖');
189
+                }
190
+                // 给上级发积分,记录日志
191
+                if ($parentId > 0) {
192
+                    $parentData = $userRepository->getSharedProsperityUserEntityByUserId($parentId);
193
+                    // 记录日志
194
+                    $logDao->create([
195
+                        'user_id' => $parentId,
196
+                        'total' => $value,
197
+                        'radio' => $radio,
198
+                        'old' => $parentData->getPoints(),
199
+                        'number' => $points,
200
+                        'after' => $parentData->getPoints() + $points
201
+                    ]);
202
+
203
+                    // 发放积分
204
+                    $userRepository->updateByUserId($parentId, ['points' => ['inc', $points]]);
205
+                }
206
+            }
207
+
208
+            Db::commit();
209
+            return true;
210
+        } catch (\Exception $e) {
211
+            DB::rollback();
212
+            throw new \Exception($e->getMessage());
213
+        }
214
+    }
126 215
 }

+ 9 - 0
app/common/repositories/shared/SharedProsperityUserRepository.php

@@ -90,6 +90,15 @@ class SharedProsperityUserRepository extends BaseRepository
90 90
         }
91 91
     }
92 92
 
93
+    public function updateByUserId($userId,$data)
94
+    {
95
+        try {
96
+            return $this->dao->updateByUserId($userId, $data);
97
+        } catch (DbException $e) {
98
+            return 0;
99
+        }
100
+    }
101
+
93 102
     /**
94 103
      * @param SharedProsperityUserEntity $sharedProsperityUserEntity
95 104
      * @return SharedProsperityUserEntity

+ 9 - 0
app/controller/api/shareProsperity/Task.php

@@ -14,4 +14,13 @@ class Task
14 14
         $repository->partner($get);
15 15
         return app('json')->success();
16 16
     }
17
+
18
+    public function thankProfits()
19
+    {
20
+        $get = app()->request->get();
21
+        /** @var SharedProsperityTaskRepository $repository */
22
+        $repository = app()->make(SharedProsperityTaskRepository::class);
23
+        $repository->thank($get);
24
+        return app('json')->success();
25
+    }
17 26
 }

+ 118 - 0
app/entity/data/shared/SharedProsperityPointEntity.php

@@ -0,0 +1,118 @@
1
+<?php
2
+
3
+namespace app\entity\data\shared;
4
+
5
+use app\entity\data\DataEntity;
6
+
7
+class SharedProsperityPointEntity extends DataEntity
8
+{
9
+    public $id = null;  // 主键
10
+    public $userId = null;  // 用户id
11
+    public $total = null;  // 总计
12
+    public $radio = null;  // 比例
13
+    public $old = null;  // 变更前佣金
14
+    public $number = null;  // 操作佣金
15
+    public $after = null;  // 变更后佣金
16
+    public $pm = null;  // 类型 1增加
17
+    public $createTime = null;  // 创建时间
18
+
19
+    public function getId()
20
+    {
21
+        return $this->id;
22
+    }
23
+
24
+    public function setId($id): SharedProsperityPointEntity
25
+    {
26
+        $this->id = $id;
27
+        return $this;
28
+    }
29
+
30
+    public function getUserId()
31
+    {
32
+        return $this->userId;
33
+    }
34
+
35
+    public function setUserId($userId): SharedProsperityPointEntity
36
+    {
37
+        $this->userId = $userId;
38
+        return $this;
39
+    }
40
+
41
+    public function getTotal()
42
+    {
43
+        return $this->total;
44
+    }
45
+
46
+    public function setTotal($total): SharedProsperityPointEntity
47
+    {
48
+        $this->total = $total;
49
+        return $this;
50
+    }
51
+
52
+    public function getRadio()
53
+    {
54
+        return $this->radio;
55
+    }
56
+
57
+    public function setRadio($radio): SharedProsperityPointEntity
58
+    {
59
+        $this->radio = $radio;
60
+        return $this;
61
+    }
62
+
63
+    public function getOld()
64
+    {
65
+        return $this->old;
66
+    }
67
+
68
+    public function setOld($old): SharedProsperityPointEntity
69
+    {
70
+        $this->old = $old;
71
+        return $this;
72
+    }
73
+
74
+    public function getNumber()
75
+    {
76
+        return $this->number;
77
+    }
78
+
79
+    public function setNumber($number): SharedProsperityPointEntity
80
+    {
81
+        $this->number = $number;
82
+        return $this;
83
+    }
84
+
85
+    public function getAfter()
86
+    {
87
+        return $this->after;
88
+    }
89
+
90
+    public function setAfter($after): SharedProsperityPointEntity
91
+    {
92
+        $this->after = $after;
93
+        return $this;
94
+    }
95
+
96
+    public function getPm()
97
+    {
98
+        return $this->pm;
99
+    }
100
+
101
+    public function setPm($pm): SharedProsperityPointEntity
102
+    {
103
+        $this->pm = $pm;
104
+        return $this;
105
+    }
106
+
107
+    public function getCreateTime()
108
+    {
109
+        return $this->createTime;
110
+    }
111
+
112
+    public function setCreateTime($createTime): SharedProsperityPointEntity
113
+    {
114
+        $this->createTime = $createTime;
115
+        return $this;
116
+    }
117
+
118
+}

+ 2 - 0
route/api.php

@@ -1381,3 +1381,5 @@ Route::group('/pages', function () {
1381 1381
 
1382 1382
 // 共富分红
1383 1383
 Route::get('share_prosperity_partner', 'api.shareProsperity.Task/partnerProfits');
1384
+// 共富感恩奖
1385
+Route::get('share_prosperity_thank', 'api.shareProsperity.Task/thankProfits');