| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182 |
- <?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;
- use Illuminate\Support\Facades\DB;
- /**
- * 发送订阅通知
- * @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)
- ->where(function($query) use ($now_time) {
- $query->where('push_time', '=', $now_time)
- ->orWhere('push_time', '>', $now_time - 300);
- })
- ->get();
- if (empty($sub_notice_list)) return true;
- //执行发送站内信
- $status1 = $this->send_internal_msg($sub_notice_list);
- //执行发送邮件内容
- $status2=$this->send_email_content($sub_notice_list);
- //执行发送短信记录
- $status3=$this->send_sms_record($sub_notice_list);
- //获取所有$sub_notice_list ID
- $id_list = array_column($sub_notice_list,'id');
- if($status1 && $status2 && $status3){
- $SubNoticeLogModel->whereIn('id',$id_list)->update(['status'=>'0']);
- }
- } 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['employee_id'],
- '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]);
- return true;
- }
- /**
- * 执行发送邮件内容
- * @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'];
- if (empty($email_to)) {
- $SubNoticeLogModel->where(['id' => $value['id']])->update(['email_status' => 0]);
- continue;
- }
- $email_content = json_decode($value['email_content'], true);
- $email_title = $email_content['title'];
- $email_content = $email_content['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]);
- }
- return true;
- }
- /**
- * 执行发送短信记录
- * @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'];
- if (empty($mobile)) {
- $SubNoticeLogModel->where(['id' => $value['id']])->update(['status' => 0]);
- continue;
- }
- $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(['sms_status' => 0]);
- }
- return true;
- }
- public function failed(\Throwable $exception)
- {
- Log::info('job_error', '发送订阅通知队列完全失败', ['data' => $this->message_data, 'error' => $exception]);
- }
- }
|