LowPriceGoodsDataJobs.php 31 KB

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