Kirin 3 年 前
コミット
78c153b061

+ 2 - 0
app/admin/controller/system/SystemCleardata.php

@@ -56,6 +56,8 @@ class SystemclearData extends AuthController
         self::clearData('user_group', 1);
         self::clearData('user_spread', 1);
         self::clearData('user_visit', 1);
+        self::clearData('tree', 1);
+        self::clearData('tree_recommend', 1);
         $this->delDirAndFile('./public/uploads/store/comment');
         self::clearData('store_product_relation', 1);
         return Json::successful('清除数据成功!');

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

@@ -12,6 +12,7 @@ use app\models\system\SystemCity;
 use app\models\system\SystemStore;
 use app\models\system\SystemStoreStaff;
 use app\models\tree\Tree;
+use app\models\tree\TreeRecommend;
 use app\models\user\User;
 use app\models\user\UserBill;
 use app\models\user\WechatUser;
@@ -35,8 +36,8 @@ class PublicController
 
     public function test()
     {
-        for ($i = 1; $i < 751; $i++) {
-            Tree::insertTree($i, rand(0, $i - 1));
+        for ($i = 1; $i < 2001; $i++) {
+            TreeRecommend::insertTree($i, rand(0, $i - 1));
         }
     }
 

+ 134 - 0
app/models/tree/TreeRecommend.php

@@ -0,0 +1,134 @@
+<?php
+
+
+namespace app\models\tree;
+
+
+use app\models\user\User;
+use crmeb\basic\BaseModel;
+use crmeb\traits\ModelTrait;
+use think\db\exception\DataNotFoundException;
+use think\db\exception\DbException;
+use think\db\exception\ModelNotFoundException;
+
+class TreeRecommend extends BaseModel
+{
+    /**
+     * 数据表主键
+     * @var string
+     */
+    protected $pk = 'id';
+
+    /**
+     * 模型名称
+     * @var string
+     */
+    protected $name = 'tree_recommend';
+
+    use ModelTrait;
+
+    /**
+     * 排点
+     * @param int $uid
+     * @param int $spread_uid
+     * @return array|bool|int[]
+     * @throws DataNotFoundException
+     * @throws DbException
+     * @throws ModelNotFoundException
+     */
+    public static function getTreePoint(int $spread_uid = 0)
+    {
+        if (!self::count()) {
+            return [0, 1];//父节点,线路
+        }
+        $parent_last_point = self::where('uid', $spread_uid)->order('add_time', 'desc')->find();
+        while (!$parent_last_point) {
+            $spread_uid = User::where('uid', $spread_uid)->value('spread_uid');
+            if (!$spread_uid)
+                $parent_last_point = self::order('add_time', 'asc')->find();
+            else
+                $parent_last_point = self::where('uid', $spread_uid)->order('add_time', 'desc')->find();
+        }
+        $parent_last_point = $parent_last_point['id'];
+        $way = 1;
+        while (1) {
+            //看线
+            $res = self::where('way', $way)->where('parent_id', $parent_last_point['id'])->find();
+            if ($res) {
+                for ($i = 0; ; $i++) {
+                    if ($i == 10) {
+                        $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();
+                    if (!$res) {
+                        return [$id, 1];
+                    }
+                }
+                $way++;
+            } else {
+                return [$parent_last_point['id'], $way];
+            }
+        }
+    }
+
+    /**
+     * 进场
+     * @param int $uid
+     * @param int $spread_uid
+     * @return bool
+     * @throws DataNotFoundException
+     * @throws DbException
+     * @throws ModelNotFoundException
+     */
+    public static function insertTree(int $uid, int $spread_uid = 0): bool
+    {
+        $res = self::getTreePoint($spread_uid);
+        if (!$res) return self::setErrorInfo(self::getErrorInfo());
+        list($tree_leader, $way) = $res;
+        self::beginTrans();
+        try {
+            $res = self::create([
+                'uid' => $uid,
+                'parent_id' => $tree_leader,
+                'way' => $way,
+                'add_time' => time(),
+            ]);
+
+            $res = $res && self::sendToUper($tree_leader);
+
+            self::checkTrans($res);
+            if ($res) {
+                return true;
+            } else {
+                return self::setErrorInfo('加入失败');
+            }
+        } catch (\Exception $e) {
+            self::rollbackTrans();
+            return self::setErrorInfo($e->getMessage());
+        }
+    }
+
+    /**
+     * 发奖
+     * @param int $point
+     * @return bool
+     * @throws DataNotFoundException
+     * @throws DbException
+     * @throws ModelNotFoundException
+     */
+    public static function sendToUper(int $point): bool
+    {
+        $info = self::where('id', $point)->find();
+        $res = true;
+        while ($info) {
+            $info->get += 130;
+            $res = $res && $info->save();
+            $info = self::where('id', $info['parent_id'])->find();
+        }
+        return $res;
+    }
+}