Files
2026-09-15 10:35:56 +08:00

94 lines
3.5 KiB
PHP

<?php
declare(strict_types=1);
namespace Esun\LingxingErp\Model\Warehouse;
use Esun\LingxingErp\Model\Api\ApiException;
use Esun\LingxingErp\Model\Api\Client;
use Esun\LingxingErp\Model\Api\Endpoint;
use Esun\LingxingErp\Model\Sync\LogWriter;
class WarehouseFetcher
{
/**
* Lingxing API documentation:
* https://apidoc.lingxing.com/#/docs/Warehouse/WarehouseLists
*/
private const PAGE_SIZE = 1000;
private const WAREHOUSE_TYPE = 3;
public function __construct(
private readonly Client $client,
private readonly LogWriter $logWriter
) {
}
/** @return array<int, array<string, mixed>> */
public function fetchAll(): array
{
$warehouses = [];
$type = self::WAREHOUSE_TYPE;
$offset = 0;
do {
$request = [
'type' => $type,
'is_delete' => '0,1',
'offset' => $offset,
'length' => self::PAGE_SIZE,
];
$started = microtime(true);
$response = [];
try {
$response = $this->client->post(Endpoint::WAREHOUSE_LIST, $request);
$rows = $response['data'] ?? null;
if (!is_array($rows)) {
throw new \RuntimeException('Lingxing warehouse response is missing data.');
}
$this->logWriter->write([
'direction' => 'pull',
'operation' => 'warehouse_sync',
'entity_type' => 'warehouse',
'external_id' => (string)$type,
'status' => 'success',
'attempt_number' => 1,
'request_id' => (string)($response['request_id'] ?? ''),
'request_payload' => $request,
'response_payload' => $response,
'duration_ms' => (int)round((microtime(true) - $started) * 1000),
]);
} catch (\Throwable $exception) {
$apiResponse = $exception instanceof ApiException ? $exception->getResponse() : $response;
$this->logWriter->write([
'direction' => 'pull',
'operation' => 'warehouse_sync',
'entity_type' => 'warehouse',
'external_id' => (string)$type,
'status' => 'failed',
'attempt_number' => 1,
'error_code' => $exception instanceof ApiException
? $exception->getErrorCode()
: (string)$exception->getCode(),
'error_message' => $exception->getMessage(),
'request_id' => (string)($apiResponse['request_id'] ?? ''),
'request_payload' => $request,
'response_payload' => $apiResponse,
'duration_ms' => (int)round((microtime(true) - $started) * 1000),
]);
throw $exception;
}
foreach ($rows as $row) {
if (!is_array($row) || (int)($row['wid'] ?? 0) <= 0) {
continue;
}
$row['type'] = (int)($row['type'] ?? $type);
$warehouses[(int)$row['wid']] = $row;
}
$received = count($rows);
$offset += $received;
$total = max(0, (int)($response['total'] ?? $offset));
} while ($received > 0 && $offset < $total);
return array_values($warehouses);
}
}