LowPriceGoodsJobs.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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. use Illuminate\Support\Carbon;
  16. /**
  17. * 数据清洗-低价挂网商品配置队列
  18. * @author 唐远望
  19. * @version 1.0
  20. * @date 2025-12-10
  21. */
  22. class LowPriceGoodsJobs 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. // $start_time = Carbon::today()->startOfDay()->getTimestamp(); // 今天开始时间 00:00:00
  46. $start_time = time() - 60 * 5; // 开始时间 5分钟之前
  47. $end_time = time(); //结束时间
  48. $ConfigLowPriceGoodsModel = new ConfigLowPriceGoodsModel();
  49. $LowPriceGoodsCompanyModel = new LowPriceGoodsCompanyModel();
  50. $limit = isset($this->message_data['limit']) ? $this->message_data['limit'] : 50;
  51. $page = isset($this->message_data['page']) ? $this->message_data['page'] : 1;
  52. $executeLog_id = isset($this->message_data['executeLog_id']) ? $this->message_data['executeLog_id'] : 0;
  53. $admin_id = isset($this->message_data['admin_id']) ? $this->message_data['admin_id'] : 0;
  54. $is_admin = isset($this->message_data['is_admin']) ? $this->message_data['is_admin'] : 0;
  55. $company_id = isset($this->message_data['company_id']) ? $this->message_data['company_id'] : 0;
  56. if ($page == 1) {
  57. $ExecuteLogModel = new ExecuteLogModel();
  58. $insert_data = ['company_id' => $company_id, 'name' => '低价挂网商品', 'code' => 'LowPriceGoodsJobs', 'admin_id' => $admin_id, 'is_admin' => $is_admin];
  59. $executeLog_id = $ExecuteLogModel->addExecuteLog_content($insert_data);
  60. }
  61. $ConfigLowPriceGoodsModel = $ConfigLowPriceGoodsModel->leftjoin('washconfig_product_category', 'washconfig_product_category.id', '=', 'washconfig_lowprice_product.category_id')
  62. ->where(['washconfig_lowprice_product.status' => 0])
  63. ->select(['washconfig_lowprice_product.*', 'washconfig_product_category.name as category_name']);
  64. if ($company_id) {
  65. $ConfigLowPriceGoodsModel = $ConfigLowPriceGoodsModel->where('washconfig_lowprice_product.company_id', $company_id);
  66. }
  67. $totle_page = 0;
  68. $list_config_data = $ConfigLowPriceGoodsModel->with(['product_keyword'])->paginate($limit, ['*'], 'page', $page)->toarray();
  69. if (!$list_config_data || empty($list_config_data['data'])) {
  70. if($page == 1 && $executeLog_id){
  71. $ExecuteLogModel->where('id', $executeLog_id)->update(['status' => 0,'update_time'=> time()]);
  72. }
  73. return true;
  74. }
  75. //获取总页码
  76. $totle_page = $list_config_data['last_page'];
  77. $list_data = $list_config_data['data'];
  78. foreach ($list_data as $key => $value) {
  79. $company_data = $LowPriceGoodsCompanyModel->where('washconfig_lowprice_product_company.lowprice_product_logid', $value['id'])
  80. ->where('washconfig_violation_company.company_type', 1)
  81. ->join('washconfig_violation_company', 'washconfig_violation_company.id', '=', 'washconfig_lowprice_product_company.company_id')
  82. ->select(['washconfig_violation_company.id', 'washconfig_violation_company.social_credit_code', 'washconfig_lowprice_product_company.company_id'])->get()->toArray();
  83. $social_credit_code = !empty($company_data) ? array_column($company_data, 'social_credit_code') : '';
  84. $message_data = [
  85. 'company_id' => $value['company_id'],
  86. 'id' => $value['id'],
  87. 'platform' => $value['platform'],
  88. 'product_name' => $value['product_name'],
  89. 'enable_full_quantity' => $value['enable_full_quantity'],
  90. 'product_specs' => $value['product_specs'],
  91. 'suggested_price' => $value['suggested_price'],
  92. 'ultra_low_price' => $value['ultra_low_price'],
  93. 'store_scope' => $value['store_scope'],
  94. 'company_scope' => $value['company_scope'],
  95. 'social_credit_code' => $social_credit_code,
  96. 'executeLog_id' => $executeLog_id,
  97. 'category_name' => $value['category_name'] ? $value['category_name'] : '',
  98. 'specify_responsible_person' => $value['specify_responsible_person'],
  99. 'product_brand' => $value['product_brand'],
  100. 'product_keyword' => $value['product_keyword'],
  101. 'item_totle_page' => $totle_page,
  102. 'item_now_page' => $page,
  103. 'start_time' => $start_time,
  104. 'end_time' => $end_time,
  105. ];
  106. // LowPriceGoodsDataJobs::dispatch($message_data);
  107. LowPriceGoodsDataJobs::dispatchSync($message_data);
  108. }
  109. $now_message_data = [
  110. 'limit' => $limit,
  111. 'page' => $page + 1,
  112. 'executeLog_id' => $executeLog_id,
  113. ];
  114. LowPriceGoodsJobs::dispatch($now_message_data)->delay(now()->addSeconds(3));
  115. } catch (\Exception $e) {
  116. Log::info('job_error', '数据清洗-低价挂网商品配置队列失败', ['data' => $this->message_data, 'error' => $e->getMessage()]);
  117. }
  118. }
  119. public function failed(\Throwable $exception)
  120. {
  121. Log::info('job_error', '数据清洗-低价挂网商品配置队列完全失败', ['data' => $this->message_data, 'error' => $exception->getMessage()]);
  122. }
  123. }