1
1
<?php
2
2
3
- namespace Alper \ LaravelOrder \ Services ;
3
+ namespace App \ Helpers \ HelperServices ;
4
4
5
5
use Illuminate \Database \Eloquent \Model ;
6
6
@@ -31,7 +31,7 @@ public function newOrder(): int
31
31
{
32
32
$ conditions = $ this ->conditions ;
33
33
$ record = $ this ->record ;
34
- return ((int ) $ this ->className ::whereNotNull ($ this ->orderAttrName )->orderByDesc ($ this ->orderAttrName )
34
+ return ((int )$ this ->className ::whereNotNull ($ this ->orderAttrName )->orderByDesc ($ this ->orderAttrName )
35
35
->where (function ($ q ) use ($ conditions , $ record ) {
36
36
foreach ($ conditions as $ condition ) {
37
37
$ q ->where ($ condition , $ record ->{$ condition });
@@ -50,7 +50,7 @@ public function updateOrder(): int
50
50
}
51
51
})->orderBy ($ this ->orderAttrName )->get ();
52
52
53
- $ new_order = (int ) $ record ->{$ this ->orderAttrName } ?? $ this ->newOrder ();
53
+ $ new_order = (int )$ record ->{$ this ->orderAttrName } ?? $ this ->newOrder ();
54
54
if (count ($ collection ) > 0 ) {
55
55
if ($ collection ->last ()?->{$ this ->orderAttrName } < $ record ->{$ this ->orderAttrName }) {
56
56
$ new_order = $ collection ->last ()->{$ this ->orderAttrName };
@@ -66,23 +66,24 @@ public function updateOrder(): int
66
66
}
67
67
68
68
while (true ) {
69
- if (! ((int ) $ new_order - 1 ) > 0 ) {
69
+ if (! ((int )$ new_order - 1 ) > 0 ) {
70
70
break ;
71
71
}
72
- $ emptyOld = $ collection ->where ($ this ->orderAttrName , ((int ) $ new_order - 1 ))->first ();
72
+ $ emptyOld = $ collection ->where ($ this ->orderAttrName , ((int )$ new_order - 1 ))->first ();
73
73
if ($ emptyOld ) {
74
74
break ;
75
75
}
76
- $ new_order = (int ) $ new_order - 1 ;
76
+ $ new_order = (int )$ new_order - 1 ;
77
77
}
78
78
79
- $ old = $ collection ->where ($ this ->orderAttrName , (int ) $ record ->{$ this ->orderAttrName })->first ();
79
+ $ old = $ collection ->where ($ this ->orderAttrName , (int )$ record ->{$ this ->orderAttrName })->first ();
80
80
if ($ old ) {
81
- $ changes = $ collection ->where ($ this ->orderAttrName , '>= ' , (int ) $ record ->{$ this ->orderAttrName });
81
+ $ changes = $ collection ->where ($ this ->orderAttrName , '>= ' , (int )$ record ->{$ this ->orderAttrName });
82
82
if ($ new_order > $ record ->{$ this ->orderAttrName }) {
83
- $ changes = $ changes ->where ($ this ->orderAttrName , '<= ' , (int ) $ new_order );
83
+ $ changes = $ changes ->where ($ this ->orderAttrName , '<= ' , (int )$ new_order );
84
84
} else {
85
- $ changes = $ changes ->where ($ this ->orderAttrName , '>= ' , (int ) $ new_order );
85
+ $ changes = $ changes ->where ($ this ->orderAttrName , '>= ' , (int )$ new_order )
86
+ ->where ('id ' , '!= ' , $ record ->id );
86
87
}
87
88
if (count ($ changes ) > 0 ) {
88
89
$ upsert = [];
@@ -94,9 +95,9 @@ public function updateOrder(): int
94
95
}
95
96
}
96
97
if ($ new_order >= $ record ->getOriginal ($ this ->orderAttrName ) && $ c [$ this ->orderAttrName ] <= $ new_order ) {
97
- $ c [$ this ->orderAttrName ] = (int ) $ c [$ this ->orderAttrName ] - 1 ;
98
+ $ c [$ this ->orderAttrName ] = (int )$ c [$ this ->orderAttrName ] - 1 ;
98
99
} elseif ($ c [$ this ->orderAttrName ] >= $ new_order ) {
99
- $ c [$ this ->orderAttrName ] = (int ) $ c [$ this ->orderAttrName ] + 1 ;
100
+ $ c [$ this ->orderAttrName ] = (int )$ c [$ this ->orderAttrName ] + 1 ;
100
101
}
101
102
$ upsert [] = $ c ;
102
103
}
@@ -120,7 +121,7 @@ public function safeDeleteWithOrder(): void
120
121
}
121
122
})->get ();
122
123
123
- $ oldAfter = $ collection ->where ($ this ->orderAttrName , '> ' , (int ) $ record ->{$ this ->orderAttrName });
124
+ $ oldAfter = $ collection ->where ($ this ->orderAttrName , '> ' , (int )$ record ->{$ this ->orderAttrName });
124
125
if (count ($ oldAfter ) > 0 ) {
125
126
$ upsert = [];
126
127
foreach ($ oldAfter as $ oa ) {
@@ -130,13 +131,12 @@ public function safeDeleteWithOrder(): void
130
131
$ oa [$ key ] = json_encode ($ attr );
131
132
}
132
133
}
133
- $ oa [$ this ->orderAttrName ] = (int ) $ oa [$ this ->orderAttrName ] - 1 ;
134
+ $ oa [$ this ->orderAttrName ] = (int )$ oa [$ this ->orderAttrName ] - 1 ;
134
135
$ upsert [] = $ oa ;
135
136
}
136
137
$ this ->className ::query ()->upsert ($ upsert , 'id ' , [$ this ->orderAttrName ]);
137
138
}
138
139
}
139
- $ record ->delete ();
140
140
}
141
141
142
142
public static function arrangeAllOrders (Model $ model ): void
@@ -145,7 +145,7 @@ public static function arrangeAllOrders(Model $model): void
145
145
foreach ($ model ->orderUnificationAttributes as $ attribute ) {
146
146
$ q ->where ($ attribute , $ model ->{$ attribute });
147
147
}
148
- })->orderBy ($ model ->orderAttrName )->get ();
148
+ })->orderBy ($ model ->orderAttrName )->where ( ' id ' , ' != ' , $ model -> id )-> get ();
149
149
$ reSortsArr = [];
150
150
$ firstNumber = 1 ;
151
151
foreach ($ reSorts ->whereNotNull ($ model ->orderAttrName )->toArray () as $ reSort ) {
@@ -155,6 +155,11 @@ public static function arrangeAllOrders(Model $model): void
155
155
}
156
156
}
157
157
$ reSort ['updated_at ' ] = now ();
158
+
159
+ if ($ firstNumber === $ model ->{($ model ->orderAttrName ?? 'order ' )} &&
160
+ $ reSort ['id ' ] !== $ model ->id ) {
161
+ $ firstNumber += 1 ;
162
+ }
158
163
$ reSort [$ model ->orderAttrName ] = $ firstNumber ;
159
164
$ reSortsArr [] = $ reSort ;
160
165
$ firstNumber += 1 ;
0 commit comments