ViolationProductDataJobs.php 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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. $category_name = $message_data['category_name']; //产品分类名称
  69. $specify_responsible_person = $message_data['specify_responsible_person']; //指派责任人 0=开启 1=关闭
  70. $limit = isset($message_data['limit']) ? $message_data['limit'] : 50;
  71. $page = isset($message_data['page']) ? $message_data['page'] : 1;
  72. $company_id = isset($message_data['company_id']) ? $message_data['company_id'] : 0;
  73. $where = [];
  74. if ($platform != '0') {
  75. $platform = explode(',', $platform);
  76. $ScrapeDataModel = $ScrapeDataModel->whereIn('platform_id', $platform);
  77. }
  78. $where[] = ['product_name', 'like', '%' . $product_name . '%'];
  79. $where[] = ['product_specs', 'like', '%' . $product_specs . '%'];
  80. if ($company_scope == 2) {
  81. $ScrapeDataModel->whereIn('qualification_number', $social_credit_code);
  82. }
  83. $product_data_info = $ScrapeDataModel->where($where)->paginate($limit, ['*'], 'page', $page)->toarray();
  84. $product_datas = $product_data_info['data'];
  85. if (empty($product_datas)) {
  86. if ($executeLog_id) {
  87. (new ExecuteLogModel())->where('id', $executeLog_id)->update(['status' => 0]);
  88. }
  89. return true;
  90. }
  91. foreach ($product_datas as $product_data) {
  92. $insert_product_data = [
  93. 'company_id' => $company_id,
  94. 'source_id' => $product_data['id'],
  95. 'platform' => $product_data['platform_id'],
  96. 'company_name' => $product_data['company_name'],
  97. 'product_name' => $product_name,
  98. 'product_specs' => $product_specs,
  99. 'inventory' => $product_data['inventory'] ? $product_data['inventory'] : '',
  100. 'sales' => $product_data['sales'] ? $product_data['sales'] : '',
  101. 'snapshot_url' => $product_data['snapshot_url'] ? $product_data['snapshot_url'] : '',
  102. 'online_posting_count' => $product_data['online_posting_count'],
  103. 'continuous_listing_count' => $product_data['continuous_listing_count'],
  104. 'link_url' => $product_data['link_url'],
  105. 'store_name' => $product_data['store_name'],
  106. 'social_credit_code' => $product_data['qualification_number'],
  107. 'province_id' => $product_data['province_id'],
  108. 'province_name' => $product_data['province_name'],
  109. 'city_id' => $product_data['city_id'],
  110. 'city_name' => $product_data['city_name'],
  111. 'area_info' => $product_data['area_info'],
  112. 'category_name' => $category_name,
  113. 'company_category_name' => '',
  114. 'first_responsible_person' => '',
  115. 'responsible_person' => '',
  116. 'source_responsible_person' => '',
  117. ];
  118. //获取公司绑定责任人信息
  119. $company_data = $ViolationStoreModel->leftjoin('washconfig_company_category', 'washconfig_company_category.id', '=', 'washconfig_violation_store.category_id')
  120. ->where('washconfig_violation_store.social_credit_code', $product_data['qualification_number'])->select(['washconfig_violation_store.id', 'washconfig_company_category.name as category_name'])->first();
  121. $employee_id_list = [];
  122. if ($company_data) {
  123. $employee_id_list = $ViolationCompanyMemberModel->where('company_logid', $company_data->id)->pluck('employee_id')->toarray();
  124. $insert_product_data['company_category_name'] = $company_data->category_name ? $company_data->category_name : '';
  125. }
  126. //获取指定责任人信息
  127. if ($specify_responsible_person == 0) {
  128. //查询配置的第一责任人
  129. $where_query1 = [];
  130. if (!empty($employee_id_list)) {
  131. $EmployeeModel = $EmployeeModel->whereIn('id', $employee_id_list);
  132. } else {
  133. $where_query1[] = ['city_ids', 'like', '%,' . $product_data['city_id'] . ',%'];
  134. }
  135. $where_query1[] = ['status', '=', 0];
  136. $where_query1[] = ['duty_type', '=', 1]; //责任类型1=第一责任人,2=责任人
  137. $first_responsible_person = $EmployeeModel->where($where_query1)->pluck('id')->implode(',');
  138. $insert_product_data['first_responsible_person'] = $first_responsible_person;
  139. //查询配置的责任人
  140. $where_query2 = [];
  141. $EmployeeModel = new EmployeeModel();
  142. if (!empty($employee_id_list)) {
  143. $EmployeeModel = $EmployeeModel->whereIn('id', $employee_id_list);
  144. } else {
  145. $where_query2[] = ['city_ids', 'like', '%,' . $product_data['city_id'] . ',%'];
  146. }
  147. $where_query2[] = ['status', '=', 0];
  148. $where_query2[] = ['duty_type', '=', 2]; //责任类型1=第一责任人,2=责任人
  149. $responsible_person = $EmployeeModel->where($where_query2)->pluck('id')->implode(',');
  150. $insert_product_data['responsible_person'] = $responsible_person;
  151. //溯源责任人
  152. $source_responsible_person = '';
  153. if ($first_responsible_person && $responsible_person) {
  154. $source_responsible_person = $first_responsible_person . ',' . $responsible_person;
  155. } else if ($first_responsible_person) {
  156. $source_responsible_person = $first_responsible_person;
  157. } else if ($responsible_person) {
  158. $source_responsible_person = $responsible_person;
  159. }
  160. $insert_product_data['source_responsible_person'] = $source_responsible_person;
  161. }
  162. //插入数据
  163. $ViolationProductModel->addViolationProduct($insert_product_data);
  164. }
  165. //继续执行下一页
  166. $message_data['page'] = $page + 1;
  167. $message_data['limit'] = $limit;
  168. ViolationProductDataJobs::dispatch($message_data);
  169. }
  170. public function failed(\Throwable $exception)
  171. {
  172. Log::info('job_error', '数据清洗-违规挂网商品数据队列完全失败', ['data' => $this->message_data, 'error' => $exception]);
  173. }
  174. }