devolp 3 年之前
父節點
當前提交
f2c3b6076c
共有 1 個文件被更改,包括 56 次插入14 次删除
  1. 56 14
      app/admin/logic/UserLogic.php

+ 56 - 14
app/admin/logic/UserLogic.php

@@ -274,17 +274,14 @@ class UserLogic
         // 判断降级或升级
 
         // 判断升级,降级、插入
-        if ($user['pid'] != 0) {
-            $operateType = $this->getOperateType($user['id'], $user['path'], $pid, $parentUser['path']);
-        }
+        list($operateType, $prefixPath) = $this->getOperateType($user['id'], $user['path'], $pid, $parentUser['path']);
 
         Db::startTrans();
         try {
 
-            //
             if ($operateType) {
                 // 死循环,分支
-                $result = $this->modifypidByResetTopLevel($user['id'], $user['path'], $pid, $parentUser['path']);
+                $result = $this->modifypidByResetTopLevel($user['id'], $user['path'], $pid, $parentUser['path'], $prefixPath);
             } else {
                 // 正常,迭代修改下级
                 $result = $this->modifypidByUpdateNextLevel($user['id'], $user['path'], $pid, $parentUser['path']);
@@ -460,9 +457,14 @@ class UserLogic
      */
     private function getOperateType($uid, $path, $pid, $pUserPath)
     {
-        $pUserPathArr = explode(',', $pUserPath);
+//        $pUserPathArr = explode(',', $pUserPath);
+        list($isFallLevel, $prefixPath) = $this->isFallLevel($uid, $pid);
+
+        if ($isFallLevel) {
+            return [true, $prefixPath];
+        }
 
-        return in_array($uid, $pUserPathArr);
+        return [false, null];
     }
 
     private function modifypidByUpdateNextLevel($id, $path, $pid, $parentUserPath)
@@ -494,17 +496,57 @@ class UserLogic
         return $result;
     }
 
-    private function modifypidByResetTopLevel($id, $path, $pid, $parentUserPath)
+    private function modifypidByResetTopLevel($id, $path, $pid, $parentUserPath, $prefixPath)
     {
         $newUserPathArr[] = $pid;
 
-        $result = $this->iteraMidifyPathByPid($newUserPathArr, $id);
-        if (!$result) return false;
+        $this->updatePidAndPathByPath($prefixPath);
+
+        $parentUser       = User::getUserById($pid);
+        $newUserPathArr   = explode(',', $parentUser['path']);
+        $newUserPathArr[] = $pid;
+
+        User::modifyUserPidAndPath($id, $pid, implode(',', $newUserPathArr));
+
+        $this->iteraMidifyPathByPid($newUserPathArr, $id);
+
+        return true;
+//        $result = User::modifyUserPidAndPath($pid, 0, '');
+//        if (!$result) return false;
+//        return User::modifyUserPidAndPath($id, $pid, implode(',', $newUserPathArr));
+    }
+
+    private function isFallLevel($uid, $pid)
+    {
+        $user = Db::query(sprintf("SELECT id,path,pid FROM db_user WHERE FIND_IN_SET('%s',path) and FIND_IN_SET('%s',path) LIMIT 1;", $uid, $pid));
+        if (empty($user)) {
+            return [false, null];
+        }
+        $userPath = explode(',', $user[0]['path']);
+        $uidIndex = array_search($uid, $userPath);
+        if ($uidIndex < array_search($pid, $userPath)) {
+            $returnPath = [];
+            if (isset($userPath[$uidIndex - 1])) {
+                $returnPath[] = $userPath[$uidIndex - 1];
+            }
+            $returnPath[] = $userPath[$uidIndex];
+            $returnPath[] = $userPath[$uidIndex + 1];
+
+            return [true, $returnPath];
+        }
+        return [false, null];
+    }
+
+    private function updatePidAndPathByPath($prefixPath)
+    {
+        $newTopUid = $prefixPath[count($prefixPath) - 1];
+
+        // 从阶段处更新,分支出来作为第一层
+        User::modifyUserPidAndPath($newTopUid, 0, '');
+
+        $newUserPathArr = [];
+        return $this->iteraMidifyPathByPid($newUserPathArr, $newTopUid);
 
-        // 独立分支
-        $result = User::modifyUserPidAndPath($pid, 0, '');
-        if (!$result) return false;
-        return User::modifyUserPidAndPath($id, $pid, implode(',', $newUserPathArr));
     }
 
 }