]> gitweb.erp-flowers.ru Git - erp24_rep/yii-erp24/.git/commitdiff
[ERP-415] Актуализация статусов подписки в Телеграм
authorAleksey Filippov <Aleksey.Filippov@erp-flowers.ru>
Thu, 22 May 2025 13:52:59 +0000 (16:52 +0300)
committerAleksey Filippov <Aleksey.Filippov@erp-flowers.ru>
Thu, 22 May 2025 13:52:59 +0000 (16:52 +0300)
erp24/commands/CronController.php
erp24/migrations/m250522_155213_add_column_telegram_updated_at_to_users_table.php [new file with mode: 0644]
erp24/records/Users.php

index da539c2764b9c0083be10cba4d5e5526dc7ffffe..52b78a6efe4908dcaf36e7d136429dd688111b6a 100644 (file)
@@ -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 (file)
index 0000000..d57329e
--- /dev/null
@@ -0,0 +1,39 @@
+<?php
+
+use yii\db\Migration;
+
+/**
+ * Handles adding columns to table `{{%users}}`.
+ */
+class m250522_155213_add_column_telegram_updated_at_to_users_table extends Migration
+{
+    const TABLE_NAME = 'erp24.users';
+    /**
+     * {@inheritdoc}
+     */
+    public function safeUp()
+    {
+        $table = $this->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');
+        }
+    }
+}
index 653e400b031c7ec1a51a712932d47744d355f0dc..2f529602e5d7050b31f64bf4d8de36ad0bc0daa2 100755 (executable)
@@ -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'
             ];
     }