CityWarningNoticeJobs.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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. /**
  28. * Execute the job.
  29. *
  30. * @return void
  31. */
  32. public function handle()
  33. {
  34. try {
  35. $LowPriceGoodsModel = new LowPriceGoodsModel();
  36. $ViolationProductModel = new ViolationProductModel();
  37. $ProcessViolationStoreModel = new ProcessViolationStoreModel();
  38. $CollectWarningNoticeModel = new CollectWarningNoticeModel();
  39. //获取已经开启邮箱通知的用户
  40. $email_notice_list = $CollectWarningNoticeModel->where([['email', '!=', '']])->where('status', 0)->get()->toarray();
  41. if (empty($email_notice_list)) return true;
  42. $start_time = Carbon::today()->startOfDay()->getTimestamp(); // 今天开始时间 00:00:00
  43. // $end_time = Carbon::today()->endOfDay()->getTimestamp(); // 今天结束时间 23:59:59
  44. $end_time = time();
  45. //获取低价商品省份城市为空或者未知的数据
  46. $map = [];
  47. $map[] = ['insert_time', '>=', $start_time];
  48. $map[] = ['insert_time', '<=', $end_time];
  49. //获取低价商品省份城市为空或者未知的数据
  50. $low_price_goods_count = $LowPriceGoodsModel->where(function ($query) {
  51. $query->orWhere('province_id', '0')->orWhere('province_name', '未知')->orWhere('province_name', '');
  52. })->where(function ($query) {
  53. $query->orWhere('city_id', '0')->orWhere('city_name', '未知')->orWhere('city_name', '');
  54. })->where($map)->count();
  55. //获取违规商品省份城市为空或者未知的数据
  56. $violation_product_count = $ViolationProductModel->where(function ($query) {
  57. $query->orWhere('province_id', '0')->orWhere('province_name', '未知')->orWhere('province_name', '');
  58. })->where(function ($query) {
  59. $query->orWhere('city_id', '0')->orWhere('city_name', '未知')->orWhere('city_name', '');
  60. })->where($map)->count();
  61. //获取违规门店省份城市为空或者未知的数据
  62. $violation_store_count = $ProcessViolationStoreModel->where(function ($query) {
  63. $query->orWhere('province_id', '0')->orWhere('province_name', '未知')->orWhere('province_name', '');
  64. })->where(function ($query) {
  65. $query->orWhere('city_id', '0')->orWhere('city_name', '未知')->orWhere('city_name', '');
  66. })->where($map)->count();
  67. if ($low_price_goods_count == 0 && $violation_product_count == 0 && $violation_store_count == 0) {
  68. return true;
  69. };
  70. //发送预警通知
  71. if ($low_price_goods_count > 0 || $violation_product_count > 0 || $violation_store_count > 0) {
  72. $email_titl = "数据清洗-城市为空预警通知";
  73. $email_content = "数据查询时间:" . date('Y-m-d H:i:s', $start_time) . " - " . date('Y-m-d H:i:s', $end_time).
  74. " 低价商品省份城市为空或者未知数据数量:" . $low_price_goods_count.'条,'.
  75. " 禁止商品省份城市为空或者未知数据数量:" . $violation_product_count.'条,'.
  76. " 违规店铺省份城市为空或者未知数据数量:" . $violation_store_count.'条';
  77. //发送预警通知
  78. foreach ($email_notice_list as $email_notice) {
  79. $message_data = [
  80. 'email' => $email_notice['email'],
  81. 'email_content' => json_encode(['title' => $email_titl,'content' => $email_content]),
  82. ];
  83. SendEmailJobs::dispatch(['notice_data_info' => $message_data]);
  84. }
  85. }
  86. } catch (\Exception $e) {
  87. Log::info('job_error', '数据清洗-城市为空预警通知队列失败', ['error' => $e->getMessage()]);
  88. }
  89. }
  90. public function failed(\Throwable $exception)
  91. {
  92. Log::info('job_error', '数据清洗-城市为空预警通知队列完全失败', ['error' => $exception->getMessage()]);
  93. }
  94. }