ViolationProductDataJobs.php 35 KB

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