Skip to content

Commit 7b6355c

Browse files
authored
Update OrderService.php
1 parent 559b0c2 commit 7b6355c

File tree

1 file changed

+21
-16
lines changed

1 file changed

+21
-16
lines changed

src/Services/OrderService.php

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22

3-
namespace Alper\LaravelOrder\Services;
3+
namespace App\Helpers\HelperServices;
44

55
use Illuminate\Database\Eloquent\Model;
66

@@ -31,7 +31,7 @@ public function newOrder(): int
3131
{
3232
$conditions = $this->conditions;
3333
$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)
3535
->where(function ($q) use ($conditions, $record) {
3636
foreach ($conditions as $condition) {
3737
$q->where($condition, $record->{$condition});
@@ -50,7 +50,7 @@ public function updateOrder(): int
5050
}
5151
})->orderBy($this->orderAttrName)->get();
5252

53-
$new_order = (int) $record->{$this->orderAttrName} ?? $this->newOrder();
53+
$new_order = (int)$record->{$this->orderAttrName} ?? $this->newOrder();
5454
if (count($collection) > 0) {
5555
if ($collection->last()?->{$this->orderAttrName} < $record->{$this->orderAttrName}) {
5656
$new_order = $collection->last()->{$this->orderAttrName};
@@ -66,23 +66,24 @@ public function updateOrder(): int
6666
}
6767

6868
while (true) {
69-
if (! ((int) $new_order - 1) > 0) {
69+
if (! ((int)$new_order - 1) > 0) {
7070
break;
7171
}
72-
$emptyOld = $collection->where($this->orderAttrName, ((int) $new_order - 1))->first();
72+
$emptyOld = $collection->where($this->orderAttrName, ((int)$new_order - 1))->first();
7373
if ($emptyOld) {
7474
break;
7575
}
76-
$new_order = (int) $new_order - 1;
76+
$new_order = (int)$new_order - 1;
7777
}
7878

79-
$old = $collection->where($this->orderAttrName, (int) $record->{$this->orderAttrName})->first();
79+
$old = $collection->where($this->orderAttrName, (int)$record->{$this->orderAttrName})->first();
8080
if ($old) {
81-
$changes = $collection->where($this->orderAttrName, '>=', (int) $record->{$this->orderAttrName});
81+
$changes = $collection->where($this->orderAttrName, '>=', (int)$record->{$this->orderAttrName});
8282
if ($new_order > $record->{$this->orderAttrName}) {
83-
$changes = $changes->where($this->orderAttrName, '<=', (int) $new_order);
83+
$changes = $changes->where($this->orderAttrName, '<=', (int)$new_order);
8484
} else {
85-
$changes = $changes->where($this->orderAttrName, '>=', (int) $new_order);
85+
$changes = $changes->where($this->orderAttrName, '>=', (int)$new_order)
86+
->where('id', '!=', $record->id);
8687
}
8788
if (count($changes) > 0) {
8889
$upsert = [];
@@ -94,9 +95,9 @@ public function updateOrder(): int
9495
}
9596
}
9697
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;
9899
} elseif ($c[$this->orderAttrName] >= $new_order) {
99-
$c[$this->orderAttrName] = (int) $c[$this->orderAttrName] + 1;
100+
$c[$this->orderAttrName] = (int)$c[$this->orderAttrName] + 1;
100101
}
101102
$upsert[] = $c;
102103
}
@@ -120,7 +121,7 @@ public function safeDeleteWithOrder(): void
120121
}
121122
})->get();
122123

123-
$oldAfter = $collection->where($this->orderAttrName, '>', (int) $record->{$this->orderAttrName});
124+
$oldAfter = $collection->where($this->orderAttrName, '>', (int)$record->{$this->orderAttrName});
124125
if (count($oldAfter) > 0) {
125126
$upsert = [];
126127
foreach ($oldAfter as $oa) {
@@ -130,13 +131,12 @@ public function safeDeleteWithOrder(): void
130131
$oa[$key] = json_encode($attr);
131132
}
132133
}
133-
$oa[$this->orderAttrName] = (int) $oa[$this->orderAttrName] - 1;
134+
$oa[$this->orderAttrName] = (int)$oa[$this->orderAttrName] - 1;
134135
$upsert[] = $oa;
135136
}
136137
$this->className::query()->upsert($upsert, 'id', [$this->orderAttrName]);
137138
}
138139
}
139-
$record->delete();
140140
}
141141

142142
public static function arrangeAllOrders(Model $model): void
@@ -145,7 +145,7 @@ public static function arrangeAllOrders(Model $model): void
145145
foreach ($model->orderUnificationAttributes as $attribute) {
146146
$q->where($attribute, $model->{$attribute});
147147
}
148-
})->orderBy($model->orderAttrName)->get();
148+
})->orderBy($model->orderAttrName)->where('id', '!=', $model->id)->get();
149149
$reSortsArr = [];
150150
$firstNumber = 1;
151151
foreach ($reSorts->whereNotNull($model->orderAttrName)->toArray() as $reSort) {
@@ -155,6 +155,11 @@ public static function arrangeAllOrders(Model $model): void
155155
}
156156
}
157157
$reSort['updated_at'] = now();
158+
159+
if ($firstNumber === $model->{($model->orderAttrName ?? 'order')} &&
160+
$reSort['id'] !== $model->id) {
161+
$firstNumber += 1;
162+
}
158163
$reSort[$model->orderAttrName] = $firstNumber;
159164
$reSortsArr[] = $reSort;
160165
$firstNumber += 1;

0 commit comments

Comments
 (0)