From 08fe192e1d99f9634a65c8a1c0ef3db566c63428 Mon Sep 17 00:00:00 2001 From: Aleksey Filippov Date: Thu, 22 May 2025 16:52:59 +0300 Subject: [PATCH] =?utf8?q?[ERP-415]=20=D0=90=D0=BA=D1=82=D1=83=D0=B0=D0=BB?= =?utf8?q?=D0=B8=D0=B7=D0=B0=D1=86=D0=B8=D1=8F=20=D1=81=D1=82=D0=B0=D1=82?= =?utf8?q?=D1=83=D1=81=D0=BE=D0=B2=20=D0=BF=D0=BE=D0=B4=D0=BF=D0=B8=D1=81?= =?utf8?q?=D0=BA=D0=B8=20=D0=B2=20=D0=A2=D0=B5=D0=BB=D0=B5=D0=B3=D1=80?= =?utf8?q?=D0=B0=D0=BC?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- erp24/commands/CronController.php | 79 +++++++++++++++++++ ...umn_telegram_updated_at_to_users_table.php | 39 +++++++++ erp24/records/Users.php | 15 +++- 3 files changed, 131 insertions(+), 2 deletions(-) create mode 100644 erp24/migrations/m250522_155213_add_column_telegram_updated_at_to_users_table.php diff --git a/erp24/commands/CronController.php b/erp24/commands/CronController.php index da539c27..52b78a6e 100644 --- a/erp24/commands/CronController.php +++ b/erp24/commands/CronController.php @@ -1274,6 +1274,85 @@ class CronController extends Controller $log->save(false); } + /** + * Обновление статуса подписки пользователя в Телеграмм. + */ + protected function actionUpdateUserSubscribe() + { + $time_start = microtime(true); + $batchSize = 500; + $offset = 0; + + while (true) { + $usersTelegram = UsersTelegram::find() + ->select(['phone','is_blocked','is_registered']) + ->where('phone IS NOT NULL') + ->limit($batchSize) + ->offset($offset) + ->asArray() + ->all(); + + /*TODO Отключить при выкатке*/ + // Для тестирования + if ($offset > 0) { + break; + } + if (empty($usersTelegram)) { + break; + } + + foreach ($usersTelegram as $userTelegram) { + $isBlocked = (int)$userTelegram['is_blocked']; + $isRegistered = (int)$userTelegram['is_registered']; + + $phoneRow = $userTelegram['phone']; + + //Тело цикла + $client = Users::find() + ->where(['phone' => $phoneRow]) + ->one(); + + if (!empty($client)) { + $clientDateIsChanged = false; + $telegramIsSubscribed = ($isBlocked == 0 && $isRegistered == 1) ? 1 : 0; + + /** @var Users $client */ + if ($client->telegram_is_subscribed == 1 && $telegramIsSubscribed == 0) { + $client->telegram_unsubscribed_at = date('Y-m-d H:i:s'); + $clientDateIsChanged = true; + } + + /** @var Users $client */ + if ($client->telegram_is_subscribed != $telegramIsSubscribed) { + $client->telegram_is_subscribed = $telegramIsSubscribed; + $client->telegram_updated_at = date('Y-m-d H:i:s'); + $clientDateIsChanged = true; + } + + /** @var Users $client */ + if (empty($client->telegram_created_at) ?? $telegramIsSubscribed == 1) { + $client->telegram_created_at = date('Y-m-d H:i:s'); + $clientDateIsChanged = true; + } + + if ($clientDateIsChanged) { + $validate = $client->validate(); + +// $client->save(false); + } + } + } + + $offset += $batchSize; + + $this->stdout("Обработано записей: {$offset}\n", BaseConsole::FG_GREEN); + } + + $time_end = microtime(true); + $execution_time = ($time_end - $time_start)/60; + $this->stdout("Total Execution Time: {$execution_time} Mins \n", BaseConsole::FG_GREEN); + } + // обновление уровней по продажам public function actionUpdateBonusLevels() { diff --git a/erp24/migrations/m250522_155213_add_column_telegram_updated_at_to_users_table.php b/erp24/migrations/m250522_155213_add_column_telegram_updated_at_to_users_table.php new file mode 100644 index 00000000..d57329ea --- /dev/null +++ b/erp24/migrations/m250522_155213_add_column_telegram_updated_at_to_users_table.php @@ -0,0 +1,39 @@ +db->schema->getTableSchema(self::TABLE_NAME); + if ($table === null) { + return; + } + + if (!$this->db->schema->getTableSchema(self::TABLE_NAME, true)->getColumn('telegram_updated_at')) { + $this->addColumn( + self::TABLE_NAME, + 'telegram_updated_at', + $this->timestamp()->null()->defaultExpression('CURRENT_TIMESTAMP')->comment('дата изменения подписки в Телеграмм'), + ); + } + } + + /** + * {@inheritdoc} + */ + public function safeDown() + { + if ($this->db->schema->getTableSchema(self::TABLE_NAME, true)->getColumn('telegram_updated_at')) { + $this->dropColumn(self::TABLE_NAME, 'telegram_updated_at'); + } + } +} diff --git a/erp24/records/Users.php b/erp24/records/Users.php index 653e400b..2f529602 100755 --- a/erp24/records/Users.php +++ b/erp24/records/Users.php @@ -70,6 +70,7 @@ use yii\helpers\BaseConsole; * @property string $info * @property string|null $bonus_level Уровань клиента в БС * @property string|null $check_id_last_sale GUID последнего чека + * @property string|null $telegram_updated_at Изменение подписки в Телеграм */ class Users extends \yii\db\ActiveRecord { @@ -195,7 +196,16 @@ class Users extends \yii\db\ActiveRecord ], 'safe' ], - [['pol', 'check_id_last_sale', 'info', 'telegram_created_at'], 'string'], + [ + [ + 'pol', + 'check_id_last_sale', + 'info', + 'telegram_created_at', + 'telegram_updated_at' + ], + 'string' + ], [ [ 'forgot', @@ -297,7 +307,8 @@ class Users extends \yii\db\ActiveRecord 'events' => 'Events', 'telegram_unsubscribed_at' => 'Telegram Unsubscribed At', 'bonus_level' => 'Bonus Level', - 'check_id_last_sale' => 'GUID Last Check' + 'check_id_last_sale' => 'GUID Last Check', + 'telegram_updated_at' => 'telegram_updated_at' ]; } -- 2.39.5