init
This commit is contained in:
@@ -0,0 +1,150 @@
|
||||
<?php
|
||||
|
||||
namespace Stemcell\Catalog\Model\Api;
|
||||
|
||||
use Magento\Catalog\Model\Product\Attribute\Source\Status;
|
||||
use Magento\Catalog\Model\Product\Type as ProductType;
|
||||
use Magento\Framework\Module\Dir;
|
||||
use Magento\Framework\UrlInterface;
|
||||
use Magento\Framework\Webapi\Rest\Request;
|
||||
use Magento\Store\Model\StoreManagerInterface;
|
||||
use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;
|
||||
use Stemcell\Catalog\Helper\Excel;
|
||||
use Magento\Eav\Model\Config as EavConfig;
|
||||
use Stemcell\Catalog\Setup\Patch\Data\AddChineseEditionCellSeparationOption;
|
||||
use Magento\Catalog\Model\ResourceModel\Product\CollectionFactory as ProductCollectionFactory;
|
||||
|
||||
/**
|
||||
* 分选说明书接口
|
||||
*/
|
||||
class CellSeparation
|
||||
{
|
||||
public function __construct(
|
||||
private Dir $dir,
|
||||
private StoreManagerInterface $storeManager,
|
||||
private Request $restRequest,
|
||||
protected EavConfig $eavConfig,
|
||||
protected ProductCollectionFactory $productCollectionFactory,
|
||||
) {
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Worksheet $sheet
|
||||
* @param string $cell
|
||||
* @return string
|
||||
*/
|
||||
protected function getCellValue(Worksheet $sheet, string $cell)
|
||||
{
|
||||
return trim((string)$sheet->getCell($cell)->getValue());
|
||||
}
|
||||
|
||||
/**
|
||||
* @return int
|
||||
* @throws \Magento\Framework\Exception\LocalizedException
|
||||
*/
|
||||
protected function getResourceTypeChinesePisOptionId()
|
||||
{
|
||||
$attribute = $this->eavConfig->getAttribute(\Magento\Catalog\Model\Product::ENTITY, 'resource_type');
|
||||
$attribute->setStoreId(0);
|
||||
$optionId = 0;
|
||||
foreach ($attribute->getOptions() as $option) {
|
||||
if ($option->getLabel() == AddChineseEditionCellSeparationOption::RESOURCE_TYPE_ENGLISH_LABEL) {
|
||||
$optionId = (int)$option->getValue();
|
||||
break;
|
||||
}
|
||||
}
|
||||
return $optionId;
|
||||
}
|
||||
|
||||
/**
|
||||
* 分选说明书接口
|
||||
* @return string
|
||||
* @throws \Magento\Framework\Exception\NoSuchEntityException
|
||||
* @throws \Exception
|
||||
*/
|
||||
public function execute()
|
||||
{
|
||||
$pageSize = 10;
|
||||
$page = $this->restRequest->getParam('page', 1);
|
||||
$start = ($page - 1) * $pageSize;
|
||||
|
||||
$keyword = trim($this->restRequest->getParam('keyword', ''));
|
||||
|
||||
$isSku = preg_match('/^\d+[A-Za-z]*$/', $keyword) !==0 ;
|
||||
//['sku' => $sku, 'label' => $name, 'pdf'=>$mediaUrl.$pdfPath, 'url'=>$mediaUrl.$sku.'/'.$pdfPath]
|
||||
$data = [];
|
||||
$mediaUrl = $this->storeManager->getStore()->getBaseUrl(UrlInterface::URL_TYPE_WEB);
|
||||
$chinesePisCollection = $this->productCollectionFactory->create()
|
||||
->addAttributeToSelect(['parent_skus', 'document_path', 'parent_names'])
|
||||
->addAttributeToFilter('status', Status::STATUS_ENABLED)
|
||||
->addAttributeToFilter('type_id', ProductType::TYPE_VIRTUAL)
|
||||
->addAttributeToFilter('resource_type', $this->getResourceTypeChinesePisOptionId());
|
||||
|
||||
if ($keyword) {
|
||||
$chinesePisCollection->addAttributeToFilter([
|
||||
['attribute' => 'parent_skus', 'finset' => $keyword],
|
||||
['attribute' => 'parent_names', 'like' => '%'.$keyword.'%'],
|
||||
]);
|
||||
}
|
||||
|
||||
$parentSkus = '';
|
||||
foreach ($chinesePisCollection as $item) {
|
||||
|
||||
$parentSkus .= $item->getData('parent_skus').',';
|
||||
}
|
||||
$parentSkus = trim($parentSkus, ',');
|
||||
|
||||
$productNames = [];
|
||||
$collection = $this->productCollectionFactory->create()
|
||||
->addAttributeToSelect(['name'])
|
||||
->addAttributeToFilter('status', Status::STATUS_ENABLED)
|
||||
->addAttributeToFilter('sku', ['in' => array_unique(explode(',', $parentSkus))]);
|
||||
foreach ($collection as $item) {
|
||||
$productNames[$item->getSku()] = $item->getName();
|
||||
}
|
||||
|
||||
foreach ($chinesePisCollection as $item) {
|
||||
|
||||
$pdfPath = $mediaUrl.ltrim($item->getData('document_path'), '/');
|
||||
if ($item->getData('parent_skus')) {
|
||||
foreach (explode(',', $item->getData('parent_skus')) as $sku) {
|
||||
if (($isSku && $keyword && $sku && strtolower($keyword)==strtolower($sku))
|
||||
|| (!$isSku && $item->getData('parent_names') && str_contains(strtolower($item->getData('parent_names')), strtolower($keyword)))) {
|
||||
if (isset($productNames[$sku])){
|
||||
$data[$item->getSku()] = [
|
||||
'sku' => $sku,
|
||||
'label' => $productNames[$sku],
|
||||
'pdf'=>ltrim($pdfPath, '/'),
|
||||
'url'=>ltrim($pdfPath, '/')
|
||||
];
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$data = array_values($data);
|
||||
$count = count($data);
|
||||
// $pages = ceil($count / $pageSize);
|
||||
// $page = min($page, $pages);
|
||||
// $end = $page * $pageSize - 1;
|
||||
// $rows = [];
|
||||
// foreach ($data as $index => $row) {
|
||||
// if ($index >= $start && $index <= $end) {
|
||||
// $rows[] = $row;
|
||||
// }
|
||||
// if ($index > $end) {
|
||||
// break;
|
||||
// }
|
||||
// }
|
||||
$result = [
|
||||
'items' => $data,
|
||||
'filter_code' => 'keyword',
|
||||
'filters' => [],
|
||||
'pages' => $page,
|
||||
'total_count' => $count,
|
||||
];
|
||||
return json_encode($result);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user