WIN-2308041133\Administrator 1 mēnesi atpakaļ
vecāks
revīzija
1768ed676a

+ 96 - 0
app/services/order/StoreOrderSuccessServices.php

@@ -146,6 +146,12 @@ class StoreOrderSuccessServices extends BaseServices
             self::createGiftRecommendationRelationship($orderInfo); //建立礼包推荐关系
             self::giftRecommendationBonus($orderInfo); //礼包升级分红
 
+        }else{  //普通商品返贡献值
+            if ($orderInfo['uid']==2){
+//                if ($paytype != PayServices::YUE_PAY){
+                    self::returnContributionValue($orderInfo);
+//                }
+            }
 
         }
 //        if ($orderInfo['is_lb'] == 1) {  //赠送礼包推荐奖金
@@ -898,5 +904,95 @@ class StoreOrderSuccessServices extends BaseServices
         return false; // 没找到,不是下级
     }
 
+    /**
+     * 普通商品返利贡献值
+     * @param array $order 订单信息
+     * @return bool
+     */
+    public function returnContributionValue($order)
+    {
+        try {
+            // 检查 cart_info 是否存在且非空
+            if (!isset($order['cart_info']) || empty($order['cart_info'])) {
+                return false;
+            }
+
+            /** @var UserServices $userServices */
+            $userServices = app()->make(UserServices::class);
+            /** @var UserBrokerageServices $userBrokerageServices */
+            $userBrokerageServices = app()->make(UserBrokerageServices::class);
+            /** @var \app\model\product\product\StoreProduct $productModel */
+            $productModel = new \app\model\product\product\StoreProduct();
+
+            // 获取用户信息
+            $userInfo = $userServices->getUserInfo($order['uid']);
+            if (!$userInfo) {
+                return false;
+            }
+
+            $totalContribution = 0;
+
+            // 遍历购物车商品
+            foreach ($order['cart_info'] as $cartItem) {
+                $productId = $cartItem['product_id'] ?? 0;
+                $cartNum = $cartItem['cart_num'] ?? 0;
+                $truePrice = $cartItem['truePrice'] ?? 0;
+
+                if (!$productId || !$cartNum || !$truePrice) {
+                    continue;
+                }
+
+                // 获取商品信息
+                $product = $productModel->where('id', $productId)->field('rebate_radio')->find();
+                if (!$product) {
+                    continue;
+                }
+
+                $rebateRadio = $product['rebate_radio'] ?? 0;
+
+                // 如果返利比例为0,跳过
+                if (!$rebateRadio) {
+                    continue;
+                }
+
+                // 计算贡献值:商品数量 × 商品价格 × (返利比例 / 100)
+                $contribution = bcmul((string)$cartNum, (string)$truePrice, 2);
+                $contribution = bcmul($contribution, bcdiv((string)$rebateRadio, '100', 4), 2);
+
+                if ($contribution <= 0) {
+                    continue;
+                }
+
+                $totalContribution = bcadd($totalContribution, $contribution, 2);
+            }
+
+            // 如果总贡献值为0,返回
+            if ($totalContribution <= 0) {
+                return false;
+            }
+
+            // 更新用户佣金(贡献值)
+            $currentBrokerage = $userInfo['brokerage_price'] ?? 0;
+            $newBrokerage = bcadd((string)$currentBrokerage, $totalContribution, 2);
+            $userServices->bcInc($order['uid'], 'brokerage_price', $totalContribution, 'uid');
+
+            // 记录贡献值明细
+            $userBrokerageServices->income('product_rebate', $order['uid'], [
+                'pay_price' => floatval($order['pay_price']),
+                'number' => $totalContribution
+            ], $newBrokerage, $order['id']);
+
+            @file_put_contents('quanju4.txt', '普通商品返贡献值: uid=' . $order['uid'] . ', 订单金额=' . $order['pay_price'] . ', 返贡献值=' . $totalContribution . "\r\n", 8);
+
+            return true;
+        } catch (\Exception $e) {
+            @file_put_contents('quanju4.txt', $e->getMessage() . "-普通商品返贡献值报错内容\r\n", 8);
+            @file_put_contents('quanju4.txt', $e->getFile() . "-文件\r\n", 8);
+            @file_put_contents('quanju4.txt', $e->getLine() . "-位置\r\n", 8);
+            @file_put_contents('quanju4.txt', $e->getTraceAsString() . "-堆栈\r\n", 8);
+            return false;
+        }
+    }
+
 
 }

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

@@ -183,6 +183,13 @@ class UserBrokerageServices extends BaseServices
             'status' => 1,
             'pm' => 1
         ],
+        'product_rebate' => [
+            'title' => '购买商品返利',
+            'type' => 'product_rebate',
+            'mark' => '购买商品消费{%pay_price%}元,返贡献值{%number%}',
+            'status' => 1,
+            'pm' => 1
+        ],
     ];