]> gitweb.erp-flowers.ru Git - erp24_rep/yii-erp24/.git/commitdiff
Правки сообщений телеграм
authorfomichev <vladimir.fomichev@erp-flowers.ru>
Mon, 21 Apr 2025 10:28:46 +0000 (13:28 +0300)
committerfomichev <vladimir.fomichev@erp-flowers.ru>
Mon, 21 Apr 2025 10:28:46 +0000 (13:28 +0300)
erp24/commands/CronController.php
erp24/records/Users.php

index 830fa07a8172f436143c688bb3d70fd22849a4f6..bcf0629aaa4924d0877126d4e2ab6b6da947729a 100644 (file)
@@ -415,28 +415,20 @@ class CronController extends Controller
                 ->asArray()
                 ->all();
 
-            $kogortStopListPhones = KogortStopList::find()->select('phone')->asArray()->all();
-            $phonesArray = array_diff(
-                array_diff(
-                    array_column($telegramUsers, 'phone'),
-                    $sentStatusKogort
-                ),
-                array_column($kogortStopListPhones, 'phone')
-            );
+            $toSend = Users::filterTelegramUsersForSending($telegramUsers, $sentStatusKogort);
 
             $chatIdsArray = array_column($telegramUsers, 'chat_id');
-            $countTelegramPhones = count($phonesArray);
+            $countTelegramPhones = count($toSend);
             $this->stdout(
                 "Всего телефонов в рассылке телеграма {$countTelegramPhones} записей.\n",
                 BaseConsole::FG_GREEN
             );
-            if (!empty($telegramUsers)) {
+            if (!empty($toSend)) {
                 $messageText = $messagesSettings->replaceShortcodes(
                     $messagesSettings->offer_1,
                     $targetDate
                 );
-                foreach ($telegramUsers as $telegramUser) {
-                    if (!in_array($telegramUser['phone'], $sentStatusKogort)) {
+                foreach ($toSend as $telegramUser) {
                         $messageData = [];
                         $messageData['chat_id'] = $telegramUser['chat_id'];
                         $messageData['phone'] = $telegramUser['phone'];
@@ -448,7 +440,6 @@ class CronController extends Controller
                         Yii::$app->queue->push(new SendTelegramMessageJob([
                             'messageData' => $messageData,
                         ]));
-                    }
                 }
                 //TODO - перенос в отправку
                 $updatedCount = SentKogort::updateAll(
@@ -461,7 +452,7 @@ class CronController extends Controller
                         'kogort_date' => $kogortDate,
                         'target_date' => $targetDate,
                         'kogort_number' => SentKogort::KOGORT_NUMBERS['target'],
-                        'phone' => $phonesArray,
+                        'phone' => array_column($toSend, 'phone'),
                     ]
                 );
 
@@ -704,28 +695,20 @@ class CronController extends Controller
                 ->asArray()
                 ->all();
 
-            $kogortStopListPhones = KogortStopList::find()->select('phone')->asArray()->all();
-            $phonesArray = array_diff(
-                array_diff(
-                    array_column($telegramUsers, 'phone'),
-                    $sentStatusKogort
-                ),
-                array_column($kogortStopListPhones, 'phone')
-            );
+            $toSend = Users::filterTelegramUsersForSending($telegramUsers, $sentStatusKogort);
 
 
             $chatIdsArray = array_column($telegramUsers, 'chat_id');
-            $countTelegramPhones = count($phonesArray);
+            $countTelegramPhones = count($toSend);
             $this->stdout(
                 "Всего телефонов в рассылке телеграма {$countTelegramPhones} записей.\n",
                 BaseConsole::FG_GREEN
             );
 
-            if (!empty($telegramUsers)) {
+            if (!empty($toSend)) {
                 $messageText = $messagesSettings
                     ->replaceShortcodes($messagesSettings->offer_2, $targetDate);
-                foreach ($telegramUsers as $telegramUser) {
-                    if (!in_array($telegramUser['phone'], $sentStatusKogort)) {
+                foreach ($toSend as $telegramUser) {
                         $messageData = [];
                         $messageData['chat_id'] = $telegramUser['chat_id'];
                         $messageData['phone'] = $telegramUser['phone'];
@@ -737,7 +720,7 @@ class CronController extends Controller
                         Yii::$app->queue->push(new SendTelegramMessageJob([
                             'messageData' => $messageData,
                         ]));
-                    }
+
                 }
                 //TODO - перенос в отправку
                 $updatedCount = SentKogort::updateAll(
@@ -748,7 +731,7 @@ class CronController extends Controller
                     [
                         'target_date' => $targetDate,
                         'kogort_number' => SentKogort::KOGORT_NUMBERS['target'],
-                        'phone' => $phonesArray,
+                        'phone' => array_column($toSend, 'phone'),
                     ]
                 );
 
index 115e9d1f9dc72479a7675072aef063faf0ea1cfd..653e400b031c7ec1a51a712932d47744d355f0dc 100755 (executable)
@@ -728,4 +728,33 @@ class Users extends \yii\db\ActiveRecord
         return $data;
     }
 
+    /**
+     * Отфильтровывает список $telegramUsers,
+     * убирая из него тех, чей телефон есть в $sentStatusKogort
+     * и тех, кто в стоп‑листе KogortStopList.
+     *
+     * @param array $telegramUsers  [['phone'=>..., 'chat_id'=>...], …]
+     * @param array $sentStatusKogort  ['79991112233', …]
+     * @return array  отфильтрованный массив тех же элементов
+     */
+    public static function filterTelegramUsersForSending(array $telegramUsers, array $sentStatusKogort): array
+    {
+        $stopListPhones = KogortStopList::find()
+            ->select('phone')
+            ->column();
+
+        return array_filter($telegramUsers, function($u) use ($sentStatusKogort, $stopListPhones) {
+
+            if (in_array($u['phone'], $sentStatusKogort, true)) {
+                return false;
+            }
+
+            if (in_array($u['phone'], $stopListPhones, true)) {
+                return false;
+            }
+            return true;
+        });
+    }
+
+
 }