WIN-2308041133\Administrator hace 2 meses
padre
commit
15bb2b4d9d

+ 17 - 0
app/api/controller/v1/user/UserBrokerageController.php

@@ -42,4 +42,21 @@ class UserBrokerageController
         $uid = (int)$request->uid();
         return app('json')->success($this->services->brokerageRank($uid, $data['type']));
     }
+
+    /**
+     * 佣金互转
+     * @param Request $request
+     * @return mixed
+     */
+    public function transfer(Request $request)
+    {
+        $data = $request->getMore([
+            ['money', ''],
+            ['to_uid', '']
+        ]);
+        $uid = (int)$request->uid();
+        return app('json')->success($this->services->transferBrokerage($uid, $data['to_uid'], $data['money']));
+    }
+
+
 }

+ 1 - 0
app/api/route/v1.php

@@ -336,6 +336,7 @@ Route::group(function () {
         /** 佣金相关 */
         Route::get('commission', 'v1.user.UserBrokerageController/commission')->name('commission')->option(['real_name' => '推广数据']);//推广数据 昨天的佣金 累计提现金额 当前佣金
         Route::get('brokerage_rank', 'v1.user.UserBrokerageController/brokerageRank')->name('brokerageRank')->option(['real_name' => '佣金排行']);//佣金排行
+        Route::post('brokerage/transfer', 'v1.user.UserBrokerageController/transfer')->name('brokerageTransfer')->option(['real_name' => '佣金互转']);//佣金互转
         /** 用户注销 */
         Route::get('user_cancel', 'v1.user.UserController/SetUserCancel')->name('SetUserCancel')->option(['real_name' => '用户注销']);//用户注销
         /** 用户浏览记录 */

+ 22 - 18
app/services/order/StoreOrderSuccessServices.php

@@ -382,25 +382,29 @@ class StoreOrderSuccessServices extends BaseServices
                 $currentUid = $parentRelation['pid'];
             }
 
-            // 2. 同组下找5人
-            if ($userRelation['group_pid'] > 0) {
-                $groupMembers = $giftGroupService->getGroupMembers($product_id, $userRelation['group_pid']);
-                $currentIndex = array_search($order['uid'], array_column($groupMembers, 'uid'));
-
-                if ($currentIndex !== false) {
-                    for ($i = $currentIndex + 1; $i < count($groupMembers) && $totalSpots < 5; $i++) {
-                        $memberBrokerage = $userServices->getOne(['uid' => $groupMembers[$i]['uid']]);
-                        $userServices->bcInc($groupMembers[$i]['uid'], 'brokerage_price', $spotBrokerage, 'uid');
-                        $balance = bcadd($memberBrokerage['brokerage_price'], $spotBrokerage);
-                        $userBrokerageServices->income('get_spot_brokerage', $groupMembers[$i]['uid'], [
-                            'nickname' => $userInfo['nickname'],
-                            'pay_price' => floatval($order['pay_price']),
-                            'number' => $spotBrokerage,
-                            'frozen_time' => $frozenTime
-                        ], $balance, $order['id']);
-                        $totalSpots++;
-                    }
+            // 2. 同组下找5人(根据pid查找链式成员)
+            $chainMembers = $giftGroupService->getGroupMembersByPid($product_id, $userRelation['pid']);
+            $currentIndex = array_search($order['uid'], array_column($chainMembers, 'uid'));
+
+            if ($currentIndex !== false) {
+                @file_put_contents('quanju4.txt', '找到当前用户在链中的位置: ' . $currentIndex . ', 链成员总数: ' . count($chainMembers) . "\r\n", 8);
+                // 从当前用户的下一个位置开始找,最多找5人
+                for ($i = $currentIndex + 1; $i < count($chainMembers) && $totalSpots < 5; $i++) {
+                    $memberBrokerage = $userServices->getOne(['uid' => $chainMembers[$i]['uid']]);
+                    $userServices->bcInc($chainMembers[$i]['uid'], 'brokerage_price', $spotBrokerage, 'uid');
+                    $balance = bcadd($memberBrokerage['brokerage_price'], $spotBrokerage);
+                    $userBrokerageServices->income('get_spot_brokerage', $chainMembers[$i]['uid'], [
+                        'nickname' => $userInfo['nickname'],
+                        'pay_price' => floatval($order['pay_price']),
+                        'number' => $spotBrokerage,
+                        'frozen_time' => $frozenTime
+                    ], $balance, $order['id']);
+                    @file_put_contents('quanju4.txt', '发放见点奖给链成员: uid=' . $chainMembers[$i]['uid'] . ', 金额=' . $spotBrokerage . "\r\n", 8);
+                    $totalSpots++;
                 }
+                @file_put_contents('quanju4.txt', '链式发放完成,共发放: ' . $totalSpots . '人' . "\r\n", 8);
+            } else {
+                @file_put_contents('quanju4.txt', '未找到当前用户在链中的位置' . "\r\n", 8);
             }
 
             // 3. 如果同组下级不足5人,找自己的直推下级

+ 89 - 0
app/services/user/UserBrokerageServices.php

@@ -147,6 +147,20 @@ class UserBrokerageServices extends BaseServices
             'status' => 1,
             'pm' => 1
         ],
+        'brokerage_transfer_out' => [
+            'title' => '佣金互转转出',
+            'type' => 'brokerage_transfer_out',
+            'mark' => '佣金互转给{%nickname%},金额{%number%}元',
+            'status' => 1,
+            'pm' => 0
+        ],
+        'brokerage_transfer_in' => [
+            'title' => '佣金互转转入',
+            'type' => 'brokerage_transfer_in',
+            'mark' => '收到{%nickname%}的佣金互转,金额{%number%}元',
+            'status' => 1,
+            'pm' => 1
+        ],
     ];
 
 
@@ -482,4 +496,79 @@ class UserBrokerageServices extends BaseServices
             'position' => $this->getUserBrokerageRank($uid, $type)
         ];
     }
+
+    /**
+     * 佣金互转
+     * @param int $uid 转账用户ID
+     * @param int $toUid 收款用户ID
+     * @param string $money 转账金额
+     * @return array
+     * @throws ApiException
+     */
+    public function transferBrokerage(int $uid, int $toUid, string $money)
+    {
+        // 验证参数
+        if ($uid <= 0) {
+            throw new ApiException(100026);
+        }
+        if ($toUid <= 0) {
+            throw new ApiException('收款用户不存在');
+        }
+        if ($uid == $toUid) {
+            throw new ApiException('不能转账给自己');
+        }
+        if (bccomp($money, '0', 2) <= 0) {
+            throw new ApiException('转账金额必须大于0');
+        }
+
+        /** @var UserServices $userService */
+        $userService = app()->make(UserServices::class);
+
+        // 获取转账方和收款方信息
+        $fromUser = $userService->getUserInfo($uid);
+        if (!$fromUser) {
+            throw new ApiException(100026);
+        }
+        $toUser = $userService->getUserInfo($toUid);
+        if (!$toUser) {
+            throw new ApiException('收款用户不存在');
+        }
+
+        // 检查转账方佣金余额
+        $fromBrokerage = $fromUser['brokerage_price'] ?? 0;
+        if (bccomp((string)$fromBrokerage, $money, 2) < 0) {
+            throw new ApiException('佣金余额不足');
+        }
+
+        // 计算手续费
+        $transferFeeRate = sys_config('transfer_fee', 0);
+        $transferFee = bcmul((string)$money, bcdiv((string)$transferFeeRate, '100', 4), 2);
+        $actualAmount = bcsub((string)$money, $transferFee, 2);
+
+        // 使用事务处理
+        return $this->transaction(function () use ($uid, $toUid, $money, $transferFee, $actualAmount, $fromUser, $toUser, $fromBrokerage) {
+            // 扣除转账方佣金
+            $newFromBrokerage = bcsub((string)$fromBrokerage, $money, 2);
+            $userService->update($uid, ['brokerage_price' => $newFromBrokerage], 'uid');
+
+            // 增加收款方佣金
+            $toBrokerage = $toUser['brokerage_price'] ?? 0;
+            $newToBrokerage = bcadd((string)$toBrokerage, $actualAmount, 2);
+            $userService->update($toUid, ['brokerage_price' => $newToBrokerage], 'uid');
+
+            // 记录转账方扣除记录
+            $this->income('brokerage_transfer_out', $uid, [
+                'mark' => '佣金互转给' . $toUser['nickname'] . ',金额' . $money . '元',
+                'number' => $money
+            ], $newFromBrokerage, 0);
+
+            // 记录收款方收入记录
+            $this->income('brokerage_transfer_in', $toUid, [
+                'mark' => '收到' . $fromUser['nickname'] . '的佣金互转,金额' . $actualAmount . '元',
+                'number' => $actualAmount
+            ], $newToBrokerage, 0);
+
+            return '转账成功';
+        });
+    }
 }

+ 49 - 25
app/services/user/UserExtractServices.php

@@ -533,19 +533,17 @@ class UserExtractServices extends BaseServices
             throw new ApiException(410024);
         }
 
-        /** @var UserBrokerageServices $services */
-        $services = app()->make(UserBrokerageServices::class);
-        $data['broken_commission'] = $services->getUserFrozenPrice($uid);
-        if ($data['broken_commission'] < 0)
-            $data['broken_commission'] = 0;
-        $data['brokerage_price'] = $user['brokerage_price'];
-        //可提现佣金
-        $commissionCount = bcsub((string)$data['brokerage_price'], (string)$data['broken_commission'], 2);
-        if ($data['money'] > $commissionCount) {
+        // ===== 余额提现逻辑(新增) =====
+        /** @var UserMoneyServices $userMoneyServices */
+        $userMoneyServices = app()->make(UserMoneyServices::class);
+
+        // 可提现余额
+        $moneyCount = $user['now_money'];
+        if ($data['money'] > $moneyCount) {
             throw new ApiException(400661);
         }
 
-        $extractPrice = $user['brokerage_price'];
+        $extractPrice = $user['now_money'];
         $userExtractMinPrice = sys_config('user_extract_min_price');
         if ($data['money'] < $userExtractMinPrice) {
             throw new ApiException(400662, ['money' => $userExtractMinPrice]);
@@ -559,15 +557,20 @@ class UserExtractServices extends BaseServices
         if ($data['money'] <= 0) {
             throw new ApiException(400664);
         }
-        $data['extract_price'] = bcmul($data['money'], '1', 2);
+
+        // 计算手续费
+        $extract_price = bcmul($data['money'], '1', 2);
+        $withdrawalFeeRate = sys_config('withdrawal_fee', '0');
+        $extract_fee = bcmul((string)$extract_price, bcdiv((string)$withdrawalFeeRate, '100', 4), 2);
+
         $insertData = [
             'wechat_order_id' => app()->make(StoreOrderCreateServices::class)->getNewOrderId('tx'),
             'uid' => $user['uid'],
             'extract_type' => $data['extract_type'],
-            'extract_price' => $data['extract_price'],
-            'extract_fee' => bcmul((string)$data['extract_price'], bcdiv((string)sys_config('withdrawal_fee', '0'), '100', 4), 2),
+            'extract_price' => $extract_price,
+            'extract_fee' => $extract_fee,
             'add_time' => time(),
-            'balance' => $user['brokerage_price'],
+            'balance' => $user['now_money'],
             'status' => 0,
             'channel_type' => $data['channel_type']
         ];
@@ -580,39 +583,60 @@ class UserExtractServices extends BaseServices
         if (isset($data['weixin'])) $insertData['wechat'] = $data['weixin'];
         else $insertData['wechat'] = $user['nickname'];
         $mark = '';
-        $feeMark = sys_config('withdrawal_fee', 0) == 0 ? '' : ',手续费' . $insertData['extract_fee'] . '元';
+        $feeMark = $withdrawalFeeRate == 0 ? '' : ',手续费' . $extract_fee . '元';
         if ($data['extract_type'] == 'alipay') {
             $insertData['alipay_code'] = $data['alipay_code'];
             $insertData['qrcode_url'] = $data['qrcode_url'];
             $insertData['user_name'] = $data['user_name'];
             $insertData['real_name'] = $data['user_name'];
-            $mark = '使用支付宝提现' . $insertData['extract_price'] . '元' . $feeMark;
+            $mark = '使用支付宝提现' . $extract_price . '元' . $feeMark;
         } else if ($data['extract_type'] == 'bank') {
-            $mark = '使用银联卡' . $insertData['bank_code'] . '提现' . $insertData['extract_price'] . '元' . $feeMark;
+            $mark = '使用银联卡' . $insertData['bank_code'] . '提现' . $extract_price . '元' . $feeMark;
         } else if ($data['extract_type'] == 'weixin') {
             $insertData['user_name'] = $data['user_name'];
             $insertData['real_name'] = $data['user_name'];
             $insertData['qrcode_url'] = $data['qrcode_url'];
-            $mark = '使用微信提现' . $insertData['extract_price'] . '元' . $feeMark;
+            $mark = '使用微信提现' . $extract_price . '元' . $feeMark;
             if (sys_config('weixin_extract_type', 0) && $openid) {
-                if ($data['extract_price'] < 0.1) {
+                if ($extract_price < 0.1) {
                     throw new ApiException(400665);
                 }
             }
         }
-        $res1 = $this->transaction(function () use ($insertData, $data, $uid, $userService, $user, $mark) {
+
+        $res1 = $this->transaction(function () use ($insertData, $data, $uid, $userService, $user, $mark, $extract_fee, $extract_price) {
             if (!$res1 = $this->dao->save($insertData)) {
                 throw new ApiException(410121);
             }
-            $balance = bcsub((string)$user['brokerage_price'], $data['extract_price'], 2) ?? 0;
-            if (!$userService->update($uid, ['brokerage_price' => $balance], 'uid')) {
+            // 扣除用户余额
+            $balance = bcsub((string)$user['now_money'], $extract_price, 2);
+            if (!$userService->update($uid, ['now_money' => $balance], 'uid')) {
                 throw new ApiException(410121);
             }
 
+            // ===== 保存余额记录(新增) =====
+            /** @var UserMoneyServices $userMoneyServices */
+            $userMoneyServices = app()->make(UserMoneyServices::class);
+            // 提现记录
+            $userMoneyServices->income('extract', $uid, [
+                'mark' => $mark,
+                'number' => $extract_price
+            ], $balance, $res1['id'], 0);
+
+            // 手续费记录
+            if ($extract_fee > 0) {
+                $userMoneyServices->income('extract_fee', $uid, [
+                    'mark' => '提现手续费',
+                    'number' => $extract_fee
+                ], $balance, $res1['id'], 0);
+            }
+
+            // ===== 原佣金记录逻辑(已注释) =====
             //保存佣金记录
             /** @var UserBrokerageServices $userBrokerageServices */
-            $userBrokerageServices = app()->make(UserBrokerageServices::class);
-            $userBrokerageServices->income('extract', $uid, ['mark' => $mark, 'number' => $data['extract_price']], $balance, $res1['id']);
+            // $userBrokerageServices = app()->make(UserBrokerageServices::class);
+            // $userBrokerageServices->income('extract', $uid, ['mark' => $mark, 'number' => $extract_price], $balance, $res1['id']);
+
             return $res1;
         });
 
@@ -624,7 +648,7 @@ class UserExtractServices extends BaseServices
         $systemAdmin = app()->make(SystemAdminServices::class);
         $systemAdmin->adminNewPush();
         //消息
-        event('NoticeListener', [['nickname' => $user['nickname'], 'money' => $data['extract_price']], 'kefu_send_extract_application']);
+        event('NoticeListener', [['nickname' => $user['nickname'], 'money' => $extract_price], 'kefu_send_extract_application']);
 
         //自定义事件-用户提现
         event('CustomEventListener', ['user_extract', [

+ 8 - 2
app/services/user/UserRechargeServices.php

@@ -362,8 +362,14 @@ class UserRechargeServices extends BaseServices
         if ($price > $commissionCount) {
             throw new ApiException(400680);
         }
+
+        // 计算手续费
+        $transferFeeRate = sys_config('transfer_fee', 0);
+        $transferFee = bcmul((string)$price, bcdiv((string)$transferFeeRate, '100', 4), 2);
+        $actualAmount = bcsub((string)$price, $transferFee, 2);
+
         $edit_data = [];
-        $edit_data['now_money'] = bcadd((string)$user['now_money'], (string)$price, 2);
+        $edit_data['now_money'] = bcadd((string)$user['now_money'], $actualAmount, 2);
         $edit_data['brokerage_price'] = $user['brokerage_price'] > $price ? bcsub((string)$user['brokerage_price'], (string)$price, 2) : 0;
         if (!$userServices->update($uid, $edit_data, 'uid')) {
             throw new ApiException(100007);
@@ -387,7 +393,7 @@ class UserRechargeServices extends BaseServices
         //余额记录
         /** @var UserMoneyServices $userMoneyServices */
         $userMoneyServices = app()->make(UserMoneyServices::class);
-        $userMoneyServices->income('brokerage_to_nowMoney', $uid, $price, $edit_data['now_money'], $re['id']);
+        $userMoneyServices->income('brokerage_to_nowMoney', $uid, $actualAmount, $edit_data['now_money'], $re['id']);
 
         //写入提现记录
         $extractInfo = [