75 lines
2.4 KiB
PHP
75 lines
2.4 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Services\WbService\SyncServices;
|
|
|
|
use App\Contracts\WbClientInterface;
|
|
use App\Contracts\WbSyncInterface;
|
|
use App\DTO\WbRequestDTO;
|
|
use App\Exceptions\WbServiceException;
|
|
use App\Models\Order;
|
|
|
|
final readonly class OrdersSyncService implements WbSyncInterface
|
|
{
|
|
public function __construct(
|
|
private WbClientInterface $client,
|
|
) {
|
|
}
|
|
|
|
/**
|
|
* @throws WbServiceException
|
|
*/
|
|
public function sync(WbRequestDTO $dto): array
|
|
{
|
|
return $this->client->fetch($dto);
|
|
}
|
|
|
|
public function save(array $data): void
|
|
{
|
|
if (empty($data)) {
|
|
return;
|
|
}
|
|
|
|
collect($data)
|
|
->chunk(500)
|
|
->each(function ($chunk) {
|
|
$rows = $chunk->map(function (array $row) {
|
|
return [
|
|
'g_number' => $row['g_number'],
|
|
'date' => !empty($row['date']) ? $row['date'] : now(),
|
|
'last_change_date' => $row['last_change_date'],
|
|
'supplier_article' => $row['supplier_article'],
|
|
'tech_size' => $row['tech_size'],
|
|
'barcode' => (int)$row['barcode'],
|
|
'total_price' => $row['total_price'],
|
|
'discount_percent' => $row['discount_percent'],
|
|
'warehouse_name' => $row['warehouse_name'],
|
|
'oblast' => $row['oblast'],
|
|
'income_id' => $row['income_id'],
|
|
'odid' => (int)$row['odid'],
|
|
'nm_id' => $row['nm_id'],
|
|
'subject' => $row['subject'],
|
|
'category' => $row['category'],
|
|
'brand' => $row['brand'],
|
|
'is_cancel' => $row['is_cancel'],
|
|
'cancel_dt' => $row['cancel_dt'],
|
|
'created_at' => now(),
|
|
'updated_at' => now(),
|
|
];
|
|
})->toArray();
|
|
|
|
try {
|
|
Order::insert($rows);
|
|
} catch (\Throwable $e) {
|
|
\Log::error('Insert failed', [
|
|
'error' => $e->getMessage(),
|
|
'row_sample' => $rows[0] ?? null,
|
|
]);
|
|
|
|
throw $e;
|
|
}
|
|
});
|
|
}
|
|
}
|