Преглед изворни кода

优化用户修改上级功能

devolp пре 3 година
родитељ
комит
d029eb8b3c
1 измењених фајлова са 86 додато и 55 уклоњено
  1. 86 55
      app/admin/logic/UserLogic.php

+ 86 - 55
app/admin/logic/UserLogic.php

@@ -212,8 +212,8 @@ class UserLogic
 
     /**
      * 修改用户手机号码
-     * @param mixed $uid
-     * @param mixed $phone
+     * @param  $uid
+     * @param  $phone
      */
     public function modifyPhone($uid, $phone)
     {
@@ -249,8 +249,8 @@ class UserLogic
 
     /**
      * 修改用户所属上级
-     * @param mixed $uid
-     * @param mixed $pid
+     * @param  $uid
+     * @param  $pid
      */
     public function modifypid($uid, $pid)
     {
@@ -266,65 +266,34 @@ class UserLogic
             return "该用户已所属待变更的上级,不能修改";
         }
 
-        // 当前用户关系层级链路
-        $newUserPath = "";
 
-        if ($pid == 0) {
-            $newPathPrefix = $user['id'];
-
-        } else {
-            $pUser = User::getUserById($pid);
-            if (empty($pUser)) {
-                return "待变更的上级用户不存在";
-            }
-
-            $userPathArr = explode(',', $user['path']);
-            if (in_array($pid, $userPathArr)) {
-                $pidIndex          = array_search($pid, $userPathArr);
-                $userPathArrPrefix = array_slice($userPathArr, 0, $pidIndex + 1);
-            } else {
-                $pidIndex            = array_search($user['pid'], $userPathArr);
-                $userPathArrPrefix   = array_slice($userPathArr, 0, $pidIndex);
-                $userPathArrPrefix[] = $pid;
-            }
-            $newUserPath = implode(',', $userPathArrPrefix);
-
-            // 子级用户Path待替换关系层级前部分链表
-            $userPathArrPrefix[] = $user['id'];
-            $newPathPrefix       = implode(',', $userPathArrPrefix);
+        $parentUser = User::getUserById($pid);
+        if (empty($parentUser)) {
+            return '所属上级不存在';
         }
+        // 判断降级或升级
 
-        // 子级用户Path被替换关系层级前部分链表
-        $oldPathPrefix = $user['path'] == 0 ? $uid : $user['path'] . ',' . $uid;
-
+        // 判断升级,降级、插入
+        if ($user['pid'] != 0) {
+            $operateType = $this->getOperateType($user['id'], $user['path'], $pid, $parentUser['path']);
+        }
 
         Db::startTrans();
         try {
-            // 子级path变更
-            $result = Db::table(User::$table)
-                ->where([
-                    ['path', 'like', $oldPathPrefix . '%'],
-                    ['id', '<>', $uid]
-                ])
-                ->field(['id', 'path'])
-                ->chunk(100, function ($users) use ($pid, $oldPathPrefix, $newPathPrefix, $uid) {
-                    foreach ($users as $user) {
-                        $newPath = str_replace($oldPathPrefix, $newPathPrefix, $user['path']);
-                        if (in_array($user['id'], explode(',', $newPath))) {
-                            return false;
-                        }
 
-                        User::modifyUserPath($user['id'], $newPath);
-                    }
-                });
+            //
+            if ($operateType) {
+                // 死循环,分支
+                $result = $this->modifypidByResetTopLevel($user['id'], $user['path'], $pid, $parentUser['path']);
+            } else {
+                // 正常,迭代修改下级
+                $result = $this->modifypidByUpdateNextLevel($user['id'], $user['path'], $pid, $parentUser['path']);
+            }
+
             if (!$result) {
                 Db::rollback();
                 return "修改用户所属上级失败,请确认用户的层级关系";
             }
-
-            // 更新用户Pid,Path
-            User::modifyUserPidAndPath($uid, $pid, $newUserPath);
-
             Db::commit();
         } catch (\Exception $exception) {
             Db::rollback();
@@ -333,11 +302,18 @@ class UserLogic
 
         return true;
 
+        // 升级,abcdef,e升级到b后 分为两种情况 1.abef 2.abcd
+
+        // 降级 abcdef,c降级到e后 分为两种情况 1. ecf 2.abcd
+
+        // 插入 abcdef,插入j到b后 分为两种情况 1.abj 2.abcdef
+
+
     }
 
     /**
      * 修改余额
-     * @param mixed $post
+     * @param  $post
      */
     public function ModifyMoney($post)
     {
@@ -387,7 +363,7 @@ class UserLogic
 
     /**
      * 修改积分
-     * @param mixed $post
+     * @param  $post
      */
     public function ModifyScore($post)
     {
@@ -453,7 +429,7 @@ class UserLogic
 
     /* 注册用户数量统计
      * @param string $time 时间节点如:2023-03-01
-     * @return mixed
+     * @return 
      * @throws \think\db\exception\DbException
      */
     public static function getCountByTime($time = '0')
@@ -476,4 +452,59 @@ class UserLogic
         return Cache::get($cacheKey);
     }
 
+    /**
+     * 判断操作类型,true-死循环 false-正常
+     * @param $pid
+     * @param $path
+     * @return int
+     */
+    private function getOperateType($uid, $path, $pid, $pUserPath)
+    {
+        $pUserPathArr = explode(',', $pUserPath);
+
+        return in_array($uid, $pUserPathArr);
+    }
+
+    private function modifypidByUpdateNextLevel($id, $path, $pid, $parentUserPath)
+    {
+        $parentUserPathArr = explode(',', $parentUserPath);
+        $newUserPathArr    = $parentUserPathArr;
+        $newUserPathArr[]  = $pid;
+
+        $result = $this->iteraMidifyPathByPid($newUserPathArr, $id);
+        if (!$result) return false;
+        // 更新用户Pid,Path
+        return User::modifyUserPidAndPath($id, $pid, implode(',', $newUserPathArr));
+    }
+
+    private function iteraMidifyPathByPid($newUserPathArr, $pid)
+    {
+        $newUserPathArr[] = $pid;
+        $result           = Db::table(User::$table)
+            ->where([
+                'pid' => $pid
+            ])
+            ->field(['id', 'path'])
+            ->chunk(100, function ($users) use ($pid, $newUserPathArr) {
+                foreach ($users as $user) {
+                    User::modifyUserPath($user['id'], implode(',', $newUserPathArr));
+                    $this->iteraMidifyPathByPid($newUserPathArr, $user['id']);
+                }
+            });
+        return $result;
+    }
+
+    private function modifypidByResetTopLevel($id, $path, $pid, $parentUserPath)
+    {
+        $newUserPathArr[] = $pid;
+
+        $result = $this->iteraMidifyPathByPid($newUserPathArr, $id);
+        if (!$result) return false;
+
+        // 独立分支
+        $result = User::modifyUserPidAndPath($pid, 0, '');
+        if (!$result) return false;
+        return User::modifyUserPidAndPath($id, $pid, implode(',', $newUserPathArr));
+    }
+
 }