ltsecurityinc安防产品在线商城代码
This commit is contained in:
+135
@@ -0,0 +1,135 @@
|
||||
<?php
|
||||
/**
|
||||
* @author Amasty Team
|
||||
* @copyright Copyright (c) Amasty (https://www.amasty.com)
|
||||
* @package Elastic Search Base for Magento 2
|
||||
*/
|
||||
|
||||
namespace Amasty\ElasticSearch\Model\Indexer\RelevanceRule;
|
||||
|
||||
use Magento\Framework\Mview\ActionInterface as MviewActionInterface;
|
||||
use Magento\Framework\Indexer\ActionInterface as IndexerActionInterface;
|
||||
use Magento\Framework\DataObject\IdentityInterface;
|
||||
|
||||
abstract class AbstractIndexer implements IndexerActionInterface, MviewActionInterface, IdentityInterface
|
||||
{
|
||||
/**
|
||||
* @var IndexBuilder
|
||||
*/
|
||||
private $indexBuilder;
|
||||
|
||||
/**
|
||||
* @var \Magento\Framework\Event\ManagerInterface
|
||||
*/
|
||||
private $eventManager;
|
||||
|
||||
/**
|
||||
* @var \Magento\Framework\App\CacheInterface
|
||||
*/
|
||||
private $cacheManager;
|
||||
|
||||
/**
|
||||
* @var \Magento\Framework\Indexer\CacheContext
|
||||
*/
|
||||
private $cacheContext;
|
||||
|
||||
public function __construct(
|
||||
IndexBuilder $indexBuilder,
|
||||
\Magento\Framework\Event\ManagerInterface $eventManager,
|
||||
\Magento\Framework\App\CacheInterface $cacheManager,
|
||||
\Magento\Framework\Indexer\CacheContext $cacheContext
|
||||
) {
|
||||
$this->indexBuilder = $indexBuilder;
|
||||
$this->eventManager = $eventManager;
|
||||
$this->cacheManager = $cacheManager;
|
||||
$this->cacheContext = $cacheContext;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int[] $ids
|
||||
* @throws \Magento\Framework\Exception\LocalizedException
|
||||
*/
|
||||
public function execute($ids)
|
||||
{
|
||||
$this->executeList($ids);
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws \Magento\Framework\Exception\LocalizedException
|
||||
*/
|
||||
public function executeFull()
|
||||
{
|
||||
$this->indexBuilder->reindexFull();
|
||||
$this->eventManager->dispatch('clean_cache_by_tags', ['object' => $this]);
|
||||
|
||||
//was needed before MAGETWO-50668 was fixed
|
||||
$this->cacheManager->clean($this->getIdentities());
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $productId
|
||||
* @throws \Magento\Framework\Exception\LocalizedException
|
||||
*/
|
||||
public function executeRow($id)
|
||||
{
|
||||
if (!$id) {
|
||||
throw new \Magento\Framework\Exception\LocalizedException(
|
||||
__('We can\'t rebuild the index for an undefined entity.')
|
||||
);
|
||||
}
|
||||
|
||||
$this->doExecuteRow($id);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int[] $ids
|
||||
* @throws \Magento\Framework\Exception\LocalizedException
|
||||
*/
|
||||
public function executeList(array $ids)
|
||||
{
|
||||
if (!$ids) {
|
||||
throw new \Magento\Framework\Exception\LocalizedException(
|
||||
__('Could not rebuild index for empty products array')
|
||||
);
|
||||
}
|
||||
$this->doExecuteList($ids);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public function getIdentities()
|
||||
{
|
||||
return [
|
||||
\Magento\Catalog\Model\Category::CACHE_TAG,
|
||||
\Magento\Catalog\Model\Product::CACHE_TAG,
|
||||
\Magento\Framework\App\Cache\Type\Block::CACHE_TAG
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $id
|
||||
*/
|
||||
abstract protected function doExecuteRow($id);
|
||||
|
||||
/**
|
||||
* @param int[] $ids
|
||||
*/
|
||||
abstract protected function doExecuteList($ids);
|
||||
|
||||
/**
|
||||
* @return IndexBuilder
|
||||
*/
|
||||
protected function getIndexBuilder()
|
||||
{
|
||||
return $this->indexBuilder;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return \Magento\Framework\Indexer\CacheContext
|
||||
*/
|
||||
protected function getCacheContext()
|
||||
{
|
||||
return $this->cacheContext;
|
||||
}
|
||||
}
|
||||
+265
@@ -0,0 +1,265 @@
|
||||
<?php
|
||||
/**
|
||||
* @author Amasty Team
|
||||
* @copyright Copyright (c) Amasty (https://www.amasty.com)
|
||||
* @package Elastic Search Base for Magento 2
|
||||
*/
|
||||
|
||||
namespace Amasty\ElasticSearch\Model\Indexer\RelevanceRule;
|
||||
|
||||
use Amasty\ElasticSearch\Api\Data\RelevanceRuleInterface;
|
||||
use Amasty\ElasticSearch\Api\RelevanceRuleRepositoryInterface;
|
||||
use Amasty\ElasticSearch\Model\ResourceModel\RelevanceRule\CollectionFactory as RelevanceRuleCollectionFactory;
|
||||
use Magento\Catalog\Api\Data\ProductInterface;
|
||||
use Magento\Catalog\Api\ProductRepositoryInterface;
|
||||
use Magento\Framework\Api\SearchCriteriaBuilder;
|
||||
use Magento\Framework\App\ResourceConnection;
|
||||
use Magento\Framework\Exception\LocalizedException;
|
||||
use Magento\Framework\Exception\NoSuchEntityException;
|
||||
use Magento\Store\Model\StoreManagerInterface;
|
||||
use Psr\Log\LoggerInterface;
|
||||
|
||||
/**
|
||||
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
|
||||
*/
|
||||
class IndexBuilder
|
||||
{
|
||||
public const SECONDS_IN_DAY = 86400;
|
||||
public const PRODUCT_ID = 'product_id';
|
||||
public const TABLE_NAME = 'amasty_elastic_relevance_rule_index_tmp';
|
||||
public const MAX_INT_MYSQL = 4294967294;
|
||||
|
||||
/**
|
||||
* @var ResourceConnection
|
||||
*/
|
||||
private $resource;
|
||||
|
||||
/**
|
||||
* @var StoreManagerInterface
|
||||
*/
|
||||
private $storeManager;
|
||||
|
||||
/**
|
||||
* @var RelevanceRuleRepositoryInterface
|
||||
*/
|
||||
private $ruleRepository;
|
||||
|
||||
/**
|
||||
* @var ProductRepositoryInterface
|
||||
*/
|
||||
private $productRepository;
|
||||
|
||||
/**
|
||||
* @var SearchCriteriaBuilder
|
||||
*/
|
||||
private $searchCriteriaBuilder;
|
||||
|
||||
/**
|
||||
* @var LoggerInterface
|
||||
*/
|
||||
private $logger;
|
||||
|
||||
/**
|
||||
* @var int
|
||||
*/
|
||||
private $batchCount;
|
||||
|
||||
/**
|
||||
* @var RelevanceRuleCollectionFactory
|
||||
*/
|
||||
private $relevanceRuleCollectionFactory;
|
||||
|
||||
public function __construct(
|
||||
ResourceConnection $resource,
|
||||
StoreManagerInterface $storeManager,
|
||||
RelevanceRuleRepositoryInterface $ruleRepository,
|
||||
ProductRepositoryInterface $productRepository,
|
||||
SearchCriteriaBuilder $searchCriteriaBuilder,
|
||||
LoggerInterface $logger,
|
||||
RelevanceRuleCollectionFactory $relevanceRuleCollectionFactory,
|
||||
$batchCount = 1000
|
||||
) {
|
||||
$this->resource = $resource;
|
||||
$this->storeManager = $storeManager;
|
||||
$this->ruleRepository = $ruleRepository;
|
||||
$this->productRepository = $productRepository;
|
||||
$this->searchCriteriaBuilder = $searchCriteriaBuilder;
|
||||
$this->logger = $logger;
|
||||
$this->batchCount = $batchCount;
|
||||
$this->relevanceRuleCollectionFactory = $relevanceRuleCollectionFactory;
|
||||
}
|
||||
|
||||
/**
|
||||
* Reindex by product Ids
|
||||
*
|
||||
* @param array $ids
|
||||
* @throws LocalizedException
|
||||
*/
|
||||
public function reindexByIds(array $ids): void
|
||||
{
|
||||
try {
|
||||
$this->cleanByIds($ids);
|
||||
$products = $this->getProducts($ids);
|
||||
|
||||
foreach ($this->getActiveRules() as $rule) {
|
||||
foreach ($products as $product) {
|
||||
$this->applyRule($rule, $product);
|
||||
}
|
||||
}
|
||||
} catch (\Exception $e) {
|
||||
$this->critical($e);
|
||||
|
||||
throw new LocalizedException(
|
||||
__("Amasty ElasticSearch Relevance rule indexing failed. See details in exception log.")
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
public function getProducts(array $productIds): iterable
|
||||
{
|
||||
$this->searchCriteriaBuilder->addFilter('entity_id', $productIds, 'in');
|
||||
$searchCriteria = $this->searchCriteriaBuilder->create();
|
||||
|
||||
return $this->productRepository->getList($searchCriteria)->getItems();
|
||||
}
|
||||
|
||||
public function reindexFull(): void
|
||||
{
|
||||
try {
|
||||
$this->resource->getConnection()->truncateTable($this->getIndexTable());
|
||||
|
||||
foreach ($this->getActiveRules() as $rule) {
|
||||
$this->doReindex($rule);
|
||||
}
|
||||
} catch (\Exception $e) {
|
||||
$this->critical($e);
|
||||
|
||||
throw new LocalizedException(
|
||||
__("Relevance rule indexing failed. See details in exception log.")
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
public function reindexByRuleIds(array $ids): void
|
||||
{
|
||||
$table = $this->getIndexTable();
|
||||
$relevanceRuleCollection = $this->relevanceRuleCollectionFactory->create();
|
||||
$relevanceRuleCollection->addFieldToFilter(RelevanceRuleInterface::RULE_ID, $ids);
|
||||
$connection = $this->resource->getConnection();
|
||||
$connection->delete(
|
||||
$table,
|
||||
[
|
||||
$connection->prepareSqlCondition(RelevanceRuleInterface::RULE_ID, ['in' => $ids])
|
||||
]
|
||||
);
|
||||
|
||||
try {
|
||||
foreach ($relevanceRuleCollection as $relevanceRule) {
|
||||
try {
|
||||
$this->doReindex($relevanceRule);
|
||||
} catch (NoSuchEntityException $e) {
|
||||
null;// do nothing
|
||||
}
|
||||
}
|
||||
} catch (\Exception $e) {
|
||||
$this->critical($e);
|
||||
|
||||
throw new LocalizedException(
|
||||
__("Relevance rule indexing failed. See details in exception log.")
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
private function doReindex(RelevanceRuleInterface $rule): void
|
||||
{
|
||||
if ($rule->isConditionEmpty()) {
|
||||
return;
|
||||
}
|
||||
|
||||
$rows = [];
|
||||
$size = 0;
|
||||
$productIds = $rule->getCatalogRule()->getMatchingProductIds();
|
||||
|
||||
foreach ($productIds as $productId => $validationByWebsite) {
|
||||
if (empty($validationByWebsite[$rule->getWebsiteId()])) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$rows[] = $this->generateIndexData($rule, $productId);
|
||||
$size++;
|
||||
|
||||
if ($size == $this->batchCount) {
|
||||
$this->resource->getConnection()->insertMultiple($this->getIndexTable(), $rows);
|
||||
$rows = [];
|
||||
$size = 0;
|
||||
}
|
||||
}
|
||||
|
||||
if (!empty($rows)) {
|
||||
$this->resource->getConnection()->insertMultiple($this->getIndexTable(), $rows);
|
||||
}
|
||||
}
|
||||
|
||||
private function applyRule(RelevanceRuleInterface $rule, ProductInterface $product): IndexBuilder
|
||||
{
|
||||
$table = $this->getIndexTable();
|
||||
$connection = $this->resource->getConnection();
|
||||
$connection->delete(
|
||||
$table,
|
||||
[
|
||||
$connection->prepareSqlCondition(RelevanceRuleInterface::RULE_ID, ['eq' => $rule->getId()]),
|
||||
$connection->prepareSqlCondition(self::PRODUCT_ID, ['eq' => $product->getId()])
|
||||
]
|
||||
);
|
||||
|
||||
if ($rule->isConditionEmpty() || !$rule->getCatalogRule()->validate($product)) {
|
||||
return $this;
|
||||
}
|
||||
|
||||
try {
|
||||
$rows = [$this->generateIndexData($rule, $product->getId())];
|
||||
$this->resource->getConnection()->insertMultiple($table, $rows);
|
||||
} catch (\Exception $e) {
|
||||
throw $e;
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
private function generateIndexData(RelevanceRuleInterface $rule, int $productId): array
|
||||
{
|
||||
return [
|
||||
RelevanceRuleInterface::RULE_ID => $rule->getId(),
|
||||
RelevanceRuleInterface::WEBSITE_ID => $rule->getWebsiteId(),
|
||||
RelevanceRuleInterface::MULTIPLIER => $rule->getMultiplier(),
|
||||
self::PRODUCT_ID => $productId
|
||||
];
|
||||
}
|
||||
|
||||
private function cleanByIds(array $productIds): void
|
||||
{
|
||||
$select = $this->resource->getConnection()
|
||||
->select()
|
||||
->from($this->getIndexTable(), self::PRODUCT_ID)
|
||||
->distinct()
|
||||
->where(self::PRODUCT_ID . ' IN (?)', $productIds);
|
||||
$query = $this->resource->getConnection()->deleteFromSelect($select, $this->getIndexTable());
|
||||
|
||||
$this->resource->getConnection()->query($query);
|
||||
}
|
||||
|
||||
private function getIndexTable(): string
|
||||
{
|
||||
return $this->resource->getTableName(self::TABLE_NAME);
|
||||
}
|
||||
|
||||
private function getActiveRules(): iterable
|
||||
{
|
||||
return $this->ruleRepository->getActiveRules();
|
||||
}
|
||||
|
||||
private function critical(\Exception $exception): void
|
||||
{
|
||||
$this->logger->critical($exception);
|
||||
}
|
||||
}
|
||||
+28
@@ -0,0 +1,28 @@
|
||||
<?php
|
||||
/**
|
||||
* @author Amasty Team
|
||||
* @copyright Copyright (c) Amasty (https://www.amasty.com)
|
||||
* @package Elastic Search Base for Magento 2
|
||||
*/
|
||||
|
||||
namespace Amasty\ElasticSearch\Model\Indexer\RelevanceRule;
|
||||
|
||||
class ProductRuleIndexer extends AbstractIndexer
|
||||
{
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
protected function doExecuteRow($id)
|
||||
{
|
||||
$this->getIndexBuilder()->reindexByIds([$id]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
protected function doExecuteList($ids)
|
||||
{
|
||||
$this->getIndexBuilder()->reindexByIds(array_unique($ids));
|
||||
$this->getCacheContext()->registerEntities(\Magento\Catalog\Model\Product::CACHE_TAG, $ids);
|
||||
}
|
||||
}
|
||||
+13
@@ -0,0 +1,13 @@
|
||||
<?php
|
||||
/**
|
||||
* @author Amasty Team
|
||||
* @copyright Copyright (c) Amasty (https://www.amasty.com)
|
||||
* @package Elastic Search Base for Magento 2
|
||||
*/
|
||||
|
||||
namespace Amasty\ElasticSearch\Model\Indexer\RelevanceRule;
|
||||
|
||||
class ProductRuleProcessor extends \Magento\Framework\Indexer\AbstractProcessor
|
||||
{
|
||||
public const INDEXER_ID = 'amasty_elastic_relevance_rule_product';
|
||||
}
|
||||
+32
@@ -0,0 +1,32 @@
|
||||
<?php
|
||||
/**
|
||||
* @author Amasty Team
|
||||
* @copyright Copyright (c) Amasty (https://www.amasty.com)
|
||||
* @package Elastic Search Base for Magento 2
|
||||
*/
|
||||
|
||||
namespace Amasty\ElasticSearch\Model\Indexer\RelevanceRule;
|
||||
|
||||
class RuleProductIndexer extends AbstractIndexer
|
||||
{
|
||||
/**
|
||||
* @inheritdoc
|
||||
*
|
||||
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
|
||||
*/
|
||||
protected function doExecuteRow($id)
|
||||
{
|
||||
$this->getIndexBuilder()->reindexByRuleIds([$id]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
*
|
||||
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
|
||||
*/
|
||||
protected function doExecuteList($ids)
|
||||
{
|
||||
$this->getIndexBuilder()->reindexByRuleIds($ids);
|
||||
$this->getCacheContext()->registerTags($this->getIdentities());
|
||||
}
|
||||
}
|
||||
+13
@@ -0,0 +1,13 @@
|
||||
<?php
|
||||
/**
|
||||
* @author Amasty Team
|
||||
* @copyright Copyright (c) Amasty (https://www.amasty.com)
|
||||
* @package Elastic Search Base for Magento 2
|
||||
*/
|
||||
|
||||
namespace Amasty\ElasticSearch\Model\Indexer\RelevanceRule;
|
||||
|
||||
class RuleProductProcessor extends \Magento\Framework\Indexer\AbstractProcessor
|
||||
{
|
||||
public const INDEXER_ID = 'amasty_elastic_relevance_rule_rule';
|
||||
}
|
||||
Reference in New Issue
Block a user