LowPriceGoodsJobs.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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. $ConfigLowPriceGoodsModel = new ConfigLowPriceGoodsModel();
  45. $LowPriceGoodsCompanyModel = new LowPriceGoodsCompanyModel();
  46. $limit = isset($this->message_data['limit']) ? $this->message_data['limit'] : 50;
  47. $page = isset($this->message_data['page']) ? $this->message_data['page'] : 1;
  48. $executeLog_id = isset($this->message_data['executeLog_id']) ? $this->message_data['executeLog_id'] : 0;
  49. $admin_id = isset($this->message_data['admin_id']) ? $this->message_data['admin_id'] : 0;
  50. $is_admin = isset($this->message_data['is_admin']) ? $this->message_data['is_admin'] : 0;
  51. $company_id = isset($this->message_data['company_id']) ? $this->message_data['company_id'] : 0;
  52. if ($page == 1) {
  53. $ExecuteLogModel = new ExecuteLogModel();
  54. $insert_data = ['company_id' => $company_id, 'name' => '低价挂网商品', 'code' => 'LowPriceGoodsJobs', 'admin_id' => $admin_id, 'is_admin' => $is_admin];
  55. $executeLog_id = $ExecuteLogModel->addExecuteLog_content($insert_data);
  56. }
  57. $ConfigLowPriceGoodsModel = $ConfigLowPriceGoodsModel->leftjoin('washconfig_product_category', 'washconfig_product_category.id', '=', 'washconfig_lowprice_product.category_id')
  58. ->where(['washconfig_lowprice_product.status' => 0])
  59. ->select(['washconfig_lowprice_product.*', 'washconfig_product_category.name as category_name']);
  60. if ($company_id) {
  61. $ConfigLowPriceGoodsModel = $ConfigLowPriceGoodsModel->where('washconfig_lowprice_product.company_id', $company_id);
  62. }
  63. $totle_page = 0;
  64. $list_config_data = $ConfigLowPriceGoodsModel->with(['product_keyword'])->paginate($limit, ['*'], 'page', $page)->toarray();
  65. if (!$list_config_data || empty($list_config_data['data'])) {
  66. if($page == 1 && $executeLog_id){
  67. $ExecuteLogModel->where('id', $executeLog_id)->update(['status' => 0,'update_time'=> time()]);
  68. }
  69. return true;
  70. }
  71. //获取总页码
  72. $totle_page = $list_config_data['last_page'];
  73. $list_data = $list_config_data['data'];
  74. foreach ($list_data as $key => $value) {
  75. $company_data = $LowPriceGoodsCompanyModel->where('washconfig_lowprice_product_company.lowprice_product_logid', $value['id'])
  76. ->where('washconfig_violation_company.company_type', 1)
  77. ->join('washconfig_violation_company', 'washconfig_violation_company.id', '=', 'washconfig_lowprice_product_company.company_id')
  78. ->select(['washconfig_violation_company.id', 'washconfig_violation_company.social_credit_code', 'washconfig_lowprice_product_company.company_id'])->get()->toArray();
  79. $social_credit_code = !empty($company_data) ? array_column($company_data, 'social_credit_code') : '';
  80. $message_data = [
  81. 'company_id' => $value['company_id'],
  82. 'id' => $value['id'],
  83. 'platform' => $value['platform'],
  84. 'product_name' => $value['product_name'],
  85. 'enable_full_quantity' => $value['enable_full_quantity'],
  86. 'product_specs' => $value['product_specs'],
  87. 'suggested_price' => $value['suggested_price'],
  88. 'store_scope' => $value['store_scope'],
  89. 'company_scope' => $value['company_scope'],
  90. 'social_credit_code' => $social_credit_code,
  91. 'executeLog_id' => $executeLog_id,
  92. 'category_name' => $value['category_name'] ? $value['category_name'] : '',
  93. 'specify_responsible_person' => $value['specify_responsible_person'],
  94. 'product_brand' => $value['product_brand'],
  95. 'product_keyword' => $value['product_keyword'],
  96. 'item_totle_page' => $totle_page,
  97. 'item_now_page' => $page,
  98. ];
  99. LowPriceGoodsDataJobs::dispatch($message_data);
  100. // LowPriceGoodsDataJobs::dispatchSync($message_data);
  101. }
  102. $now_message_data = [
  103. 'limit' => $limit,
  104. 'page' => $page + 1,
  105. 'executeLog_id' => $executeLog_id,
  106. ];
  107. LowPriceGoodsJobs::dispatch($now_message_data)->delay(now()->addSeconds(1));
  108. } catch (\Exception $e) {
  109. Log::info('job_error', '数据清洗-低价挂网商品配置队列失败', ['data' => $this->message_data, 'error' => $e->getMessage()]);
  110. }
  111. }
  112. public function failed(\Throwable $exception)
  113. {
  114. Log::info('job_error', '数据清洗-低价挂网商品配置队列完全失败', ['data' => $this->message_data, 'error' => $exception->getMessage()]);
  115. }
  116. }