Bladeren bron

[智价云] 城市为空邮箱预警通知功能

tangyuanwang 1 dag geleden
bovenliggende
commit
6b0bda4a3d

+ 45 - 0
app/Console/Commands/CityWarningNotice.php

@@ -0,0 +1,45 @@
+<?php
+
+namespace App\Console\Commands;
+
+use Illuminate\Console\Command;
+use Illuminate\Support\Facades\Log;
+use App\Jobs\Manager\Process\CityWarningNoticeJobs;
+
+
+class CityWarningNotice extends Command
+{
+    /**
+     * The name and signature of the console command.
+     *
+     * @var string
+     */
+    protected $signature = 'task:city_warning_notice';
+
+    /**
+     * 命令描述
+     *
+     * @var string
+     */
+    protected $description = '每天12点执行的定时任务';
+
+    /**
+     * 执行命令
+     *
+     * @return int
+     */
+    public function handle()
+    {
+        $this->info('开始执行每日采集预警通知任务...' . now());
+        try {
+            Log::info('每日采集预警通知任务执行中 - ' . now());
+            CityWarningNoticeJobs::dispatch();
+            $this->info('每日采集预警通知执行完成!' . now());
+            return Command::SUCCESS;
+        } catch (\Exception $e) {
+            $this->error('每日采集预警通知任务执行失败: ' . $e->getMessage());
+
+            return Command::FAILURE;
+        }
+    }
+}

+ 7 - 8
app/Console/Kernel.php

@@ -30,14 +30,13 @@ class Kernel extends ConsoleKernel
             ->withoutOverlapping() // 防止任务重叠执行
             ->appendOutputTo(storage_path('logs/daily-task.log')) // 输出到日志文件
             ->onOneServer(); // 如果使用多服务器,确保只在一台服务器上运行
-        // 每天04:00执行
-        // $schedule->command('task:collect_sync')
-        //     ->dailyAt('04:00')
-        //     ->timezone('Asia/Shanghai') // 设置时区,可选
-        //     ->runInBackground() // 在后台运行,可选
-        //     ->withoutOverlapping() // 防止任务重叠执行
-        //     ->appendOutputTo(storage_path('logs/collect-sync-task.log')) // 输出到日志文件
-        //     ->onOneServer(); // 如果使用多服务器,确保只在一台服务器上运行
+        // 每天17:00执行
+        $schedule->command('task:city_warning_notice')
+            ->dailyAt('17:00')
+            ->timezone('Asia/Shanghai') // 设置时区,可选
+            ->runInBackground() // 在后台运行,可选
+            ->appendOutputTo(storage_path('logs/city-warning-notice-task.log')) // 输出到日志文件
+            ->onOneServer(); // 如果使用多服务器,确保只在一台服务器上运行
     }
 
     /**

+ 116 - 0
app/Jobs/Manager/Process/CityWarningNoticeJobs.php

@@ -0,0 +1,116 @@
+<?php
+
+namespace App\Jobs\Manager\Process;
+
+use Illuminate\Bus\Queueable;
+use Illuminate\Contracts\Queue\ShouldBeUnique;
+use Illuminate\Contracts\Queue\ShouldQueue;
+use Illuminate\Foundation\Bus\Dispatchable;
+use Illuminate\Queue\InteractsWithQueue;
+use Illuminate\Queue\SerializesModels;
+use App\Models\Manager\Process\LowPriceGoods as LowPriceGoodsModel;
+use App\Models\Manager\Process\ViolationProduct as ViolationProductModel;
+use App\Models\Manager\Process\ViolationStore as ProcessViolationStoreModel;
+use App\Models\manager\Collect\CollectWarningNotice as CollectWarningNoticeModel;
+use App\Jobs\Manager\Process\SendEmailJobs;
+use App\Facades\Servers\Logs\Log;
+use Illuminate\Support\Carbon;
+
+/**
+ * 数据清洗-城市为空预警通知队列
+ * @author  唐远望
+ * @version 1.0
+ * @date  2026-05-20
+ */
+class CityWarningNoticeJobs implements ShouldQueue
+{
+    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
+
+    public $tries = 3; // 限制重试次数
+    public $timeout = 600; // 10分钟超时
+
+    protected $message_data;
+    /**
+     * Create a new job instance.
+     *
+     * @return void
+     */
+    public function __construct(array $message_data)
+    {
+        $this->message_data = $message_data;
+    }
+
+    /**
+     * Execute the job.
+     *
+     * @return void
+     */
+    public function handle()
+    {
+        try {
+            $LowPriceGoodsModel = new LowPriceGoodsModel();
+            $ViolationProductModel = new ViolationProductModel();
+            $ProcessViolationStoreModel = new ProcessViolationStoreModel();
+            $CollectWarningNoticeModel = new CollectWarningNoticeModel();
+            //获取已经开启邮箱通知的用户
+            $email_notice_list = $CollectWarningNoticeModel->where([['email', '!=', '']])->where('status', 0)->get()->toarray();
+            if (empty($email_notice_list)) return true;
+
+            $start_time = Carbon::today()->startOfDay()->getTimestamp(); // 今天开始时间 00:00:00
+            // $end_time = Carbon::today()->endOfDay()->getTimestamp(); // 今天结束时间 23:59:59
+            $end_time = time();
+            //获取低价商品省份城市为空或者未知的数据
+            $map = [];
+            $map[] = ['insert_time', '>=', $start_time];
+            $map[] = ['insert_time', '<=', $end_time];
+            //获取低价商品省份城市为空或者未知的数据
+            $low_price_goods_count = $LowPriceGoodsModel->where(function ($query) {
+                $query->orWhere('province_id', '0')->orWhere('province_name', '未知')->orWhere('province_name', '');
+            })->where(function ($query) {
+                $query->orWhere('city_id', '0')->orWhere('city_name', '未知')->orWhere('city_name', '');
+            })->where($map)->count();
+            //获取违规商品省份城市为空或者未知的数据
+            $violation_product_count = $ViolationProductModel->where(function ($query) {
+                $query->orWhere('province_id', '0')->orWhere('province_name', '未知')->orWhere('province_name', '');
+            })->where(function ($query) {
+                $query->orWhere('city_id', '0')->orWhere('city_name', '未知')->orWhere('city_name', '');
+            })->where($map)->count();
+            //获取违规门店省份城市为空或者未知的数据
+            $violation_store_count = $ProcessViolationStoreModel->where(function ($query) {
+                $query->orWhere('province_id', '0')->orWhere('province_name', '未知')->orWhere('province_name', '');
+            })->where(function ($query) {
+                $query->orWhere('city_id', '0')->orWhere('city_name', '未知')->orWhere('city_name', '');
+            })->where($map)->count();
+            //发送预警通知
+            if ($low_price_goods_count > 0 || $violation_product_count > 0 || $violation_store_count > 0) {
+                $email_titl = "数据清洗-城市为空预警通知:\n";
+                $email_content = "数据查询时间:" . date('Y-m-d H:i:s', $start_time) . "-" . date('Y-m-d H:i:s', $end_time) . "\n";
+                if ($low_price_goods_count > 0) {
+                    $email_content .= "低价商品省份城市为空或者未知数据数量:" . $low_price_goods_count . "\n";
+                }
+                if ($violation_product_count > 0) {
+                    $email_content .= "禁止商品省份城市为空或者未知数据数量:" . $violation_product_count . "\n";
+                }
+                if ($violation_store_count > 0) {
+                    $email_content .= "违规店铺省份城市为空或者未知数据数量:" . $violation_store_count . "\n";
+                }
+                //发送预警通知
+                foreach ($email_notice_list as $email_notice) {
+                    $message_data = [
+                        'email' => $email_notice['email'],
+                        'title' => $email_titl,
+                        'content' => $email_content,
+                    ];
+                    SendEmailJobs::dispatch(['notice_data_info' => json_encode($message_data)]);
+                }
+            }
+        } catch (\Exception $e) {
+            Log::info('job_error', '数据清洗-城市为空预警通知队列失败', ['data' => $this->message_data, 'error' => $e->getMessage()]);
+        }
+    }
+
+    public function failed(\Throwable $exception)
+    {
+        Log::info('job_error', '数据清洗-城市为空预警通知队列完全失败', ['data' => $this->message_data, 'error' => $exception->getMessage()]);
+    }
+}

+ 26 - 0
app/Models/Manager/Collect/CollectWarningNotice.php

@@ -0,0 +1,26 @@
+<?php
+
+namespace App\Models\manager\Collect;
+
+use Illuminate\Database\Eloquent\Factories\HasFactory;
+use Illuminate\Database\Eloquent\Model;
+use Illuminate\Support\Facades\DB;
+
+/**
+ * 采集预警通知配置
+ * @author: 唐远望
+ * @version: 1.0
+ * @date: 2026-05-20
+ */
+class CollectWarningNotice extends Model
+{
+    use HasFactory;
+    // 与模型关联的表名
+    protected $table = 'collect_warning_notice';
+    // 是否主动维护时间戳
+    public $timestamps = false;
+    // 定义时间戳字段名
+    // const CREATED_AT = 'insert_time';
+    // const UPDATED_AT = 'update_time';
+
+}