ltsecurityinc安防产品在线商城代码
This commit is contained in:
@@ -0,0 +1,115 @@
|
||||
<?php
|
||||
|
||||
namespace Lts\Customer\Console\Command;
|
||||
|
||||
use Lts\Customer\Api\CustomerRepositoryInterface;
|
||||
use Magento\Customer\Api\Data\CustomerInterface;
|
||||
use Magento\Customer\Api\Data\CustomerInterfaceFactory;
|
||||
use Magento\Framework\Api\DataObjectHelper;
|
||||
use Magento\Framework\Filesystem;
|
||||
use Symfony\Component\Console\Input\InputInterface;
|
||||
use Symfony\Component\Console\Input\InputOption;
|
||||
use Symfony\Component\Console\Output\OutputInterface;
|
||||
use Magento\Framework\App\Filesystem\DirectoryList;
|
||||
|
||||
class CustomerDataImportFromNetsuite extends \Symfony\Component\Console\Command\Command
|
||||
{
|
||||
private CustomerRepositoryInterface $customerRepository;
|
||||
private DataObjectHelper $dataObjectHelper;
|
||||
private CustomerInterfaceFactory $customerFactory;
|
||||
private Filesystem $filesystem;
|
||||
|
||||
public function __construct(
|
||||
CustomerRepositoryInterface $customerRepository,
|
||||
DataObjectHelper $dataObjectHelper,
|
||||
CustomerInterfaceFactory $customerFactory,
|
||||
Filesystem $filesystem,
|
||||
string $name = null,
|
||||
) {
|
||||
parent::__construct($name);
|
||||
$this->customerRepository = $customerRepository;
|
||||
$this->dataObjectHelper = $dataObjectHelper;
|
||||
$this->customerFactory = $customerFactory;
|
||||
$this->filesystem = $filesystem;
|
||||
}
|
||||
|
||||
protected function configure()
|
||||
{
|
||||
$this->setName('customer:import:netsuite');
|
||||
$this->setDescription('Update customer data from Netsuite excel file. Netsuite excel file with customer update data, must contain customer email');
|
||||
$this->addOption(
|
||||
'filePath',
|
||||
'fp',
|
||||
InputOption::VALUE_REQUIRED,
|
||||
",\r\n
|
||||
please place file in var directory, and give relative file path to filePath"
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $filePath
|
||||
* @return string
|
||||
*/
|
||||
protected function getFilePath(string $filePath)
|
||||
{
|
||||
$varDirectory = $this->filesystem->getDirectoryRead(DirectoryList::VAR_DIR);
|
||||
|
||||
// For a specific file
|
||||
return $varDirectory->getAbsolutePath($filePath);
|
||||
}
|
||||
|
||||
protected function execute(InputInterface $input, OutputInterface $output)
|
||||
{
|
||||
$filePath = $input->getOption('filePath');
|
||||
|
||||
$filePath = $this->getFilePath($filePath);
|
||||
|
||||
if (!file_exists($filePath) || !is_readable($filePath)) {
|
||||
$output->writeln(sprintf('<error>File "%s" does not exist or is not readable.</error>', $filePath));
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (!in_array(strtolower(pathinfo($filePath, PATHINFO_EXTENSION)), ['xls', 'xlsx'])) {
|
||||
$output->writeln(sprintf('<error>Import file "%s" must be excel.</error>', $filePath));
|
||||
return 1;
|
||||
}
|
||||
$reader = new \PhpOffice\PhpSpreadsheet\Reader\Xlsx();
|
||||
$reader->setReadDataOnly(true);
|
||||
$workSheet = $reader->load($filePath)->getSheet(0);
|
||||
|
||||
foreach ($workSheet->toArray() as $row => $item) {
|
||||
|
||||
if ($row > 0) {
|
||||
$item[3] = empty($item[3]) ? null : trim($item[3]);
|
||||
$item[1] = empty($item[1]) ? null : trim($item[1]);
|
||||
$item[2] = empty($item[2]) ? null : trim($item[2]);
|
||||
if (empty($item[3])) {
|
||||
$output->writeln(sprintf('<error>Row %d email is empty, ignore.</error>', $row+1));
|
||||
continue;
|
||||
}
|
||||
|
||||
if (empty($item[1]) && empty($item[2])) {
|
||||
$output->writeln(sprintf('<error>Row %d internal id and stage are empty, ignore.</error>', $row+1));
|
||||
continue;
|
||||
}
|
||||
$data = [
|
||||
'email' => $item[3],
|
||||
'extension_attributes' => [
|
||||
'netsuite_status' => strtolower($item[2]),
|
||||
'netsuite_id' => $item[1],
|
||||
],
|
||||
];
|
||||
$customer = $this->customerFactory->create();
|
||||
$this->dataObjectHelper->populateWithArray($customer, $data, CustomerInterface::class);
|
||||
$output->writeln(sprintf('Row %d import success', $row+1));
|
||||
try {
|
||||
$this->customerRepository->save($customer);
|
||||
} catch (\Exception $e) {
|
||||
$output->writeln(sprintf('<error>Row %d import fail, message: %s</error>', $row+1, $e->getMessage()));
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user