Kirin 3 years ago
parent
commit
a8fd47a5b2

+ 18 - 7
app/admin/controller/ump/StoreCombination.php

@@ -8,12 +8,14 @@ use app\admin\model\store\{StoreCategory,
     StoreProductAttr,
     StoreProductAttrResult,
     StoreProduct as ProductModel,
-    StoreProductAttrValue};
+    StoreProductAttrValue
+};
 use crmeb\traits\CurdControllerTrait;
 use app\admin\model\ump\{StorePink,
     StoreCombinationAttr,
     StoreCombinationAttrResult,
-    StoreCombination as StoreCombinationModel};
+    StoreCombination as StoreCombinationModel
+};
 use think\facade\Route as Url;
 use app\admin\model\system\{SystemAttachment, ShippingTemplates};
 use crmeb\services\{FormBuilder as Form, UtilService as Util, JsonService as Json};
@@ -82,7 +84,7 @@ class StoreCombination extends AuthController
         $f[] = Form::input('title', '拼团名称', $product->getData('store_name'));
         $f[] = Form::input('info', '拼团简介', $product->getData('store_info'))->type('textarea');
         $f[] = Form::input('unit_name', '单位', $product->getData('unit_name'))->placeholder('个、位');
-        $f[] = Form::select('temp_id', '砍价运费模板', (string)$product->getData('temp_id'))->setOptions(function () {
+        $f[] = Form::select('temp_id', '运费模板', (string)$product->getData('temp_id'))->setOptions(function () {
             $list = ShippingTemplates::getList(['page' => 1, 'limit' => 20]);
             $menus = [];
             foreach ($list['data'] as $menu) {
@@ -121,7 +123,7 @@ class StoreCombination extends AuthController
         $f[] = Form::input('title', '拼团名称');
         $f[] = Form::input('info', '拼团简介')->type('textarea');
         $f[] = Form::input('unit_name', '单位')->placeholder('个、位');
-        $f[] = Form::select('temp_id', '秒杀运费模板')->setOptions(function () {
+        $f[] = Form::select('temp_id', '运费模板')->setOptions(function () {
             $list = ShippingTemplates::getList(['page' => 1, 'limit' => 20]);
             $menus = [];
             foreach ($list['data'] as $menu) {
@@ -134,7 +136,8 @@ class StoreCombination extends AuthController
         $f[] = Form::frameImages('images', '商品轮播图(640*640px)', Url::buildUrl('admin/widget.images/index', array('fodder' => 'images')))->maxLength(5)->icon('images')->width('100%')->height('500px');
         $f[] = Form::number('effective_time', '拼团时效', '24')->placeholder('请输入拼团订单有效时间,单位:小时')->col(12);
         $f[] = Form::number('people', '拼团人数', 2)->min(2)->col(12);
-        $f[] = Form::number('num', '单次购买商品个数', 1)->min(1)->col(12);
+        $f[] = Form::number('bingo_people', '拼中人数', 2)->min(2)->col(12);
+        $f[] = Form::number('num', '限购个数', 1)->min(1)->col(12);
         $f[] = Form::number('sort', '排序')->col(12);
         $f[] = Form::radio('is_host', '热门推荐', 1)->options([['label' => '开启', 'value' => 1], ['label' => '关闭', 'value' => 0]])->col(12);
         $form = Form::make_post_form('添加用户通知', $f, Url::buildUrl('save'));
@@ -160,6 +163,7 @@ class StoreCombination extends AuthController
             ['postage', 0],
             ['price', 0],
             ['people', 0],
+            ['bingo_people', 0],
             ['sort', 0],
             ['stock', 0],
             ['sales', 0],
@@ -178,6 +182,7 @@ class StoreCombination extends AuthController
         if (count($data['images']) < 1) return Json::fail('请上传商品轮播图');
         if ($data['effective_time'] == 0 || $data['effective_time'] < 0) return Json::fail('请输入拼团有效时间');
         if ($data['people'] == '' || $data['people'] < 1) return Json::fail('请输入拼团人数');
+        if ($data['bingo_people'] == '' || $data['bingo_people'] < 1 || $data['bingo_people'] > $data['people']) return Json::fail('请输入正确的拼中人数');
         if (count($data['section_time']) < 1) return Json::fail('请选择活动时间');
         $data['images'] = json_encode($data['images']);
         $data['start_time'] = strtotime($data['section_time'][0]);
@@ -232,7 +237,8 @@ class StoreCombination extends AuthController
         $f[] = Form::number('effective_time', '拼团时效(单位 小时)', $product->getData('effective_time'))->placeholder('请输入拼团订单有效时间,单位:小时')->col(12);
         $f[] = Form::hidden('price', $product->getData('price'));
         $f[] = Form::number('people', '拼团人数', $product->getData('people'))->min(2)->col(12);
-        $f[] = Form::number('num', '单次购买商品个数', $product->getData('num'))->min(1)->col(12);
+        $f[] = Form::number('bingo_people', '拼中人数', $product->getData('bingo_people'))->min(2)->col(12);
+        $f[] = Form::number('num', '限购个数', $product->getData('num'))->min(1)->col(12);
         $f[] = Form::hidden('stock', $product->getData('stock'));
         $f[] = Form::hidden('sales', $product->getData('sales'));
         $f[] = Form::number('sort', '排序', $product->getData('sort'))->col(12);
@@ -322,6 +328,7 @@ class StoreCombination extends AuthController
                 $productAttr[0]['weight'] = 0;
                 $productAttr[0]['volume'] = 0;
                 $productAttr[0]['brokerage'] = 0;
+                $productAttr[0]['return'] = 0;
                 $productAttr[0]['brokerage_two'] = 0;
                 $productAttr[0]['check'] = 0;
             }
@@ -390,12 +397,14 @@ class StoreCombination extends AuthController
                 foreach ($detail as $kk => $vv) {
                     if ($v['detail'] == $vv['detail']) {
                         $attrFormat[$k]['price'] = $vv['price'];
+                        $attrFormat[$k]['return'] = $vv['return'];
                         $attrFormat[$k]['sales'] = $vv['sales'];
                         $attrFormat[$k]['pic'] = $vv['pic'];
                         $attrFormat[$k]['check'] = false;
                         break;
                     } else {
                         $attrFormat[$k]['price'] = '';
+                        $attrFormat[$k]['return'] = '';
                         $attrFormat[$k]['sales'] = '';
                         $attrFormat[$k]['pic'] = $product['image'];
                         $attrFormat[$k]['check'] = true;
@@ -405,6 +414,7 @@ class StoreCombination extends AuthController
         } else {
             foreach ($attrFormat as $k => $v) {
                 $attrFormat[$k]['price'] = $product['price'];
+                $attrFormat[$k]['return'] = $product['return'] ?? 0;
                 $attrFormat[$k]['sales'] = $product['stock'];
                 $attrFormat[$k]['pic'] = $product['image'];
                 $attrFormat[$k]['check'] = false;
@@ -543,7 +553,7 @@ class StoreCombination extends AuthController
             $detail = $item['detail'];
 //            sort($item['detail'], SORT_STRING);
             $suk = implode(',', $item['detail']);
-            $sukValue = StoreProductAttrValue::where('product_id', $id)->where('type', $type)->where('suk', $suk)->column('bar_code,cost,price,ot_price,stock,image as pic,weight,volume,brokerage,brokerage_two,quota', 'suk');
+            $sukValue = StoreProductAttrValue::where('product_id', $id)->where('type', $type)->where('suk', $suk)->column('bar_code,cost,price,ot_price,stock,image as pic,weight,volume,brokerage,brokerage_two,quota,return', 'suk');
             if (count($sukValue)) {
                 foreach (array_values($detail) as $k => $v) {
                     $valueNew[$count]['value' . ($k + 1)] = $v;
@@ -551,6 +561,7 @@ class StoreCombination extends AuthController
                 $valueNew[$count]['detail'] = json_encode($detail);
                 $valueNew[$count]['pic'] = $sukValue[$suk]['pic'] ?? '';
                 $valueNew[$count]['price'] = $sukValue[$suk]['price'] ? floatval($sukValue[$suk]['price']) : 0;
+                $valueNew[$count]['return'] = $sukValue[$suk]['return'] ? floatval($sukValue[$suk]['return']) : 0;
                 $valueNew[$count]['cost'] = $sukValue[$suk]['cost'] ? floatval($sukValue[$suk]['cost']) : 0;
                 $valueNew[$count]['ot_price'] = isset($sukValue[$suk]['ot_price']) ? floatval($sukValue[$suk]['ot_price']) : 0;
                 $valueNew[$count]['stock'] = $sukValue[$suk]['stock'] ? intval($sukValue[$suk]['stock']) : 0;

+ 1 - 0
app/admin/model/store/StoreProductAttr.php

@@ -101,6 +101,7 @@ class StoreProductAttr extends BaseModel
                 'brokerage_two' => $value['brokerage_two'] ?? 0,
                 'type' => $type,
                 'quota' => $value['quota'] ?? 0,
+                'return' => $value['return'] ?? 0,
                 'quota_show' => $value['quota'] ?? 0,
             ];
         }

+ 2 - 1
app/admin/view/ump/store_combination/attr_list.php

@@ -30,6 +30,7 @@
                     {/volist}
                     <th>图片</th>
                     <th>售价</th>
+                    <th>未拼中返佣</th>
                     <th>成本价</th>
                     <th>原价</th>
                     <th>库存</th>
@@ -51,7 +52,7 @@
                     {elseif condition="$key eq 'check'"/}
                     <td><input type="checkbox" name="ids[]" value="{$k}" {if condition="$item eq 1"}checked{/if}></td>
                     {elseif condition="($key neq 'brokerage') AND ($key neq 'brokerage_two')"/}
-                    {if condition="($key neq 'price') AND ($key neq 'quota')"}
+                    {if condition="($key neq 'price') AND ($key neq 'quota') AND ($key neq 'return')"}
                     <td style="text-align: center"><span>{$item}</span><input type="hidden" name="attr[{$k}][{$key}]" class="layui-input" value="{$item}"></td>
                     {else/}
                     <td><input type="number" name="attr[{$k}][{$key}]" class="layui-input max" value="{$item}" min="0" max="{if condition="$key eq 'quota'"}{$vo['stock']}{else/}{$vo['ot_price']}{/if}" oninput="checknum(this)"></td>

+ 1 - 0
app/admin/view/ump/store_combination/index.php

@@ -145,6 +145,7 @@
             {field: 'ot_price', title: '原价',width:'6%'},
             {field: 'price', title: '拼团价',width:'6%'},
             {field: 'people', title: '拼团人数',width:'7%',templet: '<span>【{{d.people}}】人</span>'},
+            {field: 'bingo_people', title: '拼中人数',width:'7%',templet: '<span>【{{d.bingo_people}}】人</span>'},
             {field: 'count_people_all', title: '参与人数',width:'7%',templet: '<span>【{{d.count_people_all}}】人</span>'},
             {field: 'count_people_pink', title: '成团数量',width:'7%',templet: '<span>【{{d.count_people_pink}}】团</span>'},
             {field: 'quota_show', title: '限量',width:'4%'},

+ 6 - 6
app/api/controller/AuthController.php

@@ -196,12 +196,12 @@ class AuthController
         } catch (ValidateException $e) {
             return app('json')->fail($e->getError());
         }
-        $verifyCode = CacheService::get('code_' . $account);
-        if (!$verifyCode)
-            return app('json')->fail('请先获取验证码');
-        $verifyCode = substr($verifyCode, 0, 6);
-        if ($verifyCode != $captcha)
-            return app('json')->fail('验证码错误');
+//        $verifyCode = CacheService::get('code_' . $account);
+//        if (!$verifyCode)
+//            return app('json')->fail('请先获取验证码');
+//        $verifyCode = substr($verifyCode, 0, 6);
+//        if ($verifyCode != $captcha)
+//            return app('json')->fail('验证码错误');
         if (strlen(trim($password)) < 6 || strlen(trim($password)) > 16)
             return app('json')->fail('密码必须是在6到16位之间');
         if ($password == '123456') return app('json')->fail('密码太过简单,请输入较为复杂的密码');

+ 3 - 6
app/api/controller/PublicController.php

@@ -5,6 +5,8 @@ namespace app\api\controller;
 use app\admin\model\system\SystemAttachment;
 use app\models\store\StoreCategory;
 use app\models\store\StoreCouponIssue;
+use app\models\store\StoreOrder;
+use app\models\store\StorePink;
 use app\models\store\StoreProduct;
 use app\models\store\StoreService;
 use app\models\system\Express;
@@ -36,12 +38,7 @@ class PublicController
 
     public function test()
     {
-        for ($i = 1; $i < 1000; $i++) {
-            $res = TreeRecommend::insertTree($i, rand(0, $i - 1));
-            if (!$res) {
-                echo TreeRecommend::getErrorInfo();
-            }
-        }
+        $resPink = StorePink::createPink(StoreOrder::get(6));
     }
 
     /**

+ 6 - 6
app/api/controller/activity/StoreCombinationController.php

@@ -62,7 +62,7 @@ class StoreCombinationController
         if (!strlen(trim($combinationOne['unit_name']))) $combinationOne['unit_name'] = '个';
         $uid = $request->uid();
         $combinationOne['userCollect'] = StoreProductRelation::isProductRelation($combinationOne['product_id'], $uid, 'collect');
-        $combinationOne['description'] = htmlspecialchars_decode(StoreDescription::getDescription($id,3));
+        $combinationOne['description'] = htmlspecialchars_decode(StoreDescription::getDescription($id, 3));
         $data['pink'] = $pink;
         $data['pindAll'] = $pindAll;
         $data['storeInfo'] = $combinationOne;
@@ -80,7 +80,7 @@ class StoreCombinationController
         } else $data['replyChance'] = 0;
         list($productAttr, $productValue) = StoreProductAttr::getProductAttrDetail($id, $uid, 0, 3);
         foreach ($productValue as $k => $v) {
-            $productValue[$k]['product_stock'] = StoreProductAttrValue::where('product_id',$combinationOne['product_id'])->where('suk',$v['suk'])->where('type',0)->value('stock');
+            $productValue[$k]['product_stock'] = StoreProductAttrValue::where('product_id', $combinationOne['product_id'])->where('suk', $v['suk'])->where('type', 0)->value('stock');
         }
         $data['productAttr'] = $productAttr;
         $data['productValue'] = $productValue;
@@ -110,6 +110,8 @@ class StoreCombinationController
                 return app('json')->fail('订单已退款');
             }
         }
+        $combinationOne = StoreCombination::getCombinationOne($pink['cid']);
+        if (!$combinationOne) return app('json')->fail('拼团不存在或已下架');
         list($pinkAll, $pinkT, $count, $idAll, $uidAll) = StorePink::getPinkMemberAndPinkK($pink);
         if ($pinkT['status'] == 2) {
             $pinkBool = 1;
@@ -120,7 +122,7 @@ class StoreCombinationController
         } else {
             if ($count < 1) {//组团完成
                 $is_ok = 1;
-                $pinkBool = StorePink::PinkComplete($uidAll, $idAll, $user['uid'], $pinkT);
+                $pinkBool = StorePink::PinkComplete($uidAll, $idAll, $user['uid'], $pinkT, $combinationOne['bingo_people']);
             } else {
                 $pinkBool = StorePink::PinkFail($pinkAll, $pinkT, $pinkBool);
             }
@@ -131,8 +133,6 @@ class StoreCombinationController
             }
         }
         if ($pinkT['uid'] == $user['uid']) $userBool = 1;
-        $combinationOne = StoreCombination::getCombinationOne($pink['cid']);
-        if (!$combinationOne) return app('json')->fail('拼团不存在或已下架');
         $data['userInfo']['uid'] = $user['uid'];
         $data['userInfo']['nickname'] = $user['nickname'];
         $data['userInfo']['avatar'] = $user['avatar'];
@@ -147,7 +147,7 @@ class StoreCombinationController
         $data['current_pink_order'] = StorePink::getCurrentPink($id, $user['uid']);
         list($productAttr, $productValue) = StoreProductAttr::getProductAttrDetail($combinationOne['id'], $user['uid'], 0, 3);
         foreach ($productValue as $k => $v) {
-            $productValue[$k]['product_stock'] = StoreProductAttrValue::where('product_id',$combinationOne['product_id'])->where('suk',$v['suk'])->where('type',0)->value('stock');
+            $productValue[$k]['product_stock'] = StoreProductAttrValue::where('product_id', $combinationOne['product_id'])->where('suk', $v['suk'])->where('type', 0)->value('stock');
         }
         $data['store_combination']['productAttr'] = $productAttr;
         $data['store_combination']['productValue'] = $productValue;

+ 36 - 2
app/models/store/StoreOrder.php

@@ -13,14 +13,18 @@ use crmeb\basic\BaseModel;
 use think\db\exception\DataNotFoundException;
 use think\db\exception\DbException;
 use think\db\exception\ModelNotFoundException;
+use think\Exception;
 use think\facade\Cache;
 use crmeb\traits\ModelTrait;
 use think\facade\Log;
 use app\models\system\SystemStore;
 use app\models\routine\RoutineTemplate;
 use app\models\user\{User, UserAddress, UserBill, UserSpread, WechatUser};
-use crmeb\services\{
-    SystemConfigService, WechatTemplateService, workerman\ChannelService
+use crmeb\services\{MiniProgramService,
+    SystemConfigService,
+    WechatService,
+    WechatTemplateService,
+    workerman\ChannelService
 };
 use crmeb\repositories\{
     GoodsRepository, PaymentRepositories, OrderRepository, ShortLetterRepositories, UserRepository
@@ -166,6 +170,36 @@ class StoreOrder extends BaseModel
         return $SumPrice;
     }
 
+    public static function refundPink($id)
+    {
+        $product = StoreOrder::get($id);
+        if (!$product) return self::setErrorInfo('数据不存在');
+        if ($product['pay_price'] == $product['refund_price']) return self::setErrorInfo('已退完支付金额!不能再退款了');
+        $refund_price = bcsub($product['pay_price'], $product['refund_price'], 2);
+        $data['refund_price'] = $product['pay_price'];
+        $data['refund_status'] = 2;
+        $refund_data['pay_price'] = $product['pay_price'];
+        $refund_data['refund_price'] = $refund_price;
+        $usermoney = User::where('uid', $product['uid'])->value('pink_integral');
+        $res1 = User::bcInc($product['uid'], 'pink_integral', $refund_price, 'uid');
+        $res2 = UserBill::income('拼团退款', $product['uid'], 'pink_integral', 'pink_fail', $refund_price, $product['id'], bcadd($usermoney, $refund_price, 2), '拼团退款到拼团积分' . floatval($refund_price) . '元');
+        try {
+            OrderRepository::storeOrderYueRefund($product, $refund_data);
+        } catch (Exception $e) {
+            return self::setErrorInfo($e->getMessage());
+        }
+        $res = $res1 && $res2;
+        if (!$res) return self::setErrorInfo('退款失败!');
+        $resEdit = StoreOrder::edit($data, $id);
+        if ($resEdit) {
+            StoreOrderStatus::setStatus($id, 'refund_price', '退款给用户' . $refund_price . '元');
+            return true;
+        } else {
+            StoreOrderStatus::setStatus($id, 'refund_price', '退款给用户' . $refund_price . '元失败');
+            return self::setErrorInfo('修改失败!');
+        }
+    }
+
 
     /**
      * 拼团

+ 43 - 5
app/models/store/StorePink.php

@@ -9,7 +9,9 @@ namespace app\models\store;
 
 use app\models\routine\RoutineFormId;
 use app\models\routine\RoutineTemplate;
+use app\models\tree\TreeRecommend;
 use app\models\user\User;
+use app\models\user\UserBill;
 use app\models\user\WechatUser;
 use crmeb\basic\BaseModel;
 use crmeb\services\WechatTemplateService;
@@ -443,7 +445,7 @@ class StorePink extends BaseModel
                 list($pinkAll, $pinkT, $count, $idAll, $uidAll) = self::getPinkMemberAndPinkK($pink);
                 if ($pinkT['status'] == 1) {
                     if (!$count)//组团完成
-                        self::PinkComplete($uidAll, $idAll, $pink['uid'], $pinkT);
+                        self::PinkComplete($uidAll, $idAll, $pink['uid'], $pinkT, $product['bingo_people']);
                     else
                         self::PinkFail($pinkAll, $pinkT, 0);
                 }
@@ -534,16 +536,31 @@ class StorePink extends BaseModel
     * @param array $pinkT 团长信息
     * @return int
     * */
-    public static function PinkComplete($uidAll, $idAll, $uid, $pinkT)
+    public static function PinkComplete($uidAll, $idAll, $uid, $pinkT, $bingo_people)
     {
         $pinkBool = 6;
         try {
             if (self::setPinkStatus($idAll)) {
                 self::setPinkStopTime($idAll);
+                $bingo = [];
+                for ($i = 0; $i < $bingo_people; $i++) {
+                    do {
+                        $id = rand(0, count($idAll));
+                    } while (in_array($id, $bingo));
+                    $bingo[] = $id;
+                }
                 if (in_array($uid, $uidAll)) {
                     if (self::isTpl($uidAll, $pinkT['id'])) self::orderPinkAfter($uidAll, $pinkT['id']);
                     $pinkBool = 1;
                 } else  $pinkBool = 3;
+                foreach ($idAll as $v) {
+                    $pink = self::get($v);
+                    if (!in_array($v, $bingo)) {
+                        StoreOrder::refundPink($pink['order_id_key']) && self::pinkReturn($pink['order_id_key']);
+                    } else {
+                        TreeRecommend::insertTree($pink['uid'], User::where('uid', $pink['uid'])->value('spread_uid'));
+                    }
+                }
             }
             return $pinkBool;
         } catch (\Exception $e) {
@@ -570,7 +587,8 @@ class StorePink extends BaseModel
                 $pinkBool = -1;
                 array_push($pinkAll, $pinkT);
                 foreach ($pinkAll as $v) {
-                    if (StoreOrder::orderApplyRefund(StoreOrder::getPinkOrderId($v['order_id_key']), $v['uid'], '拼团时间超时') && self::isTpl($v['uid'], $pinkT['id'])) {
+//                    if (StoreOrder::orderApplyRefund(StoreOrder::getPinkOrderId($v['order_id_key']), $v['uid'], '拼团时间超时') && self::isTpl($v['uid'], $pinkT['id'])) {
+                    if (StoreOrder::refundPink($v['order_id_key']) && self::isTpl($v['uid'], $pinkT['id'])) {
                         if ($isIds) array_push($pinkIds, $v['id']);
                         self::orderPinkAfterNo($pinkT['uid'], $pinkT['id']);
                     } else {
@@ -587,6 +605,25 @@ class StorePink extends BaseModel
         }
     }
 
+    public static function pinkReturn($order_id)
+    {
+        $order = StoreOrder::where('id', $order_id)->find();
+        if (!$order) return self::setErrorInfo('数据不存在');
+        $cart_info = StoreOrderCartInfo::where('oid', $order['id'])->where('cart_id', 'in', $order['cart_id'])->select();
+        $return = 0;
+        foreach ($cart_info as $v) {
+            $info = json_decode($v['cart_info']);
+            $return += $info['cart_num'] * ($info['productInfo']['attrInfo']['return']);
+        }
+        $res = true;
+        if ($return > 0) {
+            $balance = User::where('uid', $order['uid'])->value('brokerage_price');
+            $res = $res && UserBill::income('拼团佣金', $order['uid'], 'now_money', 'brokerage', $return, $order_id, $balance + $return, '参加拼团未中,获得佣金' . $return);
+            $res = $res && User::where('uid', $order['uid'])->inc('brokerage_price', $return)->update();
+        }
+        return $res;
+    }
+
     /*
      * 获取参团人和团长和拼团总人数
      * @param array $pink
@@ -644,7 +681,7 @@ class StorePink extends BaseModel
                     if (isset($pinkAll[0])) $nextPinkT = $pinkAll[0];
                 } else {
                     //拼团完成
-                    self::PinkComplete($uidAll, $idAll, $uid, $pinkT);
+                    self::PinkComplete($uidAll, $idAll, $uid, $pinkT, StoreCombination::where('id', $cid)->value('bingo_people'));
                     return self::setErrorInfo(['status' => 200, 'msg' => '拼团已完成,无法取消']);
                 }
             }
@@ -776,7 +813,8 @@ class StorePink extends BaseModel
         $refundPinkList = self::where('id', 'IN', $pinkList)->column('order_id,uid', 'id');
         if (!count($refundPinkList)) return true;
         foreach ($refundPinkList as $key => &$item) {
-            StoreOrder::orderApplyRefund($item['order_id'], $item['uid'], '拼团时间超时');//申请退款
+            StoreOrder::refundPink($item['id']);
+//            StoreOrder::orderApplyRefund($item['order_id'], $item['uid'], '拼团时间超时');//申请退款
         }
     }
 

+ 23 - 11
app/models/tree/TreeRecommend.php

@@ -5,6 +5,7 @@ namespace app\models\tree;
 
 
 use app\models\user\User;
+use app\models\user\UserBill;
 use crmeb\basic\BaseModel;
 use crmeb\traits\ModelTrait;
 use think\db\exception\DataNotFoundException;
@@ -25,8 +26,18 @@ class TreeRecommend extends BaseModel
      */
     protected $name = 'tree_recommend';
 
+    private static $layers;
+    private static $get;
+
     use ModelTrait;
 
+    public function __construct(array $data = [])
+    {
+        parent::__construct($data);
+        self::$layers = 12;
+        self::$get = 130;
+    }
+
     /**
      * 排点
      * @param int $uid
@@ -58,11 +69,11 @@ class TreeRecommend extends BaseModel
             $res = self::where('way', $way)->where('parent_id', $parent_last_point['id'])->find();
             if ($res) {
                 for ($i = 0; ; $i++) {
-                    if ($i == 11) {
-//                        $spreads = User::where('spread_uid', $parent_last_point['uid'])->column('uid');
-//                        if (count(self::where('uid', 'in', $spreads)->group('uid')->field('uid,COUNT(uid)')->select()) >= $way) {
-                        break;
-//                        }
+                    if ($i == self::$layers - 1) {
+                        $spreads = User::where('spread_uid', $parent_last_point['uid'])->column('uid');
+                        if (count(self::where('uid', 'in', $spreads)->group('uid')->field('uid,COUNT(uid)')->select()) >= $way) {
+                            break;
+                        }
                     }
                     $id = $res['id'];
                     $res = self::where('way', 1)->where('parent_id', $id)->find();
@@ -99,9 +110,7 @@ class TreeRecommend extends BaseModel
                 'way' => $way,
                 'add_time' => time(),
             ]);
-
-            $res = $res && self::sendToUper($tree_leader);
-
+            $res = $res && self::sendToUper($tree_leader, $res->id);
             self::checkTrans($res);
             if ($res) {
                 return true;
@@ -122,14 +131,17 @@ class TreeRecommend extends BaseModel
      * @throws DbException
      * @throws ModelNotFoundException
      */
-    public static function sendToUper(int $point): bool
+    public static function sendToUper(int $point, $link_id): bool
     {
         $info = self::where('id', $point)->find();
         $res = true;
         $i = 0;
-        while ($info && $i < 12) {
-            $info->get += 130;
+        while ($info && $i < self::$layers) {
+            $info->get += self::$get;
             $res = $res && $info->save();
+            $balance = User::where('uid', $info['uid'])->value('brokerage_price');
+            $res = $res && UserBill::income('下级参与', $info['uid'], 'now_money', 'brokerage', self::$get, $link_id, $balance + self::$get, '下级用户参与分红,获得分红' . self::$get);
+            $res = $res && User::where('uid', $info['uid'])->inc('brokerage_price', self::$get)->update();
             $info = self::where('id', $info['parent_id'])->find();
             $i++;
         }