ViolationProductDataJobs.php 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631
  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\WashConfig\ViolationCompany as ViolationCompanyModel;
  14. use App\Models\Manager\WashConfig\ViolationCompanyMember as ViolationCompanyMemberModel;
  15. use App\Models\Manager\Citys as CitysModel;
  16. use App\Models\Manager\Personnel\EmployeePlatform as EmployeePlatformModel;
  17. use App\Models\Manager\Personnel\EmployeeArea as EmployeeAreaModel;
  18. use App\Models\Manager\WashConfig\ViolationStore as ViolationStoreModel;
  19. /**
  20. * 数据清洗-违规挂网商品数据队列
  21. * @author 唐远望
  22. * @version 1.0
  23. * @date 2025-12-10
  24. */
  25. class ViolationProductDataJobs implements ShouldQueue
  26. {
  27. use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
  28. public $tries = 3; // 限制重试次数
  29. public $timeout = 600; // 5分钟超时
  30. protected $message_data;
  31. /**
  32. * Create a new job instance.
  33. *
  34. * @return void
  35. */
  36. public function __construct(array $message_data)
  37. {
  38. $this->message_data = $message_data;
  39. }
  40. /**
  41. * Execute the job.
  42. *
  43. * @return void
  44. */
  45. public function handle()
  46. {
  47. try {
  48. $this->getViolationProductData($this->message_data);
  49. } catch (\Exception $e) {
  50. Log::info('job_error', '数据清洗-违规挂网商品数据队列失败', ['data' => $this->message_data, 'error' => $e->getMessage()]);
  51. }
  52. }
  53. /**
  54. * 采集商品数据清洗
  55. * @author 唐远望
  56. * @version 1.0
  57. * @date 2025-12-10
  58. */
  59. public function getViolationProductData($message_data)
  60. {
  61. $CitysModel = new CitysModel();
  62. $EmployeeModel = new EmployeeModel();
  63. $ViolationProductModel = new ViolationProductModel();
  64. $ViolationCompanyModel = new ViolationCompanyModel();
  65. $ViolationCompanyMemberModel = new ViolationCompanyMemberModel();
  66. $EmployeePlatformModel = new EmployeePlatformModel();
  67. $EmployeeAreaModel = new EmployeeAreaModel();
  68. $platform = $message_data['platform']; //多个平台配置
  69. $product_name = $message_data['product_name']; //商品名称
  70. $enable_full_quantity = $message_data['enable_full_quantity']; //是否启用全量采集0=是 1=否
  71. $product_specs = $message_data['product_specs']; //商品规格
  72. $store_scope = $message_data['store_scope']; //店铺范围1=全部店铺2=指定店铺
  73. $company_scope = $message_data['company_scope']; //公司范围1=全部公司2=指定公司
  74. $social_credit_code = $message_data['social_credit_code']; //社会信用代码
  75. $executeLog_id = $message_data['executeLog_id'];
  76. $category_name = $message_data['category_name']; //产品分类名称
  77. $specify_responsible_person = $message_data['specify_responsible_person']; //指派责任人 0=开启 1=关闭
  78. $company_id = isset($message_data['company_id']) ? $message_data['company_id'] : 0; //品牌方公司ID
  79. $product_brand = $message_data['product_brand']; //商品品牌名称
  80. $product_keywords = $message_data['product_keyword']; //关键字
  81. $product_data = isset($this->message_data['product_data']) ? $this->message_data['product_data'] : [];
  82. $index_number = isset($this->message_data['index_number']) ? $this->message_data['index_number'] : 0;
  83. $data_totle = isset($this->message_data['data_totle']) ? $this->message_data['data_totle'] : 0;
  84. //-------------------------------------- 清洗规则(开始) --------------------------------------
  85. //过滤没有链接数据
  86. if (trim($product_data['link_url']) == ''){
  87. // Log::info('job_error', '数据清洗-违规挂网商品队列日志,未命中链接数据', ['data' => $this->message_data]);
  88. return true;
  89. }
  90. //过滤售罄数据,是否售罄0=否1=是
  91. // if ($product_data['is_sold_out'] == 1) return true;
  92. //数据是否命中商品名称
  93. if ($product_name != '') {
  94. $hit_product_name = false;
  95. if (strpos($product_data['product_name'], $product_name) !== false) {
  96. $hit_product_name = true;
  97. }
  98. if ($hit_product_name != true){
  99. // Log::info('job_error', '数据清洗-违规挂网商品队列日志,未命中商品名称', ['data' => $this->message_data]);
  100. return true;
  101. }
  102. }
  103. //数据是否命中规格
  104. if ($enable_full_quantity == 1 && $product_specs) {
  105. $hit_product_specs = false;
  106. //product_name 包含规格
  107. if (strpos($product_data['product_name'], $product_specs) !== false) {
  108. $hit_product_specs = true;
  109. } else if (strpos($product_data['product_specs'], $product_specs) !== false) {
  110. $hit_product_specs = true;
  111. }
  112. if ($hit_product_specs != true){
  113. // Log::info('job_error', '数据清洗-违规挂网商品队列日志,未命中规格', ['data' => $this->message_data]);
  114. return true;
  115. }
  116. }
  117. //数据是否命中关键字
  118. if (!empty($product_keywords)) {
  119. $hit_product_keywords = false;
  120. $product_keywords = explode(',', $product_keywords);
  121. foreach ($product_keywords as $product_keyword) {
  122. if (empty($product_keyword)) continue;
  123. if (strpos($product_data['product_name'], $product_keyword) !== false) {
  124. $hit_product_keywords = true;
  125. break;
  126. }
  127. }
  128. if ($hit_product_keywords != true){
  129. // Log::info('job_error', '数据清洗-违规挂网商品队列日志,未命中关键字', ['data' => $this->message_data]);
  130. return true;
  131. }
  132. }
  133. //数据是否命中指定公司
  134. if ($company_scope == 2 && $social_credit_code != '') {
  135. $hit_company_scope = false;
  136. if ($product_data['qualification_number'] == $social_credit_code) {
  137. $hit_company_scope = true;
  138. }
  139. if ($hit_company_scope != true){
  140. // Log::info('job_error', '数据清洗-违规挂网商品队列日志,未命中指定公司', ['data' => $this->message_data]);
  141. return true;
  142. }
  143. }
  144. //数据是否命中平台
  145. if ($platform != '0' && !empty($platform)) {
  146. $hit_platform = false;
  147. $platform = explode(',', $platform);
  148. if (in_array($product_data['platform_id'], $platform)) {
  149. $hit_platform = true;
  150. }
  151. if ($hit_platform != true){
  152. // Log::info('job_error', '数据清洗-违规挂网商品队列日志,未命中平台', ['data' => $this->message_data]);
  153. return true;
  154. }
  155. }
  156. //-------------------------------------- 清洗规则(结束) --------------------------------------
  157. //-------------------------------------- 处理营业执照地区信息(开始) --------------------------------------
  158. $province_name = $product_data['province_name'];
  159. $city_name = $product_data['city_name'];
  160. //特殊地区1级移除市
  161. if ($province_name && in_array($province_name, ['北京市', '天津市', '上海市', '重庆市'])) {
  162. //移除市这个字符
  163. $province_name = trim(str_replace('市', '', $province_name));
  164. } else if ($province_name && in_array($province_name, ['北京', '天津', '上海', '重庆'])) {
  165. } else if ($province_name && in_array($province_name, ['内蒙古', '广西', '西藏', '新疆', '宁夏'])) {
  166. switch ($province_name) {
  167. case '内蒙古':
  168. $province_name = '内蒙古自治区';
  169. break;
  170. case '广西':
  171. $province_name = '广西壮族自治区';
  172. break;
  173. case '西藏':
  174. $province_name = '西藏自治区';
  175. break;
  176. case '新疆':
  177. $province_name = '新疆维吾尔自治区';
  178. break;
  179. case '宁夏':
  180. $province_name = '宁夏回族自治区';
  181. break;
  182. }
  183. } else if ($province_name && in_array($province_name, ['内蒙古自治区', '广西壮族自治区', '西藏自治区', '新疆维吾尔自治区', '宁夏回族自治区'])) {
  184. //完整匹配不做处理
  185. } else if (trim($province_name) != '' && strpos($province_name, '省') === false) {
  186. //是否存在市省,如果不存在则补全
  187. $province_name = $province_name . '省';
  188. }
  189. if (trim($province_name) != '') {
  190. //根据最新处理后的省份名称获取省份ID
  191. $db_province_id = $CitysModel->where([['name', '=', $province_name], ['level', '=', '1']])->value('id');
  192. if (!empty($db_province_id)) {
  193. $product_data['province_id'] = $db_province_id;
  194. } else {
  195. //逆推省份ID失败,则修改为未知名称
  196. $province_name = '未知';
  197. }
  198. } else {
  199. //尝试从地区详情里面匹配省份名称
  200. $db_province_data_list = $CitysModel->where([['level', '=', '1']])->select(['id', 'name'])->get()->toarray();
  201. foreach ($db_province_data_list as $db_province_data) {
  202. $db_province_name = $db_province_data['name'];
  203. if (strpos($product_data['area_info'], $db_province_name) !== false) {
  204. $province_name = $db_province_name;
  205. $product_data['province_id'] = $db_province_data['id'];
  206. break;
  207. }
  208. }
  209. }
  210. if (trim($city_name) != '') {
  211. //校验是否存在县这个字眼
  212. if (strpos($city_name, '县') !== false) {
  213. $db_city_id = $CitysModel->where([['name', '=', $city_name], ['level', '=', '2']])->value('id');
  214. if (!empty($db_city_id)) {
  215. $product_data['city_id'] = $db_city_id;
  216. }
  217. } elseif (trim($city_name) != '' && strpos($city_name, '市') === false) {
  218. //是否存在市
  219. $city_name = $city_name . '市';
  220. //根据最新处理后的市名称获取市ID
  221. $db_city_id = $CitysModel->where([['name', '=', $city_name], ['level', '=', '2']])->value('id');
  222. if (!empty($db_city_id)) {
  223. $product_data['city_id'] = $db_city_id;
  224. } else {
  225. $city_name = '未知';
  226. }
  227. } else {
  228. $db_city_id = $CitysModel->where([['name', '=', $city_name], ['level', '=', '2']])->value('id');
  229. if (!empty($db_city_id)) {
  230. $product_data['city_id'] = $db_city_id;
  231. } else {
  232. $city_name = '未知';
  233. }
  234. }
  235. } else {
  236. if (trim($product_data['area_info']) != '') {
  237. //尝试从地区详情里面匹配市名称
  238. $db_city_name_list = $CitysModel->where([['level', '=', '2']])->pluck('name')->toarray();
  239. foreach ($db_city_name_list as $db_city_name) {
  240. if (strpos($product_data['area_info'], $db_city_name) !== false) {
  241. $db_city_id = $CitysModel->where([['name', '=', $db_city_name], ['level', '=', '2']])->value('id');
  242. $product_data['city_id'] = $db_city_id;
  243. $product_data['city_name'] = $db_city_name;
  244. $city_name = $db_city_name;
  245. break;
  246. }
  247. }
  248. }
  249. }
  250. //如果存在市区city_id 则逆推省份ID以及名称
  251. if (!empty($product_data['city_id']) && empty($product_data['province_id'])) {
  252. $db_province_id = $CitysModel->where([['id', '=', $product_data['city_id']], ['level', '=', '2']])->value('pid');
  253. $db_province_name = $CitysModel->where([['id', '=', $db_province_id], ['level', '=', '1']])->value('name');
  254. $product_data['province_id'] = $db_province_id;
  255. $product_data['province_name'] = $db_province_name;
  256. $province_name = $db_province_name;
  257. }
  258. if (trim($product_data['area_info']) == '' && trim($city_name) != '' && trim($province_name) != '') {
  259. $product_data['area_info'] = $province_name . $city_name;
  260. }
  261. //-------------------------------------- 处理营业执照地区信息(结束) --------------------------------------
  262. //-------------------------------------- 处理发货省份地区信息(开始) --------------------------------------
  263. $shipment_province_name = $product_data['shipment_province_name'];
  264. $shipment_city_name = $product_data['shipment_city_name'];
  265. //特殊地区1级移除市
  266. if ($shipment_province_name && in_array($shipment_province_name, ['北京市', '天津市', '上海市', '重庆市'])) {
  267. //移除市这个字符
  268. $shipment_province_name = trim(str_replace('市', '', $shipment_province_name));
  269. } else if ($shipment_province_name && in_array($shipment_province_name, ['北京', '天津', '上海', '重庆'])) {
  270. } else if ($shipment_province_name && in_array($shipment_province_name, ['内蒙古', '广西', '西藏', '新疆', '宁夏'])) {
  271. switch ($shipment_province_name) {
  272. case '内蒙古':
  273. $shipment_province_name = '内蒙古自治区';
  274. break;
  275. case '广西':
  276. $shipment_province_name = '广西壮族自治区';
  277. break;
  278. case '西藏':
  279. $shipment_province_name = '西藏自治区';
  280. break;
  281. case '新疆':
  282. $shipment_province_name = '新疆维吾尔自治区';
  283. break;
  284. case '宁夏':
  285. $shipment_province_name = '宁夏回族自治区';
  286. break;
  287. }
  288. } else if ($shipment_province_name && in_array($shipment_province_name, ['内蒙古自治区', '广西壮族自治区', '西藏自治区', '新疆维吾尔自治区', '宁夏回族自治区'])) {
  289. //完整匹配不做处理
  290. } else if (trim($shipment_province_name) != '' && strpos($shipment_province_name, '省') === false) {
  291. //是否存在市省,如果不存在则补全
  292. $shipment_province_name = $shipment_province_name . '省';
  293. }
  294. if (trim($shipment_province_name) != '') {
  295. //根据最新处理后的省份名称获取省份ID
  296. $db_shipment_province_id = $CitysModel->where([['name', '=', $shipment_province_name], ['level', '=', '1']])->value('id');
  297. if (!empty($db_shipment_province_id)) {
  298. $product_data['shipment_province_id'] = $db_shipment_province_id;
  299. } else {
  300. //逆推省份ID失败,则修改为未知名称
  301. $shipment_province_name = '未知';
  302. }
  303. }
  304. if (trim($shipment_city_name) != '') {
  305. //校验是否存在县这个字眼
  306. if (strpos($shipment_city_name, '县') !== false) {
  307. $db_shipment_city_id = $CitysModel->where([['name', '=', $shipment_city_name], ['level', '=', '2']])->value('id');
  308. if (!empty($db_shipment_city_id)) {
  309. $product_data['shipment_city_id'] = $db_shipment_city_id;
  310. }
  311. } elseif (trim($shipment_city_name) != '' && strpos($shipment_city_name, '市') === false) {
  312. //是否存在市
  313. $shipment_city_name = $shipment_city_name . '市';
  314. //根据最新处理后的市名称获取市ID
  315. $db_shipment_city_id = $CitysModel->where([['name', '=', $shipment_city_name], ['level', '=', '2']])->value('id');
  316. if (!empty($db_shipment_city_id)) {
  317. $product_data['shipment_city_id'] = $db_shipment_city_id;
  318. } else {
  319. //逆推市ID失败,则修改为未知名称
  320. $shipment_city_name = '未知';
  321. }
  322. } else {
  323. $db_shipment_city_id = $CitysModel->where([['name', '=', $shipment_city_name], ['level', '=', '2']])->value('id');
  324. if (!empty($db_shipment_city_id)) {
  325. $product_data['shipment_city_id'] = $db_shipment_city_id;
  326. } else {
  327. //逆推市ID失败,则修改为未知名称
  328. $shipment_city_name = '未知';
  329. }
  330. }
  331. }
  332. //如果存在市区shipment_city_id 则逆推省份ID以及名称
  333. if (!empty($product_data['shipment_city_id']) && empty($product_data['shipment_province_id'])) {
  334. $db_shipment_province_id = $CitysModel->where([['id', '=', $product_data['shipment_city_id']], ['level', '=', '2']])->value('pid');
  335. $db_shipment_province_name = $CitysModel->where([['id', '=', $db_shipment_province_id], ['level', '=', '1']])->value('name');
  336. $product_data['shipment_province_id'] = $db_shipment_province_id;
  337. $product_data['shipment_province_name'] = $db_shipment_province_name;
  338. }
  339. //-------------------------------------- 处理发货省份地区信息(结束) --------------------------------------
  340. //处理链接信息
  341. preg_match('/https?:\/\/[^\s\'"<>]+/i', $product_data['link_url'], $matches);
  342. if (!empty($matches)) {
  343. $product_data['link_url'] = $matches[0];
  344. }
  345. //合并省份市区ID
  346. $merge_city_id = 0;
  347. $merge_province_id = 0;
  348. $merge_city_name = '';
  349. $merge_province_name = '';
  350. if (!empty($product_data['city_id']) && !empty($product_data['province_id'])) {
  351. $merge_city_id = $product_data['city_id'];
  352. $merge_province_id = $product_data['province_id'];
  353. $merge_city_name = $city_name;
  354. $merge_province_name = $province_name;
  355. } else if (!empty($product_data['shipment_city_id']) && !empty($product_data['shipment_province_id'])) {
  356. $merge_city_id = $product_data['shipment_city_id'];
  357. $merge_province_id = $product_data['shipment_province_id'];
  358. $merge_city_name = $shipment_city_name;
  359. $merge_province_name = $shipment_province_name;
  360. }
  361. $insert_product_data = [
  362. 'company_id' => $company_id,
  363. 'source_id' => $product_data['id'],
  364. 'platform' => $product_data['platform_id'],
  365. 'company_name' => $product_data['company_name'],
  366. 'product_brand' => $product_brand != '' ? $product_brand : $product_data['product_brand'],
  367. 'product_name' => $product_name,
  368. 'product_specs' => $product_specs,
  369. 'inventory' => $product_data['inventory'] ? $product_data['inventory'] : '',
  370. 'sales' => $product_data['sales'] ? $product_data['sales'] : '',
  371. 'snapshot_url' => $product_data['snapshot_url'] ? $product_data['snapshot_url'] : '',
  372. 'online_posting_count' => $product_data['online_posting_count'],
  373. 'continuous_listing_count' => $product_data['continuous_listing_count'],
  374. 'link_url' => $product_data['link_url'],
  375. 'store_name' => $product_data['store_name'],
  376. 'anonymous_store_name' => $product_data['anonymous_store_name'],
  377. 'social_credit_code' => $product_data['qualification_number'],
  378. 'province_id' => $product_data['province_id'],
  379. 'province_name' => $province_name,
  380. 'city_id' => $product_data['city_id'],
  381. 'city_name' => $city_name,
  382. 'area_info' => $product_data['area_info'],
  383. 'category_name' => $category_name,
  384. 'company_category_name' => '',
  385. 'first_responsible_person' => '',
  386. 'responsible_person' => '',
  387. 'source_responsible_person' => '',
  388. 'scrape_date' => $product_data['scrape_date'],
  389. 'collection_time' => strtotime($product_data['insert_time']),
  390. 'shipment_province_id' => $product_data['shipment_province_id'],
  391. 'shipment_province_name' => $product_data['shipment_province_name'],
  392. 'shipment_city_id' => $product_data['shipment_city_id'],
  393. 'shipment_city_name' => $product_data['shipment_city_name'],
  394. 'collect_config_info' => $product_data['collect_config_info'],
  395. 'merge_city_id' => $merge_city_id,
  396. 'merge_province_id' => $merge_province_id,
  397. 'merge_city_name' => $merge_city_name,
  398. 'merge_province_name' => $merge_province_name,
  399. ];
  400. //获取指定人员信息
  401. if ($specify_responsible_person == 0) {
  402. //获取店铺责任人信息
  403. $ViolationStoreModel = new ViolationStoreModel();
  404. $store_data_info = $ViolationStoreModel->where('store_name', $product_data['store_name'])->first();
  405. if (!empty($store_data_info) && trim($store_data_info->employee_ids) != '') {
  406. //查询店铺第一责任人信息
  407. $EmployeeModel = new EmployeeModel();
  408. $store_where_query = [];
  409. $employee_id_store = explode(',', $store_data_info->employee_ids);
  410. $store_where_query[] = ['company_id', '=', $company_id];
  411. $store_where_query[] = ['id', 'in', $employee_id_store];
  412. $store_where_query[] = ['status', '=', 0];
  413. $store_where_query[] = ['duty_type', '=', 1]; //责任类型1=第一责任人,2=责任人
  414. $first_responsible_person = $EmployeeModel->pluck('id')->implode(',');
  415. $insert_product_data['first_responsible_person'] = $first_responsible_person;
  416. //查询店铺责任人信息
  417. $EmployeeModel = new EmployeeModel();
  418. $store_where_query = [];
  419. $employee_id_store = explode(',', $store_data_info->employee_ids);
  420. $store_where_query[] = ['company_id', '=', $company_id];
  421. $store_where_query[] = ['id', 'in', $employee_id_store];
  422. $store_where_query[] = ['status', '=', 0];
  423. $store_where_query[] = ['duty_type', '=', 2]; //责任类型1=第一责任人,2=责任人
  424. $responsible_person = $EmployeeModel->pluck('id')->implode(',');
  425. $insert_product_data['responsible_person'] = $responsible_person;
  426. //溯源责任人
  427. $source_responsible_person = '';
  428. if ($first_responsible_person && $responsible_person) {
  429. //转换成数组,合并后在去重
  430. $first_responsible_person = explode(',', $first_responsible_person);
  431. $responsible_person = explode(',', $responsible_person);
  432. $source_responsible_person = array_unique(array_merge($first_responsible_person, $responsible_person));
  433. $source_responsible_person = ',' . implode(',', $source_responsible_person) . ',';
  434. } else if ($first_responsible_person) {
  435. $source_responsible_person = $first_responsible_person;
  436. } else if ($responsible_person) {
  437. $source_responsible_person = $responsible_person;
  438. }
  439. $insert_product_data['source_responsible_person'] = $source_responsible_person;
  440. } else {
  441. //获取公司绑定责任人信息
  442. $company_data = $ViolationCompanyModel->leftjoin('washconfig_company_category', 'washconfig_company_category.id', '=', 'washconfig_violation_company.category_id')
  443. ->where('washconfig_violation_company.social_credit_code', $product_data['qualification_number'])
  444. ->where('washconfig_violation_company.company_id', $company_id)
  445. ->select(['washconfig_violation_company.id', 'washconfig_company_category.name as category_name'])->first();
  446. $employee_id_list = [];
  447. if ($company_data) {
  448. $employee_id_list = $ViolationCompanyMemberModel->where('company_logid', $company_data->id)->pluck('employee_id')->toarray();
  449. $insert_product_data['company_category_name'] = $company_data->category_name ? $company_data->category_name : '';
  450. }
  451. $where_query1 = [];
  452. $where_query2 = [];
  453. $where_query3 = [];
  454. //查询指定公司第一责任人
  455. if (!empty($employee_id_list)) {
  456. $where_query1[] = ['company_id', '=', $company_id];
  457. $where_query1[] = ['id', 'in', $employee_id_list];
  458. $where_query1[] = ['status', '=', 0];
  459. $where_query1[] = ['duty_type', '=', 1]; //责任类型1=第一责任人,2=责任人
  460. }
  461. //查询地区配置的第一责任人
  462. $employee_id_area = $EmployeeAreaModel->where('city_id', $product_data['city_id'])->pluck('employee_id')->toarray();
  463. if (!empty($employee_id_area)) {
  464. $where_query2[] = ['company_id', '=', $company_id];
  465. $where_query2[] = ['id', 'in', $employee_id_area];
  466. $where_query2[] = ['status', '=', 0];
  467. $where_query2[] = ['duty_type', '=', 1]; //责任类型1=第一责任人,2=责任人
  468. }
  469. //查询平台配置的第一责任人
  470. $employee_id_platform = $EmployeePlatformModel->where('platform_id', $product_data['platform_id'])->pluck('employee_id')->toarray();
  471. if (!empty($employee_id_platform)) {
  472. $where_query3[] = ['company_id', '=', $company_id];
  473. $where_query3[] = ['id', 'in', $employee_id_platform];
  474. $where_query3[] = ['status', '=', 0];
  475. $where_query3[] = ['duty_type', '=', 1]; //责任类型1=第一责任人,2=责任人
  476. }
  477. //并行查询第一责任人
  478. $EmployeeModel = new EmployeeModel();
  479. $EmployeeModel = $EmployeeModel
  480. ->orWhere(function ($q) use ($employee_id_list, $company_id) {
  481. if (!empty($employee_id_list)) {
  482. $q->where('company_id', $company_id)
  483. ->orWhereIn('id', $employee_id_list)
  484. ->where('duty_type', 1)
  485. ->where('status', 0);
  486. }
  487. })->orWhere(function ($q) use ($employee_id_area, $company_id) {
  488. if (!empty($employee_id_area)) {
  489. $q->where('company_id', $company_id)
  490. ->whereIn('id', $employee_id_area)
  491. ->where('duty_type', 1)
  492. ->where('status', 0);
  493. }
  494. })->orWhere(function ($q) use ($employee_id_platform, $company_id) {
  495. if (!empty($employee_id_platform)) {
  496. $q->where('company_id', $company_id)
  497. ->whereIn('id', $employee_id_platform)
  498. ->where('duty_type', 1)
  499. ->where('status', 0);
  500. }
  501. });
  502. $first_responsible_person = $EmployeeModel->pluck('id')->implode(',');
  503. //当以上规则匹配不到责任人时,则去查询发货地区信息关联责任人
  504. if (trim($first_responsible_person) == '' && trim($product_data['shipment_city_id']) != '') {
  505. $employee_id_area = $EmployeeAreaModel->where('city_id', $product_data['city_id'])->pluck('employee_id')->toarray();
  506. if (!empty($employee_id_area)) {
  507. $where_city[] = ['company_id', '=', $company_id];
  508. $where_city[] = ['id', 'in', $employee_id_area];
  509. $where_city[] = ['status', '=', 0];
  510. $where_city[] = ['duty_type', '=', 1]; //责任类型1=第一责任人,2=责任人
  511. $EmployeeModel = new EmployeeModel();
  512. $first_responsible_person = $EmployeeModel->where($where_city)->pluck('id')->implode(',');
  513. }
  514. }
  515. //调试记录查询条件
  516. $insert_product_data['first_responsible_person'] = $first_responsible_person;
  517. //查询责任人
  518. $where_query1 = [];
  519. $where_query2 = [];
  520. $where_query3 = [];
  521. //查询指定公司责任人
  522. if (!empty($employee_id_list)) {
  523. $where_query1[] = ['company_id', '=', $company_id];
  524. $where_query1[] = ['id', 'in', $employee_id_list];
  525. $where_query1[] = ['status', '=', 0];
  526. $where_query1[] = ['duty_type', '=', 2]; //责任类型1=第一责任人,2=责任人
  527. }
  528. //查询地区配置的员工
  529. $employee_id_area = $EmployeeAreaModel->where('city_id', $product_data['city_id'])->pluck('employee_id')->toarray();
  530. if (!empty($employee_id_area)) {
  531. $where_query2[] = ['company_id', '=', $company_id];
  532. $where_query2[] = ['id', 'in', $employee_id_area];
  533. $where_query2[] = ['status', '=', 0];
  534. $where_query2[] = ['duty_type', '=', 2]; //责任类型1=第一责任人,2=责任人
  535. }
  536. //查询平台配置的员工
  537. $employee_id_platform = $EmployeePlatformModel->where('platform_id', $product_data['platform_id'])->pluck('employee_id')->toarray();
  538. if (!empty($employee_id_platform)) {
  539. $where_query3[] = ['company_id', '=', $company_id];
  540. $where_query3[] = ['id', 'in', $employee_id_platform];
  541. $where_query3[] = ['status', '=', 0];
  542. $where_query3[] = ['duty_type', '=', 2]; //责任类型1=第一责任人,2=责任人
  543. }
  544. //并行查询责任人
  545. $EmployeeModel = new EmployeeModel();
  546. $EmployeeModel = $EmployeeModel
  547. ->orWhere(function ($q) use ($employee_id_list, $company_id) {
  548. if (!empty($employee_id_list)) {
  549. $q->where('company_id', $company_id)
  550. ->orWhereIn('id', $employee_id_list)
  551. ->where('duty_type', 2)
  552. ->where('status', 0);
  553. }
  554. })->orWhere(function ($q) use ($employee_id_area, $company_id) {
  555. if (!empty($employee_id_area)) {
  556. $q->where('company_id', $company_id)
  557. ->whereIn('id', $employee_id_area)
  558. ->where('duty_type', 2)
  559. ->where('status', 0);
  560. }
  561. })->orWhere(function ($q) use ($employee_id_platform, $company_id) {
  562. if (!empty($employee_id_platform)) {
  563. $q->where('company_id', $company_id)
  564. ->whereIn('id', $employee_id_platform)
  565. ->where('duty_type', 2)
  566. ->where('status', 0);
  567. }
  568. });
  569. $responsible_person = $EmployeeModel->pluck('id')->implode(',');
  570. //当以上规则匹配不到责任人时,则去查询发货地区信息关联责任人
  571. if (trim($responsible_person) == '' && trim($product_data['shipment_city_id']) != '') {
  572. $employee_id_area = $EmployeeAreaModel->where('city_id', $product_data['city_id'])->pluck('employee_id')->toarray();
  573. if (!empty($employee_id_area)) {
  574. $where_city[] = ['company_id', '=', $company_id];
  575. $where_city[] = ['id', 'in', $employee_id_area];
  576. $where_city[] = ['status', '=', 0];
  577. $where_city[] = ['duty_type', '=', 2]; //责任类型1=第一责任人,2=责任人
  578. $EmployeeModel = new EmployeeModel();
  579. $responsible_person = $EmployeeModel->where($where_city)->pluck('id')->implode(',');
  580. }
  581. }
  582. $insert_product_data['responsible_person'] = $responsible_person;
  583. //溯源责任人
  584. $source_responsible_person = '';
  585. if ($first_responsible_person && $responsible_person) {
  586. //转换成数组,合并后在去重
  587. $first_responsible_person = explode(',', $first_responsible_person);
  588. $responsible_person = explode(',', $responsible_person);
  589. $source_responsible_person = array_unique(array_merge($first_responsible_person, $responsible_person));
  590. $source_responsible_person = ',' . implode(',', $source_responsible_person) . ',';
  591. } else if ($first_responsible_person) {
  592. $source_responsible_person = $first_responsible_person;
  593. } else if ($responsible_person) {
  594. $source_responsible_person = $responsible_person;
  595. }
  596. $insert_product_data['source_responsible_person'] = $source_responsible_person;
  597. }
  598. }
  599. //插入数据
  600. $ViolationProductModel->addViolationProduct($insert_product_data);
  601. if ($index_number == $data_totle) {
  602. (new ExecuteLogModel())->where('id', $executeLog_id)->update(['status' => 0, 'update_time' => time()]);
  603. }
  604. }
  605. public function failed(\Throwable $exception)
  606. {
  607. Log::info('job_error', '数据清洗-违规挂网商品数据队列完全失败', ['data' => $this->message_data, 'error' => $exception->getMessage()]);
  608. }
  609. }