ViolationProductOnlineStatisticsjobs.php 4.4 KB

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