From: fomichev Date: Mon, 21 Apr 2025 10:28:46 +0000 (+0300) Subject: Правки сообщений телеграм X-Git-Url: https://gitweb.erp-flowers.ru/?a=commitdiff_plain;h=04645026d724c14e131ce7ee0906ed6979a4cdd3;p=erp24_rep%2Fyii-erp24%2F.git Правки сообщений телеграм --- diff --git a/erp24/commands/CronController.php b/erp24/commands/CronController.php index 830fa07a..bcf0629a 100644 --- a/erp24/commands/CronController.php +++ b/erp24/commands/CronController.php @@ -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'), ] ); diff --git a/erp24/records/Users.php b/erp24/records/Users.php index 115e9d1f..653e400b 100755 --- a/erp24/records/Users.php +++ b/erp24/records/Users.php @@ -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; + }); + } + + }