Browse Source

update
修复ModelTree排序更改bug

jqh 5 years ago
parent
commit
08e371be11
3 changed files with 47 additions and 16 deletions
  1. 5 1
      src/Controllers/PermissionController.php
  2. 2 1
      src/Grid/Concerns/HasTree.php
  3. 40 14
      src/Traits/ModelTree.php

+ 5 - 1
src/Controllers/PermissionController.php

@@ -296,7 +296,11 @@ class PermissionController extends Controller
 
             $form->display('id', 'ID');
 
-            $form->select('parent_id', trans('admin.parent_id'))->options($permissionModel::selectOptions());
+            $form->select('parent_id', trans('admin.parent_id'))
+                ->options($permissionModel::selectOptions())
+                ->saving(function ($v) {
+                   return (int) $v;
+                });
 
             $form->text('slug', trans('admin.slug'))
                 ->required()

+ 2 - 1
src/Grid/Concerns/HasTree.php

@@ -140,7 +140,8 @@ HTML
             && ! $this->findQueryByMethod('orderByDesc')
             && ($orderColumn = $this->repository->getOrderColumn())
         ) {
-            $this->orderBy($orderColumn);
+            $this->orderBy($orderColumn)
+                ->orderBy($this->repository->getKeyName());
         }
     }
 

+ 40 - 14
src/Traits/ModelTree.php

@@ -222,7 +222,17 @@ trait ModelTree
         $orderColumnName = $this->determineOrderColumnName();
         $parentColumnName = $this->getParentColumn();
 
-        $swapWithModel = $this->buildSortQuery()->limit(1)
+        $sameOrderModel = $this->getSameOrderModel('>');
+
+        if ($sameOrderModel) {
+            $this->$orderColumnName = $this->$orderColumnName + 1;
+
+            $this->save();
+            return $this;
+        }
+
+        $swapWithModel = $this->buildSortQuery()
+            ->limit(1)
             ->ordered()
             ->where($orderColumnName, '>', $this->$orderColumnName)
             ->where($parentColumnName, $this->$parentColumnName)
@@ -240,24 +250,49 @@ trait ModelTree
         $orderColumnName = $this->determineOrderColumnName();
         $parentColumnName = $this->getParentColumn();
 
-        $swapWithModel = $this->buildSortQuery()->limit(1)
+        $swapWithModel = $this->buildSortQuery()
+            ->limit(1)
             ->ordered('desc')
             ->where($orderColumnName, '<', $this->$orderColumnName)
             ->where($parentColumnName, $this->$parentColumnName)
             ->first();
 
-        if (! $swapWithModel) {
+        if ($swapWithModel) {
+            return $this->swapOrderWithModel($swapWithModel);
+        }
+
+        $sameOrderModel = $this->getSameOrderModel('<');
+
+        if (! $sameOrderModel) {
             return false;
         }
+        $sameOrderModel->$orderColumnName = $sameOrderModel->$orderColumnName + 1;
+        $sameOrderModel->save();
 
-        return $this->swapOrderWithModel($swapWithModel);
+        return $this;
+    }
+
+    protected function getSameOrderModel(string $operator = '<')
+    {
+        $orderColumnName = $this->determineOrderColumnName();
+        $parentColumnName = $this->getParentColumn();
+
+        return $this->buildSortQuery()
+            ->limit(1)
+            ->orderBy($orderColumnName)
+            ->orderBy($this->getKeyName())
+            ->where($this->getKeyName(), $operator, $this->getKey())
+            ->where($orderColumnName, $this->$orderColumnName)
+            ->where($parentColumnName, $this->$parentColumnName)
+            ->first();
     }
 
     public function moveToStart()
     {
         $parentColumnName = $this->getParentColumn();
 
-        $firstModel = $this->buildSortQuery()->limit(1)
+        $firstModel = $this->buildSortQuery()
+            ->limit(1)
             ->ordered()
             ->where($parentColumnName, $this->$parentColumnName)
             ->first();
@@ -276,15 +311,6 @@ trait ModelTree
         return $this;
     }
 
-    public function getHighestOrderNumber(): int
-    {
-        $parentColumnName = $this->getParentColumn();
-
-        return (int) $this->buildSortQuery()
-            ->where($parentColumnName, $this->$parentColumnName)
-            ->max($this->determineOrderColumnName());
-    }
-
     /**
      * Get options for Select field in form.
      *