67 lines
2.2 KiB
PHP
67 lines
2.2 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\Stock;
|
|
|
|
final readonly class StocksSyncService 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 [
|
|
'date' => $row['date'],
|
|
'last_change_date' => $row['last_change_date'],
|
|
'supplier_article' => $row['supplier_article'],
|
|
'tech_size' => $row['tech_size'],
|
|
'barcode' => (string)$row['barcode'],
|
|
'quantity' => $row['quantity'],
|
|
'is_supply' => $row['is_supply'],
|
|
'is_realization' => $row['is_realization'],
|
|
'quantity_full' => $row['quantity_full'],
|
|
'warehouse_name' => $row['warehouse_name'],
|
|
'in_way_to_client' => $row['in_way_to_client'],
|
|
'in_way_from_client' => $row['in_way_from_client'],
|
|
'nm_id' => $row['nm_id'],
|
|
'subject' => $row['subject'],
|
|
'category' => $row['category'],
|
|
'brand' => $row['brand'],
|
|
'sc_code' => (string)$row['sc_code'],
|
|
'price' => $row['price'],
|
|
'discount' => $row['discount'],
|
|
'updated_at' => now(),
|
|
'created_at' => now(),
|
|
];
|
|
})->toArray();
|
|
|
|
Stock::insert($rows);// todo upsert
|
|
});
|
|
}
|
|
}
|