62 lines
2.0 KiB
PHP
62 lines
2.0 KiB
PHP
<?php
|
|
|
|
namespace Stemcell\Catalog\Model\Api;
|
|
|
|
use Magento\Framework\Webapi\Rest\Request as RestRequest;
|
|
use Magento\Store\Model\StoreManagerInterface;
|
|
use Stemcell\Catalog\Model\ResourceModel\ExperimentalScheme\CollectionFactory as CollectionFactory;
|
|
|
|
/**
|
|
* 实验方案
|
|
*/
|
|
class ExperimentalScheme
|
|
{
|
|
const PAGE_SIZE = 10;
|
|
public function __construct(
|
|
private RestRequest $restRequest,
|
|
private CollectionFactory $collectionFactory,
|
|
private StoreManagerInterface $storeManager,
|
|
) {
|
|
}
|
|
|
|
protected function getFilters() : array
|
|
{
|
|
$collection = $this->collectionFactory->create();
|
|
$table = $collection->getMainTable();
|
|
$rows = $collection->getConnection()->fetchAll(
|
|
'select MIN(collection_name) AS collection_name, collection_sort from '.$table
|
|
.' group by collection_sort order by collection_sort asc'
|
|
);
|
|
$data = [];
|
|
|
|
foreach ($rows as $row) {
|
|
$data[] = ['collection_name'=>$row['collection_name']];
|
|
}
|
|
|
|
return array_values($data);
|
|
}
|
|
|
|
/**
|
|
* @return string
|
|
*/
|
|
public function execute()
|
|
{
|
|
$collectionName = trim((string)$this->restRequest->getParam('collection'));
|
|
$page = intval($this->restRequest->getParam('page', 1));
|
|
$collection = $this->collectionFactory->create();
|
|
if (!empty($collectionName)) {
|
|
$collection->addFieldToFilter('collection_name', $collectionName);
|
|
}
|
|
$collection->setPageSize(self::PAGE_SIZE)->setCurPage($page)->setOrder('collection_sort', 'asc');
|
|
$mediaUrl = $this->storeManager->getStore()->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_MEDIA);
|
|
$data = $collection->getData();
|
|
foreach ($data as &$item) {
|
|
$item['image'] = $mediaUrl . $item['image'];
|
|
}
|
|
return json_encode([
|
|
'data'=>['items' => $data, 'total_count'=>$collection->getSize()],
|
|
'filters' => ['collection'=>$this->getFilters()]
|
|
]);
|
|
}
|
|
}
|