ViolationCompanyDataJobs.php 31 KB

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