increase speed
This commit is contained in:
@@ -15,7 +15,13 @@ use RuntimeException;
|
||||
use SplFileObject;
|
||||
use Symfony\Component\Finder\Finder;
|
||||
|
||||
|
||||
/**
|
||||
* Class HiltesImport
|
||||
*
|
||||
* This class is responsible for importing data from files into the application.
|
||||
* It uses Symfony's Finder component to locate the files and then processes them line by line.
|
||||
* The data is then saved into the database using the repositories for the Product, Stock and Warehouse entities.
|
||||
*/
|
||||
class HiltesImport
|
||||
{
|
||||
protected $currentDirPath;
|
||||
@@ -29,8 +35,17 @@ class HiltesImport
|
||||
private $cachedStockIds;
|
||||
private $rootPath;
|
||||
|
||||
private $deleteFiles = false;
|
||||
private $deleteFiles = true;
|
||||
|
||||
/**
|
||||
* HiltesImport constructor.
|
||||
*
|
||||
* @param ProductRepository $productRepository
|
||||
* @param WarehouseRepository $warehouseRepository
|
||||
* @param StockRepository $stockRepository
|
||||
* @param LoggerInterface $logger
|
||||
* @param string $rootPath
|
||||
*/
|
||||
public function __construct(ProductRepository $productRepository, WarehouseRepository $warehouseRepository, StockRepository $stockRepository, LoggerInterface $logger, string $rootPath)
|
||||
{
|
||||
$this->productRepository = $productRepository;
|
||||
@@ -44,8 +59,11 @@ class HiltesImport
|
||||
|
||||
|
||||
/**
|
||||
* Starts the import process.
|
||||
*
|
||||
* @param bool $delta
|
||||
* @return array
|
||||
* @throws RuntimeException
|
||||
*/
|
||||
public function startImport(bool $delta = false): array
|
||||
{
|
||||
@@ -53,17 +71,17 @@ class HiltesImport
|
||||
if ($this->getFiles($delta)) {
|
||||
#*** Holt alle Stocks und setzt ein Array **************
|
||||
$this->getStocks();
|
||||
$count = 0;
|
||||
|
||||
if (!empty($this->arrData['orgFiles']['data']) && count($this->arrData['orgFiles']['data'])) {
|
||||
foreach ($this->arrData['orgFiles']['data'] as $file) {
|
||||
|
||||
if (is_file($file['realPath'])) {
|
||||
$count += $this->loadFiles($file['realPath']);
|
||||
$this->loadFiles($file['realPath']);
|
||||
} else {
|
||||
throw new RuntimeException("Error: File not found - " . $file['realPath']);
|
||||
}
|
||||
}
|
||||
$this->logger->info("Imported $count stocks");
|
||||
|
||||
return $this->cachedProdIds;
|
||||
} else {
|
||||
$this->logger->info('No Files');
|
||||
@@ -75,6 +93,9 @@ class HiltesImport
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieves the files to be imported.
|
||||
*
|
||||
* @param bool $delta
|
||||
* @return bool
|
||||
*/
|
||||
protected function getFiles(bool $delta = false): bool
|
||||
@@ -102,7 +123,7 @@ class HiltesImport
|
||||
return true;
|
||||
}
|
||||
|
||||
protected function getStocks()
|
||||
protected function getStocks(): void
|
||||
{
|
||||
$stocks = $this->stockRepository->findAll();
|
||||
|
||||
@@ -111,19 +132,42 @@ class HiltesImport
|
||||
}
|
||||
}
|
||||
|
||||
protected function loadFiles($srcFile)
|
||||
/**
|
||||
* Loads the files and processes them line by line.
|
||||
*
|
||||
* @param $srcFile
|
||||
* @return void
|
||||
* @throws Exception
|
||||
*/
|
||||
protected function loadFiles($srcFile): void
|
||||
{
|
||||
try {
|
||||
$file = new SplFileObject($srcFile);
|
||||
|
||||
$this->logger->info('Starte Import von ' . $file->getRealPath());
|
||||
$count = 0;
|
||||
|
||||
while (!$file->eof()) {
|
||||
$this->processLine($file->fgets());
|
||||
$this->processLine($file->fgets(), "product");
|
||||
$count++;
|
||||
}
|
||||
|
||||
$this->cachedProdIds = $this->productRepository->saveAll();
|
||||
|
||||
//Setzte den Zeiger wieder auf den Anfang
|
||||
$file->seek(0);
|
||||
|
||||
while (!$file->eof()) {
|
||||
$this->processLine($file->fgets(), "stock");
|
||||
$count++;
|
||||
}
|
||||
|
||||
//Save Stocks
|
||||
$this->stockRepository->saveAll();
|
||||
|
||||
} catch (Exception $e) {
|
||||
$this->logger->error($e->getMessage());
|
||||
throw new Exception($e->getMessage());
|
||||
}
|
||||
|
||||
if ($this->deleteFiles) {
|
||||
@@ -132,21 +176,47 @@ class HiltesImport
|
||||
}
|
||||
|
||||
$this->logger->info($count . ' Datensätze importiert');
|
||||
|
||||
return $count;
|
||||
}
|
||||
|
||||
protected function processLine($line)
|
||||
protected function processLine($line, $type = "stock"): void
|
||||
{
|
||||
$data = str_getcsv($line, ';', '"');
|
||||
|
||||
if (!empty($data[0])) {
|
||||
$this->trimArray($data);
|
||||
// $this->logger->info($data[0] . ' ' . $data[1] . ' ' . $data[3]);
|
||||
if (empty($data[0])) {
|
||||
//$this->logger->warning('Keine Daten in Zeile' . $line);
|
||||
return;
|
||||
}
|
||||
|
||||
if ($type == "stock") {
|
||||
$this->saveData($data);
|
||||
} else {
|
||||
$this->processProduct($data);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Processes product data.
|
||||
*
|
||||
* @param $prodData
|
||||
*/
|
||||
private function processProduct(array $prodData): void
|
||||
{
|
||||
|
||||
$gtin = substr($prodData[0], 1);
|
||||
|
||||
$product = $this->productRepository->findOneBy(['gtin' => $gtin]);
|
||||
if (empty($product)) {
|
||||
$product = new Product();
|
||||
$product->setGtin($gtin);
|
||||
}
|
||||
$this->productRepository->add($product);
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Trims all elements of an array.
|
||||
*
|
||||
* @param array $arr
|
||||
* @return void
|
||||
*/
|
||||
@@ -158,34 +228,47 @@ class HiltesImport
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $data
|
||||
* @return false
|
||||
* Saves the data into the database.
|
||||
*
|
||||
* @param array $prodData
|
||||
* @return void
|
||||
*/
|
||||
protected function saveData(array $data): bool
|
||||
protected function saveData(array $prodData): void
|
||||
{
|
||||
if (!isset($data[3])) {
|
||||
$this->logger->error('No Warehouse' . $data[3]);
|
||||
return false;
|
||||
|
||||
$warehouseNumber = trim($prodData[3]);
|
||||
if (empty($prodData[1])) {
|
||||
$this->logger->info('Kein Bestand für ' . $prodData[0]);
|
||||
return;
|
||||
}
|
||||
$inStock = (int)$prodData[1] / 100;
|
||||
$gtin = substr($prodData[0], 1);
|
||||
|
||||
$warehouse = $this->checkWarehouseName(trim($data[3]));
|
||||
$gtin = $this->checkProduct(substr($data[0], 1));
|
||||
|
||||
if (!empty($warehouse) && !empty($this->cachedStockIds[$gtin][$warehouse->getId()])) {
|
||||
$stock = $this->cachedStockIds[$gtin][$warehouse->getId()];
|
||||
//Prüfe Lager
|
||||
$warehouse = $this->checkWarehouseName($warehouseNumber);
|
||||
|
||||
//Hole Produkt Id bzw lege Produkt an
|
||||
$product_id = $this->checkProduct($gtin);
|
||||
|
||||
if (!empty($warehouse) && !empty($this->cachedStockIds[$product_id][$warehouse->getId()])) {
|
||||
|
||||
$stock = $this->cachedStockIds[$product_id][$warehouse->getId()];
|
||||
|
||||
} else {
|
||||
$stock = new Stock();
|
||||
$stock->setProductId($gtin);
|
||||
$stock->setProductId($product_id);
|
||||
$stock->setWarehouse($warehouse);
|
||||
}
|
||||
|
||||
$stock->setInstock((int)$data[1] / 100);
|
||||
$this->stockRepository->save($stock, true);
|
||||
$stock->setInstock($inStock);
|
||||
$this->stockRepository->add($stock);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks the warehouse name and returns the corresponding warehouse.
|
||||
*
|
||||
* @param string $warehouseName
|
||||
* @return int
|
||||
*/
|
||||
@@ -199,7 +282,6 @@ class HiltesImport
|
||||
//Wenn kein Lager gefunden wurde, dann lege es an
|
||||
if (empty($warehouse)) {
|
||||
$warehouse = new Warehouse();
|
||||
//$warehouse->setId((int)$warehouseName);
|
||||
$warehouse->setName($warehouseName);
|
||||
$warehouseId = $this->warehouseRepository->save($warehouse, true);
|
||||
$newWarehouse = $this->warehouseRepository->findOneBy(['id' => $warehouseId]);
|
||||
@@ -213,11 +295,14 @@ class HiltesImport
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks the product and returns the corresponding product id.
|
||||
*
|
||||
* @param string $gtin
|
||||
* @return false|int|mixed|null
|
||||
*/
|
||||
private function checkProduct(string $gtin)
|
||||
{
|
||||
|
||||
#*** WEnn keine geCached Id Vorhanden
|
||||
if (empty($this->cachedProdIds[$gtin])) {
|
||||
$product = $this->productRepository->findOneBy(['gtin' => $gtin]);
|
||||
@@ -225,9 +310,10 @@ class HiltesImport
|
||||
if (empty($product)) {
|
||||
$product = new Product();
|
||||
$product->setGtin($gtin);
|
||||
$this->cachedProdIds["$gtin"] = $this->productRepository->save($product, true);
|
||||
//$this->productRepository->add($product);
|
||||
$this->cachedProdIds[$gtin] = $this->productRepository->save($product, true);
|
||||
} else {
|
||||
$this->cachedProdIds["$gtin"] = $product->getId();
|
||||
$this->cachedProdIds[$gtin] = $product->getId();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user