Sfoglia il codice sorgente

Merge branch 'dev-sc-260204' of shop/php into dev

shishichen 5 mesi fa
parent
commit
c19a219786

+ 9 - 83
app/common/dao/shared/SharedProsperityUserDao.php

@@ -3,6 +3,7 @@
3 3
 namespace app\common\dao\shared;
4 4
 
5 5
 use app\common\dao\BaseDao;
6
+use app\common\enum\CommonEnum;
6 7
 use app\common\model\BaseModel;
7 8
 use app\common\model\shared\SharedProsperityUser;
8 9
 use app\entity\data\shared\SharedProsperityUserEntity;
@@ -95,22 +96,27 @@ class SharedProsperityUserDao extends BaseDao
95 96
      * 递归获取团队所有下级用户ID(可选择是否包含自己)
96 97
      * @param int $userId
97 98
      * @param bool $includeSelf 是否包含自己
99
+     * @param int $hierarchy 层级限制,0表示不限制,>0表示限制层级
98 100
      * @return int[]
99 101
      */
100
-    public function getAllSubordinateUserIds(int $userId, bool $includeSelf = false): array
102
+    public function getAllSubordinateUserIds(int $userId, bool $includeSelf = false, int $hierarchy = CommonEnum::ORDER_EARNING_ALLOCATION['SharedProsperity']['Hierarchy']['code']): array
101 103
     {
102 104
         $allIds = [];
103 105
         try {
104 106
             /** @var SharedProsperityUser $model */
105 107
             $model = $this->getModel();
106
-            $getRecursiveChildren = function ($parentId) use (&$getRecursiveChildren, $model) {
108
+            $getRecursiveChildren = function ($parentId, $currentLevel = 1) use (&$getRecursiveChildren, $model, $hierarchy) {
109
+                // 如果设置了层级限制且当前层级超过限制,则停止递归
110
+                if ($hierarchy > 0 && $currentLevel > $hierarchy) {
111
+                    return [];
112
+                }
107 113
                 $children = $model::getDB()
108 114
                     ->where('parent_id', $parentId)
109 115
                     ->column('user_id');
110 116
                 $allChildren = [];
111 117
                 foreach ($children as $childId) {
112 118
                     $allChildren[] = $childId;
113
-                    $allChildren = array_merge($allChildren, $getRecursiveChildren($childId));
119
+                    $allChildren = array_merge($allChildren, $getRecursiveChildren($childId, $currentLevel + 1));
114 120
                 }
115 121
                 return $allChildren;
116 122
             };
@@ -171,86 +177,6 @@ class SharedProsperityUserDao extends BaseDao
171 177
     }
172 178
 
173 179
     /**
174
-     * 获取团队所有下级用户实体列表(不包括自己)
175
-     * @param int $userId
176
-     * @return SharedProsperityUserEntity[]
177
-     */
178
-    public function getTeamUserEntities(int $userId): array
179
-    {
180
-        $userIds = $this->getAllSubordinateUserIds($userId, false);
181
-        if (empty($userIds)) {
182
-            return [];
183
-        }
184
-        return $this->getSharedProsperityUserEntityListByUserIdList($userIds);
185
-    }
186
-
187
-    /**
188
-     * 获取团队所有下级用户实体列表(包括自己)
189
-     * @param int $userId
190
-     * @return SharedProsperityUserEntity[]
191
-     */
192
-    public function getTeamUserEntitiesWithSelf(int $userId): array
193
-    {
194
-        $userIds = $this->getAllSubordinateUserIds($userId, true);
195
-        if (empty($userIds)) {
196
-            return [];
197
-        }
198
-        return $this->getSharedProsperityUserEntityListByUserIdList($userIds);
199
-    }
200
-
201
-    /**
202
-     * 获取团队所有下级用户(包括分页,不包括自己)
203
-     * @param int $userId
204
-     * @param int $page
205
-     * @param int $pageSize
206
-     * @return array
207
-     */
208
-    public function getTeamUsers(int $userId, int $page = 1, int $pageSize = 10): array
209
-    {
210
-        $userIds = $this->getAllSubordinateUserIds($userId, false);
211
-        if (empty($userIds)) {
212
-            return ['data' => [], 'total' => 0, 'per_page' => $pageSize, 'current_page' => $page, 'last_page' => 1];
213
-        }
214
-        try {
215
-            /** @var SharedProsperityUser $model */
216
-            $model = $this->getModel();
217
-            $list = $model::getDB()
218
-                ->whereIn('user_id', $userIds)
219
-                ->paginate(['page' => $page, 'list_rows' => $pageSize])
220
-                ->toArray();
221
-            return $list;
222
-        } catch (\Exception $e) {
223
-            return ['data' => [], 'total' => 0, 'per_page' => $pageSize, 'current_page' => $page, 'last_page' => 1];
224
-        }
225
-    }
226
-
227
-    /**
228
-     * 获取团队所有下级用户(包括分页,包括自己)
229
-     * @param int $userId
230
-     * @param int $page
231
-     * @param int $pageSize
232
-     * @return array
233
-     */
234
-    public function getTeamUsersWithSelf(int $userId, int $page = 1, int $pageSize = 10): array
235
-    {
236
-        $userIds = $this->getAllSubordinateUserIds($userId, true);
237
-        if (empty($userIds)) {
238
-            return ['data' => [], 'total' => 0, 'per_page' => $pageSize, 'current_page' => $page, 'last_page' => 1];
239
-        }
240
-        try {
241
-            /** @var SharedProsperityUser $model */
242
-            $model = $this->getModel();
243
-            $list = $model::getDB()
244
-                ->whereIn('user_id', $userIds)
245
-                ->paginate(['page' => $page, 'list_rows' => $pageSize])
246
-                ->toArray();
247
-            return $list;
248
-        } catch (\Exception $e) {
249
-            return ['data' => [], 'total' => 0, 'per_page' => $pageSize, 'current_page' => $page, 'last_page' => 1];
250
-        }
251
-    }
252
-
253
-    /**
254 180
      * 批量更新
255 181
      * @param $dataList
256 182
      * @return array

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

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

+ 86 - 52
app/common/repositories/shared/SharedProsperityTaskRepository.php

@@ -24,7 +24,7 @@ class SharedProsperityTaskRepository extends BaseRepository
24 24
      * @throws \Exception
25 25
      * @author 史晨
26 26
      * @date 2026/2/10 10:10
27
-     * 合伙人分红
27
+     * 合伙人分红(加权分配)
28 28
      */
29 29
     public function partner($data)
30 30
     {
@@ -62,63 +62,43 @@ class SharedProsperityTaskRepository extends BaseRepository
62 62
 
63 63
         Db::startTrans();
64 64
         try {
65
-            foreach ($users as $key => $user) {
65
+            foreach ($users as $key => $userIds) {
66 66
                 if ($key == 0) {
67 67
                     continue;
68 68
                 }
69 69
                 $radio = $config[$key];
70
-                $userNum = count($user);
70
+                // 该等级总分红
71
+                $levelTotal = floor(($conPri * ($radio / 100)) * 100) / 100;
71 72
 
72
-                foreach ($user as $u) {
73
-                    // 计算每个用户分配的利润
74
-                    $userReward = floor(($conPri * ($radio / 100) / $userNum) * 100) / 100;
75
-                    // 记录日志
76
-                    $userData = $userDao->getWhere(['user_id' => $u])->toArray();
77
-                    $balanceDao->create([
78
-                        'user_id' => $u,
79
-                        'level' => $userData['partner_level'],
80
-                        'radio' => $radio,
81
-                        'total_profit' => $conPri,
82
-                        'old' => $userData['balance'],
83
-                        'number' => $userReward,
84
-                        'after' => $userData['balance'] + $userReward
85
-                    ]);
73
+                // 获取该等级所有用户的基本信息
74
+                $userInfos = $userDao->getSharedProsperityUserEntityListByUserIdList($userIds);
75
+                if (empty($userInfos)) {
76
+                    continue;
77
+                }
86 78
 
87
-                    $bill = [
88
-                        'uid' => $u,
89
-                        'link_id' => 0,//关联订单id
90
-                        'pm' => 1,//0:支出,1:获得
91
-                        'title' => '共富体系合伙人分佣',//账单标题
92
-                        'category' => 'now_money',//明细种类
93
-                        'type' => 'commission',//明细类型
94
-                        'number' => $userReward,//明细数字
95
-                        'balance' => 0,//剩余
96
-                        'mark' => '共富体系合伙人分佣明细入账',//备注
97
-                        'create_time' => date('Y-m-d H:i:s'),//添加时间
98
-                        'status' => 1,//0待确定,1有效,-1无效
99
-                        'commission_type' => CommonEnum::COMMISSION_TYPE['SHARED_PROSPERITY_PARTNER_COMMISSION']['code'],//佣金类型 1代理费 2 消费佣金 3直推奖√ 4辖区佣金\r\n5 养老金√ 6 广告费
100
-                        //7 跨店奖励√ 8平台奖励 9渠道商\r\n10 推荐创客收益√ 11分红奖金√ 12代理区域收益√ 13消费循环佣金
101
-                        //14-推荐代理收益√ 15邀请小区团长升级√ 16大v分享奖√ 17创客合伙人√ 18积分奖励√ 19创客补贴√’
102
-                        'order_sn' => 0,//订单号
103
-                        'tripartite' => 0,//
104
-                        'type_shop' => 0,//0平台1多多进宝2唯品会3苏宁易购4网易考拉5淘宝客6京东联盟7饿了么8线上
105
-                        'mer_id' => 0,//商户id
106
-                        'source' => 0,//1线下 0线上
107
-                        'order_type' => 1,//1自营  2 第三方  订单类型
108
-                        'is_red_brokerage' => 0,//1红积分对冲佣金 0正常佣金
109
-                        'gzc' => 3,//养老金是否已进入公证处log表0:未进入。1:已进入
110
-                        'take_time' => time(),//结算时间
111
-                        'district_id' => '',//
112
-                        'street_id' => '',//
113
-                        'month' => '',//月份
114
-
115
-
116
-                    ];
117
-
118
-                    Db::name('user_bill')->insert($bill);
119
-
120
-                    // 发放佣金
121
-                    $userDao->updateByUserId($u, ['balance' => ['inc', $userReward]]);
79
+                // 计算每个用户的团队业绩(实时统计)
80
+                $teamPvMap = [];
81
+                $totalTeamPv = 0;
82
+                foreach ($userInfos as $userInfo) {
83
+                    $teamPv = $userDao->getTeamPvSum($userInfo->getUserId());
84
+                    $teamPvMap[$userInfo->getUserId()] = $teamPv;
85
+                    $totalTeamPv += $teamPv;
86
+                }
87
+                // 如果总团队业绩为0,则平均分配
88
+                if ($totalTeamPv == 0) {
89
+                    $userNum = count($userInfos);
90
+                    $eachReward = floor(($levelTotal / $userNum) * 100) / 100;
91
+                    foreach ($userInfos as $userInfo) {
92
+                        $userReward = $eachReward;
93
+                        $this->distributeReward($userInfo, $userReward, $radio, $levelTotal, $balanceDao, $userDao);
94
+                    }
95
+                } else {
96
+                    // 加权分配
97
+                    foreach ($userInfos as $userInfo) {
98
+                        $weight = $teamPvMap[$userInfo->getUserId()] / $totalTeamPv;
99
+                        $userReward = floor($levelTotal * $weight * 100) / 100;
100
+                        $this->distributeReward($userInfo, $userReward, $radio, $levelTotal, $balanceDao, $userDao);
101
+                    }
122 102
                 }
123 103
             }
124 104
             Db::commit();
@@ -129,6 +109,60 @@ class SharedProsperityTaskRepository extends BaseRepository
129 109
     }
130 110
 
131 111
     /**
112
+     * 分发奖励(记录日志和更新余额)
113
+     * @param $userInfo
114
+     * @param $userReward
115
+     * @param $radio
116
+     * @param $levelTotal
117
+     * @param $balanceDao
118
+     * @param $userDao
119
+     * @return void
120
+     */
121
+    private function distributeReward($userInfo, $userReward, $radio, $levelTotal, $balanceDao, $userDao)
122
+    {
123
+        $userId = $userInfo->getUserId();
124
+        $balanceDao->create([
125
+            'user_id' => $userId,
126
+            'level' => $userInfo->getPartnerLevel(),
127
+            'radio' => $radio,
128
+            'total_profit' => $levelTotal,
129
+            'old' => $userInfo->getBalance(),
130
+            'number' => $userReward,
131
+            'after' => $userInfo->getBalance() + $userReward
132
+        ]);
133
+
134
+        $bill = [
135
+            'uid' => $userId,
136
+            'link_id' => 0,
137
+            'pm' => 1,
138
+            'title' => '共富体系合伙人分佣(加权)',
139
+            'category' => 'now_money',
140
+            'type' => 'commission',
141
+            'number' => $userReward,
142
+            'balance' => 0,
143
+            'mark' => '共富体系合伙人分佣明细入账(加权)',
144
+            'create_time' => date('Y-m-d H:i:s'),
145
+            'status' => 1,
146
+            'commission_type' => CommonEnum::COMMISSION_TYPE['SHARED_PROSPERITY_PARTNER_COMMISSION']['code'],
147
+            'order_sn' => 0,
148
+            'tripartite' => 0,
149
+            'type_shop' => 0,
150
+            'mer_id' => 0,
151
+            'source' => 0,
152
+            'order_type' => 1,
153
+            'is_red_brokerage' => 0,
154
+            'gzc' => 3,
155
+            'take_time' => time(),
156
+            'district_id' => '',
157
+            'street_id' => '',
158
+            'month' => '',
159
+        ];
160
+
161
+        Db::name('user_bill')->insert($bill);
162
+        $userDao->updateByUserId($userId, ['balance' => ['inc', $userReward]]);
163
+    }
164
+
165
+    /**
132 166
      * @param $data
133 167
      * @return true
134 168
      * @throws \think\db\exception\DataNotFoundException

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

@@ -4,6 +4,7 @@ namespace app\common\repositories\shared;
4 4
 
5 5
 use app\common\dao\shared\SharedProsperityUserDao;
6 6
 use app\common\dao\user\UserDao;
7
+use app\common\enum\CommonEnum;
7 8
 use app\common\repositories\BaseRepository;
8 9
 use app\entity\data\shared\SharedProsperityRecommendConfigEntity;
9 10
 use app\entity\data\shared\SharedProsperityUserEntity;