60 lines
2.5 KiB
PHP
60 lines
2.5 KiB
PHP
<?php
|
|
|
|
namespace Stemcell\Customer\Controller\Lab;
|
|
|
|
use Magento\Framework\Controller\Result\RedirectFactory;
|
|
use Magento\Framework\Message\ManagerInterface as MessageManagerInterface;
|
|
use PhpOffice\PhpSpreadsheet\Writer\Exception;
|
|
use Psr\Log\LoggerInterface;
|
|
use Stemcell\Customer\Api\LabRepositoryInterface;
|
|
use Stemcell\Customer\Helper\LabDownload;
|
|
|
|
class Download implements \Magento\Customer\Controller\AccountInterface
|
|
{
|
|
private \Magento\Customer\Model\Session $customerSession;
|
|
private LabRepositoryInterface $labRepository;
|
|
private RedirectFactory $redirectFactory;
|
|
private MessageManagerInterface $messageManager;
|
|
private \Magento\Framework\Controller\Result\RawFactory $resultRawFactory;
|
|
private \Magento\Framework\App\Response\Http\FileFactory $fileFactory;
|
|
private LabDownload $labDownloadHelper;
|
|
private LoggerInterface $logger;
|
|
|
|
public function __construct(
|
|
\Magento\Customer\Model\Session $customerSession,
|
|
LabRepositoryInterface $labRepository,
|
|
RedirectFactory $redirectFactory,
|
|
MessageManagerInterface $messageManager,
|
|
LabDownload $labDownloadHelper,
|
|
\Magento\Framework\Controller\Result\RawFactory $resultRawFactory,
|
|
\Magento\Framework\App\Response\Http\FileFactory $fileFactory,
|
|
LoggerInterface $logger
|
|
) {
|
|
$this->customerSession = $customerSession;
|
|
$this->labRepository = $labRepository;
|
|
$this->redirectFactory = $redirectFactory;
|
|
$this->messageManager = $messageManager;
|
|
$this->labDownloadHelper = $labDownloadHelper;
|
|
$this->fileFactory = $fileFactory;
|
|
$this->resultRawFactory = $resultRawFactory;
|
|
$this->logger = $logger;
|
|
}
|
|
|
|
public function execute()
|
|
{
|
|
$labs = $this->labRepository->getByDealerId($this->customerSession->getCustomerId());
|
|
if (!$labs->count()) {
|
|
$this->messageManager->addErrorMessage('您暂无可下载的Lab');
|
|
return $this->redirectFactory->create()->setPath('customer/account/index/');
|
|
}
|
|
|
|
try {
|
|
return $this->fileFactory->create('export-'.date('YmdHis').'.xlsx', $this->labDownloadHelper->write($labs), 'media');
|
|
} catch (\Exception | \PhpOffice\PhpSpreadsheet\Exception $e) {
|
|
$this->logger->critical('Lab下载失败, 原因:'.$e->getMessage());
|
|
$this->messageManager->addErrorMessage('Lab下载失败');
|
|
return $this->redirectFactory->create()->setPath('customer/account/index/');
|
|
}
|
|
}
|
|
}
|