ltsecurityinc安防产品在线商城代码
This commit is contained in:
@@ -0,0 +1,99 @@
|
||||
<?php
|
||||
|
||||
namespace Lts\Customer\Console\Command;
|
||||
|
||||
use Lts\Customer\Cron\CustomerNetsuiteSync;
|
||||
use Lts\Inventory\Api\SourceCustomerRepositoryInterface;
|
||||
use Lts\Inventory\Api\Data\SourceCustomerInterfaceFactory;
|
||||
use Magento\Customer\Model\ResourceModel\Customer\Collection;
|
||||
use Magento\Framework\Exception\LocalizedException;
|
||||
use Magento\Framework\Exception\NoSuchEntityException;
|
||||
use Magento\InventoryApi\Api\Data\SourceInterface;
|
||||
use Symfony\Component\Console\Input\InputInterface;
|
||||
use Symfony\Component\Console\Output\OutputInterface;
|
||||
use Magento\Inventory\Model\ResourceModel\Source;
|
||||
use Magento\InventoryApi\Api\Data\SourceInterfaceFactory;
|
||||
|
||||
class AddInventorySourceCustomer extends \Symfony\Component\Console\Command\Command
|
||||
{
|
||||
private Collection $customerCollection;
|
||||
private SourceCustomerRepositoryInterface $sourceCustomerRepository;
|
||||
private SourceInterfaceFactory $sourceFactory;
|
||||
private Source $sourceModel;
|
||||
|
||||
const DEFAULT_SOURCE = 'CA-LA';
|
||||
|
||||
/**
|
||||
* @var SourceInterface[]
|
||||
*/
|
||||
private array $sources;
|
||||
private SourceCustomerInterfaceFactory $customerInterfaceFactory;
|
||||
private CustomerNetsuiteSync $sourceNetsuiteSync;
|
||||
|
||||
public function __construct(
|
||||
Collection $customerCollection,
|
||||
SourceCustomerRepositoryInterface $sourceCustomerRepository,
|
||||
SourceInterfaceFactory $sourceFactory,
|
||||
Source $sourceModel,
|
||||
SourceCustomerInterfaceFactory $customerInterfaceFactory,
|
||||
CustomerNetsuiteSync $sourceNetsuiteSync
|
||||
) {
|
||||
$this->customerCollection = $customerCollection;
|
||||
$this->sourceCustomerRepository = $sourceCustomerRepository;
|
||||
$this->sourceFactory = $sourceFactory;
|
||||
$this->sourceModel = $sourceModel;
|
||||
$this->customerInterfaceFactory = $customerInterfaceFactory;
|
||||
$this->sourceNetsuiteSync = $sourceNetsuiteSync;
|
||||
parent::__construct();
|
||||
}
|
||||
|
||||
protected function configure()
|
||||
{
|
||||
$this->setName('customer:source:sync');
|
||||
$this->setDescription('Add customer init data to inventory_source_customer table');
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $sourceName
|
||||
* @return SourceInterface
|
||||
*/
|
||||
protected function getSourceByName(string $sourceName)
|
||||
{
|
||||
$sourceName = trim($sourceName);
|
||||
if (isset($this->sources[$sourceName]) && $this->sources[$sourceName]->getSourceCode()) {
|
||||
return $this->sources[$sourceName];
|
||||
}
|
||||
$source = $this->sourceFactory->create();
|
||||
$this->sourceModel->load($source, $sourceName, 'name');
|
||||
$this->sources[$sourceName] = $source;
|
||||
return $this->sources[$sourceName];
|
||||
}
|
||||
|
||||
protected function execute(InputInterface $input, OutputInterface $output)
|
||||
{
|
||||
$this->customerCollection->load();
|
||||
|
||||
foreach ($this->customerCollection as $customer) {
|
||||
/* @var $customer \Magento\Customer\Model\Customer */
|
||||
$attribute = $customer->getAttribute('lts_office');
|
||||
$name = $attribute->getSource()->getOptionText($customer->getLtsOffice());
|
||||
try {
|
||||
try {
|
||||
$this->sourceCustomerRepository->getFromCustomerId($customer->getId());
|
||||
$output->writeln(sprintf("customer %s, source name %s already exists in customer source.", $customer->getEmail(), $name));
|
||||
} catch (NoSuchEntityException $e) {
|
||||
$source = $this->getSourceByName($name);
|
||||
$sourceCustomer = $this->customerInterfaceFactory->create();
|
||||
$sourceCustomer->setCustomerId($customer->getId())->setSourceCode($source->getSourceCode()?? self::DEFAULT_SOURCE);
|
||||
$this->sourceCustomerRepository->save($sourceCustomer);
|
||||
$output->writeln(sprintf("customer %s, source name %s sync success.", $customer->getEmail(), $name));
|
||||
}
|
||||
|
||||
} catch (\Exception $e) {
|
||||
$output->writeln(sprintf("customer source sync fail with customer %s, source name %s . Exception message %s", $customer->getEmail(), $name, $e->getMessage()));
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
<?php
|
||||
|
||||
namespace Lts\Customer\Console\Command;
|
||||
|
||||
use Lts\Sales\Helper\Email;
|
||||
use Lts\Sales\Helper\CredisStorage;
|
||||
use Magento\Framework\Api\SearchCriteriaBuilderFactory;
|
||||
use Magento\Framework\App\Area;
|
||||
use Magento\Framework\Exception\LocalizedException;
|
||||
use Magento\Framework\Exception\MailException;
|
||||
use Magento\InventoryApi\Api\Data\SourceItemInterface;
|
||||
use Magento\InventoryApi\Api\SourceItemRepositoryInterface;
|
||||
use Symfony\Component\Console\Input\InputInterface;
|
||||
use Symfony\Component\Console\Output\OutputInterface;
|
||||
use Magento\InventoryApi\Api\GetSourceItemsBySkuInterface;
|
||||
use Magento\Framework\App\State;
|
||||
|
||||
|
||||
class Test extends \Symfony\Component\Console\Command\Command
|
||||
{
|
||||
public function __construct(
|
||||
protected SourceItemRepositoryInterface $sourceItemRepository,
|
||||
protected SearchCriteriaBuilderFactory $searchCriteriaBuilderFactory,
|
||||
protected GetSourceItemsBySkuInterface $getSourceItemsBySku,
|
||||
protected Email $email,
|
||||
protected State $state,
|
||||
protected CredisStorage $credis
|
||||
) {
|
||||
parent::__construct('test');
|
||||
}
|
||||
|
||||
protected function execute(InputInterface $input, OutputInterface $output)
|
||||
{
|
||||
$this->credis->set('lts_test', ['abc', 'cde', 'cde']);
|
||||
|
||||
var_dump($this->credis->isExist('lts_test', 'cde'));
|
||||
var_dump($this->credis->get('lts_test'));
|
||||
var_dump($this->credis->get('lts_test1'));
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user