This commit is contained in:
2026-09-15 10:35:56 +08:00
commit 782f8ea7aa
4086 changed files with 631288 additions and 0 deletions
@@ -0,0 +1,97 @@
<?php
declare(strict_types=1);
namespace Esun\LingxingErp\Model\Sku;
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 PairFetcher
{
/**
* Lingxing API documentation:
* https://apidoc.lingxing.com/#/docs/MultiPlatform/V2/PairListV2
*/
private const MAX_PAGES = 10000;
private const MSKU_BATCH_SIZE = 200;
public function __construct(
private readonly Client $client,
private readonly LogWriter $logWriter
) {
}
/** @param string[] $mskus @return array<int, array<string, mixed>> */
public function fetchAll(int $platformCode, string $storeId, array $mskus, int $pageSize): array
{
$pairs = [];
foreach (array_chunk(array_values(array_unique($mskus)), self::MSKU_BATCH_SIZE) as $mskuBatch) {
$offset = 0;
for ($page = 1; $page <= self::MAX_PAGES; $page++) {
$request = [
'offset' => $offset,
'length' => $pageSize,
'msku' => $mskuBatch,
'platform_codes' => [(string)$platformCode],
'store_ids' => [$storeId],
];
$started = microtime(true);
$response = [];
try {
$response = $this->client->post(Endpoint::PAIR_LIST, $request);
$rows = $response['data']['list'] ?? null;
if (!is_array($rows)) {
throw new \RuntimeException('Lingxing PairListV2 response is missing data.list.');
}
$this->logWriter->write([
'direction' => 'pull',
'operation' => 'sku_pair_lookup',
'entity_type' => 'product',
'external_id' => $storeId,
'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' => 'sku_pair_lookup',
'entity_type' => 'product',
'external_id' => $storeId,
'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)) {
$pairs[] = $row;
}
}
$count = count($rows);
if ($count < $pageSize) {
break;
}
$offset += $count;
if ($page === self::MAX_PAGES) {
throw new \RuntimeException('Lingxing PairListV2 exceeded the maximum pagination limit.');
}
}
}
return $pairs;
}
}
@@ -0,0 +1,219 @@
<?php
declare(strict_types=1);
namespace Esun\LingxingErp\Model\Sku;
use Magento\Catalog\Model\ResourceModel\Product\Action as ProductActionResource;
use Magento\Catalog\Api\Data\ProductInterface;
use Magento\Catalog\Model\Product;
use Magento\Eav\Model\Config as EavConfig;
use Magento\Framework\App\ResourceConnection;
use Magento\Framework\EntityManager\MetadataPool;
use Magento\Store\Model\StoreManagerInterface;
class ProductErpSkuRepository
{
public function __construct(
private readonly ResourceConnection $resource,
private readonly ProductActionResource $productActionResource,
private readonly StoreManagerInterface $storeManager,
private readonly EavConfig $eavConfig,
private readonly MetadataPool $metadataPool
) {
}
/** @return string[] */
public function getWebsiteProductSkus(int $websiteId): array
{
return array_keys($this->getWebsiteProductIdsBySku($websiteId));
}
/** @return string[] */
public function getWebsiteProductSkusWithErpSku(int $websiteId): array
{
$defaultStore = $this->getDefaultStore($websiteId);
$attributeId = (int)$this->eavConfig
->getAttribute(Product::ENTITY, 'erp_sku')
->getAttributeId();
if ($attributeId <= 0) {
throw new \RuntimeException('The erp_sku product attribute does not exist.');
}
$connection = $this->resource->getConnection();
$linkField = $this->metadataPool->getMetadata(ProductInterface::class)->getLinkField();
$select = $connection->select()
->distinct()
->from(
['product' => $this->resource->getTableName('catalog_product_entity')],
['sku']
)
->joinInner(
['website' => $this->resource->getTableName('catalog_product_website')],
'website.product_id = product.entity_id',
[]
)
->joinInner(
['erp_sku_value' => $this->resource->getTableName('catalog_product_entity_varchar')],
sprintf('erp_sku_value.%1$s = product.%1$s', $linkField),
[]
)
->where('website.website_id = ?', $websiteId)
->where('erp_sku_value.attribute_id = ?', $attributeId)
->where('erp_sku_value.store_id = ?', (int)$defaultStore->getId())
->where('erp_sku_value.value IS NOT NULL')
->where('TRIM(erp_sku_value.value) <> ?', '')
->order('product.entity_id ASC');
return array_map('strval', $connection->fetchCol($select));
}
/** @return string[] */
public function findMskusByErpSku(int $websiteId, string $erpSku): array
{
$erpSku = trim($erpSku);
if ($erpSku === '') {
return [];
}
$defaultStore = $this->getDefaultStore($websiteId);
$attributeId = (int)$this->eavConfig
->getAttribute(Product::ENTITY, 'erp_sku')
->getAttributeId();
if ($attributeId <= 0) {
throw new \RuntimeException('The erp_sku product attribute does not exist.');
}
$connection = $this->resource->getConnection();
$linkField = $this->metadataPool->getMetadata(ProductInterface::class)->getLinkField();
$select = $connection->select()
->from(
['product' => $this->resource->getTableName('catalog_product_entity')],
['sku']
)
->joinInner(
['website' => $this->resource->getTableName('catalog_product_website')],
'website.product_id = product.entity_id',
[]
)
->joinInner(
['erp_sku_value' => $this->resource->getTableName('catalog_product_entity_varchar')],
sprintf('erp_sku_value.%1$s = product.%1$s', $linkField),
[]
)
->where('website.website_id = ?', $websiteId)
->where('erp_sku_value.attribute_id = ?', $attributeId)
->where('erp_sku_value.store_id = ?', (int)$defaultStore->getId())
->where('erp_sku_value.value = ?', $erpSku)
->order('product.entity_id ASC');
return array_values(array_unique(array_map('strval', $connection->fetchCol($select))));
}
/**
* @param array<int, array<string, mixed>> $rawPairs
* @return array{requested:int,pairs:int,mapped:int,unmapped:int,unmapped_mskus:string[]}
*/
public function updateWebsiteErpSkus(
int $websiteId,
int $platformCode,
string $storeId,
array $rawPairs
): array {
$pairs = $this->normalizeAndValidate($platformCode, $storeId, $rawPairs);
$productIdsBySku = $this->getWebsiteProductIdsBySku($websiteId);
$defaultStore = $this->getDefaultStore($websiteId);
$storeViewId = (int)$defaultStore->getId();
if ($productIdsBySku !== []) {
$this->productActionResource->updateAttributes(
array_values($productIdsBySku),
['erp_sku' => null],
$storeViewId
);
}
$mappedMskus = [];
foreach ($pairs as $msku => $erpSku) {
if (!isset($productIdsBySku[$msku])) {
continue;
}
$this->productActionResource->updateAttributes(
[$productIdsBySku[$msku]],
['erp_sku' => $erpSku],
$storeViewId
);
$mappedMskus[$msku] = true;
}
$unmappedMskus = array_values(array_diff(array_keys($productIdsBySku), array_keys($mappedMskus)));
return [
'requested' => count($productIdsBySku),
'pairs' => count($pairs),
'mapped' => count($mappedMskus),
'unmapped' => count($unmappedMskus),
// Keep attempt logs useful without allowing one response to exceed the log column.
'unmapped_mskus' => array_slice($unmappedMskus, 0, 200),
];
}
/** @param array<int, array<string, mixed>> $rawPairs @return array<string, string> */
private function normalizeAndValidate(int $platformCode, string $storeId, array $rawPairs): array
{
$byMsku = [];
$erpSkuByNormalizedMsku = [];
foreach ($rawPairs as $row) {
if ((string)($row['platform_code'] ?? '') !== (string)$platformCode
|| (string)($row['store_id'] ?? '') !== $storeId
) {
continue;
}
$msku = trim((string)($row['msku'] ?? ''));
$erpSku = trim((string)($row['sku'] ?? ''));
if ($msku === '' || $erpSku === '') {
continue;
}
$normalizedMsku = mb_strtolower($msku);
if (isset($erpSkuByNormalizedMsku[$normalizedMsku])
&& $erpSkuByNormalizedMsku[$normalizedMsku] !== $erpSku
) {
throw new \RuntimeException(sprintf(
'Lingxing returned multiple ERP SKUs for MSKU "%s" in Website mapping %d/%s.',
$msku,
$platformCode,
$storeId
));
}
$byMsku[$msku] = $erpSku;
$erpSkuByNormalizedMsku[$normalizedMsku] = $erpSku;
}
return $byMsku;
}
/** @return array<string, int> */
private function getWebsiteProductIdsBySku(int $websiteId): array
{
$connection = $this->resource->getConnection();
$select = $connection->select()
->from(['product' => $this->resource->getTableName('catalog_product_entity')], ['sku', 'entity_id'])
->joinInner(
['website' => $this->resource->getTableName('catalog_product_website')],
'website.product_id = product.entity_id',
[]
)
->where('website.website_id = ?', $websiteId)
->where('product.sku <> ?', '')
->order('product.entity_id ASC');
$result = [];
foreach ($connection->fetchAll($select) as $row) {
$result[(string)$row['sku']] = (int)$row['entity_id'];
}
return $result;
}
private function getDefaultStore(int $websiteId): \Magento\Store\Api\Data\StoreInterface
{
$store = $this->storeManager->getWebsite($websiteId)->getDefaultStore();
if ($store === null) {
throw new \RuntimeException(sprintf('Magento Website ID %d has no default store view.', $websiteId));
}
return $store;
}
}
@@ -0,0 +1,96 @@
<?php
declare(strict_types=1);
namespace Esun\LingxingErp\Model\Sku;
use Esun\LingxingErp\Model\Config;
use Esun\LingxingErp\Model\Sync\LogWriter;
use Magento\Framework\Lock\LockManagerInterface;
use Magento\Store\Model\StoreManagerInterface;
class SkuSynchronizer
{
private const LOCK_NAME = 'esun_lingxing_sku_sync';
public function __construct(
private readonly Config $config,
private readonly StoreManagerInterface $storeManager,
private readonly PairFetcher $pairFetcher,
private readonly ProductErpSkuRepository $repository,
private readonly LogWriter $logWriter,
private readonly LockManagerInterface $lockManager
) {
}
/** @return array{websites:int,pairs:int,mapped:int,unmapped:int,errors:array<int, string>} */
public function synchronizeAll(): array
{
if (!$this->lockManager->lock(self::LOCK_NAME, 0)) {
throw new \RuntimeException('ERP SKU synchronization is already running.');
}
$summary = ['websites' => 0, 'pairs' => 0, 'mapped' => 0, 'unmapped' => 0, 'errors' => []];
try {
foreach ($this->storeManager->getWebsites(false) as $website) {
$websiteId = (int)$website->getId();
if ($websiteId <= 0 || !$this->config->hasStoreMapping($websiteId)) {
continue;
}
$started = microtime(true);
$platformCode = $this->config->getPlatformCode($websiteId);
$storeId = $this->config->getLingxingStoreId($websiteId);
$request = [
'website_id' => $websiteId,
'platform_codes' => [(string)$platformCode],
'store_ids' => [$storeId],
];
try {
$mskus = $this->repository->getWebsiteProductSkus($websiteId);
$pairs = $this->pairFetcher->fetchAll(
$platformCode,
$storeId,
$mskus,
$this->config->getSkuSyncPageSize()
);
$result = $this->repository->updateWebsiteErpSkus(
$websiteId,
$platformCode,
$storeId,
$pairs
);
$summary['websites']++;
$summary['pairs'] += $result['pairs'];
$summary['mapped'] += $result['mapped'];
$summary['unmapped'] += $result['unmapped'];
$this->logWriter->write([
'direction' => 'pull',
'operation' => 'sku_sync',
'entity_type' => 'product',
'external_id' => $storeId,
'status' => 'success',
'attempt_number' => 1,
'request_payload' => $request,
'response_payload' => $result,
'duration_ms' => (int)round((microtime(true) - $started) * 1000),
]);
} catch (\Throwable $exception) {
$summary['errors'][$websiteId] = $exception->getMessage();
$this->logWriter->write([
'direction' => 'pull',
'operation' => 'sku_sync',
'entity_type' => 'product',
'external_id' => $storeId,
'status' => 'failed',
'attempt_number' => 1,
'error_code' => (string)$exception->getCode(),
'error_message' => $exception->getMessage(),
'request_payload' => $request,
'duration_ms' => (int)round((microtime(true) - $started) * 1000),
]);
}
}
} finally {
$this->lockManager->unlock(self::LOCK_NAME);
}
return $summary;
}
}