From 4bc60d305c370d746395a381b90e82eba5bfc117 Mon Sep 17 00:00:00 2001 From: Alexander Smirnov Date: Wed, 19 Mar 2025 09:21:15 +0300 Subject: [PATCH] [ERP-356] logs --- erp24/api2/controllers/BonusController.php | 15 +++++- erp24/jobs/SendBonusInfoToSiteJob.php | 25 +++++++++ ...reate_table_user_bonus_send_to_tg_logs.php | 39 ++++++++++++++ erp24/records/UserBonusSendToTgLogs.php | 53 +++++++++++++++++++ 4 files changed, 130 insertions(+), 2 deletions(-) create mode 100755 erp24/migrations/m250319_054813_create_table_user_bonus_send_to_tg_logs.php create mode 100644 erp24/records/UserBonusSendToTgLogs.php diff --git a/erp24/api2/controllers/BonusController.php b/erp24/api2/controllers/BonusController.php index b7f9586d..d9e77dcd 100644 --- a/erp24/api2/controllers/BonusController.php +++ b/erp24/api2/controllers/BonusController.php @@ -18,6 +18,7 @@ use yii_app\records\Products1c; use yii_app\records\Sales; use yii_app\records\Timetable; use yii_app\records\UniversalCatalogItem; +use yii_app\records\UserBonusSendToTgLogs; use yii_app\records\Users; use yii_app\records\UsersAuthCallLog; use yii_app\records\UsersBonus; @@ -1210,12 +1211,22 @@ class BonusController extends BaseController LogService::apiLogs(1, json_encode($mess, JSON_UNESCAPED_UNICODE)); file_put_contents(self::OUT_DIR . '/sale_bonuses_' . $fl . '.json', PHP_EOL . '--' . __LINE__ . ' OK ', FILE_APPEND); - Yii::$app->queue->push(new SendBonusInfoToSiteJob([ + $input = [ 'phone' => $phone, 'bonusCount' => $back10 + $back20, 'purchaseDate' => date("Y-m-d H:i:s"), 'orderId' => $check_id, - ])); + ]; + + $userBonusSendToTgLogs = new UserBonusSendToTgLogs; + $userBonusSendToTgLogs->input_hash = md5(Json::encode($input)); + $userBonusSendToTgLogs->input = Json::encode($input); + $userBonusSendToTgLogs->status = 1; + $userBonusSendToTgLogs->save(); + if ($userBonusSendToTgLogs->getErrors()) { + LogService::apiErrorLog(json_encode(["error_id" => 100.001, "error" => $userBonusSendToTgLogs->getErrors()], JSON_UNESCAPED_UNICODE)); + } + Yii::$app->queue->push(new SendBonusInfoToSiteJob($input)); // SiteService::notifySiteAboutBonuses($phone, $back10 + $back20, date("Y-m-d H:i:s"), $check_id); diff --git a/erp24/jobs/SendBonusInfoToSiteJob.php b/erp24/jobs/SendBonusInfoToSiteJob.php index 7cefdeb5..099e78a1 100644 --- a/erp24/jobs/SendBonusInfoToSiteJob.php +++ b/erp24/jobs/SendBonusInfoToSiteJob.php @@ -4,7 +4,10 @@ namespace yii_app\jobs; use Yii; +use yii\helpers\Json; use yii\queue\JobInterface; +use yii_app\records\UserBonusSendToTgLogs; +use yii_app\services\LogService; use yii_app\services\SiteService; class SendBonusInfoToSiteJob extends \yii\base\BaseObject implements JobInterface @@ -20,8 +23,30 @@ class SendBonusInfoToSiteJob extends \yii\base\BaseObject implements JobInterfac $purchaseDate = $this->purchaseDate; $orderId = $this->orderId; + $input = [ + 'phone' => $phone, + 'bonusCount' => $bonusCount, + 'purchaseDate' => $purchaseDate, + 'orderId' => $orderId, + ]; + + $inputHash = md5(Json::encode($input)); + $userBonusSendToTgLogs = UserBonusSendToTgLogs::find()->where(['input_hash' => $inputHash])->one(); + $userBonusSendToTgLogs->status ++; + $userBonusSendToTgLogs->save(); + if ($userBonusSendToTgLogs->getErrors()) { + LogService::apiErrorLog(json_encode(["error_id" => 100.021, "error" => $userBonusSendToTgLogs->getErrors()], JSON_UNESCAPED_UNICODE)); + } try { $result = SiteService::notifySiteAboutBonuses($phone, $bonusCount, $purchaseDate, $orderId); + + $userBonusSendToTgLogs->output = Json::encode($result); + $userBonusSendToTgLogs->status ++; + $userBonusSendToTgLogs->save(); + if ($userBonusSendToTgLogs->getErrors()) { + LogService::apiErrorLog(json_encode(["error_id" => 100.321, "error" => $userBonusSendToTgLogs->getErrors()], JSON_UNESCAPED_UNICODE)); + } + if ($result) { Yii::warning("Сообщение успешно отправлено на сайт", 'site'); } else { diff --git a/erp24/migrations/m250319_054813_create_table_user_bonus_send_to_tg_logs.php b/erp24/migrations/m250319_054813_create_table_user_bonus_send_to_tg_logs.php new file mode 100755 index 00000000..c8071186 --- /dev/null +++ b/erp24/migrations/m250319_054813_create_table_user_bonus_send_to_tg_logs.php @@ -0,0 +1,39 @@ +db->getTableSchema(self::TABLE_NAME); + if (!isset($tableSchema)) { + $this->createTable(self::TABLE_NAME, [ + 'id' => $this->primaryKey(), + 'input_hash' => $this->string()->notNull()->comment('md5(input)'), + 'input' => $this->text()->null()->comment('Входной json'), + 'output' => $this->text()->null()->comment('Выходной json'), + 'status' => $this->integer()->notNull()->defaultValue(0)->comment('Кол-во упоминаний данной записи'), + ]); + } + } + + /** + * {@inheritdoc} + */ + public function safeDown() + { + $tableSchema = $this->db->getTableSchema(self::TABLE_NAME); + + if (isset($tableSchema)) { + $this->dropTable(self::TABLE_NAME); + } + } +} diff --git a/erp24/records/UserBonusSendToTgLogs.php b/erp24/records/UserBonusSendToTgLogs.php new file mode 100644 index 00000000..2093bbb4 --- /dev/null +++ b/erp24/records/UserBonusSendToTgLogs.php @@ -0,0 +1,53 @@ + null], + [['status'], 'integer'], + [['input_hash'], 'string', 'max' => 255], + ]; + } + + /** + * {@inheritdoc} + */ + public function attributeLabels() + { + return [ + 'id' => 'ID', + 'input_hash' => 'Input Hash', + 'input' => 'Input', + 'output' => 'Output', + 'status' => 'Status', + ]; + } +} -- 2.39.5