ScrapeDataProductJobs.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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\ScrapeData as ScrapeDataModel;
  11. use Illuminate\Support\Facades\DB;
  12. use Illuminate\Support\Facades\Cache;
  13. use App\Jobs\Manager\Process\LowPriceGoodsJobs;
  14. use App\Jobs\Manager\Process\ViolationProductJobs;
  15. use App\Jobs\Manager\Process\ViolationCompanyJobs;
  16. /**
  17. * 数据清洗-读取采集商品数据队列
  18. * @author 唐远望
  19. * @version 1.0
  20. * @date 2025-12-10
  21. */
  22. class ScrapeDataProductJobs 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. $ScrapeDataModel = new ScrapeDataModel();
  46. $limit = isset($message_data['limit']) ? $message_data['limit'] : 10000;
  47. $page = isset($message_data['page']) ? $message_data['page'] : 1;
  48. $company_id = isset($message_data['company_id']) ? $message_data['company_id'] : 0; //品牌方公司ID
  49. $start_time = $this->message_data['start_time'];
  50. $end_time = $this->message_data['end_time'];
  51. $start_time_string = date('Y-m-d H:i:s', $start_time);
  52. $end_time_string = date('Y-m-d H:i:s', $end_time);
  53. $key_name = 'ScrapeDataProductJobs_' . $company_id . '_' . $start_time . '_' . $end_time;
  54. $product_datas = Cache::get($key_name);
  55. if (empty($product_datas)) {
  56. $where = [];
  57. $where[] = ['insert_time', '>=', $start_time_string];
  58. $where[] = ['insert_time', '<=', $end_time_string];
  59. $where[] = ['min_price', '>=', '0.01'];
  60. $where[] = ['number', '>=', '1'];
  61. $where[] = ['enterprise_id', '=', $company_id];
  62. $product_data_info = $ScrapeDataModel->select('*', DB::raw('ROUND(min_price / NULLIF(number, 0), 2) as unit_price'))
  63. ->where($where)->orderbyDesc('id')->paginate($limit, ['*'], 'page', $page)->toarray();
  64. $product_datas = $product_data_info['data'];
  65. if (empty($product_datas) && $page == 1) {
  66. //如果查询第一页为空,则直接返回
  67. return true;
  68. } else if (empty($product_datas) && $page > 1) {
  69. //如果查询第二页为空,则直接返回,表示查询完毕,则处理数据清洗任务
  70. $data_totle = count($product_datas);
  71. $index_number = 0;
  72. foreach ($product_datas as $key => $product_data) {
  73. $index_number = $key + 1;
  74. $message_data = ['company_id' => $company_id, 'page' => '1', 'limit' => '50', 'product_data' => $product_data, 'index_number' => $index_number, 'data_totle' => $data_totle];
  75. LowPriceGoodsJobs::dispatch($message_data);
  76. // LowPriceGoodsJobs::dispatchSync($message_data);
  77. ViolationProductJobs::dispatch($message_data);
  78. // ViolationProductJobs::dispatchSync($message_data);
  79. ViolationCompanyJobs::dispatch($message_data);
  80. // ViolationCompanyJobs::dispatchSync($message_data);
  81. }
  82. }
  83. return true;
  84. } else {
  85. //合并数据
  86. $product_datas = array_merge($product_datas, Cache::get($key_name));
  87. Cache::put($key_name, $product_datas, 360); //缓存6分钟
  88. //继续执行下一页
  89. $message_data['page'] = $page + 1;
  90. $message_data['limit'] = $limit;
  91. ScrapeDataProductJobs::dispatch($message_data)->delay(now()->addSeconds(1));
  92. }
  93. } catch (\Exception $e) {
  94. Log::info('job_error', '数据清洗-读取采集商品数据队列失败', ['data' => $this->message_data, 'error' => $e->getMessage()]);
  95. }
  96. }
  97. public function failed(\Throwable $exception)
  98. {
  99. Log::info('job_error', '数据清洗-读取采集商品数据队列完全失败', ['data' => $this->message_data, 'error' => $exception->getMessage()]);
  100. }
  101. }