WIN-2308041133\Administrator 3 ماه پیش
والد
کامیت
f56045692e
1فایلهای تغییر یافته به همراه131 افزوده شده و 69 حذف شده
  1. 131 69
      app/models/store/StoreOrder.php

+ 131 - 69
app/models/store/StoreOrder.php

@@ -923,27 +923,9 @@ class StoreOrder extends BaseModel
     }
 
 //    分发团队奖励  1首单 2复购
-    public static function team_rewards($order, $type)
+    public static function team_rewards($order, $type,$uid=0,$spread_ratio=0)  //上一层的分账比例
     {
         try {
-            $nickname = User::where('uid', $order['uid'])->value('nickname');
-            $user_spread_group = self::get_spread_user($order['uid']);  //获取所有上级id
-            @file_put_contents('quanju.txt', json_encode($user_spread_group) . "-所有上级\r\n", 8);
-            $group_info = User::where('uid', 'in', $user_spread_group)->column('uid,level');  //获取所有上级信息
-            @file_put_contents('quanju.txt', json_encode($group_info) . "-所有人\r\n", 8);
-            // 提取所有level组成新数组
-            $groupIds = array_column($group_info, 'level');
-            @file_put_contents('quanju.txt', json_encode($groupIds) . "-新数组\r\n", 8);
-            $make = app()->make(SystemUserLevel::class);
-            // 统计每个level出现的次数
-            $levelCounts = array_count_values($groupIds);
-            @file_put_contents('quanju.txt', json_encode($levelCounts) . "-所有等级的人数2222\r\n", 8);
-            $levelCounts2 = [];
-            foreach ($levelCounts as $k => $v) {  //遍历所有等级
-                $level_id = $make->where('id', $k)->value('grade');
-                $levelCounts2[$level_id] = $v;
-            }
-
             if ($type == 1) {
                 $field = 'first_ratio';
                 $title = '首单奖励';
@@ -951,61 +933,141 @@ class StoreOrder extends BaseModel
                 $field = 'ratio';
                 $title = '复购奖励';
             }
-            ksort($levelCounts2);
-            @file_put_contents('quanju.txt', json_encode($levelCounts2) . "-所有等级的人数\r\n", 8);
-            $last_num = 0;
-            $last_level = 0;
-            foreach ($levelCounts2 as $k => $v) {  //遍历所有等级
-                if ($v > 0) {
-                    @file_put_contents('quanju.txt', $v . "-当前等级的人数\r\n", 8);
-                    @file_put_contents('quanju.txt', $k . "-当前等级\r\n", 8);
-                    $level_group = User::where('level', $k)->where('uid', 'in', $user_spread_group)->column('uid,nickname,brokerage_price,level');  //获取这个等级的所有用户
-
-                    $level = $make->where('grade', $k)->find();  //用户等级
-                    if ($level) {
-                        $ratio = bcmul($level[$field], 0.01, 2);  //奖励比例
-                        @file_put_contents('quanju.txt', $ratio . "-当前等级比例\r\n", 8);
-                        $award_price = bcmul($order['total_price'], $ratio, 2);  //奖励金额
-                        @file_put_contents('quanju.txt', $award_price . "-当前等级分到的总金额\r\n", 8);
-                        if ($last_num > 0) {
-                            $award_price = bcsub($award_price, $last_num, 2); //当前等级分到的总金额扣除上个等级分到的总金额
-                        }
-                        @file_put_contents('quanju.txt', $award_price . "-扣除上个等级分到的总金额后的当前等级分到的总金额\r\n", 8);
-                        $last_num = bcadd($last_num, $award_price, 2); //之前等级分到的总金额
-                        @file_put_contents('quanju.txt', $last_num . "-需要扣除的上个等级分到的总金额\r\n", 8);
-                        $user_award = bcdiv($award_price, $v, 2); //每个用户奖励金额
-                        @file_put_contents('quanju.txt', $user_award . "-当前等级分到的个人金额\r\n", 8);
-                        if ($user_award <= 0) {
-                            continue;
-                        }
-                        foreach ($level_group as $key => $value) {
-                            $after = bcadd($value['brokerage_price'], $user_award, 2);  //更新用户奖励金额
-                            @file_put_contents('quanju.txt', $after . "-当前等级分到个人金额结算之后\r\n", 8);
-                            $mark = $value['nickname'] . '获得分账健康币' . $title . ':' . $user_award . '元';
-                            $res = UserBill::income('获得分账健康币', $value['uid'], 'brokerage_price', 'spread_award', $user_award, $order['id'], $after, $mark, 1);
-                            User::where('uid', $value['uid'])->update(['brokerage_price' => $after]);
-                            //        通知提现用户
-                            $openid = WechatUser::where('uid', $value['uid'])->value('openid');
-                            if (!empty($openid)) {
-                                $group = [
-                                    'first' => '获得健康币分账',
-                                    'remark' => '点击查看订单详情'
-                                ];
-                                $time = date('Y-m-d H:i:s', time());
-                                $group = array_merge($group, [
-                                    'amount1' => $user_award,  //到账金额
+            if ($uid>0){
+                $user_info = User::getUserInfo($uid);
+            }else{
+                $user_info = User::getUserInfo($order['uid']);
+            }
+            $make = app()->make(SystemUserLevel::class);
+            $level = $make->where('id', $user_info['level'])->find();  //用户等级
+            if ($level){
+                $ratio = bcmul($level[$field], 0.01, 2);  //奖励比例
+                @file_put_contents('quanju.txt', $ratio . "-当前等级比例\r\n", 8);
+                if ($ratio <= $spread_ratio){  //如果分账比例小于上一位就不分了
+                    if ($user_info['spread_uid']>0){
+                        self::team_rewards($order,$type,$user_info['spread_uid'],$spread_ratio);
+                    } else{
+                        return true;
+                    }
+                    return true;
+                }elseif($ratio>$spread_ratio){
+                    $spread_ratio2 = $ratio;
+                    $ratio = bcsub($ratio,$spread_ratio,2);
+                    $user_award = bcmul($order['total_price'], $ratio, 2);  //奖励金额
+                    $after = bcadd($user_info['brokerage_price'], $user_award, 2);  //更新用户奖励金额
+                    @file_put_contents('quanju.txt', $after . "-当前等级分到个人金额结算之后\r\n", 8);
+                    $mark = $user_info['nickname'] . '获得分账健康币' . $title . ':' . $user_award . '元';
+                    $res = UserBill::income('获得分账健康币', $uid, 'brokerage_price', 'spread_award', $user_award, $order['id'], $after, $mark, 1);
+                    User::where('uid', $uid)->update(['brokerage_price' => $after]);
+                    //        通知提现用户
+                    $openid = WechatUser::where('uid', $uid)->value('openid');
+                    if (!empty($openid)) {
+                        $group = [
+                            'first' => '获得健康币分账',
+                            'remark' => '点击查看订单详情'
+                        ];
+                        $time = date('Y-m-d H:i:s', time());
+                        $group = array_merge($group, [
+                            'amount1' => $user_award,  //到账金额
 //                                    'thing12' => $nickname,  //付款人
-                                    'time3' => $time, //支付时间
-                                ]);
+                            'time3' => $time, //支付时间
+                        ]);
 
-                                @file_put_contents('quanju.txt', json_encode($group) . "-模版消息5\r\n", 8);
-                                $res = WechatTemplateService::sendTemplate($openid, WechatTemplateService::BROKERAGE_PRICE, $group);
-                                @file_put_contents('quanju.txt', json_encode($res) . "-模版消息返回5\r\n", 8);
-                            }
-                        }
+                        @file_put_contents('quanju.txt', json_encode($group) . "-模版消息5\r\n", 8);
+                        $res = WechatTemplateService::sendTemplate($openid, WechatTemplateService::BROKERAGE_PRICE, $group);
+                        @file_put_contents('quanju.txt', json_encode($res) . "-模版消息返回5\r\n", 8);
+                    }
+                    if ($user_info['spread_uid']>0){
+                        self::team_rewards($order,$type,$user_info['spread_uid'],$spread_ratio2);
+                    }else{
+                        return true;
                     }
                 }
+
             }
+
+
+//            $nickname = User::where('uid', $order['uid'])->value('nickname');
+//            $user_spread_group = self::get_spread_user($order['uid']);  //获取所有上级id
+//            @file_put_contents('quanju.txt', json_encode($user_spread_group) . "-所有上级\r\n", 8);
+//            $group_info = User::where('uid', 'in', $user_spread_group)->column('uid,level');  //获取所有上级信息
+//            @file_put_contents('quanju.txt', json_encode($group_info) . "-所有人\r\n", 8);
+////             提取所有level组成新数组
+//            $groupIds = array_column($group_info, 'level');
+//            @file_put_contents('quanju.txt', json_encode($groupIds) . "-新数组\r\n", 8);
+//
+//            // 统计每个level出现的次数
+//            $levelCounts = array_count_values($groupIds);
+//            @file_put_contents('quanju.txt', json_encode($levelCounts) . "-所有等级的人数2222\r\n", 8);
+//            $levelCounts2 = [];
+//            foreach ($levelCounts as $k => $v) {  //遍历所有等级
+//                $level_id = $make->where('id', $k)->value('grade');
+//                $levelCounts2[$level_id] = $v;
+//
+//            }
+
+//            if ($type == 1) {
+//                $field = 'first_ratio';
+//                $title = '首单奖励';
+//            } elseif ($type == 2) {
+//                $field = 'ratio';
+//                $title = '复购奖励';
+//            }
+//            ksort($levelCounts2);
+//            @file_put_contents('quanju.txt', json_encode($levelCounts2) . "-所有等级的人数\r\n", 8);
+//            $last_num = 0;
+//            $last_level = 0;
+//            foreach ($levelCounts2 as $k => $v) {  //遍历所有等级
+//                if ($v > 0) {
+//                    @file_put_contents('quanju.txt', $v . "-当前等级的人数\r\n", 8);
+//                    @file_put_contents('quanju.txt', $k . "-当前等级\r\n", 8);
+//                    $level_group = User::where('level', $k)->where('uid', 'in', $user_spread_group)->column('uid,nickname,brokerage_price,level');  //获取这个等级的所有用户
+//
+//                    $level = $make->where('grade', $k)->find();  //用户等级
+//                    if ($level) {
+//                        $ratio = bcmul($level[$field], 0.01, 2);  //奖励比例
+//                        @file_put_contents('quanju.txt', $ratio . "-当前等级比例\r\n", 8);
+//                        $award_price = bcmul($order['total_price'], $ratio, 2);  //奖励金额
+//                        @file_put_contents('quanju.txt', $award_price . "-当前等级分到的总金额\r\n", 8);
+//                        if ($last_num > 0) {
+//                            $award_price = bcsub($award_price, $last_num, 2); //当前等级分到的总金额扣除上个等级分到的总金额
+//                        }
+//                        @file_put_contents('quanju.txt', $award_price . "-扣除上个等级分到的总金额后的当前等级分到的总金额\r\n", 8);
+//                        $last_num = bcadd($last_num, $award_price, 2); //之前等级分到的总金额
+//                        @file_put_contents('quanju.txt', $last_num . "-需要扣除的上个等级分到的总金额\r\n", 8);
+//                        $user_award = bcdiv($award_price, $v, 2); //每个用户奖励金额
+//                        @file_put_contents('quanju.txt', $user_award . "-当前等级分到的个人金额\r\n", 8);
+//                        if ($user_award <= 0) {
+//                            continue;
+//                        }
+//                        foreach ($level_group as $key => $value) {
+//                            $after = bcadd($value['brokerage_price'], $user_award, 2);  //更新用户奖励金额
+//                            @file_put_contents('quanju.txt', $after . "-当前等级分到个人金额结算之后\r\n", 8);
+//                            $mark = $value['nickname'] . '获得分账健康币' . $title . ':' . $user_award . '元';
+//                            $res = UserBill::income('获得分账健康币', $value['uid'], 'brokerage_price', 'spread_award', $user_award, $order['id'], $after, $mark, 1);
+//                            User::where('uid', $value['uid'])->update(['brokerage_price' => $after]);
+//                            //        通知提现用户
+//                            $openid = WechatUser::where('uid', $value['uid'])->value('openid');
+//                            if (!empty($openid)) {
+//                                $group = [
+//                                    'first' => '获得健康币分账',
+//                                    'remark' => '点击查看订单详情'
+//                                ];
+//                                $time = date('Y-m-d H:i:s', time());
+//                                $group = array_merge($group, [
+//                                    'amount1' => $user_award,  //到账金额
+////                                    'thing12' => $nickname,  //付款人
+//                                    'time3' => $time, //支付时间
+//                                ]);
+//
+//                                @file_put_contents('quanju.txt', json_encode($group) . "-模版消息5\r\n", 8);
+//                                $res = WechatTemplateService::sendTemplate($openid, WechatTemplateService::BROKERAGE_PRICE, $group);
+//                                @file_put_contents('quanju.txt', json_encode($res) . "-模版消息返回5\r\n", 8);
+//                            }
+//                        }
+//                    }
+//                }
+//            }
         } catch (\Exception $e) {
             @file_put_contents('quanju.txt', $e->getMessage() . "-团队奖励错误信息\r\n", 8);
             @file_put_contents('quanju.txt', $e->getLine() . "-团队奖励错误位置\r\n", 8);