->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'];
Yii::$app->queue->push(new SendTelegramMessageJob([
'messageData' => $messageData,
]));
- }
}
//TODO - перенос в отправку
$updatedCount = SentKogort::updateAll(
'kogort_date' => $kogortDate,
'target_date' => $targetDate,
'kogort_number' => SentKogort::KOGORT_NUMBERS['target'],
- 'phone' => $phonesArray,
+ 'phone' => array_column($toSend, 'phone'),
]
);
->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'];
Yii::$app->queue->push(new SendTelegramMessageJob([
'messageData' => $messageData,
]));
- }
+
}
//TODO - перенос в отправку
$updatedCount = SentKogort::updateAll(
[
'target_date' => $targetDate,
'kogort_number' => SentKogort::KOGORT_NUMBERS['target'],
- 'phone' => $phonesArray,
+ 'phone' => array_column($toSend, 'phone'),
]
);
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;
+ });
+ }
+
+
}