LowPriceOnlineStatisticsjobs.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. <?php
  2. namespace App\Jobs\Manager\CollectData\Backfill;
  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 Illuminate\Support\Facades\DB;
  11. use App\Facades\Servers\Logs\Log;
  12. /**
  13. * 低价商品数据数据清洗-低价挂网数据统计
  14. * @author: 唐远望
  15. * @version: 1.0
  16. * @date: 2026-05-29
  17. */
  18. class LowPriceOnlineStatisticsjobs implements ShouldQueue
  19. {
  20. use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
  21. public $tries = 3; // 限制重试次数
  22. public $timeout = 600; // 10分钟超时
  23. protected $message_data;
  24. /**
  25. * Create a new job instance.
  26. *
  27. * @return void
  28. */
  29. public function __construct(array $message_data)
  30. {
  31. $this->message_data = $message_data;
  32. }
  33. /**
  34. * Execute the job.
  35. *
  36. * @return void
  37. */
  38. public function handle()
  39. {
  40. $LowPriceGoodsModel = new LowPriceGoodsModel();
  41. $company_id = $this->message_data['company_id'];
  42. $map_one = [];
  43. $map_one[] = ['process_lowprice_product.company_id', '=', $company_id];
  44. $map_one[] = ['process_lowprice_product.online_posting_count', '>', 0];
  45. $map_two = [];
  46. $map_two[] = ['process_lowprice_product.company_id', '=', $company_id];
  47. $map_two[] = ['process_lowprice_product.continuous_listing_count', '>', 0];
  48. $page = isset($this->message_data['page']) ? $this->message_data['page'] : 1;
  49. $limit = isset($this->message_data['limit']) ? $this->message_data['limit'] : 10;
  50. DB::beginTransaction();
  51. try {
  52. $filed =['process_lowprice_product.*','scrape_data.collect_config_info'];
  53. $result = $LowPriceGoodsModel->leftJoin('scrape_data', 'process_lowprice_product.source_id', '=', 'scrape_data.id')
  54. ->Orwhere($map_one)->Orwhere($map_two)->orderBy('process_lowprice_product.scrape_date', 'asc')->paginate($limit,$filed, 'page', $page)->toarray();
  55. $select_product_datas = $result['data'];
  56. $last_page = $result['last_page'];
  57. if (empty($select_product_datas)) {
  58. return true;
  59. } else {
  60. if ($page < $last_page) {
  61. //继续执行下一页
  62. $this->message_data['page'] = $page + 1;
  63. $this->message_data['limit'] = $limit;
  64. LowPriceOnlineStatisticsjobs::dispatch($this->message_data);
  65. }
  66. foreach ($result['data'] as $key => $product_data) {
  67. $update_data = [
  68. 'online_posting_count' =>'1',
  69. 'continuous_listing_count' =>'1',
  70. ];
  71. $select_data = $LowPriceGoodsModel->handleCountData($product_data);
  72. if(isset($select_data['online_posting_count']) && $select_data['online_posting_count'] > 1){
  73. $update_data['online_posting_count'] = $select_data['online_posting_count']; //累计挂网次数
  74. }
  75. if(isset($select_data['continuous_listing_count']) && $select_data['continuous_listing_count'] > 1){
  76. $update_data['continuous_listing_count'] = $select_data['continuous_listing_count'];//连续挂网次数
  77. }
  78. if (!empty($update_data) && count($update_data) > 0) {
  79. $LowPriceGoodsModel->where('id', $product_data['id'])->update($update_data);
  80. }
  81. }
  82. }
  83. DB::commit();
  84. // 成功处理...
  85. } catch (\Exception $e) {
  86. DB::rollBack();
  87. Log::info('job_error', '数据回填-低价商品挂网统计数据回填队列失败', ['data' => $this->message_data, 'error' => $e->getMessage()]);
  88. }
  89. }
  90. public function failed(\Throwable $exception)
  91. {
  92. Log::info('job_error', '数据回填-低价商品挂网统计数据回填队列完全失败', ['data' => $this->message_data, 'error' => $exception->getMessage()]);
  93. }
  94. }