SendNoticeJobs.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. <?php
  2. namespace App\Jobs\Manager\Process;
  3. use Illuminate\Bus\Queueable;
  4. use Illuminate\Contracts\Queue\ShouldBeUnique;
  5. use Illuminate\Contracts\Queue\ShouldQueue;
  6. use Illuminate\Foundation\Bus\Dispatchable;
  7. use Illuminate\Queue\InteractsWithQueue;
  8. use Illuminate\Queue\SerializesModels;
  9. use App\Facades\Servers\Logs\Log;
  10. use App\Models\Manager\Process\SubNoticeLog as SubNoticeLogModel;
  11. use App\Models\Manager\Process\StatisticsNotices as StatisticsNoticesModel;
  12. use App\Servers\Email\VerifyCode as EmailVerifyCode;
  13. use App\Servers\Sms\VerifyCode as SmsVerifyCode;
  14. use Illuminate\Support\Facades\DB;
  15. use App\Jobs\Manager\Process\SendEmailJobs;
  16. use App\Jobs\Manager\Process\SendAliyunSmsJobs;
  17. /**
  18. * 发送订阅通知
  19. * @author 唐远望
  20. * @version 1.0
  21. * @date 2026-04-11
  22. */
  23. class SendNoticeJobs implements ShouldQueue
  24. {
  25. use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
  26. protected $message_data;
  27. protected $user_data;
  28. /**
  29. * Create a new job instance.
  30. *
  31. * @return void
  32. */
  33. public function __construct(array $message_data)
  34. {
  35. $this->message_data = $message_data;
  36. }
  37. /**
  38. * Execute the job.
  39. *
  40. * @return void
  41. */
  42. public function handle()
  43. {
  44. try {
  45. $company_id = $this->message_data['company_id'];
  46. $SubNoticeLogModel = new SubNoticeLogModel();
  47. //查询需要发送的通知,push_time等于当前时间,或者大于当前时间5分钟的通知
  48. $now_time = time();
  49. $sub_notice_list = $SubNoticeLogModel->where('company_id', $company_id)
  50. ->where(function($query) use ($now_time) {
  51. $query->where('push_time', '=', $now_time)
  52. ->orWhere('push_time', '>', $now_time - 300);
  53. })
  54. ->get()->toarray();
  55. if (empty($sub_notice_list)) return true;
  56. //执行发送站内信
  57. $status1 = $this->send_internal_msg($sub_notice_list);
  58. //执行发送邮件内容
  59. $status2=$this->send_email_content($sub_notice_list);
  60. //执行发送短信记录
  61. $status3=$this->send_sms_record($sub_notice_list);
  62. //获取所有$sub_notice_list ID
  63. $id_list = array_column($sub_notice_list,'id');
  64. if($status1 && $status2 && $status3){
  65. $SubNoticeLogModel->whereIn('id',$id_list)->update(['status'=>'0']);
  66. }
  67. } catch (\Exception $e) {
  68. Log::info('job_error', '发送订阅通知队列失败', ['data' => $this->message_data, 'error' => $e->getMessage()]);
  69. }
  70. }
  71. /**
  72. * 发送站内信通知
  73. * @author 唐远望
  74. * @version 1.0
  75. * @date 2026-04-11
  76. * @param $data
  77. * @return bool
  78. */
  79. public function send_internal_msg($sub_notice_list)
  80. {
  81. $StatisticsNoticesModel = new StatisticsNoticesModel();
  82. $SubNoticeLogModel = new SubNoticeLogModel();
  83. $notices_data = [];
  84. foreach ($sub_notice_list as $key => $value) {
  85. if ($value['internal_notice_status'] == 0) {
  86. continue;
  87. }
  88. $notices_data[] = [
  89. 'company_id' => $value['company_id'],
  90. 'employee_id' => $value['employee_id'],
  91. 'title' => $value['internal_notice_content'],
  92. 'ext_data' => $value['ext_data'],
  93. 'insert_time' => time()
  94. ];
  95. }
  96. //发送站内信通知
  97. $StatisticsNoticesModel->insert($notices_data);
  98. //更新发送状态
  99. $SubNoticeLogModel->whereIn('id', array_column($sub_notice_list, 'id'))->update(['internal_notice_status' => 0]);
  100. return true;
  101. }
  102. /**
  103. * 执行发送邮件内容
  104. * @author 唐远望
  105. * @version 1.0
  106. * @date 2026-04-11
  107. * @param $data
  108. * @return bool
  109. */
  110. public function send_email_content($sub_notice_list)
  111. {
  112. $EmailVerifyCode = new EmailVerifyCode();
  113. $SubNoticeLogModel = new SubNoticeLogModel();
  114. foreach ($sub_notice_list as $key => $value) {
  115. if ($value['email_status'] == 0) {
  116. continue;
  117. }
  118. $email_to = $value['email'];
  119. if (empty($email_to)) {
  120. $SubNoticeLogModel->where(['id' => $value['id']])->update(['email_status' => 0]);
  121. continue;
  122. }
  123. SendEmailJobs::dispatch(['notice_data_info'=> $value]);
  124. // SendEmailJobs::dispatchSync(['notice_data_info'=> $value]);
  125. // $email_content = json_decode($value['email_content'], true);
  126. // $email_title = $email_content['title'];
  127. // $email_content = $email_content['content'];
  128. // $res_msg = $EmailVerifyCode->sendSmtpEmail($email_to, $email_title, $email_content);
  129. // Log::info('job_send_email', '订阅邮件通知推送队列记录', ['email' => $email_to, 'email_content' => $email_content, 'msg' => $res_msg]);
  130. // //更新发送状态
  131. // $SubNoticeLogModel->where(['id' => $value['id']])->update(['email_status' => 0]);
  132. }
  133. return true;
  134. }
  135. /**
  136. * 执行发送短信记录
  137. * @author 唐远望
  138. * @version 1.0
  139. * @date 2026-04-11
  140. * @param $data
  141. * @return bool
  142. */
  143. public function send_sms_record($sub_notice_list)
  144. {
  145. $SmsVerifyCode = new SmsVerifyCode();
  146. $SubNoticeLogModel = new SubNoticeLogModel();
  147. foreach ($sub_notice_list as $key => $value) {
  148. if ($value['status'] == 0) {
  149. continue;
  150. }
  151. $mobile = $value['mobile'];
  152. if (empty($mobile)) {
  153. $SubNoticeLogModel->where(['id' => $value['id']])->update(['status' => 0]);
  154. continue;
  155. }
  156. SendAliyunSmsJobs::dispatch(['notice_data_info'=> $value]);
  157. // SendAliyunSmsJobs::dispatchSync(['notice_data_info'=> $value]);
  158. // $sms_content = json_decode($value['mobile_content'], true);
  159. // $totle_number = $sms_content['totle_number'];
  160. // $number1 = $sms_content['number1'];
  161. // $number2 = $sms_content['number2'];
  162. // $number3 = $sms_content['number3'];
  163. // $sms_tpl_id = $sms_content['sms_tpl_id'];
  164. // $res_msg = $SmsVerifyCode->sendContent($mobile, ['totle_number' => $totle_number, 'number1' => $number1, 'number2' => $number2, 'number3' => $number3], $sms_tpl_id);
  165. // Log::info('job_send_sms', '订阅短信通知推送队列记录', ['email' => $mobile, 'sms_tpl_id' => $sms_tpl_id, 'msg' => $res_msg]);
  166. // //更新发送状态
  167. // $SubNoticeLogModel->where(['id' => $value['id']])->update(['sms_status' => 0]);
  168. }
  169. return true;
  170. }
  171. public function failed(\Throwable $exception)
  172. {
  173. Log::info('job_error', '发送订阅通知队列完全失败', ['data' => $this->message_data, 'error' => $exception]);
  174. }
  175. }