From: Aleksey Filippov Date: Thu, 22 May 2025 13:52:59 +0000 (+0300) Subject: [ERP-415] Актуализация статусов подписки в Телеграм X-Git-Url: https://gitweb.erp-flowers.ru/?a=commitdiff_plain;h=08fe192e1d99f9634a65c8a1c0ef3db566c63428;p=erp24_rep%2Fyii-erp24%2F.git [ERP-415] Актуализация статусов подписки в Телеграм --- 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' ]; }