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

67 lines
2.0 KiB
PHP

<?php
declare(strict_types=1);
namespace Esun\LingxingErp\Model\Sync;
use Magento\Framework\App\ResourceConnection;
class DetailProvider
{
private const SYNC_TABLES = [
'order' => 'esun_lingxing_order_sync',
'shipment' => 'esun_lingxing_shipment_sync',
];
public function __construct(private readonly ResourceConnection $resource)
{
}
/** @return array<string, mixed>|null */
public function getRecord(string $type, int $id): ?array
{
$table = $type === 'log'
? 'esun_lingxing_sync_log'
: (self::SYNC_TABLES[$type] ?? '');
if ($table === '' || $id <= 0) {
return null;
}
$connection = $this->resource->getConnection();
$row = $connection->fetchRow(
$connection->select()
->from($this->resource->getTableName($table))
->where('entity_id = ?', $id)
->limit(1)
);
return is_array($row) && $row !== [] ? $row : null;
}
/** @return array<int, array<string, mixed>> */
public function getLogs(string $type, array $record): array
{
if ($type === 'log') {
return [$record];
}
if (!isset(self::SYNC_TABLES[$type])) {
return [];
}
$connection = $this->resource->getConnection();
$select = $connection->select()
->from($this->resource->getTableName('esun_lingxing_sync_log'))
->where('entity_type = ?', $type)
->order('entity_id DESC');
$localId = (int)($record['entity_id'] ?? 0);
$requestId = trim((string)($record['request_id'] ?? ''));
if ($requestId !== '') {
$select->where(sprintf(
'(%s OR %s)',
$connection->quoteInto('local_entity_id = ?', $localId),
$connection->quoteInto('request_id = ?', $requestId)
));
} else {
$select->where('local_entity_id = ?', $localId);
}
return $connection->fetchAll($select);
}
}