init
This commit is contained in:
@@ -0,0 +1,174 @@
|
||||
<?php
|
||||
/**
|
||||
* Copyright © All rights reserved.
|
||||
* See COPYING.txt for license details.
|
||||
*/
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Stemcell\SubItemProduct\Console\Command;
|
||||
|
||||
use Magento\Catalog\Model\Product\Visibility;
|
||||
use Magento\Catalog\Model\ProductFactory;
|
||||
use Magento\Catalog\Model\ProductRepository;
|
||||
use Magento\CatalogInventory\Model\StockRegistry;
|
||||
use Magento\Framework\App\State;
|
||||
use Magento\Framework\Exception\LocalizedException;
|
||||
use Magento\Framework\Module\Dir;
|
||||
use Magento\Store\Model\StoreManagerInterface;
|
||||
use PhpOffice\PhpSpreadsheet\IOFactory;
|
||||
use Stemcell\SubItemProduct\Api\Data\SubItemInterfaceFactory;
|
||||
use Stemcell\SubItemProduct\Model\ResourceModel\SubItem as SubItemResourceModel;
|
||||
use Stemcell\SubItemProduct\Model\SubItemRepository;
|
||||
use Symfony\Component\Console\Command\Command;
|
||||
use Symfony\Component\Console\Input\InputInterface;
|
||||
use Symfony\Component\Console\Output\OutputInterface;
|
||||
|
||||
/**
|
||||
* 导入组分配比
|
||||
* 导入magento产品(一般是子产品)中包含的组分sku、每个magento产品包含的组分数量、组分库存。在导入时候,如果已经存在配比数据,会先清除
|
||||
* 在运行这个命令时候,可以只有subitem.xlsx
|
||||
*/
|
||||
class SubItemImport extends Command
|
||||
{
|
||||
|
||||
const COMMAND_NAME = 'stemcell:subitem:import';
|
||||
|
||||
protected array $data = [];
|
||||
public function __construct(
|
||||
private Dir $dir,
|
||||
private ProductRepository $productRepository,
|
||||
private State $state,
|
||||
private ProductFactory $productFactory,
|
||||
private StoreManagerInterface $storeManager,
|
||||
private SubItemInterfaceFactory $subItemImporterFactory,
|
||||
private SubItemRepository $subItemRepository,
|
||||
private StockRegistry $stockRegistry,
|
||||
protected SubItemResourceModel $subItemResourceModel,
|
||||
) {
|
||||
parent::__construct(self::COMMAND_NAME);
|
||||
}
|
||||
|
||||
/**
|
||||
* 简单产品sku以及其中包含的小货号sku、每个简单产品包含的小货号数量
|
||||
* @return array
|
||||
* @throws \Exception
|
||||
*/
|
||||
protected function getSubItemData()
|
||||
{
|
||||
$path = $this->dir->getDir('Stemcell_SubItemProduct').'/data/subitem.xlsx';
|
||||
if (!file_exists($path)) {
|
||||
throw new \Exception(sprintf('File %s does not exist', $path));
|
||||
}
|
||||
|
||||
$spreadSheet = IOFactory::load($path);
|
||||
|
||||
$data = $spreadSheet->getActiveSheet()->toArray('');
|
||||
array_shift($data);
|
||||
|
||||
return $data;
|
||||
}
|
||||
|
||||
/**
|
||||
* 小货号库存
|
||||
* @return array
|
||||
* @throws \Exception
|
||||
*/
|
||||
protected function getSubItemStock()
|
||||
{
|
||||
$path = $this->dir->getDir('Stemcell_SubItemProduct').'/data/stock2026.1.5.xlsx';
|
||||
if (!file_exists($path)) {
|
||||
throw new \Exception(sprintf('File %s does not exist', $path));
|
||||
}
|
||||
|
||||
$spreadSheet = IOFactory::load($path);
|
||||
$data = $spreadSheet->getActiveSheet()->toArray('');
|
||||
array_shift($data);
|
||||
|
||||
$temp = [];
|
||||
foreach ($data as $row) {
|
||||
$temp[$row[0]] = $row[1];
|
||||
}
|
||||
return $temp;
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
protected function execute(
|
||||
InputInterface $input,
|
||||
OutputInterface $output
|
||||
): int {
|
||||
|
||||
$this->state->setAreaCode(\Magento\Framework\App\Area::AREA_ADMINHTML);
|
||||
|
||||
$subItemData = $this->getSubItemData();
|
||||
$qty = $this->getSubItemStock();
|
||||
|
||||
$group = [];
|
||||
foreach ($subItemData as $subItem) {
|
||||
$group[$subItem[0]][] = $subItem;
|
||||
}
|
||||
|
||||
|
||||
$fail = 0;
|
||||
$count = count($subItemData);
|
||||
$failedSku = [];
|
||||
foreach ($group as $sku => $subItems) {
|
||||
try {
|
||||
$productSql = 'select entity_id from catalog_product_entity where sku = "'.$sku.'";';
|
||||
$productId = $this->subItemResourceModel->getConnection()->fetchOne($productSql);
|
||||
|
||||
if (!$productId) {
|
||||
$output->writeln(sprintf('大货号%s找不到,sql: %s', $sku, $productSql));
|
||||
$failedSku[] = $sku;
|
||||
continue;
|
||||
}
|
||||
$deleteSql = 'DELETE FROM subitem_product WHERE product_id = '.$productId.';';
|
||||
$this->subItemResourceModel->getConnection()->query($deleteSql);
|
||||
|
||||
$subSkus = [];
|
||||
foreach ($subItems as $subItem) {
|
||||
if (!$subItem[2]) {
|
||||
$subItem[2] = $sku;
|
||||
}
|
||||
$stock = $qty[$subItem[2]] ?? 0;
|
||||
|
||||
$sql = "
|
||||
INSERT INTO subitem_product (product_id, sub_sku, bundle_qty, stock_qty)
|
||||
VALUES ({$productId}, '{$subItem[2]}', {$subItem[4]}, $stock) AS new
|
||||
ON DUPLICATE KEY UPDATE
|
||||
bundle_qty = new.bundle_qty;
|
||||
";
|
||||
$subSkus[] = $subItem[2];
|
||||
$output->writeln($sql);
|
||||
$this->subItemResourceModel->getConnection()->query($sql);
|
||||
}
|
||||
|
||||
$output->writeln(sprintf('大货号%s, 包含小货号产品%s ', $sku, implode(', ', $subSkus)));
|
||||
|
||||
} catch (\Exception $e) {
|
||||
$fail++;
|
||||
$output->writeln(sprintf('大货号%s绑定小货号产品%s关联失败,原因:%s', $subItem[0], $subItem[2], $e->getMessage() ));
|
||||
}
|
||||
$output->writeln(str_repeat('-', 200));
|
||||
}
|
||||
|
||||
$output->writeln(sprintf('共有%d行数据,其中%d导入失败', $count, $fail));
|
||||
if (!empty($failedSku)) {
|
||||
$output->writeln('不存在的大货号如下:');
|
||||
$output->writeln(implode(', ', $failedSku));
|
||||
}
|
||||
|
||||
return Command::SUCCESS;
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
protected function configure(): void
|
||||
{
|
||||
$this->setDescription('导入简单产品中包含的小货号sku、每个简单产品包含的小货号数量、小货号库存');
|
||||
parent::configure();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user