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); } }