LowPriceGoodsDataJobs.php 30 KB

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