LowPriceGoodsJobs.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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\WashConfig\LowPriceGoods as ConfigLowPriceGoodsModel;
  11. use App\Models\Manager\Process\ExecuteLog as ExecuteLogModel;
  12. use App\Jobs\Manager\Process\LowPriceGoodsDataJobs;
  13. use App\Models\Manager\WashConfig\ViolationStore as ViolationStoreModel;
  14. use App\Models\Manager\WashConfig\LowPriceGoodsCompany as LowPriceGoodsCompanyModel;
  15. /**
  16. * 数据清洗-低价挂网商品配置队列
  17. * @author 唐远望
  18. * @version 1.0
  19. * @date 2025-12-10
  20. */
  21. class LowPriceGoodsJobs implements ShouldQueue
  22. {
  23. use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
  24. public $tries = 3; // 限制重试次数
  25. public $timeout = 600; // 10分钟超时
  26. protected $message_data;
  27. /**
  28. * Create a new job instance.
  29. *
  30. * @return void
  31. */
  32. public function __construct(array $message_data)
  33. {
  34. $this->message_data = $message_data;
  35. }
  36. /**
  37. * Execute the job.
  38. *
  39. * @return void
  40. */
  41. public function handle()
  42. {
  43. try {
  44. $start_time = time() - 60 * 5; // 开始时间 5分钟之前
  45. $end_time = time(); //结束时间
  46. $ConfigLowPriceGoodsModel = new ConfigLowPriceGoodsModel();
  47. $LowPriceGoodsCompanyModel = new LowPriceGoodsCompanyModel();
  48. $limit = isset($this->message_data['limit']) ? $this->message_data['limit'] : 50;
  49. $page = isset($this->message_data['page']) ? $this->message_data['page'] : 1;
  50. $executeLog_id = isset($this->message_data['executeLog_id']) ? $this->message_data['executeLog_id'] : 0;
  51. $admin_id = isset($this->message_data['admin_id']) ? $this->message_data['admin_id'] : 0;
  52. $is_admin = isset($this->message_data['is_admin']) ? $this->message_data['is_admin'] : 0;
  53. $company_id = isset($this->message_data['company_id']) ? $this->message_data['company_id'] : 0;
  54. if ($page == 1) {
  55. $ExecuteLogModel = new ExecuteLogModel();
  56. $insert_data = ['company_id' => $company_id, 'name' => '低价挂网商品', 'code' => 'LowPriceGoodsJobs', 'admin_id' => $admin_id, 'is_admin' => $is_admin];
  57. $executeLog_id = $ExecuteLogModel->addExecuteLog_content($insert_data);
  58. }
  59. $ConfigLowPriceGoodsModel = $ConfigLowPriceGoodsModel->leftjoin('washconfig_product_category', 'washconfig_product_category.id', '=', 'washconfig_lowprice_product.category_id')
  60. ->where(['washconfig_lowprice_product.status' => 0])
  61. ->select(['washconfig_lowprice_product.*', 'washconfig_product_category.name as category_name']);
  62. if ($company_id) {
  63. $ConfigLowPriceGoodsModel = $ConfigLowPriceGoodsModel->where('washconfig_lowprice_product.company_id', $company_id);
  64. }
  65. $totle_page = 0;
  66. $list_config_data = $ConfigLowPriceGoodsModel->with(['product_keyword'])->paginate($limit, ['*'], 'page', $page)->toarray();
  67. if (!$list_config_data || empty($list_config_data['data'])) {
  68. if($page == 1 && $executeLog_id){
  69. $ExecuteLogModel->where('id', $executeLog_id)->update(['status' => 0,'update_time'=> time()]);
  70. }
  71. return true;
  72. }
  73. //获取总页码
  74. $totle_page = $list_config_data['last_page'];
  75. $list_data = $list_config_data['data'];
  76. foreach ($list_data as $key => $value) {
  77. $company_data = $LowPriceGoodsCompanyModel->where('washconfig_lowprice_product_company.lowprice_product_logid', $value['id'])
  78. ->where('washconfig_violation_company.company_type', 1)
  79. ->join('washconfig_violation_company', 'washconfig_violation_company.id', '=', 'washconfig_lowprice_product_company.company_id')
  80. ->select(['washconfig_violation_company.id', 'washconfig_violation_company.social_credit_code', 'washconfig_lowprice_product_company.company_id'])->get()->toArray();
  81. $social_credit_code = !empty($company_data) ? array_column($company_data, 'social_credit_code') : '';
  82. $message_data = [
  83. 'company_id' => $value['company_id'],
  84. 'id' => $value['id'],
  85. 'platform' => $value['platform'],
  86. 'product_name' => $value['product_name'],
  87. 'enable_full_quantity' => $value['enable_full_quantity'],
  88. 'product_specs' => $value['product_specs'],
  89. 'suggested_price' => $value['suggested_price'],
  90. 'ultra_low_price' => $value['ultra_low_price'],
  91. 'store_scope' => $value['store_scope'],
  92. 'company_scope' => $value['company_scope'],
  93. 'social_credit_code' => $social_credit_code,
  94. 'executeLog_id' => $executeLog_id,
  95. 'category_name' => $value['category_name'] ? $value['category_name'] : '',
  96. 'specify_responsible_person' => $value['specify_responsible_person'],
  97. 'product_brand' => $value['product_brand'],
  98. 'product_keyword' => $value['product_keyword'],
  99. 'item_totle_page' => $totle_page,
  100. 'item_now_page' => $page,
  101. 'start_time' => $start_time,
  102. 'end_time' => $end_time,
  103. ];
  104. LowPriceGoodsDataJobs::dispatch($message_data);
  105. // LowPriceGoodsDataJobs::dispatchSync($message_data);
  106. }
  107. $now_message_data = [
  108. 'limit' => $limit,
  109. 'page' => $page + 1,
  110. 'executeLog_id' => $executeLog_id,
  111. ];
  112. LowPriceGoodsJobs::dispatch($now_message_data)->delay(now()->addSeconds(3));
  113. } catch (\Exception $e) {
  114. Log::info('job_error', '数据清洗-低价挂网商品配置队列失败', ['data' => $this->message_data, 'error' => $e->getMessage()]);
  115. }
  116. }
  117. public function failed(\Throwable $exception)
  118. {
  119. Log::info('job_error', '数据清洗-低价挂网商品配置队列完全失败', ['data' => $this->message_data, 'error' => $exception->getMessage()]);
  120. }
  121. }