ViolationProductDataJobs.php 34 KB

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