Pārlūkot izejas kodu

feat(AlibabaAgent): 添加1688商品分类追溯功能

- 实现根据1688分类ID追溯顶级分类的逻辑
- 添加分类信息查询和层级遍历功能
- 实现顶级分类信息保存到store_category表
- 新增分类不存在时的创建和路径设置逻辑
- 将商品数据中的cate_id字段关联到追溯的分类ID
shichen 2 mēneši atpakaļ
vecāks
revīzija
54ab2062a8

+ 87 - 1
app/services/ThirdParty/AlibabaAgent/ProductService.php

@@ -448,6 +448,92 @@ class ProductService extends AlibabaAgentBaseService
448 448
                 $costPrice = $prices;
449 449
             }
450 450
 
451
+            // ============================================================
452
+            // 分类处理:根据1688分类ID追溯顶级分类,保存到 store_category 表
453
+            // ============================================================
454
+            $categoryId = $productInfo['categoryId'] ?? 0;
455
+            $storeCategoryId = 0;
456
+
457
+            if (!empty($categoryId)) {
458
+                // 追溯顶级分类
459
+                $currentCategoryId = $categoryId;
460
+                $maxLevels = 10;
461
+                $level = 0;
462
+                $topAlibabaCateId = 0;
463
+                $topAlibabaCateName = '';
464
+
465
+                while ($currentCategoryId > 0 && $level < $maxLevels) {
466
+                    $level++;
467
+                    $categoryInfoList = $this->getCategories((string)$currentCategoryId);
468
+
469
+                    if ($categoryInfoList === false || empty($categoryInfoList)) {
470
+                        break;
471
+                    }
472
+
473
+                    // 查找与当前分类ID匹配的分类信息
474
+                    $currentCategoryInfo = null;
475
+                    foreach ($categoryInfoList as $cat) {
476
+                        if (($cat['categoryID'] ?? 0) == $currentCategoryId) {
477
+                            $currentCategoryInfo = $cat;
478
+                            break;
479
+                        }
480
+                    }
481
+                    if ($currentCategoryInfo === null) {
482
+                        $currentCategoryInfo = $categoryInfoList[0] ?? null;
483
+                    }
484
+                    if ($currentCategoryInfo === null) {
485
+                        break;
486
+                    }
487
+
488
+                    $catId = $currentCategoryInfo['categoryID'] ?? 0;
489
+                    $catName = $currentCategoryInfo['name'] ?? '';
490
+                    $parentIDs = $currentCategoryInfo['parentIDs'] ?? [];
491
+
492
+                    // 记录顶级分类信息
493
+                    $topAlibabaCateId = $catId;
494
+                    $topAlibabaCateName = $catName;
495
+
496
+                    if (empty($parentIDs) || in_array(0, $parentIDs, true) || in_array('0', $parentIDs, true)) {
497
+                        break;
498
+                    }
499
+
500
+                    $currentCategoryId = (int)$parentIDs[0];
501
+                }
502
+
503
+                // 保存顶级分类到 store_category 表
504
+                if (!empty($topAlibabaCateId)) {
505
+                    $existingCategory = Db::name('store_category')
506
+                        ->where('alibaba_cate_id', $topAlibabaCateId)
507
+                        ->find();
508
+
509
+                    if ($existingCategory) {
510
+                        $storeCategoryId = $existingCategory['store_category_id'];
511
+                    } else {
512
+                        $insertData = [
513
+                            'pid'             => 564,
514
+                            'cate_name'       => $topAlibabaCateName,
515
+                            'path'            => '',
516
+                            'sort'            => 0,
517
+                            'pic'             => '',
518
+                            'is_show'         => 1,
519
+                            'level'           => 1,
520
+                            'mer_id'          => 0,
521
+                            'create_time'     => date('Y-m-d H:i:s'),
522
+                            'alibaba_cate_id' => $topAlibabaCateId,
523
+                        ];
524
+
525
+                        $storeCategoryId = Db::name('store_category')->insertGetId($insertData);
526
+
527
+                        if ($storeCategoryId) {
528
+                            $path = '/563/564/';
529
+                            Db::name('store_category')
530
+                                ->where('store_category_id', $storeCategoryId)
531
+                                ->update(['path' => $path]);
532
+                        }
533
+                    }
534
+                }
535
+            }
536
+
451 537
             // 7. 写入 store_product 表
452 538
             $productData = [
453 539
                 'mer_id'         => $merId,
@@ -458,7 +544,7 @@ class ProductService extends AlibabaAgentBaseService
458 544
                 'keyword'        => mb_substr($title, 0, 10),
459 545
                 'is_show'        => 0,
460 546
                 'status'         => 1,
461
-                'cate_id'        => 0,
547
+                'cate_id'        => $storeCategoryId,
462 548
                 'unit_name'      => $productInfo['unit'] ?? '件',
463 549
                 'price'          => $minPrice,
464 550
                 'cost'           => $costPrice,