From 5387637e35cc5b9f7eead0e12b6ea77b8ccc3e53 Mon Sep 17 00:00:00 2001 From: Arsen Mirzaev Tatyano-Muradovich Date: Fri, 23 Jun 2023 00:03:22 +0700 Subject: [PATCH] =?UTF-8?q?=D1=82=D0=B0=D0=B9=D0=BC=D0=B5=D1=80=20=D0=B4?= =?UTF-8?q?=D0=BB=D1=8F=20=D0=BE=D0=B1=D1=85=D0=BE=D0=B4=D0=B0=20=D0=B1?= =?UTF-8?q?=D0=B0=D0=BD=D0=B0,=20=D0=B4=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=D0=B0=20=D0=B2=D0=BE=D0=B7=D0=BC=D0=BE=D0=B6=D0=BD?= =?UTF-8?q?=D0=BE=D1=81=D1=82=D1=8C=20=D1=80=D1=83=D1=87=D0=BD=D0=BE=D0=B9?= =?UTF-8?q?=20=D0=B7=D0=B0=D0=BF=D0=B8=D1=81=D0=B8=20=D0=B2=20=D0=BF=D1=80?= =?UTF-8?q?=D0=B0=D0=B2=D1=83=D1=8E=20=D1=87=D0=B0=D1=81=D1=82=D1=8C=20?= =?UTF-8?q?=D1=82=D0=B0=D0=B1=D0=BB=D0=B8=D1=86=D1=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../parser/system/public/markets.php | 11 +++--- .../parser/system/public/workers.php | 3 ++ .../parser/system/public/works.php | 34 ++++++++++++------- 3 files changed, 31 insertions(+), 17 deletions(-) diff --git a/mirzaev/spetsresurs/google_sheets/parser/system/public/markets.php b/mirzaev/spetsresurs/google_sheets/parser/system/public/markets.php index ba3cf8f..ca8d472 100644 --- a/mirzaev/spetsresurs/google_sheets/parser/system/public/markets.php +++ b/mirzaev/spetsresurs/google_sheets/parser/system/public/markets.php @@ -106,8 +106,7 @@ function sync(Row &$row, string $city = 'Красноярск'): void // Обновление инстанции документа в базе данных document::update($arangodb->session, $market); } else if ( - !empty($_row['id']) - && $market = collection::search( + $market = collection::search( $arangodb->session, sprintf( "FOR d IN markets FILTER d._id == '%s' RETURN d", @@ -148,16 +147,20 @@ foreach ($sheets as $sheet) { $rows = (new Flow())->read(new GoogleSheetExtractor($api, $document, new Columns($sheet, 'A', 'D'), true, 1000, 'row')); $i = 1; - foreach ($rows->fetch(100) as $row) { + foreach ($rows->fetch(3000) as $row) { ++$i; $buffer = $row; sync($row, $sheet); - if ($buffer !== $row) + if ($buffer !== $row) { $api->spreadsheets_values->update( $document, "$sheet!A$i:D$i", new ValueRange(['values' => [array_values($row->entries()->toArray()['row'])]]), ['valueInputOption' => 'USER_ENTERED'] ); + + // Ожидание для того, чтобы снизить шанс блокировки от Google + sleep(3); + } } } diff --git a/mirzaev/spetsresurs/google_sheets/parser/system/public/workers.php b/mirzaev/spetsresurs/google_sheets/parser/system/public/workers.php index 89b1d3a..7fe376e 100644 --- a/mirzaev/spetsresurs/google_sheets/parser/system/public/workers.php +++ b/mirzaev/spetsresurs/google_sheets/parser/system/public/workers.php @@ -292,6 +292,9 @@ foreach ($sheets as $sheet) { new ValueRange(['values' => [array_values($row->entries()->toArray()['row'])]]), ['valueInputOption' => 'USER_ENTERED'] ); + + // Ожидание для того, чтобы снизить шанс блокировки от Google + sleep(3); } } } diff --git a/mirzaev/spetsresurs/google_sheets/parser/system/public/works.php b/mirzaev/spetsresurs/google_sheets/parser/system/public/works.php index 8f9ddb8..fb20d2d 100644 --- a/mirzaev/spetsresurs/google_sheets/parser/system/public/works.php +++ b/mirzaev/spetsresurs/google_sheets/parser/system/public/works.php @@ -95,7 +95,7 @@ function filterWorker(?string $worker): string { global $arangodb; - return match ($worker) { + return match ((int) $worker) { '', 'Отмена', 'отмена', 0, 00, 000, 0000, 00000, 000000, 0000000, 00000000, 000000000, 0000000000 => '', default => (function () use ($worker, $arangodb) { if ( @@ -103,7 +103,7 @@ function filterWorker(?string $worker): string && collection::search( $arangodb->session, sprintf( - "FOR d IN workers FILTER d.id == %s RETURN d", + "FOR d IN workers FILTER d.id == '%s' RETURN d", $worker ) ) @@ -130,6 +130,10 @@ function sync(int $_i, Row &$row, ?array $raw = null): void // Инициализация строки в Google Sheet $_row = init($row->toArray()['row']); + // Замена пустой строки на NULL (для логических операций) + foreach ($_row as $key => &$value) if ($value === '') $value = null; + foreach ($raw as $key => &$value) if ($value === '') $value = null; + if (collection::init($arangodb->session, 'works')) if (!empty($_row['_id']) && $work = collection::search($arangodb->session, sprintf("FOR d IN works FILTER d._id == '%s' RETURN d", $_row['_id']))) { // Найдена запись работы (строки) в базе данных @@ -300,9 +304,9 @@ function sync(int $_i, Row &$row, ?array $raw = null): void // Обновление инстанции документа в базе данных document::update($arangodb->session, $work); } else if ( - !empty($_row['imported_market']) + (!empty($_row['imported_market']) || !empty($_row['market'])) && collection::init($arangodb->session, 'requests', true) && collection::init($arangodb->session, 'markets') - && ($market = collection::search($arangodb->session, sprintf("FOR d IN markets FILTER d.id == '%s' RETURN d", $_row['imported_market']))) + && ($market = collection::search($arangodb->session, sprintf("FOR d IN markets FILTER d.id == '%s' RETURN d", $_row['imported_market'] ?? $_row['market']))) && $work = collection::search( $arangodb->session, sprintf( @@ -377,17 +381,17 @@ function sync(int $_i, Row &$row, ?array $raw = null): void 'imported_start' => $raw[5] ?? '', 'imported_end' => $raw[6] ?? '', 'imported_hours' => $_row['imported_hours'] ?? '', - 'created_in_sheets' => $raw[0] ?? '', - 'date' => $raw[1] ?? '', - 'market' => $_row['imported_market'] ?? '', + 'created_in_sheets' => $raw[0] ?? $raw[8] ?? '', + 'date' => $raw[1] ?? $raw[9] ?? '', + 'market' => $_row['imported_market'] ?? $_row['market'] ?? '', 'type' => empty($_row['type']) ? "=ЕСЛИ(СОВПАД(I$_i;\"\");\"\"; IFNA(ВПР(K$_i;part_import_KRSK!\$B\$2:\$E\$15603;2;);\"Нет в базе\"))" : $_row['type'], 'address' => empty($_row['address']) ? "=ЕСЛИ(СОВПАД(I$_i;\"\");\"\"; IFNA(ВПР(K$_i;part_import_KRSK!\$B\$2:\$E\$15603;4;);\"Нет в базе\"))" : $_row['address'], - 'worker' => filterWorker($_row['imported_worker']), + 'worker' => filterWorker($_row['imported_worker'] ?? $_row['worker'] ?? ''), 'name' => empty($_row['name']) ? "=ЕСЛИ(СОВПАД(\$I$_i;\"\");\"\"; ЕСЛИ( НЕ(СОВПАД(IFNA(ВПР(\$N$_i;part_import_KRSK!\$R$2:\$R$4999;1;);\"\");\$N$_i)); ЕСЛИ((СОВПАД(IFNA(ВПР(\$N$_i;part_import_KRSK!\$I\$2:\$L\$4999;4);\"\");\"\")); IFNA(ВПР(\$N$_i;part_import_KRSK!\$I\$2:\$J\$4999;2;); \"Сотрудник не назначен\"); \"УВОЛЕН (В списке работающих)\"); \"УВОЛЕН (В списке уволенных)\"))" : $_row['name'], - 'work' => $_row['imported_work'] ?? '', - 'start' => $raw[5] ?? '', - 'end' => $raw[6] ?? '', - 'hours' => $_row['imported_hours'] ?? '', + 'work' => $_row['imported_work'] ?? $_row['work'] ?? '', + 'start' => $raw[5] ?? $raw[16] ?? '', + 'end' => $raw[6] ?? $raw[17] ?? '', + 'hours' => $_row['imported_hours'] ?? $_row['hours'] ?? '', 'tax' => empty($_row['tax']) ? "=ЕСЛИ(СОВПАД(\$I$_i;\"\");\"\"; IFNA(ВПР(\$N$_i;part_import_KRSK!\$I\$2:\$K\$5000;3;); IFNA(ВПР(\$N$_i;part_import_KRSK!\$R\$2:\$T\$5000;3;);\"000000000000\")))" : $_row['tax'], 'confirmed' => $_row['confirmed'] ?? '', 'commentary' => $_row['commentary'] ?? '', @@ -431,12 +435,16 @@ foreach ($sheets as $sheet) { sync($i, $row, $sheets->spreadsheets_values->get($document, "$sheet!A$i:X$i")[0] ?? null); // Запись изменений строки в Google Sheet - if ($buffer !== $row) + if ($buffer !== $row) { $sheets->spreadsheets_values->update( $document, "$sheet!A$i:X$i", new ValueRange(['values' => [array_values($row->entries()->toArray()['row'])]]), ['valueInputOption' => 'USER_ENTERED'] ); + + // Ожидание для того, чтобы снизить шанс блокировки от Google + sleep(3); + } } }