ViolationProductDataJobs.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  1. <?php
  2. namespace App\Jobs\Manager\Process;
  3. use Illuminate\Bus\Queueable;
  4. use Illuminate\Contracts\Queue\ShouldBeUnique;
  5. use Illuminate\Contracts\Queue\ShouldQueue;
  6. use Illuminate\Foundation\Bus\Dispatchable;
  7. use Illuminate\Queue\InteractsWithQueue;
  8. use Illuminate\Queue\SerializesModels;
  9. use App\Facades\Servers\Logs\Log;
  10. use App\Models\Manager\Process\ViolationProduct as ViolationProductModel;
  11. use App\Models\Manager\Personnel\Employee as EmployeeModel;
  12. use App\Models\Api\Process\ExecuteLog as ExecuteLogModel;
  13. use App\Models\Manager\Process\ScrapeData as ScrapeDataModel;
  14. use App\Models\Manager\WashConfig\ViolationStore as ViolationStoreModel;
  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 ViolationProductDataJobs 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->getViolationProductData($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 getViolationProductData($message_data)
  60. {
  61. $CitysModel = new CitysModel();
  62. $EmployeeModel = new EmployeeModel();
  63. $ViolationProductModel = new ViolationProductModel();
  64. $ScrapeDataModel = new ScrapeDataModel();
  65. $ViolationStoreModel = new ViolationStoreModel();
  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. $enable_full_quantity = $message_data['enable_full_quantity']; //是否启用全量采集0=是 1=否
  72. $product_specs = $message_data['product_specs']; //商品规格
  73. $store_scope = $message_data['store_scope']; //店铺范围1=全部店铺2=指定店铺
  74. $company_scope = $message_data['company_scope']; //公司范围1=全部公司2=指定公司
  75. $social_credit_code = $message_data['social_credit_code']; //社会信用代码
  76. $executeLog_id = $message_data['executeLog_id'];
  77. $category_name = $message_data['category_name']; //产品分类名称
  78. $specify_responsible_person = $message_data['specify_responsible_person']; //指派责任人 0=开启 1=关闭
  79. $limit = isset($message_data['limit']) ? $message_data['limit'] : 50;
  80. $page = isset($message_data['page']) ? $message_data['page'] : 1;
  81. $company_id = isset($message_data['company_id']) ? $message_data['company_id'] : 0;//品牌方公司ID
  82. $product_brand = $message_data['product_brand']; //商品品牌名称
  83. $product_keywords = $message_data['product_keyword']; //关键字
  84. $item_totle_page = $message_data['item_totle_page']; //清洗商品总页数
  85. $item_now_page = $message_data['item_now_page']; //清洗商品当前页
  86. $where = [];
  87. if ($platform != '0' && !empty($platform)) {
  88. $platform = explode(',', $platform);
  89. $ScrapeDataModel = $ScrapeDataModel->whereIn('platform_id', $platform);
  90. }
  91. $yesterdayStart = Carbon::today()->subDays(6)->startOfDay()->getTimestamp(); // 最近7天开始时间 00:00:00
  92. // $yesterdayStart = Carbon::yesterday()->startOfDay()->getTimestamp(); // 昨天开始时间 00:00:00
  93. $yesterdayEnd = Carbon::yesterday()->endOfDay()->getTimestamp(); // 昨天结束时间 23:59:59
  94. $yesterdayStart = date('Y-m-d', $yesterdayStart);
  95. $yesterdayEnd = date('Y-m-d', $yesterdayEnd);
  96. $where[] = ['scrape_date', '>=', $yesterdayStart];
  97. $where[] = ['scrape_date', '<=', $yesterdayEnd];
  98. $where[] = ['product_name', 'like', '%' . $product_name . '%'];
  99. if($enable_full_quantity == 1) $where[] = ['product_specs', 'like', '%' . $product_specs . '%']; //如果不是全量清洗,则查询规格
  100. if ($product_brand) $where[] = ['product_name', 'like', '%' . $product_brand . '%'];
  101. if (!empty($product_keyword)) {
  102. foreach ($product_keywords as $key => $keywords) {
  103. if (trim($keywords) == '') continue;
  104. $where[] = ['product_name', 'like', '%' . $keywords . '%'];
  105. }
  106. }
  107. if ($company_scope == 2) {
  108. $ScrapeDataModel->whereIn('qualification_number', $social_credit_code);
  109. }
  110. $product_data_info = $ScrapeDataModel->where($where)->paginate($limit, ['*'], 'page', $page)->toarray();
  111. $product_datas = $product_data_info['data'];
  112. if (empty($product_datas)) {
  113. if ($executeLog_id && $item_now_page >= $item_totle_page) {
  114. (new ExecuteLogModel())->where('id', $executeLog_id)->update(['status' => 0,'update_time' => time()]);
  115. }
  116. return true;
  117. }
  118. foreach ($product_datas as $product_data) {
  119. $province_name = $product_data['province_name'];
  120. $city_name = $product_data['city_name'];
  121. //特殊地区1级移除市
  122. if ($province_name && in_array($province_name, ['北京市', '天津市', '上海市', '重庆市'])) {
  123. //移除市这个字符
  124. $province_name = trim(str_replace('市', '', $province_name));
  125. } else if ($province_name && in_array($province_name, ['北京', '天津', '上海', '重庆'])) {
  126. } else if ($province_name && in_array($province_name, ['内蒙古', '广西', '西藏', '新疆', '宁夏'])) {
  127. switch ($province_name) {
  128. case '内蒙古':
  129. $province_name = '内蒙古自治区';
  130. break;
  131. case '广西':
  132. $province_name = '广西壮族自治区';
  133. break;
  134. case '西藏':
  135. $province_name = '西藏自治区';
  136. break;
  137. case '新疆':
  138. $province_name = '新疆维吾尔自治区';
  139. break;
  140. case '宁夏':
  141. $province_name = '宁夏回族自治区';
  142. break;
  143. }
  144. } else if ($province_name && in_array($province_name, ['内蒙古自治区', '广西壮族自治区', '西藏自治区', '新疆维吾尔自治区', '宁夏回族自治区'])) {
  145. } else if (strpos($province_name, '省') === false) {
  146. //是否存在市省,如果不存在则补全
  147. if (trim($province_name) != '' && strpos($province_name, '省') === false) {
  148. $province_name = $province_name . '省';
  149. }
  150. }
  151. if ($city_name != '') {
  152. $city_name_value = $CitysModel->where([['id', '=', $product_data['city_id']], ['level', '=', '2']])->value('name');
  153. $city_name = !empty($city_name_value) ? $city_name_value : '';
  154. }
  155. $insert_product_data = [
  156. 'company_id' => $company_id,
  157. 'source_id' => $product_data['id'],
  158. 'platform' => $product_data['platform_id'],
  159. 'company_name' => $product_data['company_name'],
  160. 'product_brand' => $product_data['product_brand'],
  161. 'product_name' => $product_name,
  162. 'product_specs' => $product_specs,
  163. 'inventory' => $product_data['inventory'] ? $product_data['inventory'] : '',
  164. 'sales' => $product_data['sales'] ? $product_data['sales'] : '',
  165. 'snapshot_url' => $product_data['snapshot_url'] ? $product_data['snapshot_url'] : '',
  166. 'online_posting_count' => $product_data['online_posting_count'],
  167. 'continuous_listing_count' => $product_data['continuous_listing_count'],
  168. 'link_url' => $product_data['link_url'],
  169. 'store_name' => $product_data['store_name'],
  170. 'social_credit_code' => $product_data['qualification_number'],
  171. 'province_id' => $product_data['province_id'],
  172. 'province_name' => $province_name,
  173. 'city_id' => $product_data['city_id'],
  174. 'city_name' => $city_name,
  175. 'area_info' => $product_data['area_info'],
  176. 'category_name' => $category_name,
  177. 'company_category_name' => '',
  178. 'first_responsible_person' => '',
  179. 'responsible_person' => '',
  180. 'source_responsible_person' => '',
  181. 'scrape_date' => $product_data['scrape_date'],
  182. 'shipment_province_id' => $product_data['shipment_province_id'],
  183. 'shipment_province_name' => $product_data['shipment_province_name'],
  184. 'shipment_city_id' => $product_data['shipment_city_id'],
  185. 'shipment_city_name' => $product_data['shipment_city_name'],
  186. ];
  187. //获取公司绑定责任人信息
  188. $company_data = $ViolationStoreModel->leftjoin('washconfig_company_category', 'washconfig_company_category.id', '=', 'washconfig_violation_store.category_id')
  189. ->where('washconfig_violation_store.social_credit_code', $product_data['qualification_number'])
  190. ->where('washconfig_violation_store.company_id', $company_id)
  191. ->select(['washconfig_violation_store.id', 'washconfig_company_category.name as category_name'])->first();
  192. $employee_id_list = [];
  193. if ($company_data) {
  194. $employee_id_list = $ViolationCompanyMemberModel->where('company_logid', $company_data->id)->pluck('employee_id')->toarray();
  195. $insert_product_data['company_category_name'] = $company_data->category_name ? $company_data->category_name : '';
  196. }
  197. //获取指定人员信息
  198. if ($specify_responsible_person == 0) {
  199. $where_query1 = [];
  200. $where_query2 = [];
  201. $where_query3 = [];
  202. //查询指定公司第一责任人
  203. if (!empty($employee_id_list)) {
  204. $where_query1[] = ['company_id', '=', $company_id];
  205. $where_query1[] = ['id', 'in', $employee_id_list];
  206. $where_query1[] = ['status', '=', 0];
  207. $where_query1[] = ['duty_type', '=', 1]; //责任类型1=第一责任人,2=责任人
  208. }
  209. //查询地区配置的第一责任人
  210. $employee_id_area = $EmployeeAreaModel->where('city_id', $product_data['city_id'])->pluck('employee_id')->toarray();
  211. if (!empty($employee_id_area)) {
  212. $where_query2[] = ['company_id', '=', $company_id];
  213. $where_query2[] = ['id', 'in', $employee_id_area];
  214. $where_query2[] = ['status', '=', 0];
  215. $where_query2[] = ['duty_type', '=', 1]; //责任类型1=第一责任人,2=责任人
  216. }
  217. //查询平台配置的第一责任人
  218. $employee_id_platform = $EmployeePlatformModel->where('platform_id', $product_data['platform_id'])->pluck('employee_id')->toarray();
  219. if (!empty($employee_id_platform)) {
  220. $where_query3[] = ['company_id', '=', $company_id];
  221. $where_query3[] = ['id', 'in', $employee_id_platform];
  222. $where_query3[] = ['status', '=', 0];
  223. $where_query3[] = ['duty_type', '=', 1]; //责任类型1=第一责任人,2=责任人
  224. }
  225. //并行查询第一责任人
  226. $EmployeeModel = $EmployeeModel->where(function ($q) use ($employee_id_list, $company_id) {
  227. if (!empty($employee_id_list)) {
  228. $q->where('company_id', $company_id)
  229. ->orWhereIn('id', $employee_id_list)
  230. ->where('status', 0)
  231. ->where('duty_type', 1);
  232. }
  233. })->orWhere(function ($q) use ($employee_id_area, $company_id) {
  234. if (!empty($employee_id_area)) {
  235. $q->where('company_id', $company_id)
  236. ->whereIn('id', $employee_id_area)
  237. ->where('status', 0)
  238. ->where('duty_type', 1);
  239. }
  240. })->orWhere(function ($q) use ($employee_id_platform, $company_id) {
  241. if (!empty($employee_id_platform)) {
  242. $q->where('company_id', $company_id)
  243. ->whereIn('id', $employee_id_platform)
  244. ->where('status', 0)
  245. ->where('duty_type', 1);
  246. }
  247. });
  248. $first_responsible_person = $EmployeeModel->pluck('id')->implode(',');
  249. //当以上规则匹配不到责任人时,则去查询发货地区信息关联责任人
  250. if (trim($first_responsible_person) == '' && trim($product_data['shipment_city_id']) !='') {
  251. $employee_id_area = $EmployeeAreaModel->where('city_id', $product_data['city_id'])->pluck('employee_id')->toarray();
  252. if (!empty($employee_id_area)) {
  253. $where_city[] = ['company_id', '=', $company_id];
  254. $where_city[] = ['id', 'in', $employee_id_area];
  255. $where_city[] = ['status', '=', 0];
  256. $where_city[] = ['duty_type', '=', 1]; //责任类型1=第一责任人,2=责任人
  257. $EmployeeModel = new EmployeeModel();
  258. $first_responsible_person = $EmployeeModel->where($where_city)->pluck('id')->implode(',');
  259. }
  260. }
  261. //调试记录查询条件
  262. $insert_product_data['first_responsible_person'] = $first_responsible_person;
  263. //查询责任人
  264. $where_query1 = [];
  265. $where_query2 = [];
  266. $where_query3 = [];
  267. //查询指定公司责任人
  268. if (!empty($employee_id_list)) {
  269. $where_query1[] = ['company_id', '=', $company_id];
  270. $where_query1[] = ['id', 'in', $employee_id_list];
  271. $where_query1[] = ['status', '=', 0];
  272. $where_query1[] = ['duty_type', '=', 2]; //责任类型1=第一责任人,2=责任人
  273. }
  274. //查询地区配置的员工
  275. $employee_id_area = $EmployeeAreaModel->where('city_id', $product_data['city_id'])->pluck('employee_id')->toarray();
  276. if (!empty($employee_id_area)) {
  277. $where_query2[] = ['company_id', '=', $company_id];
  278. $where_query2[] = ['id', 'in', $employee_id_area];
  279. $where_query2[] = ['status', '=', 0];
  280. $where_query2[] = ['duty_type', '=', 2]; //责任类型1=第一责任人,2=责任人
  281. }
  282. //查询平台配置的员工
  283. $employee_id_platform = $EmployeePlatformModel->where('platform_id', $product_data['platform_id'])->pluck('employee_id')->toarray();
  284. if (!empty($employee_id_platform)) {
  285. $where_query3[] = ['company_id', '=', $company_id];
  286. $where_query3[] = ['id', 'in', $employee_id_platform];
  287. $where_query3[] = ['status', '=', 0];
  288. $where_query3[] = ['duty_type', '=', 2]; //责任类型1=第一责任人,2=责任人
  289. }
  290. //并行查询责任人
  291. $EmployeeModel = new EmployeeModel();
  292. $EmployeeModel = $EmployeeModel->where(function ($q) use ($employee_id_list, $company_id) {
  293. if (!empty($employee_id_list)) {
  294. $q->where('company_id', $company_id)
  295. ->orWhereIn('id', $employee_id_list)
  296. ->where('status', 0)
  297. ->where('duty_type', 2);//责任类型1=第一责任人,2=责任人
  298. }
  299. })->orWhere(function ($q) use ($employee_id_area, $company_id) {
  300. if (!empty($employee_id_area)) {
  301. $q->where('company_id', $company_id)
  302. ->whereIn('id', $employee_id_area)
  303. ->where('status', 0)
  304. ->where('duty_type', 2);//责任类型1=第一责任人,2=责任人
  305. }
  306. })->orWhere(function ($q) use ($employee_id_platform, $company_id) {
  307. if (!empty($employee_id_platform)) {
  308. $q->where('company_id', $company_id)
  309. ->whereIn('id', $employee_id_platform)
  310. ->where('status', 0)
  311. ->where('duty_type', 2);//责任类型1=第一责任人,2=责任人
  312. }
  313. });
  314. $responsible_person = $EmployeeModel->pluck('id')->implode(',');
  315. //当以上规则匹配不到责任人时,则去查询发货地区信息关联责任人
  316. if (trim($responsible_person) == '' && trim($product_data['shipment_city_id']) !='') {
  317. $employee_id_area = $EmployeeAreaModel->where('city_id', $product_data['city_id'])->pluck('employee_id')->toarray();
  318. if (!empty($employee_id_area)) {
  319. $where_city[] = ['company_id', '=', $company_id];
  320. $where_city[] = ['id', 'in', $employee_id_area];
  321. $where_city[] = ['status', '=', 0];
  322. $where_city[] = ['duty_type', '=', 2]; //责任类型1=第一责任人,2=责任人
  323. $EmployeeModel = new EmployeeModel();
  324. $responsible_person = $EmployeeModel->where($where_city)->pluck('id')->implode(',');
  325. }
  326. }
  327. $insert_product_data['responsible_person'] = $responsible_person;
  328. //溯源责任人
  329. $source_responsible_person = '';
  330. if ($first_responsible_person && $responsible_person) {
  331. //转换成数组,合并后在去重
  332. $first_responsible_person = explode(',', $first_responsible_person);
  333. $responsible_person = explode(',', $responsible_person);
  334. $source_responsible_person = array_unique(array_merge($first_responsible_person, $responsible_person));
  335. $source_responsible_person = ','.implode(',', $source_responsible_person).',';
  336. } else if ($first_responsible_person) {
  337. $source_responsible_person = $first_responsible_person;
  338. } else if ($responsible_person) {
  339. $source_responsible_person = $responsible_person;
  340. }
  341. $insert_product_data['source_responsible_person'] = $source_responsible_person;
  342. }
  343. //插入数据
  344. $ViolationProductModel->addViolationProduct($insert_product_data);
  345. }
  346. //继续执行下一页
  347. $message_data['page'] = $page + 1;
  348. $message_data['limit'] = $limit;
  349. ViolationProductDataJobs::dispatch($message_data);
  350. }
  351. public function failed(\Throwable $exception)
  352. {
  353. Log::info('job_error', '数据清洗-违规挂网商品数据队列完全失败', ['data' => $this->message_data, 'error' => $exception]);
  354. }
  355. }