Files
2026-09-15 10:26:34 +08:00

121 lines
5.1 KiB
PHP

<?php
namespace Stemcell\Customer\Controller\Adminhtml\Salesperson;
use Magento\Backend\App\Action\Context;
use Magento\Customer\Model\Customer;
use Magento\Customer\Model\ResourceModel\Customer\CollectionFactory as CustomerCollectionFactory;
use Magento\Framework\App\Action\HttpPostActionInterface;
use Magento\Framework\Controller\ResultFactory;
use Magento\Framework\Exception\FileSystemException;
use Magento\Framework\Filesystem\Driver\File;
use Magento\MediaStorage\Model\File\UploaderFactory;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\Filesystem;
use Magento\Store\Model\StoreManagerInterface;
use Magento\Framework\App\Filesystem\DirectoryList;
use PhpOffice\PhpSpreadsheet\IOFactory;
use PhpOffice\PhpSpreadsheet\Reader\Exception;
use Stemcell\Customer\Api\Data\SalespersonInterface;
use Stemcell\Customer\Api\SalespersonRepositoryInterface;
/**
* 由于生产服务器是两台,只有media目录是两台服务器共享,因此将文件上传到media下面
*/
class ImportPost extends \Magento\Backend\App\Action implements HttpPostActionInterface
{
private UploaderFactory $fileUploaderFactory;
private Filesystem\Directory\WriteInterface $mediaDirectory;
private StoreManagerInterface $storeManager;
private Filesystem $fileSystem;
private File $driverFile;
private CustomerCollectionFactory $customerCollection;
private SalespersonInterface $salesperson;
private SalespersonRepositoryInterface $salespersonRepository;
/**
* @throws FileSystemException
*/
public function __construct(
Context $context,
UploaderFactory $fileUploaderFactory,
Filesystem $filesystem,
StoreManagerInterface $storeManager,
Filesystem $fileSystem,
File $driverFile,
CustomerCollectionFactory $customerCollection,
SalespersonRepositoryInterface $salespersonRepository,
SalespersonInterface $salesperson
) {
parent::__construct($context);
$this->fileUploaderFactory = $fileUploaderFactory;
$this->mediaDirectory = $filesystem->getDirectoryWrite(DirectoryList::MEDIA);
$this->storeManager = $storeManager;
$this->fileSystem = $fileSystem;
$this->driverFile = $driverFile;
$this->customerCollection = $customerCollection;
$this->salespersonRepository = $salespersonRepository;
$this->salesperson = $salesperson;
}
public function execute()
{
$jsonResult = $this->resultFactory->create(ResultFactory::TYPE_JSON);
try {
$fileUploader = $this->fileUploaderFactory->create([
'fileId' => 'salesperson[import]'
]);
$fileUploader->setAllowedExtensions(['xls', 'xlsx']);
$fileUploader->setAllowRenameFiles(true);
$fileUploader->setAllowCreateFolders(true);
$fileUploader->setFilesDispersion(false);
$dir = $this->mediaDirectory->getAbsolutePath(DirectoryList::VAR_IMPORT_EXPORT.'/salesperson');
!is_dir($dir) && mkdir($dir, 0755, true);
$data = $fileUploader->save($dir);
$resource = $this->driverFile->fileOpen($dir.'/'.$fileUploader->getUploadedFileName(), 'r');
/* @var $logger \Psr\Log\LoggerInterface */
$logger = \Magento\Framework\App\ObjectManager::getInstance()->get(\Psr\Log\LoggerInterface::class);
try {
$spreadsheet = IOFactory::load($dir.'/'.$fileUploader->getUploadedFileName());
$data = $spreadsheet->getActiveSheet()->toArray();
array_shift($data);
foreach ($data as $content) {
try {
array_walk($content, function (&$value) {
if (is_string($value)) {
$value = trim($value);
}
});
if (empty(array_filter($content))) {
continue;
}
$content = array_slice($content, 0, 5);
$row = array_combine(
['name', 'email', 'telephone', 'rsm_email', 'sisr_email', 'csr_email'],
$content
);
$this->salesperson->setData($row);
$this->salespersonRepository->save($this->salesperson);
$this->salesperson->unsetData();
} catch (FileSystemException | \Exception $e) {
$logger->critical('导入销售失败,原因:'.$e->getMessage(), ['data' => $row]);
}
}
$this->driverFile->fileClose($resource);
$this->driverFile->deleteFile($dir.'/'.$fileUploader->getUploadedFileName());
} catch (FileSystemException $exception) {
$logger->debug($exception->getMessage());
}
return $jsonResult->setData($data);
} catch (LocalizedException | \Exception $error) {
return $jsonResult->setData(['errorcode' => 0, 'error' => $error->getMessage()]);
}
}
}