ViolationProductDataJobs.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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\Process\ViolationProduct as ViolationProductModel;
  11. use App\Models\Manager\Personnel\Employee as EmployeeModel;
  12. use App\Models\Api\Process\ExecuteLog as ExecuteLogModel;
  13. use App\Models\Manager\Process\ScrapeData as ScrapeDataModel;
  14. use App\Models\Manager\WashConfig\ViolationStore as ViolationStoreModel;
  15. use App\Models\Manager\WashConfig\ViolationCompanyMember as ViolationCompanyMemberModel;
  16. /**
  17. * 数据清洗-违规挂网商品数据队列
  18. * @author 唐远望
  19. * @version 1.0
  20. * @date 2025-12-10
  21. */
  22. class ViolationProductDataJobs implements ShouldQueue
  23. {
  24. use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
  25. protected $message_data;
  26. /**
  27. * Create a new job instance.
  28. *
  29. * @return void
  30. */
  31. public function __construct(array $message_data)
  32. {
  33. $this->message_data = $message_data;
  34. }
  35. /**
  36. * Execute the job.
  37. *
  38. * @return void
  39. */
  40. public function handle()
  41. {
  42. try {
  43. $this->getViolationProductData($this->message_data);
  44. } catch (\Exception $e) {
  45. Log::info('job_error', '数据清洗-违规挂网商品数据队列失败', ['data' => $this->message_data, 'error' => $e->getMessage()]);
  46. }
  47. }
  48. /**
  49. * 采集商品数据清洗
  50. * @author 唐远望
  51. * @version 1.0
  52. * @date 2025-12-10
  53. */
  54. public function getViolationProductData($message_data)
  55. {
  56. $EmployeeModel = new EmployeeModel();
  57. $ViolationProductModel = new ViolationProductModel();
  58. $ScrapeDataModel = new ScrapeDataModel();
  59. $ViolationStoreModel = new ViolationStoreModel();
  60. $ViolationCompanyMemberModel = new ViolationCompanyMemberModel();
  61. $platform = $message_data['platform'];//多个平台配置
  62. $product_name = $message_data['product_name'];//商品名称
  63. $product_specs = $message_data['product_specs'];//商品规格
  64. $store_scope = $message_data['store_scope']; //店铺范围1=全部店铺2=指定店铺
  65. $company_scope = $message_data['company_scope']; //公司范围1=全部公司2=指定公司
  66. $social_credit_code = $message_data['social_credit_code']; //社会信用代码
  67. $executeLog_id = $message_data['executeLog_id'];
  68. $limit = isset($message_data['limit']) ? $message_data['limit'] : 50;
  69. $page = isset($message_data['page']) ? $message_data['page'] : 1;
  70. $where = [];
  71. if ($platform != '0') {
  72. $platform = explode(',', $platform);
  73. $ScrapeDataModel=$ScrapeDataModel->whereIn('platform', $platform);
  74. }
  75. $where[] = ['product_name', 'like', '%' . $product_name . '%'];
  76. $where[] = ['product_specs', 'like', '%' . $product_specs . '%'];
  77. if ($company_scope == 2) {
  78. $ScrapeDataModel->whereIn('qualification_number', $social_credit_code);
  79. }
  80. $product_data_info = $ScrapeDataModel->where($where)->paginate($limit, ['*'], 'page', $page)->toarray();
  81. $product_datas = $product_data_info['data'];
  82. if (empty($product_datas)) {
  83. if($executeLog_id){
  84. (new ExecuteLogModel())->where('id', $executeLog_id)->update(['status' => 0]);
  85. }
  86. return true;
  87. }
  88. foreach ($product_datas as $product_data) {
  89. $insert_product_data = [
  90. 'source_id'=> $product_data['id'],
  91. 'platform' => $product_data['platform_id'],
  92. 'company_name' => $product_data['company_name'],
  93. 'product_name' => $product_name,
  94. 'product_specs' => $product_specs,
  95. 'online_posting_cunt' => $product_data['online_posting_cunt'],
  96. 'link_url' => $product_data['link_url'],
  97. 'store_name' => $product_data['store_name'],
  98. 'social_credit_code' => $product_data['qualification_number'],
  99. 'province_id' => $product_data['province_id'],
  100. 'city_id' => $product_data['city_id'],
  101. 'area_info' => $product_data['area_info'],
  102. ];
  103. //获取公司绑定责任人信息
  104. $company_data = $ViolationStoreModel->where('social_credit_code',$product_data['qualification_number'])->first();
  105. $employee_id_list=[];
  106. if($company_data){
  107. $employee_id_list =$ViolationCompanyMemberModel->where('company_logid',$company_data->id)->pluck('employee_id')->toarray();
  108. }
  109. //查询配置的第一责任人
  110. if(!empty($employee_id_list)){
  111. $EmployeeModel = $EmployeeModel->whereIn('id',$employee_id_list);
  112. }else{
  113. $where_query1[] = ['city_ids', 'like', '%,' . $product_data['city_id'] . ',%'];
  114. }
  115. $where_query1[] = ['status', '=', 0];
  116. $where_query1[] = ['duty_type', '=', 1]; //责任类型1=第一责任人,2=责任人
  117. $first_responsible_person = $EmployeeModel->where($where_query1)->pluck('id')->implode(',');
  118. $insert_product_data['first_responsible_person'] = $first_responsible_person;
  119. //查询配置的责任人
  120. $EmployeeModel = new EmployeeModel();
  121. if(!empty($employee_id_list)){
  122. $EmployeeModel = $EmployeeModel->whereIn('id',$employee_id_list);
  123. }else{
  124. $where_query2[] = ['city_ids', 'like', '%,' . $product_data['city_id'] . ',%'];
  125. }
  126. $where_query2[] = ['status', '=', 0];
  127. $where_query2[] = ['duty_type', '=', 2]; //责任类型1=第一责任人,2=责任人
  128. $responsible_person = $EmployeeModel->where($where_query2)->pluck('id')->implode(',');
  129. $insert_product_data['responsible_person'] = $responsible_person;
  130. //溯源责任人
  131. $source_responsible_person = '';
  132. if ($first_responsible_person && $responsible_person) {
  133. $source_responsible_person = $first_responsible_person . ',' . $responsible_person;
  134. }
  135. $insert_product_data['source_responsible_person'] = $source_responsible_person;
  136. //插入数据
  137. $ViolationProductModel->addViolationProduct($insert_product_data);
  138. }
  139. //继续执行下一页
  140. $message_data['page'] = $page + 1;
  141. $message_data['limit'] = $limit;
  142. ViolationProductDataJobs::dispatch($message_data);
  143. }
  144. public function failed(\Throwable $exception)
  145. {
  146. Log::info('job_error', '数据清洗-违规挂网商品数据队列完全失败', ['data' => $this->message_data, 'error' => $exception]);
  147. }
  148. }