"Всего телефонов в когорте {$countPhones} записей.\n",
BaseConsole::FG_GREEN
);
+ } else {
+ $this->stdout(
+ "Нет данных для формирования когорты для целевой даты {$targetDate}.\n",
+ BaseConsole::FG_RED
+ );
+ }
+
+ // Генерация для будущих дат (от текущей даты на $step1 дней вперёд)
+ for ($i = 1; $i <= $step1; $i++) {
+ $kogortDate = date('Y-m-d', strtotime("+$i days", strtotime($currentDate))); // Дата когорты
+ $targetDate = date('Y-m-d', strtotime("+$step1 days", strtotime($kogortDate))); // Целевая дата
+
+ $kogortPhones = Users::formKogortByDateAndType($targetDate, 'target');
+
+ if (!empty($kogortPhones)) {
+ Users::saveKogort($kogortPhones, $kogortDate, $targetDate, SentKogort::KOGORT_NUMBERS['target'], true);
+ $this->stdout(
+ "Когорта таргета на {$kogortDate} для целевой даты {$targetDate} успешно сохранена.\n",
+ BaseConsole::FG_GREEN
+ );
+ $countPhones = count($kogortPhones);
+ $this->stdout(
+ "Всего телефонов в когорте {$countPhones} записей.\n",
+ BaseConsole::FG_GREEN
+ );
+ } else {
+ $this->stdout(
+ "Нет данных для формирования когорты для целевой даты {$targetDate}.\n",
+ BaseConsole::FG_RED
+ );
+ }
+ }
+
+ $this->stdout("Генерация корорт завершена.\n", BaseConsole::FG_GREEN);
+ return ExitCode::OK;
+ }
+
+
+ public function actionSendFirstTelegramMessage()
+ {
+ $messagesSettings = UsersMessageManagement::find()->one();
+ if (!$messagesSettings) {
+ $this->stdout(
+ "Рассылка неактивна (не найдена настройка). Отправка сообщений прервана.\n",
+ BaseConsole::FG_RED
+ );
+ return ExitCode::UNAVAILABLE;
+ }
+
+ if (!$messagesSettings->active) {
+ $this->stdout(
+ "Рассылка неактивна (поле active = 0). Отправка сообщений прервана.\n",
+ BaseConsole::FG_RED
+ );
+ return ExitCode::UNAVAILABLE;
+ }
+
+ $this->stdout("Рассылка активна. Начинаем Отправку сообщений...\n", BaseConsole::FG_GREEN);
+
+ date_default_timezone_set('Europe/Moscow');
+
+ if ($this->time && !is_numeric($this->time)) {
+ $this->stdout("Некорректный формат времени\n", BaseConsole::FG_RED);
+ return ExitCode::DATAERR;
+ }
+
+ $time = $this->time ? (int)$this->time : time();
+
+ $currentYear = date('Y', $time);
+ $currentMonth = date('m', $time);
+ $currentDay = date('j', $time);
+ $currentDate = sprintf('%04d-%02d-%02d', $currentYear, $currentMonth, $currentDay);
+
+ $step1 = $messagesSettings ? $messagesSettings->day_before_step1 : 10;
+
+ // Генерация для текущей даты
+ $kogortDate = $currentDate;
+ $targetDate = date('Y-m-d', strtotime("+$step1 days", strtotime($kogortDate)));
+
+ $kogortPhones = SentKogort::find()
+ ->select('phone')
+ ->where(['kogort_date' => $kogortDate])
+ ->andWhere(['kogort_number' => 1])
+ ->andWhere(['target_date' => $targetDate])
+ ->andWhere(['status' => 1])
+ ->column();
+
+ if (!empty($kogortPhones)) {
+ $countPhones = count($kogortPhones);
+ $this->stdout(
+ "Всего телефонов в когорте {$countPhones} записей.\n",
+ BaseConsole::FG_GREEN
+ );
$sentStatusKogort = SentKogort::find()
->select('phone')
->where(['kogort_date' => $kogortDate])
+ ->andWhere(['kogort_number' => 1])
->andWhere(['target_date' => $targetDate])
->andWhere(['status' => 2])
->column();
$messageData['kogort_date'] = $kogortDate;
$messageData['target_date'] = $targetDate;
$messageData['type'] = 1;
- $messageData['message'] = $messagesSettings->replaceShortcodes($messagesSettings->offer_1, $kogortDate);
+ $messageData['message'] = $messagesSettings->replaceShortcodes(
+ $messagesSettings->offer_1,
+ $kogortDate
+ );
Yii::$app->queue->push(new SendTelegramMessageDBJob([
'messageData' => $messageData,
]));
- Yii::$app->queue->push(new SendTelegramMessageJob([
+ /* Yii::$app->queue->push(new SendTelegramMessageJob([
'chatId' => $telegramUser['chat_id'],
'message' => $messageData['message'],
- ]));
+ ]));*/
}
}
$daysToEnd = $step1 + 1;
foreach ($kogortPhones as $key => $phone) {
$userBonusExist = UsersBonus::find()
- ->where(['phone' => $phone])
- ->andWhere(['tip_sale' => 'target'])
- ->andWhere(['date_start' => $kogortDate])
- ->one();
+ ->where(['phone' => $phone])
+ ->andWhere(['tip_sale' => 'target'])
+ ->andWhere(['date_start' => $kogortDate])
+ ->one();
if (!$userBonusExist) {
$userBonus4 = new UsersBonus();
$userBonus4->phone = '' . $phone;
}
} else {
$this->stdout(
- "Нет данных для формирования когорты для целевой даты {$targetDate}.\n",
+ "Нет данных для отправки сообщений для когорты для целевой даты {$targetDate}.\n",
BaseConsole::FG_RED
);
}
- // Генерация для будущих дат (от текущей даты на $step1 дней вперёд)
- for ($i = 1; $i <= $step1; $i++) {
- $kogortDate = date('Y-m-d', strtotime("+$i days", strtotime($currentDate))); // Дата когорты
- $targetDate = date('Y-m-d', strtotime("+$step1 days", strtotime($kogortDate))); // Целевая дата
-
- $kogortPhones = Users::formKogortByDateAndType($targetDate, 'target');
-
- if (!empty($kogortPhones)) {
- Users::saveKogort($kogortPhones, $kogortDate, $targetDate, SentKogort::KOGORT_NUMBERS['target'], true);
- $this->stdout(
- "Когорта таргета на {$kogortDate} для целевой даты {$targetDate} успешно сохранена.\n",
- BaseConsole::FG_GREEN
- );
- $countPhones = count($kogortPhones);
- $this->stdout(
- "Всего телефонов в когорте {$countPhones} записей.\n",
- BaseConsole::FG_GREEN
- );
- } else {
- $this->stdout(
- "Нет данных для формирования когорты для целевой даты {$targetDate}.\n",
- BaseConsole::FG_RED
- );
- }
- }
-
- $this->stdout("Генерация корорт завершена.\n", BaseConsole::FG_GREEN);
+ $this->stdout(
+ "Отправка сообщений в телеграм для таргет корорты на текущую дату завершена.\n",
+ BaseConsole::FG_GREEN
+ );
return ExitCode::OK;
}
"Всего телефонов в когорте {$countPhones} записей.\n",
BaseConsole::FG_GREEN
);
-
- if ($i == 0) {
- $sentStatusKogort = SentKogort::find()
- ->select('phone')
- ->where(['kogort_date' => $kogortDate])
- ->andWhere(['target_date' => $targetDate])
- ->andWhere(['status' => 3])
- ->column();
- // Выбираем номера для отправки через бота
- $telegramUsers = UsersTelegram::find()
- ->where(['is_blocked' => 0, 'is_registered' => 1])
- ->andWhere(['phone' => $kogortPhones['secondTarget']])
- ->select(['phone', 'chat_id'])
- ->asArray()
- ->all();
- $phonesArray = array_diff(array_column($telegramUsers, 'phone'), $sentStatusKogort);
- $chatIdsArray = array_column($telegramUsers, 'chat_id');
- $countTelegramPhones = count($phonesArray);
- $this->stdout(
- "Всего телефонов в рассылке телеграма {$countTelegramPhones} записей.\n",
- BaseConsole::FG_GREEN
- );
-
- if (!empty($telegramUsers)) {
- foreach ($telegramUsers as $telegramUser) {
- if (!in_array($telegramUser['phone'], $sentStatusKogort)) {
- $messageData = [];
- $messageData['chat_id'] = $telegramUser['chat_id'];
- $messageData['phone'] = $telegramUser['phone'];
- $messageData['kogort_date'] = $kogortDate;
- $messageData['target_date'] = $targetDate;
- $messageData['type'] = 2;
- $messageData['message'] = $messagesSettings
- ->replaceShortcodes($messagesSettings->offer_2, $kogortDate);
-
- Yii::$app->queue->push(new SendTelegramMessageDBJob([
- 'messageData' => $messageData,
- ]));
- Yii::$app->queue->push(new SendTelegramMessageJob([
- 'chatId' => $telegramUser['chat_id'],
- 'message' => $messageData['message'],
- ]));
- }
- }
- $updatedCount = SentKogort::updateAll(
- ['status' => SentKogort::STATUSES['second']], // Устанавливаем статус "вторая рассылка"
- [
- 'target_date' => $targetDate,
- 'kogort_number' => SentKogort::KOGORT_NUMBERS['target'],
- 'phone' => $phonesArray,
- ]
- );
-
- if ($updatedCount) {
- $this->stdout(
- "Статус записей для когорты {$kogortDate} обновлён на 'second' для {$updatedCount} записей.\n",
- BaseConsole::FG_GREEN
- );
- } else {
- $this->stdout(
- "Не найдено записей для обновления статуса на 'second'.\n",
- BaseConsole::FG_RED
- );
- }
- }
- }
} else {
$this->stdout(
- "Нет данных для формирования когорты для Ватсапа на {$kogortDate} для целевой даты {$targetDate}.\n",
+ "Нет данных для формирования когорты для Ватсапа
+ на {$kogortDate} для целевой даты {$targetDate}.\n",
BaseConsole::FG_RED
);
}
return ExitCode::OK;
}
+
+ public function actionSendSecondTelegramMessage()
+ {
+ $messagesSettings = UsersMessageManagement::find()->one();
+ if (!$messagesSettings) {
+ $this->stdout(
+ "Рассылка неактивна (не найдена настройка). Отправка сообщений прервана.\n",
+ BaseConsole::FG_RED
+ );
+ return ExitCode::UNAVAILABLE;
+ }
+
+ if (!$messagesSettings->active) {
+ $this->stdout(
+ "Рассылка неактивна (поле active = 0). Отправка сообщений прервана.\n",
+ BaseConsole::FG_RED
+ );
+ return ExitCode::UNAVAILABLE;
+ }
+
+ $this->stdout("Рассылка активна. Начинаем отправку второго сообщения...\n", BaseConsole::FG_GREEN);
+ date_default_timezone_set('Europe/Moscow');
+
+ $step1 = $messagesSettings ? $messagesSettings->day_before_step1 : 10;
+ $step2 = $messagesSettings ? $messagesSettings->day_before_step2 : 4;
+ $currentDate = date('Y-m-d');
+ $targetDate = date('Y-m-d', strtotime("+$step2 days", strtotime($currentDate)));
+ $kogortDate = date('Y-m-d', strtotime("-$step1 days", strtotime($currentDate)));
+
+ $kogortPhones = SentKogort::find()
+ ->select('phone')
+ ->andWhere(['kogort_number' => 1])
+ ->andWhere(['target_date' => $targetDate])
+ ->column();
+
+ if (!empty($kogortPhones)) {
+ $countPhones = count($kogortPhones);
+ $this->stdout(
+ "Всего телефонов в когорте {$countPhones} записей.\n",
+ BaseConsole::FG_GREEN
+ );
+
+ $sentStatusKogort = SentKogort::find()
+ ->select('phone')
+ ->andWhere(['kogort_number' => 1])
+ ->andWhere(['target_date' => $targetDate])
+ ->andWhere(['status' => 3])
+ ->column();
+ // Выбираем номера для отправки через бота
+ $telegramUsers = UsersTelegram::find()
+ ->where(['is_blocked' => 0, 'is_registered' => 1])
+ ->andWhere(['phone' => $kogortPhones])
+ ->select(['phone', 'chat_id'])
+ ->asArray()
+ ->all();
+ $phonesArray = array_diff(array_column($telegramUsers, 'phone'), $sentStatusKogort);
+ $chatIdsArray = array_column($telegramUsers, 'chat_id');
+ $countTelegramPhones = count($phonesArray);
+ $this->stdout(
+ "Всего телефонов в рассылке телеграма {$countTelegramPhones} записей.\n",
+ BaseConsole::FG_GREEN
+ );
+
+ if (!empty($telegramUsers)) {
+ foreach ($telegramUsers as $telegramUser) {
+ if (!in_array($telegramUser['phone'], $sentStatusKogort)) {
+ $messageData = [];
+ $messageData['chat_id'] = $telegramUser['chat_id'];
+ $messageData['phone'] = $telegramUser['phone'];
+ $messageData['kogort_date'] = $kogortDate;
+ $messageData['target_date'] = $targetDate;
+ $messageData['type'] = 2;
+ $messageData['message'] = $messagesSettings
+ ->replaceShortcodes($messagesSettings->offer_2, $kogortDate);
+
+ Yii::$app->queue->push(new SendTelegramMessageDBJob([
+ 'messageData' => $messageData,
+ ]));
+ /*Yii::$app->queue->push(new SendTelegramMessageJob([
+ 'chatId' => $telegramUser['chat_id'],
+ 'message' => $messageData['message'],
+ ]));*/
+ }
+ }
+ $updatedCount = SentKogort::updateAll(
+ ['status' => SentKogort::STATUSES['second']], // Устанавливаем статус "вторая рассылка"
+ [
+ 'target_date' => $targetDate,
+ 'kogort_number' => SentKogort::KOGORT_NUMBERS['target'],
+ 'phone' => $phonesArray,
+ ]
+ );
+
+ if ($updatedCount) {
+ $this->stdout(
+ "Статус записей для когорты {$kogortDate} обновлён на 'second' для {$updatedCount} записей.\n",
+ BaseConsole::FG_GREEN
+ );
+ } else {
+ $this->stdout(
+ "Не найдено записей для обновления статуса на 'second'.\n",
+ BaseConsole::FG_RED
+ );
+ }
+ }
+ } else {
+ $this->stdout(
+ "Нет данных для отправки второго сообщения в телеграм
+ на {$kogortDate} для целевой даты {$targetDate}.\n",
+ BaseConsole::FG_RED
+ );
+ }
+
+ $this->stdout(
+ "Отправка второго сообщения в телеграм для корорты (ватсап)
+ на {$kogortDate} для целевой даты {$targetDate}.\n",
+ BaseConsole::FG_GREEN
+ );
+ return ExitCode::OK;
+ }
+
public function actionGenerateCallKogorts()
{
$messagesSettings = UsersMessageManagement::find()->one();