CityWarningNoticeJobs.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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\Models\Manager\Process\LowPriceGoods as LowPriceGoodsModel;
  10. use App\Models\Manager\Process\ViolationProduct as ViolationProductModel;
  11. use App\Models\Manager\Process\ViolationStore as ProcessViolationStoreModel;
  12. use App\Models\manager\Collect\CollectWarningNotice as CollectWarningNoticeModel;
  13. use App\Jobs\Manager\Process\SendEmailJobs;
  14. use App\Facades\Servers\Logs\Log;
  15. use Illuminate\Support\Carbon;
  16. /**
  17. * 数据清洗-城市为空预警通知队列
  18. * @author 唐远望
  19. * @version 1.0
  20. * @date 2026-05-20
  21. */
  22. class CityWarningNoticeJobs implements ShouldQueue
  23. {
  24. use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
  25. public $tries = 3; // 限制重试次数
  26. public $timeout = 600; // 10分钟超时
  27. protected $message_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. $LowPriceGoodsModel = new LowPriceGoodsModel();
  46. $ViolationProductModel = new ViolationProductModel();
  47. $ProcessViolationStoreModel = new ProcessViolationStoreModel();
  48. $CollectWarningNoticeModel = new CollectWarningNoticeModel();
  49. //获取已经开启邮箱通知的用户
  50. $email_notice_list = $CollectWarningNoticeModel->where([['email', '!=', '']])->where('status', 0)->get()->toarray();
  51. if (empty($email_notice_list)) return true;
  52. $start_time = Carbon::today()->startOfDay()->getTimestamp(); // 今天开始时间 00:00:00
  53. // $end_time = Carbon::today()->endOfDay()->getTimestamp(); // 今天结束时间 23:59:59
  54. $end_time = time();
  55. //获取低价商品省份城市为空或者未知的数据
  56. $map = [];
  57. $map[] = ['insert_time', '>=', $start_time];
  58. $map[] = ['insert_time', '<=', $end_time];
  59. //获取低价商品省份城市为空或者未知的数据
  60. $low_price_goods_count = $LowPriceGoodsModel->where(function ($query) {
  61. $query->orWhere('province_id', '0')->orWhere('province_name', '未知')->orWhere('province_name', '');
  62. })->where(function ($query) {
  63. $query->orWhere('city_id', '0')->orWhere('city_name', '未知')->orWhere('city_name', '');
  64. })->where($map)->count();
  65. //获取违规商品省份城市为空或者未知的数据
  66. $violation_product_count = $ViolationProductModel->where(function ($query) {
  67. $query->orWhere('province_id', '0')->orWhere('province_name', '未知')->orWhere('province_name', '');
  68. })->where(function ($query) {
  69. $query->orWhere('city_id', '0')->orWhere('city_name', '未知')->orWhere('city_name', '');
  70. })->where($map)->count();
  71. //获取违规门店省份城市为空或者未知的数据
  72. $violation_store_count = $ProcessViolationStoreModel->where(function ($query) {
  73. $query->orWhere('province_id', '0')->orWhere('province_name', '未知')->orWhere('province_name', '');
  74. })->where(function ($query) {
  75. $query->orWhere('city_id', '0')->orWhere('city_name', '未知')->orWhere('city_name', '');
  76. })->where($map)->count();
  77. //发送预警通知
  78. if ($low_price_goods_count > 0 || $violation_product_count > 0 || $violation_store_count > 0) {
  79. $email_titl = "数据清洗-城市为空预警通知:\n";
  80. $email_content = "数据查询时间:" . date('Y-m-d H:i:s', $start_time) . "-" . date('Y-m-d H:i:s', $end_time) . "\n";
  81. if ($low_price_goods_count > 0) {
  82. $email_content .= "低价商品省份城市为空或者未知数据数量:" . $low_price_goods_count . "\n";
  83. }
  84. if ($violation_product_count > 0) {
  85. $email_content .= "禁止商品省份城市为空或者未知数据数量:" . $violation_product_count . "\n";
  86. }
  87. if ($violation_store_count > 0) {
  88. $email_content .= "违规店铺省份城市为空或者未知数据数量:" . $violation_store_count . "\n";
  89. }
  90. //发送预警通知
  91. foreach ($email_notice_list as $email_notice) {
  92. $message_data = [
  93. 'email' => $email_notice['email'],
  94. 'title' => $email_titl,
  95. 'content' => $email_content,
  96. ];
  97. SendEmailJobs::dispatch(['notice_data_info' => json_encode($message_data)]);
  98. }
  99. }
  100. } catch (\Exception $e) {
  101. Log::info('job_error', '数据清洗-城市为空预警通知队列失败', ['data' => $this->message_data, 'error' => $e->getMessage()]);
  102. }
  103. }
  104. public function failed(\Throwable $exception)
  105. {
  106. Log::info('job_error', '数据清洗-城市为空预警通知队列完全失败', ['data' => $this->message_data, 'error' => $exception->getMessage()]);
  107. }
  108. }