ソースを参照

feat(shared-prosperity): 添加共富分红功能实现

- 新增共富分红路由接口 share_prosperity_partner
- 创建 SharedProsperityBalanceEntity 实体类用于存储余额信息
- 添加 SharedProsperityBalanceLog 模型和数据访问对象
- 扩展 SharedProsperityRecommendConfigDao 支持非实体返回
- 在 SharedProsperityRewardRecordDao 中增加按时间范围查询方法
- 实现 SharedProsperityTaskRepository 处理合伙人分红逻辑
- 添加 SharedProsperityUserDao 用户更新和等级分组功能
- 扩展 StoreOrderDao 支持共富体系订单查询
- 创建 Task 控制器提供合伙人分红 API 接口
shichen 5 ヶ月 前
コミット
ecb15bbbcc

+ 18 - 0
app/common/dao/shared/SharedProsperityBalanceLogDao.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\SharedProsperityBalanceLog;
8
+
9
+class SharedProsperityBalanceLogDao extends BaseDao
10
+{
11
+    /**
12
+     * @return BaseModel
13
+     */
14
+    protected function getModel(): string
15
+    {
16
+        return SharedProsperityBalanceLog::class;
17
+    }
18
+}

+ 4 - 2
app/common/dao/shared/SharedProsperityRecommendConfigDao.php

@@ -20,7 +20,7 @@ class SharedProsperityRecommendConfigDao extends BaseDao
20 20
      * @date 2026/2/5 14:43
21 21
      * 获取推荐配置
22 22
      */
23
-    public function getConfigList(): array
23
+    public function getConfigList($idEntity = true): array
24 24
     {
25 25
         $entityList = [];
26 26
         try {
@@ -29,10 +29,12 @@ class SharedProsperityRecommendConfigDao extends BaseDao
29 29
             $dataRes = $model::getDB()
30 30
                 ->select()
31 31
                 ->toArray();
32
-            if (!empty($dataRes)) {
32
+            if (!empty($dataRes) && $idEntity) {
33 33
                 $entityList = array_map(function ($data) {
34 34
                     return SharedProsperityRecommendConfigEntity::newInstance($data);
35 35
                 }, $dataRes);
36
+            }else{
37
+                $entityList = $dataRes;
36 38
             }
37 39
         } catch (\Exception $e) {
38 40
         }

+ 21 - 0
app/common/dao/shared/SharedProsperityRewardRecordDao.php

@@ -176,4 +176,25 @@ class SharedProsperityRewardRecordDao extends BaseDao
176 176
             return 0.0;
177 177
         }
178 178
     }
179
+
180
+    /**
181
+     * @param $start
182
+     * @param $end
183
+     * @return array
184
+     * @throws DataNotFoundException
185
+     * @throws DbException
186
+     * @throws ModelNotFoundException
187
+     * @author 史晨
188
+     * @date 2026/2/10 13:56
189
+     * 时间范围搜索
190
+     */
191
+    public function getListByTime($start, $end)
192
+    {
193
+        $model = $this->getModel();
194
+        return $model::getDB()
195
+            ->where('take_time', '>', $start)
196
+            ->where('take_time', '<', $end)
197
+            ->select()
198
+            ->toArray();
199
+    }
179 200
 }

+ 45 - 0
app/common/dao/shared/SharedProsperityUserDao.php

@@ -238,4 +238,49 @@ class SharedProsperityUserDao extends BaseDao
238 238
             return [];
239 239
         }
240 240
     }
241
+
242
+    /**
243
+     * 根据用户ID更新记录
244
+     * @param int $userId
245
+     * @param array $data
246
+     * @return int
247
+     */
248
+    public function updateByUserId(int $userId, array $data): int
249
+    {
250
+        try {
251
+            /** @var SharedProsperityUser $model */
252
+            $model = $this->getModel();
253
+            return $model::getDB()->where('user_id', $userId)->update($data);
254
+        } catch (\Exception $e) {
255
+            return 0;
256
+        }
257
+    }
258
+
259
+    /**
260
+     * 按合伙人等级分组获取用户
261
+     * @return array [partner_level => [user_id1, user_id2, ...]]
262
+     */
263
+    public function getUserGroupByPartnerLevel(): array
264
+    {
265
+        try {
266
+            /** @var SharedProsperityUser $model */
267
+            $model = $this->getModel();
268
+            $dataRes = $model::getDB()
269
+                ->field('partner_level, user_id')
270
+                ->select()
271
+                ->toArray();
272
+            
273
+            $grouped = [];
274
+            foreach ($dataRes as $row) {
275
+                $level = $row['partner_level'];
276
+                if (!isset($grouped[$level])) {
277
+                    $grouped[$level] = [];
278
+                }
279
+                $grouped[$level][] = $row['user_id'];
280
+            }
281
+            return $grouped;
282
+        } catch (\Exception $e) {
283
+            return [];
284
+        }
285
+    }
241 286
 }

+ 15 - 0
app/common/dao/store/order/StoreOrderDao.php

@@ -756,4 +756,19 @@ class StoreOrderDao extends BaseDao
756 756
             return [];
757 757
         }
758 758
     }
759
+
760
+    public function shareProsperityOrder($merId, $start, $end)
761
+    {
762
+        /** @var StoreOrder $model */
763
+        $model = $this->getModel();
764
+        return  $model::getDB()
765
+            ->where('mer_id', $merId)
766
+            ->where('pay_time', '>', 0)
767
+            ->where('con_pri', '>', 0)
768
+            ->where('pay_time', '>', $start)
769
+            ->where('pay_time', '<', $end)
770
+            ->field('order_id,order_sn,pay_time,con_pri')
771
+            ->select()
772
+            ->toArray();
773
+    }
759 774
 }

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

@@ -0,0 +1,17 @@
1
+<?php
2
+namespace app\common\model\shared;
3
+
4
+use app\common\model\BaseModel;
5
+
6
+class SharedProsperityBalanceLog 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_balance_log';
16
+    }
17
+}

+ 16 - 0
app/common/repositories/shared/SharedProsperityRewardRecordRepository.php

@@ -353,4 +353,20 @@ class SharedProsperityRewardRecordRepository extends BaseRepository
353 353
             'small_department_contribution' => (float) $smallDepartmentContribution,
354 354
         ];
355 355
     }
356
+
357
+    /**
358
+     * @param $startTime
359
+     * @param $endTime
360
+     * @return array
361
+     * @throws DbException
362
+     * @throws \think\db\exception\DataNotFoundException
363
+     * @throws \think\db\exception\ModelNotFoundException
364
+     * @author 史晨
365
+     * @date 2026/2/10 14:07
366
+     * 获取时间段内奖励列表
367
+     */
368
+    public function getListByTime($startTime, $endTime)
369
+    {
370
+        return $this->dao->getListByTime($startTime, $endTime);
371
+    }
356 372
 }

+ 126 - 0
app/common/repositories/shared/SharedProsperityTaskRepository.php

@@ -0,0 +1,126 @@
1
+<?php
2
+
3
+namespace app\common\repositories\shared;
4
+
5
+use app\common\dao\shared\SharedProsperityBalanceLogDao;
6
+use app\common\dao\shared\SharedProsperityRecommendConfigDao;
7
+use app\common\dao\shared\SharedProsperityUserDao;
8
+use app\common\dao\store\order\StoreOrderDao;
9
+use app\common\enum\CommonEnum;
10
+use app\common\repositories\BaseRepository;
11
+use think\facade\Db;
12
+
13
+class SharedProsperityTaskRepository extends BaseRepository
14
+{
15
+    const PARTNER_RADIO = 1;
16
+
17
+    /**
18
+     * @param $data
19
+     * @return void
20
+     * @throws \Exception
21
+     * @author 史晨
22
+     * @date 2026/2/10 10:10
23
+     * 合伙人分红
24
+     */
25
+    public function partner($data)
26
+    {
27
+        // 获取时间范围内订单
28
+        $merId = CommonEnum::DESIGN_MERCHANT_ID['SharedProsperity']['code'];
29
+        $start = $data['startTime'];
30
+        $end = $data['endTime'];
31
+
32
+        // 获取pv
33
+        /** @var SharedProsperityRewardRecordRepository $rewardRecordRepository */
34
+        $rewardRecordRepository = app()->make(SharedProsperityRewardRecordRepository::class);
35
+        $orderList = $rewardRecordRepository->getListByTime($start, $end);
36
+        if (empty($orderList)) {
37
+            throw new \Exception('没有订单');
38
+        }
39
+        // 计算可分配利润
40
+        $totalConPri = array_sum(array_column($orderList, 'number'));
41
+        $conPri = floor($totalConPri * self::PARTNER_RADIO * 100) / 100;
42
+
43
+        // 获取合伙人分配规则
44
+        /** @var SharedProsperityRecommendConfigDao $configDao */
45
+        $configDao = app()->make(SharedProsperityRecommendConfigDao::class);
46
+        $config = $configDao->getConfigList(false);
47
+        $config = array_column($config, 'profit_ratio', 'level');
48
+        if (empty($config)) {
49
+            throw new \Exception('没有合伙人分配规则');
50
+        }
51
+        // 获取合伙人
52
+        /** @var SharedProsperityUserDao $userDao */
53
+        $userDao = app()->make(SharedProsperityUserDao::class);
54
+        $users = $userDao->getUserGroupByPartnerLevel();
55
+
56
+        /** @var SharedProsperityBalanceLogDao $balanceDao */
57
+        $balanceDao = app()->make(SharedProsperityBalanceLogDao::class);
58
+
59
+        Db::startTrans();
60
+        try {
61
+            foreach ($users as $key => $user) {
62
+                if ($key == 0) {
63
+                    continue;
64
+                }
65
+                $radio = $config[$key];
66
+                $userNum = count($user);
67
+
68
+                foreach ($user as $u) {
69
+                    // 计算每个用户分配的利润
70
+                    $userReward = floor(($conPri * ($radio / 100) / $userNum) * 100) / 100;
71
+                    // 记录日志
72
+                    $userData = $userDao->getWhere(['user_id' => $u])->toArray();
73
+                    $balanceDao->create([
74
+                        'user_id' => $u,
75
+                        'level' => $userData['partner_level'],
76
+                        'radio' => $radio,
77
+                        'total_profit' => $conPri,
78
+                        'old' => $userData['balance'],
79
+                        'number' => $userReward,
80
+                        'after' => $userData['balance'] + $userReward
81
+                    ]);
82
+
83
+                    $bill = [
84
+                        'uid' => $u,
85
+                        'link_id' => 0,//关联订单id
86
+                        'pm' => 1,//0:支出,1:获得
87
+                        'title' => '共富体系合伙人分佣',//账单标题
88
+                        'category' => 'now_money',//明细种类
89
+                        'type' => 'commission',//明细类型
90
+                        'number' => $userReward,//明细数字
91
+                        'balance' => 0,//剩余
92
+                        'mark' => '共富体系合伙人分佣明细入账',//备注
93
+                        'create_time' => date('Y-m-d H:i:s'),//添加时间
94
+                        'status' => 1,//0待确定,1有效,-1无效
95
+                        'commission_type' => CommonEnum::COMMISSION_TYPE['SHARED_PROSPERITY_PARTNER_COMMISSION']['code'],//佣金类型 1代理费 2 消费佣金 3直推奖√ 4辖区佣金\r\n5 养老金√ 6 广告费
96
+                        //7 跨店奖励√ 8平台奖励 9渠道商\r\n10 推荐创客收益√ 11分红奖金√ 12代理区域收益√ 13消费循环佣金
97
+                        //14-推荐代理收益√ 15邀请小区团长升级√ 16大v分享奖√ 17创客合伙人√ 18积分奖励√ 19创客补贴√’
98
+                        'order_sn' => 0,//订单号
99
+                        'tripartite' => 0,//
100
+                        'type_shop' => 0,//0平台1多多进宝2唯品会3苏宁易购4网易考拉5淘宝客6京东联盟7饿了么8线上
101
+                        'mer_id' => 0,//商户id
102
+                        'source' => 0,//1线下 0线上
103
+                        'order_type' => 1,//1自营  2 第三方  订单类型
104
+                        'is_red_brokerage' => 0,//1红积分对冲佣金 0正常佣金
105
+                        'gzc' => 3,//养老金是否已进入公证处log表0:未进入。1:已进入
106
+                        'take_time' => time(),//结算时间
107
+                        'district_id' => '',//
108
+                        'street_id' => '',//
109
+                        'month' => '',//月份
110
+
111
+
112
+                    ];
113
+
114
+                    Db::name('user_bill')->insert($bill);
115
+
116
+                    // 发放佣金
117
+                    $userDao->updateByUserId($u, ['balance' => ['inc', $userReward]]);
118
+                }
119
+            }
120
+            Db::commit();
121
+        } catch (\Exception $e) {
122
+            Db::rollback();
123
+            throw new \Exception($e->getMessage());
124
+        }
125
+    }
126
+}

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

@@ -0,0 +1,17 @@
1
+<?php
2
+
3
+namespace app\controller\api\shareProsperity;
4
+use app\common\repositories\shared\SharedProsperityTaskRepository;
5
+
6
+class Task
7
+{
8
+    // 合伙人分红
9
+    public function partnerProfits()
10
+    {
11
+        $get = app()->request->get();
12
+        /** @var SharedProsperityTaskRepository $repository */
13
+        $repository = app()->make(SharedProsperityTaskRepository::class);
14
+        $repository->partner($get);
15
+        return app('json')->success();
16
+    }
17
+}

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

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

+ 3 - 0
route/api.php

@@ -1378,3 +1378,6 @@ Route::group('/pages', function () {
1378 1378
     });
1379 1379
 })->middleware(\app\common\middleware\InstallMiddleware::class)
1380 1380
     ->middleware(\app\common\middleware\CheckSiteOpenMiddleware::class);
1381
+
1382
+// 共富分红
1383
+Route::get('share_prosperity_partner', 'api.shareProsperity.Task/partnerProfits');