Ver código fonte

[智价云] 发送通知任务执行

tangyuanwang 21 horas atrás
pai
commit
7891469ea7

+ 4 - 0
app/Http/Controllers/Manager/Process/SubNotice.php

@@ -9,6 +9,7 @@ use App\Models\Manager\Process\ExecuteLog as ExecuteLogModel;
 use App\Models\Manager\Process\SubNoticeLog as SubNoticeLogModel;
 use Illuminate\Support\Carbon;
 use Illuminate\Support\Facades\DB;
+use App\Jobs\Manager\Process\SendNoticeJobs;
 
 /**
  * 违规处理订阅推送
@@ -65,6 +66,9 @@ class SubNotice extends Controller
             $message_data = ['company_id' => $item['id']];
             // SubNoticeJobs::dispatch($message_data);
             SubNoticeJobs::dispatchSync($message_data);
+            //执行发送通知任务
+            // SendNoticeJobs::dispatch($message_data);
+            SendNoticeJobs::dispatchSync($message_data);
         }
         // 告知结果
         return 6;

+ 161 - 0
app/Jobs/Manager/Process/SendNoticeJobs.php

@@ -0,0 +1,161 @@
+<?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\Facades\Servers\Logs\Log;
+use App\Models\Manager\Process\SubNoticeLog as SubNoticeLogModel;
+use App\Models\Manager\Process\StatisticsNotices as StatisticsNoticesModel;
+use App\Servers\Email\VerifyCode as EmailVerifyCode;
+use App\Servers\Sms\VerifyCode as SmsVerifyCode;
+
+/**
+ * 发送订阅通知
+ * @author  唐远望
+ * @version 1.0
+ * @date  2026-04-11
+ */
+class SendNoticeJobs implements ShouldQueue
+{
+    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
+    protected $message_data;
+    protected $user_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 {
+            $company_id = $this->message_data['company_id'];
+            $SubNoticeLogModel = new SubNoticeLogModel();
+
+            //查询需要发送的通知,push_time等于当前时间,或者大于当前时间5分钟的通知
+            $now_time = time();
+            $sub_notice_list = $SubNoticeLogModel->where(['company_id' => $company_id])->whereOr([['push_time', '=', $now_time], ['push_time', '>', $now_time - 300]])->get()->toarray();
+            if (empty($sub_notice_list)) return true;
+            //执行发送站内信
+            $this->send_internal_msg($sub_notice_list);
+            //执行发送邮件内容
+            $this->send_email_content($sub_notice_list);
+            //执行发送短信记录
+            // $this->send_sms_record($sub_notice_list);
+
+        } catch (\Exception $e) {
+            Log::info('job_error', '发送订阅通知队列失败', ['data' => $this->message_data, 'error' => $e->getMessage()]);
+        }
+    }
+
+
+
+    /**
+     * 发送站内信通知
+     * @author 唐远望
+     * @version 1.0
+     * @date 2026-04-11
+     * @param $data
+     * @return bool
+     */
+    public function send_internal_msg($sub_notice_list)
+    {
+        $StatisticsNoticesModel = new StatisticsNoticesModel();
+        $SubNoticeLogModel = new SubNoticeLogModel();
+        $notices_data = [];
+        foreach ($sub_notice_list as $key => $value) {
+            if ($value['internal_notice_status'] == 0) {
+                continue;
+            }
+            $notices_data[] = [
+                'company_id' => $value['company_id'],
+                'employee_id' => $value['custom_uid'],
+                'title' => $value['internal_notice_content'],
+                'ext_data' => $value['ext_data'],
+                'insert_time' => time()
+            ];
+        }
+        //发送站内信通知
+        $StatisticsNoticesModel->insert($notices_data);
+        //更新发送状态
+        $SubNoticeLogModel->whereIn('id', array_column($sub_notice_list, 'id'))->update(['internal_notice_status' => 0]);
+    }
+
+    /**
+     * 执行发送邮件内容
+     * @author 唐远望
+     * @version 1.0
+     * @date 2026-04-11
+     * @param $data
+     * @return bool
+     */
+    public function send_email_content($sub_notice_list)
+    {
+        $EmailVerifyCode = new EmailVerifyCode();
+        $SubNoticeLogModel = new SubNoticeLogModel();
+        foreach ($sub_notice_list as $key => $value) {
+            if ($value['email_status'] == 0) {
+                continue;
+            }
+            $email_to = $value['email'];
+            $email_content = json_decode($value['email_content'], true);
+            $email_title = $email_content['email_title'];
+            $email_content = $email_content['email_content'];
+            $res_msg = $EmailVerifyCode->sendSmtpEmail($email_to, $email_title, $email_content);
+            Log::info('job_send_email', '订阅邮件通知推送队列记录', ['email' => $email_to, 'email_content' => $email_content, 'msg' => $res_msg]);
+            //更新发送状态
+            $SubNoticeLogModel->where(['id' => $value['id']])->update(['email_status' => 0]);
+        }
+    }
+
+    /**
+     * 执行发送短信记录
+     * @author 唐远望
+     * @version 1.0
+     * @date 2026-04-11
+     * @param $data
+     * @return bool
+     */
+    public function send_sms_record($sub_notice_list)
+    {
+        $SmsVerifyCode = new SmsVerifyCode();
+        $SubNoticeLogModel = new SubNoticeLogModel();
+        foreach ($sub_notice_list as $key => $value) {
+            if ($value['status'] == 0) {
+                continue;
+            }
+            $mobile = $value['mobile'];
+            $sms_content = json_decode($value['sms_content'], true);
+            $totle_number = $sms_content['totle_number'];
+            $number1 = $sms_content['number1'];
+            $number2 = $sms_content['number2'];
+            $number3 = $sms_content['number3'];
+            $sms_tpl_id = $sms_content['sms_tpl_id'];
+            $res_msg = $SmsVerifyCode->sendContent($mobile, ['totle_number' => $totle_number, 'number1' => $number1, 'number2' => $number2, 'number3' => $number3], $sms_tpl_id);
+            Log::info('job_send_sms', '订阅短信通知推送队列记录', ['email' => $mobile, 'sms_tpl_id' => $sms_tpl_id, 'msg' => $res_msg]);
+            //更新发送状态
+            $SubNoticeLogModel->where(['id' => $value['id']])->update(['status' => 0]);
+        }
+    }
+
+
+
+    public function failed(\Throwable $exception)
+    {
+        Log::info('job_error', '发送订阅通知队列完全失败', ['data' => $this->message_data, 'error' => $exception]);
+    }
+}

+ 5 - 0
app/Jobs/Manager/Process/SubNoticeJobs.php

@@ -20,6 +20,7 @@ use App\Facades\Servers\Sms\VerifyCode as Sms;
 use App\Servers\Email\VerifyCode as EmailVerifyCode;
 use App\Models\Manager\Personnel\NoticeConfig as NoticeConfigModel;
 use App\Models\Manager\Process\SubNoticeLog as SubNoticeLogModel;
+use App\Models\Manager\Personnel\Employee as EmployeeModel;
 
 /**
  * 订阅通知
@@ -60,6 +61,7 @@ class SubNoticeJobs implements ShouldQueue
             $statistics_end_time = $notice_config_data['statistics_end_time'];
             $push_time = $notice_config_data['push_time'];
             $EmailVerifyCode = new EmailVerifyCode();
+            $EmployeeModel = new EmployeeModel();
             $sms_tpl_id =  config('verifycode.aliyun_process_merge_notice.sms_tpl');
             $company_id = $this->message_data['company_id'];
             $action_one = $this->send_low_price_goods_notice($company_id, $statistics_start_time, $statistics_end_time);
@@ -72,6 +74,9 @@ class SubNoticeJobs implements ShouldQueue
                     $mobile = $value['mobile'];
                     $email_to = $value['email'];
                     $user_id = $key;
+                    //查询已已经开启通知设置的用户
+                    $employee_ids = $EmployeeModel->where('id', $user_id)->where('open_notice', 0)->get();
+                    if (empty($employee_ids))  continue;
                     $number1 = isset($this->user_data[$key]['lowprice_product_logids']) ? count($this->user_data[$key]['lowprice_product_logids']) : 0;
                     $number2 = isset($this->user_data[$key]['violation_product_logids']) ? count($this->user_data[$key]['violation_product_logids']) : 0;
                     $number3 = isset($this->user_data[$key]['violation_store_logids']) ? count($this->user_data[$key]['violation_store_logids']) : 0;

+ 27 - 0
app/Models/Manager/Process/StatisticsNotices.php

@@ -0,0 +1,27 @@
+<?php
+
+namespace App\Models\manager\Process;
+
+use Illuminate\Database\Eloquent\Factories\HasFactory;
+use Illuminate\Database\Eloquent\Model;
+use Illuminate\Support\Facades\DB;
+
+/**
+ * 统计通知消息模型
+ * @author: 唐远望
+ * @version: 1.0
+ * @date: 2025-04-11
+ */
+class StatisticsNotices extends Model
+{
+    use HasFactory;
+    // 与模型关联的表名
+    protected $table = 'process_statistics_notices';
+    // 是否主动维护时间戳
+    public $timestamps = false;
+    // 定义时间戳字段名
+    // const CREATED_AT = 'insert_time';
+    // const UPDATED_AT = 'update_time';
+
+
+}