ltsecurityinc安防产品在线商城代码
This commit is contained in:
+190
@@ -0,0 +1,190 @@
|
||||
<?php
|
||||
|
||||
namespace JR\NetSuite\Queue;
|
||||
|
||||
use Magento\Framework\Serialize\SerializerInterface;
|
||||
use JR\NetSuite\Queue\AbstractQueueManager;
|
||||
use JR\NetSuite\Lib\NetSuiteAPI;
|
||||
use JR\NetSuite\Model\ResourceModel\ProductQueue;
|
||||
use JR\NetSuite\Model\ResourceModel\ProductQueue\CollectionFactory as ProductQueueCollectionFactory;
|
||||
use JR\NetSuite\Service\ProductsSyncService;
|
||||
use JR\NetSuite\Service\AttributesSyncService;
|
||||
use JR\NetSuite\Service\CategoriesSyncService;
|
||||
use JR\NetSuite\Model\Data\ProductQueueItem;
|
||||
use JR\NetSuite\Mapper\ProductMapper;
|
||||
|
||||
class ProductQueueManager extends AbstractQueueManager {
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
protected $name = 'Product';
|
||||
|
||||
/**
|
||||
* @var int
|
||||
*/
|
||||
protected $pageSize = 200;
|
||||
|
||||
/**
|
||||
* @var ProductQueue
|
||||
*/
|
||||
protected $productQueue;
|
||||
|
||||
/**
|
||||
* @var ProductQueueCollectionFactory
|
||||
*/
|
||||
protected $productQueueCollectionFactory;
|
||||
|
||||
/**
|
||||
* @var ProductsSyncService
|
||||
*/
|
||||
protected $productsSync;
|
||||
|
||||
/**
|
||||
* @var AttributesSyncService
|
||||
*/
|
||||
protected $attributesSync;
|
||||
|
||||
/**
|
||||
* @var CategoriesSyncService
|
||||
*/
|
||||
protected $categoriesSync;
|
||||
|
||||
/**
|
||||
* @var ProductQueueItem
|
||||
*/
|
||||
protected $queueItem;
|
||||
/**
|
||||
* @var ProductMapper
|
||||
*/
|
||||
protected $productMapper;
|
||||
|
||||
/**
|
||||
* ProductQueueManager constructor.
|
||||
*
|
||||
* @param ProductQueue $productQueue
|
||||
* @param ProductQueueCollectionFactory $productQueueCollectionFactory
|
||||
* @param \JR\NetSuite\Helper\Data $helper
|
||||
* @param \JR\NetSuite\Logger\Logger $logger
|
||||
* @param \Magento\Framework\Json\Decoder $jsonDecoder
|
||||
* @param \Magento\Framework\App\State $appState
|
||||
* @param ProductsSyncService $productsSync
|
||||
* @param AttributesSyncService $attributesSync
|
||||
* @param CategoriesSyncService $categoriesSync
|
||||
* @param ProductQueueItem $queueItem
|
||||
* @param NetSuiteAPI $netSuiteAPI
|
||||
* @param ProductMapper $productMapper
|
||||
* @return void
|
||||
*/
|
||||
public function __construct(
|
||||
\JR\NetSuite\Helper\Data $helper,
|
||||
\JR\NetSuite\Logger\Logger $logger,
|
||||
SerializerInterface $serializer,
|
||||
NetSuiteAPI $netSuiteAPI,
|
||||
ProductQueue $productQueue,
|
||||
ProductQueueCollectionFactory $productQueueCollectionFactory,
|
||||
ProductsSyncService $productsSync,
|
||||
AttributesSyncService $attributesSync,
|
||||
CategoriesSyncService $categoriesSync,
|
||||
ProductMapper $productMapper
|
||||
) {
|
||||
parent::__construct($helper, $logger, $serializer, $netSuiteAPI);
|
||||
$this->productQueue = $productQueue;
|
||||
$this->productQueueCollectionFactory = $productQueueCollectionFactory;
|
||||
$this->helper = $helper;
|
||||
$this->logger = $logger;
|
||||
$this->productsSync = $productsSync;
|
||||
$this->attributesSync = $attributesSync;
|
||||
$this->categoriesSync = $categoriesSync;
|
||||
$this->netSuiteAPI = $netSuiteAPI;
|
||||
$this->productMapper = $productMapper;
|
||||
}
|
||||
|
||||
/**
|
||||
* Fetch data from NetSuite and add it to the queue.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function fetch($pageSize = 50, $page = 1) {
|
||||
if ($this->helper->getMultiLocationEnabled()) {
|
||||
return $this->netSuiteAPI->fetchProductsWithLocation($pageSize, $page);
|
||||
}
|
||||
|
||||
return $this->netSuiteAPI->fetchProducts($pageSize, $page);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getPageSize() {
|
||||
return $this->pageSize;
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert NetSuite data to Magento format
|
||||
*
|
||||
* @param array $data
|
||||
* @return array
|
||||
*/
|
||||
public function convertToMagentoFormat($data) {
|
||||
return $this->productMapper->convertToMagentoFormat($data);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* adds data to the queue.
|
||||
*
|
||||
* @param array $data
|
||||
* @return void
|
||||
*/
|
||||
public function enqueue($data) {
|
||||
foreach($data as $item) {
|
||||
$this->productQueue->insertOrUpdate($item);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get next queue items to process
|
||||
*
|
||||
* @param int $batchSize
|
||||
* @return array
|
||||
*/
|
||||
public function dequeue($batchSize = 50) {
|
||||
return $this->productQueueCollectionFactory->create()->getNext($batchSize);
|
||||
}
|
||||
|
||||
/**
|
||||
* Process queue item
|
||||
*
|
||||
* @param $queueItem
|
||||
* @return array
|
||||
*/
|
||||
public function processQueueItem($queueItem) {
|
||||
$data = $this->serializer->unserialize($queueItem->getData('data'));
|
||||
$configurableProductsData = null;
|
||||
|
||||
// if (isset($data['Children'])) {
|
||||
// //Create or Update the simple products
|
||||
// foreach ($data['Children'] as $childData) {
|
||||
// if (!$product = $this->productsSync->createOrUpdate($childData, true)) {
|
||||
// continue;
|
||||
// }
|
||||
|
||||
// if ($attributeData = $this->attributesSync->processAttributes($product, $childData)) {
|
||||
// $configurableProductsData[$product->getId()] = $attributeData;
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
|
||||
$categoryIds = $this->categoriesSync->getOrCreateCategoryIds($data);
|
||||
$product = $this->productsSync->createOrUpdate($data, false, $configurableProductsData, $categoryIds);
|
||||
|
||||
// if (isset($product['data'])) {
|
||||
// $this->attributesSync->processAttributes($product['data'], $data);
|
||||
// }
|
||||
|
||||
return $product;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user