LowPriceGoodsDataJobs.php 31 KB

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