ViolationProductDataJobs.php 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514
  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. use App\Models\Manager\Citys as CitysModel;
  17. use App\Models\Manager\Personnel\EmployeePlatform as EmployeePlatformModel;
  18. use App\Models\Manager\Personnel\EmployeeArea as EmployeeAreaModel;
  19. use Illuminate\Support\Facades\DB;
  20. use Illuminate\Support\Carbon;
  21. /**
  22. * 数据清洗-违规挂网商品数据队列
  23. * @author 唐远望
  24. * @version 1.0
  25. * @date 2025-12-10
  26. */
  27. class ViolationProductDataJobs implements ShouldQueue
  28. {
  29. use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
  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. $ScrapeDataModel = new ScrapeDataModel();
  65. $ViolationStoreModel = new ViolationStoreModel();
  66. $ViolationCompanyMemberModel = new ViolationCompanyMemberModel();
  67. $EmployeePlatformModel = new EmployeePlatformModel();
  68. $EmployeeAreaModel = new EmployeeAreaModel();
  69. $platform = $message_data['platform']; //多个平台配置
  70. $product_name = $message_data['product_name']; //商品名称
  71. $enable_full_quantity = $message_data['enable_full_quantity']; //是否启用全量采集0=是 1=否
  72. $product_specs = $message_data['product_specs']; //商品规格
  73. $store_scope = $message_data['store_scope']; //店铺范围1=全部店铺2=指定店铺
  74. $company_scope = $message_data['company_scope']; //公司范围1=全部公司2=指定公司
  75. $social_credit_code = $message_data['social_credit_code']; //社会信用代码
  76. $executeLog_id = $message_data['executeLog_id'];
  77. $category_name = $message_data['category_name']; //产品分类名称
  78. $specify_responsible_person = $message_data['specify_responsible_person']; //指派责任人 0=开启 1=关闭
  79. $limit = isset($message_data['limit']) ? $message_data['limit'] : 50;
  80. $page = isset($message_data['page']) ? $message_data['page'] : 1;
  81. $company_id = isset($message_data['company_id']) ? $message_data['company_id'] : 0; //品牌方公司ID
  82. $product_brand = $message_data['product_brand']; //商品品牌名称
  83. $product_keywords = $message_data['product_keyword']; //关键字
  84. $item_totle_page = $message_data['item_totle_page']; //清洗商品总页数
  85. $item_now_page = $message_data['item_now_page']; //清洗商品当前页
  86. $where = [];
  87. if ($platform != '0' && !empty($platform)) {
  88. $platform = explode(',', $platform);
  89. $ScrapeDataModel = $ScrapeDataModel->whereIn('platform_id', $platform);
  90. }
  91. // $yesterdayStart = Carbon::today()->subDays(6)->startOfDay()->getTimestamp(); // 最近7天开始时间 00:00:00
  92. $yesterdayStart = Carbon::yesterday()->startOfDay()->getTimestamp(); // 昨天开始时间 00:00:00
  93. $yesterdayEnd = Carbon::yesterday()->endOfDay()->getTimestamp(); // 昨天结束时间 23:59:59
  94. $yesterdayStart = date('Y-m-d', $yesterdayStart);
  95. $yesterdayEnd = date('Y-m-d', $yesterdayEnd);
  96. $where[] = ['scrape_date', '>=', $yesterdayStart];
  97. $where[] = ['scrape_date', '<=', $yesterdayEnd];
  98. $where[] = ['product_name', 'like', '%' . $product_name . '%'];
  99. if ($enable_full_quantity == 1) $where[] = ['product_specs', 'like', '%' . $product_specs . '%']; //如果不是全量清洗,则查询规格
  100. // if ($product_brand) $where[] = ['product_name', 'like', '%' . $product_brand . '%'];
  101. if ($product_brand) $where[] = ['product_brand', 'like', '%' . $category_name . '%'];
  102. if (!empty($product_keyword)) {
  103. $ScrapeDataModel = $ScrapeDataModel->where(function ($query) use ($product_keywords) {
  104. // 平台条件(固定)
  105. $query->where(function ($query_li) use ($product_keywords) {
  106. foreach ($product_keywords as $product_keyword) {
  107. if (empty($product_keyword)) continue;
  108. $query_li->orWhere([['product_name', 'like', '%' . $product_keyword . '%']]);
  109. }
  110. });
  111. });
  112. }
  113. if ($company_scope == 2) {
  114. $ScrapeDataModel->whereIn('qualification_number', $social_credit_code);
  115. }
  116. $product_data_info = $ScrapeDataModel->where($where)->paginate($limit, ['*'], 'page', $page)->toarray();
  117. $product_datas = $product_data_info['data'];
  118. if (empty($product_datas)) {
  119. if ($executeLog_id && $item_now_page >= $item_totle_page) {
  120. (new ExecuteLogModel())->where('id', $executeLog_id)->update(['status' => 0, 'update_time' => time()]);
  121. }
  122. return true;
  123. }
  124. foreach ($product_datas as $product_data) {
  125. //-------------------------------------- 处理营业执照地区信息(开始) --------------------------------------
  126. $province_name = $product_data['province_name'];
  127. $city_name = $product_data['city_name'];
  128. //特殊地区1级移除市
  129. if ($province_name && in_array($province_name, ['北京市', '天津市', '上海市', '重庆市'])) {
  130. //移除市这个字符
  131. $province_name = trim(str_replace('市', '', $province_name));
  132. } else if ($province_name && in_array($province_name, ['北京', '天津', '上海', '重庆'])) {
  133. } else if ($province_name && in_array($province_name, ['内蒙古', '广西', '西藏', '新疆', '宁夏'])) {
  134. switch ($province_name) {
  135. case '内蒙古':
  136. $province_name = '内蒙古自治区';
  137. break;
  138. case '广西':
  139. $province_name = '广西壮族自治区';
  140. break;
  141. case '西藏':
  142. $province_name = '西藏自治区';
  143. break;
  144. case '新疆':
  145. $province_name = '新疆维吾尔自治区';
  146. break;
  147. case '宁夏':
  148. $province_name = '宁夏回族自治区';
  149. break;
  150. }
  151. } else if ($province_name && in_array($province_name, ['内蒙古自治区', '广西壮族自治区', '西藏自治区', '新疆维吾尔自治区', '宁夏回族自治区'])) {
  152. //完整匹配不做处理
  153. } else if (trim($province_name) != '' && strpos($province_name, '省') === false) {
  154. //是否存在市省,如果不存在则补全
  155. $province_name = $province_name . '省';
  156. }
  157. if (trim($province_name) != '') {
  158. //根据最新处理后的省份名称获取省份ID
  159. $db_province_id = $CitysModel->where([['name', '=', $province_name], ['level', '=', '1']])->value('id');
  160. if (!empty($db_province_id)) {
  161. $product_data['province_id'] = $db_province_id;
  162. }
  163. } else {
  164. //尝试从地区详情里面匹配省份名称
  165. $db_province_name_list = $CitysModel->where([['level', '=', '1']])->pluck('name')->toarray();
  166. foreach ($db_province_name_list as $db_province_name) {
  167. if (strpos($product_data['area_info'], $db_province_name) !== false) {
  168. $province_name = $db_province_name;
  169. break;
  170. }
  171. }
  172. }
  173. if (trim($city_name) != '') {
  174. //校验是否存在县这个字眼
  175. if (strpos($city_name, '县') !== false) {
  176. $db_city_id = $CitysModel->where([['name', '=', $city_name], ['level', '=', '2']])->value('id');
  177. if (!empty($db_city_id)) {
  178. $product_data['city_id'] = $db_city_id;
  179. }
  180. } elseif (trim($city_name) != '' && strpos($city_name, '市') === false) {
  181. //是否存在市
  182. $city_name = $city_name . '市';
  183. //根据最新处理后的市名称获取市ID
  184. $db_city_id = $CitysModel->where([['name', '=', $city_name], ['level', '=', '2']])->value('id');
  185. if (!empty($db_city_id)) {
  186. $product_data['city_id'] = $db_city_id;
  187. }
  188. } else {
  189. $db_city_id = $CitysModel->where([['name', '=', $city_name], ['level', '=', '2']])->value('id');
  190. if (!empty($db_city_id)) {
  191. $product_data['city_id'] = $db_city_id;
  192. }
  193. }
  194. } else {
  195. //尝试从地区详情里面匹配市名称
  196. $db_city_name_list = $CitysModel->where([['level', '=', '2']])->pluck('name')->toarray();
  197. foreach ($db_city_name_list as $db_city_name) {
  198. if (strpos($product_data['area_info'], $db_city_name) !== false) {
  199. $db_city_id = $CitysModel->where([['name', '=', $db_city_name], ['level', '=', '2']])->value('id');
  200. $product_data['city_id'] = $db_city_id;
  201. $product_data['city_name'] = $db_city_name;
  202. $city_name = $db_city_name;
  203. break;
  204. }
  205. }
  206. }
  207. //如果存在市区city_id 则逆推省份ID以及名称
  208. if (!empty($product_data['city_id']) && empty($product_data['province_id'])) {
  209. $db_province_id = $CitysModel->where([['id', '=', $product_data['city_id']], ['level', '=', '2']])->value('pid');
  210. $db_province_name = $CitysModel->where([['id', '=', $db_province_id], ['level', '=', '1']])->value('name');
  211. $product_data['province_id'] = $db_province_id;
  212. $product_data['province_name'] = $db_province_name;
  213. $province_name = $db_province_name;
  214. }
  215. if (trim($product_data['area_info']) == '' && trim($city_name) != '' && trim($province_name) != '') {
  216. $product_data['area_info'] = $province_name . $city_name;
  217. }
  218. //-------------------------------------- 处理营业执照地区信息(结束) --------------------------------------
  219. //-------------------------------------- 处理发货省份地区信息(开始) --------------------------------------
  220. $shipment_province_name = $product_data['shipment_province_name'];
  221. $shipment_city_name = $product_data['shipment_city_name'];
  222. //特殊地区1级移除市
  223. if ($shipment_province_name && in_array($shipment_province_name, ['北京市', '天津市', '上海市', '重庆市'])) {
  224. //移除市这个字符
  225. $shipment_province_name = trim(str_replace('市', '', $shipment_province_name));
  226. } else if ($shipment_province_name && in_array($shipment_province_name, ['北京', '天津', '上海', '重庆'])) {
  227. } else if ($shipment_province_name && in_array($shipment_province_name, ['内蒙古', '广西', '西藏', '新疆', '宁夏'])) {
  228. switch ($shipment_province_name) {
  229. case '内蒙古':
  230. $shipment_province_name = '内蒙古自治区';
  231. break;
  232. case '广西':
  233. $shipment_province_name = '广西壮族自治区';
  234. break;
  235. case '西藏':
  236. $shipment_province_name = '西藏自治区';
  237. break;
  238. case '新疆':
  239. $shipment_province_name = '新疆维吾尔自治区';
  240. break;
  241. case '宁夏':
  242. $shipment_province_name = '宁夏回族自治区';
  243. break;
  244. }
  245. } else if ($shipment_province_name && in_array($shipment_province_name, ['内蒙古自治区', '广西壮族自治区', '西藏自治区', '新疆维吾尔自治区', '宁夏回族自治区'])) {
  246. //完整匹配不做处理
  247. } else if (trim($shipment_province_name) != '' && strpos($shipment_province_name, '省') === false) {
  248. //是否存在市省,如果不存在则补全
  249. $shipment_province_name = $shipment_province_name . '省';
  250. }
  251. if (trim($shipment_province_name) != '') {
  252. //根据最新处理后的省份名称获取省份ID
  253. $db_shipment_province_id = $CitysModel->where([['name', '=', $shipment_province_name], ['level', '=', '1']])->value('id');
  254. if (!empty($db_shipment_province_id)) {
  255. $product_data['shipment_province_id'] = $db_shipment_province_id;
  256. }
  257. }
  258. if (trim($shipment_city_name) != '') {
  259. //校验是否存在县这个字眼
  260. if (strpos($shipment_city_name, '县') !== false) {
  261. $db_shipment_city_id = $CitysModel->where([['name', '=', $shipment_city_name], ['level', '=', '2']])->value('id');
  262. if (!empty($db_shipment_city_id)) {
  263. $product_data['shipment_city_id'] = $db_shipment_city_id;
  264. }
  265. } elseif (trim($shipment_city_name) != '' && strpos($shipment_city_name, '市') === false) {
  266. //是否存在市
  267. $shipment_city_name = $shipment_city_name . '市';
  268. //根据最新处理后的市名称获取市ID
  269. $db_shipment_city_id = $CitysModel->where([['name', '=', $shipment_city_name], ['level', '=', '2']])->value('id');
  270. if (!empty($db_shipment_city_id)) {
  271. $product_data['shipment_city_id'] = $db_shipment_city_id;
  272. }
  273. } else {
  274. $db_shipment_city_id = $CitysModel->where([['name', '=', $shipment_city_name], ['level', '=', '2']])->value('id');
  275. if (!empty($db_shipment_city_id)) {
  276. $product_data['shipment_city_id'] = $db_shipment_city_id;
  277. }
  278. }
  279. }
  280. //如果存在市区shipment_city_id 则逆推省份ID以及名称
  281. if (!empty($product_data['shipment_city_id']) && empty($product_data['shipment_province_id'])) {
  282. $db_shipment_province_id = $CitysModel->where([['id', '=', $product_data['shipment_city_id']], ['level', '=', '2']])->value('pid');
  283. $db_shipment_province_name = $CitysModel->where([['id', '=', $db_shipment_province_id], ['level', '=', '1']])->value('name');
  284. $product_data['shipment_province_id'] = $db_shipment_province_id;
  285. $product_data['shipment_province_name'] = $db_shipment_province_name;
  286. }
  287. //-------------------------------------- 处理发货省份地区信息(结束) --------------------------------------
  288. //处理链接信息
  289. preg_match('/https?:\/\/[^\s\'"<>]+/i', $product_data['link_url'], $matches);
  290. if (!empty($matches)) {
  291. $product_data['link_url'] = $matches[0];
  292. }
  293. if (trim($product_data['link_url']) == '') continue;
  294. $insert_product_data = [
  295. 'company_id' => $company_id,
  296. 'source_id' => $product_data['id'],
  297. 'platform' => $product_data['platform_id'],
  298. 'company_name' => $product_data['company_name'],
  299. 'product_brand' => $product_brand != '' ? $product_brand : $product_data['product_brand'],
  300. 'product_name' => $product_name,
  301. 'product_specs' => $product_specs,
  302. 'inventory' => $product_data['inventory'] ? $product_data['inventory'] : '',
  303. 'sales' => $product_data['sales'] ? $product_data['sales'] : '',
  304. 'snapshot_url' => $product_data['snapshot_url'] ? $product_data['snapshot_url'] : '',
  305. 'online_posting_count' => $product_data['online_posting_count'],
  306. 'continuous_listing_count' => $product_data['continuous_listing_count'],
  307. 'link_url' => $product_data['link_url'],
  308. 'store_name' => $product_data['store_name'],
  309. 'anonymous_store_name' => $product_data['anonymous_store_name'],
  310. 'social_credit_code' => $product_data['qualification_number'],
  311. 'province_id' => $product_data['province_id'],
  312. 'province_name' => $province_name,
  313. 'city_id' => $product_data['city_id'],
  314. 'city_name' => $city_name,
  315. 'area_info' => $product_data['area_info'],
  316. 'category_name' => $category_name,
  317. 'company_category_name' => '',
  318. 'first_responsible_person' => '',
  319. 'responsible_person' => '',
  320. 'source_responsible_person' => '',
  321. 'scrape_date' => $product_data['scrape_date'],
  322. 'collection_time' => strtotime($product_data['insert_time']),
  323. 'shipment_province_id' => $product_data['shipment_province_id'],
  324. 'shipment_province_name' => $product_data['shipment_province_name'],
  325. 'shipment_city_id' => $product_data['shipment_city_id'],
  326. 'shipment_city_name' => $product_data['shipment_city_name'],
  327. ];
  328. //获取公司绑定责任人信息
  329. $company_data = $ViolationStoreModel->leftjoin('washconfig_company_category', 'washconfig_company_category.id', '=', 'washconfig_violation_company.category_id')
  330. ->where('washconfig_violation_company.social_credit_code', $product_data['qualification_number'])
  331. ->where('washconfig_violation_company.company_id', $company_id)
  332. ->select(['washconfig_violation_company.id', 'washconfig_company_category.name as category_name'])->first();
  333. $employee_id_list = [];
  334. if ($company_data) {
  335. $employee_id_list = $ViolationCompanyMemberModel->where('company_logid', $company_data->id)->pluck('employee_id')->toarray();
  336. $insert_product_data['company_category_name'] = $company_data->category_name ? $company_data->category_name : '';
  337. }
  338. //获取指定人员信息
  339. if ($specify_responsible_person == 0) {
  340. $where_query1 = [];
  341. $where_query2 = [];
  342. $where_query3 = [];
  343. //查询指定公司第一责任人
  344. if (!empty($employee_id_list)) {
  345. $where_query1[] = ['company_id', '=', $company_id];
  346. $where_query1[] = ['id', 'in', $employee_id_list];
  347. $where_query1[] = ['status', '=', 0];
  348. $where_query1[] = ['duty_type', '=', 1]; //责任类型1=第一责任人,2=责任人
  349. }
  350. //查询地区配置的第一责任人
  351. $employee_id_area = $EmployeeAreaModel->where('city_id', $product_data['city_id'])->pluck('employee_id')->toarray();
  352. if (!empty($employee_id_area)) {
  353. $where_query2[] = ['company_id', '=', $company_id];
  354. $where_query2[] = ['id', 'in', $employee_id_area];
  355. $where_query2[] = ['status', '=', 0];
  356. $where_query2[] = ['duty_type', '=', 1]; //责任类型1=第一责任人,2=责任人
  357. }
  358. //查询平台配置的第一责任人
  359. $employee_id_platform = $EmployeePlatformModel->where('platform_id', $product_data['platform_id'])->pluck('employee_id')->toarray();
  360. if (!empty($employee_id_platform)) {
  361. $where_query3[] = ['company_id', '=', $company_id];
  362. $where_query3[] = ['id', 'in', $employee_id_platform];
  363. $where_query3[] = ['status', '=', 0];
  364. $where_query3[] = ['duty_type', '=', 1]; //责任类型1=第一责任人,2=责任人
  365. }
  366. //并行查询第一责任人
  367. $EmployeeModel = new EmployeeModel();
  368. $EmployeeModel = $EmployeeModel
  369. ->orWhere(function ($q) use ($employee_id_list, $company_id) {
  370. if (!empty($employee_id_list)) {
  371. $q->where('company_id', $company_id)
  372. ->orWhereIn('id', $employee_id_list)
  373. ->where('duty_type', 1)
  374. ->where('status', 0);
  375. }
  376. })->orWhere(function ($q) use ($employee_id_area, $company_id) {
  377. if (!empty($employee_id_area)) {
  378. $q->where('company_id', $company_id)
  379. ->whereIn('id', $employee_id_area)
  380. ->where('duty_type', 1)
  381. ->where('status', 0);
  382. }
  383. })->orWhere(function ($q) use ($employee_id_platform, $company_id) {
  384. if (!empty($employee_id_platform)) {
  385. $q->where('company_id', $company_id)
  386. ->whereIn('id', $employee_id_platform)
  387. ->where('duty_type', 1)
  388. ->where('status', 0);
  389. }
  390. });
  391. $first_responsible_person = $EmployeeModel->pluck('id')->implode(',');
  392. //当以上规则匹配不到责任人时,则去查询发货地区信息关联责任人
  393. if (trim($first_responsible_person) == '' && trim($product_data['shipment_city_id']) != '') {
  394. $employee_id_area = $EmployeeAreaModel->where('city_id', $product_data['city_id'])->pluck('employee_id')->toarray();
  395. if (!empty($employee_id_area)) {
  396. $where_city[] = ['company_id', '=', $company_id];
  397. $where_city[] = ['id', 'in', $employee_id_area];
  398. $where_city[] = ['status', '=', 0];
  399. $where_city[] = ['duty_type', '=', 1]; //责任类型1=第一责任人,2=责任人
  400. $EmployeeModel = new EmployeeModel();
  401. $first_responsible_person = $EmployeeModel->where($where_city)->pluck('id')->implode(',');
  402. }
  403. }
  404. //调试记录查询条件
  405. $insert_product_data['first_responsible_person'] = $first_responsible_person;
  406. //查询责任人
  407. $where_query1 = [];
  408. $where_query2 = [];
  409. $where_query3 = [];
  410. //查询指定公司责任人
  411. if (!empty($employee_id_list)) {
  412. $where_query1[] = ['company_id', '=', $company_id];
  413. $where_query1[] = ['id', 'in', $employee_id_list];
  414. $where_query1[] = ['status', '=', 0];
  415. $where_query1[] = ['duty_type', '=', 2]; //责任类型1=第一责任人,2=责任人
  416. }
  417. //查询地区配置的员工
  418. $employee_id_area = $EmployeeAreaModel->where('city_id', $product_data['city_id'])->pluck('employee_id')->toarray();
  419. if (!empty($employee_id_area)) {
  420. $where_query2[] = ['company_id', '=', $company_id];
  421. $where_query2[] = ['id', 'in', $employee_id_area];
  422. $where_query2[] = ['status', '=', 0];
  423. $where_query2[] = ['duty_type', '=', 2]; //责任类型1=第一责任人,2=责任人
  424. }
  425. //查询平台配置的员工
  426. $employee_id_platform = $EmployeePlatformModel->where('platform_id', $product_data['platform_id'])->pluck('employee_id')->toarray();
  427. if (!empty($employee_id_platform)) {
  428. $where_query3[] = ['company_id', '=', $company_id];
  429. $where_query3[] = ['id', 'in', $employee_id_platform];
  430. $where_query3[] = ['status', '=', 0];
  431. $where_query3[] = ['duty_type', '=', 2]; //责任类型1=第一责任人,2=责任人
  432. }
  433. //并行查询责任人
  434. $EmployeeModel = new EmployeeModel();
  435. $EmployeeModel = $EmployeeModel
  436. ->orWhere(function ($q) use ($employee_id_list, $company_id) {
  437. if (!empty($employee_id_list)) {
  438. $q->where('company_id', $company_id)
  439. ->orWhereIn('id', $employee_id_list)
  440. ->where('duty_type', 2)
  441. ->where('status', 0);
  442. }
  443. })->orWhere(function ($q) use ($employee_id_area, $company_id) {
  444. if (!empty($employee_id_area)) {
  445. $q->where('company_id', $company_id)
  446. ->whereIn('id', $employee_id_area)
  447. ->where('duty_type', 2)
  448. ->where('status', 0);
  449. }
  450. })->orWhere(function ($q) use ($employee_id_platform, $company_id) {
  451. if (!empty($employee_id_platform)) {
  452. $q->where('company_id', $company_id)
  453. ->whereIn('id', $employee_id_platform)
  454. ->where('duty_type', 2)
  455. ->where('status', 0);
  456. }
  457. });
  458. $responsible_person = $EmployeeModel->pluck('id')->implode(',');
  459. //当以上规则匹配不到责任人时,则去查询发货地区信息关联责任人
  460. if (trim($responsible_person) == '' && trim($product_data['shipment_city_id']) != '') {
  461. $employee_id_area = $EmployeeAreaModel->where('city_id', $product_data['city_id'])->pluck('employee_id')->toarray();
  462. if (!empty($employee_id_area)) {
  463. $where_city[] = ['company_id', '=', $company_id];
  464. $where_city[] = ['id', 'in', $employee_id_area];
  465. $where_city[] = ['status', '=', 0];
  466. $where_city[] = ['duty_type', '=', 2]; //责任类型1=第一责任人,2=责任人
  467. $EmployeeModel = new EmployeeModel();
  468. $responsible_person = $EmployeeModel->where($where_city)->pluck('id')->implode(',');
  469. }
  470. }
  471. $insert_product_data['responsible_person'] = $responsible_person;
  472. //溯源责任人
  473. $source_responsible_person = '';
  474. if ($first_responsible_person && $responsible_person) {
  475. //转换成数组,合并后在去重
  476. $first_responsible_person = explode(',', $first_responsible_person);
  477. $responsible_person = explode(',', $responsible_person);
  478. $source_responsible_person = array_unique(array_merge($first_responsible_person, $responsible_person));
  479. $source_responsible_person = ',' . implode(',', $source_responsible_person) . ',';
  480. } else if ($first_responsible_person) {
  481. $source_responsible_person = $first_responsible_person;
  482. } else if ($responsible_person) {
  483. $source_responsible_person = $responsible_person;
  484. }
  485. $insert_product_data['source_responsible_person'] = $source_responsible_person;
  486. }
  487. //插入数据
  488. $ViolationProductModel->addViolationProduct($insert_product_data);
  489. }
  490. //继续执行下一页
  491. $message_data['page'] = $page + 1;
  492. $message_data['limit'] = $limit;
  493. ViolationProductDataJobs::dispatch($message_data);
  494. }
  495. public function failed(\Throwable $exception)
  496. {
  497. Log::info('job_error', '数据清洗-违规挂网商品数据队列完全失败', ['data' => $this->message_data, 'error' => $exception]);
  498. }
  499. }