From c74a1b7360fa5d187403925e9c8db7a0505ec54e Mon Sep 17 00:00:00 2001 From: lqmac Date: Fri, 7 Jun 2024 17:36:38 +0800 Subject: [PATCH] 1 --- app/job/service/goods/Collector.php | 106 ++++++++++++++++++++++++---- app/store/model/goods/Import.php | 10 ++- 2 files changed, 102 insertions(+), 14 deletions(-) diff --git a/app/job/service/goods/Collector.php b/app/job/service/goods/Collector.php index 6a73cb99..164df64b 100644 --- a/app/job/service/goods/Collector.php +++ b/app/job/service/goods/Collector.php @@ -132,7 +132,74 @@ class Collector extends BaseService $this->updateRecord($recordId, \count($urls)); return true; } - + /** + * 后台单个采集 + * [single description] + * @param string $url [description] + * @param array $form [description] + * @param int $storeId [description] + * @return [type] [description] + */ + public function single1(string $url, array $form, int $storeId): bool + { + try { + //var_dump($url); + // 采集第三方商品数据 + $original = $this->collector1($url, $storeId); + if ($original['spec_type'] == 20) { + $original['spec_type'] = 10; + unset($original['specData']); + + } + $original['goods_price'] = $form['goods_price'] ?? 0.00; + $original['line_price'] = $form['goods_price'] ?? 0.00; + $original['cost_price'] = $form['cost_price'] ?? 0.00; + $original['stock_num'] = $form['stock_num'] ?? 0; + $original['remark'] = $form['remark'] ?? ""; + $original['cmmdty_model'] = $form['cmmdty_model'] ?? ""; + $original['goods_no'] = $form['goods_no'] ?? ""; + $original['region'] = $form['region'] ?? ""; + $original['region_text'] = $form['region_text'] ?? ""; + $original['delivery_time'] = $form['delivery_time'] ?? ""; + $original['is_check'] = $form['is_check'] ?? 0; + $original['goods_source'] = $form['goods_source'] ?? ""; + $original['data_type'] = 1; + $original['link'] = $url; + // echo "
";
+            // print_r($original);
+            // exit();
+            // 下载远程商品图片
+            $original = $this->thirdPartyImages($original, $form['imageStorage'], $storeId);
+        } catch (\Throwable $e) {
+            // var_dump($e->getMessage());
+            // exit;
+            tre($e->getTraceAsString());
+            $this->errorLog[] = ['url' => trim($url), 'message' => $e->getMessage()];
+            return false;
+        }
+        // 生成商品数据(用于写入数据库)
+        $data = $this->singleCreateData($original, $form, $storeId);
+        // echo "
";
+        // print_r($data);
+        // exit();
+        // 事务处理:添加商品
+        $model = new GoodsModel();
+        $model->transaction(function () use ($model, $data, $storeId) {
+            // 添加商品
+            $model->save($data);
+            $model->where('goods_id', (int)$model['goods_id'])->update(['spu_id' => (int)$model['goods_id']]);
+            // 新增商品与分类关联
+            GoodsCategoryRelModel::increased((int)$model['goods_id'], $data['categoryIds'], $storeId);
+            // 新增商品与图片关联
+            GoodsImageModel::increased((int)$model['goods_id'], $data['imagesIds'], $storeId);
+            // 新增商品与规格关联
+            GoodsSpecRelModel::increased((int)$model['goods_id'], $data['newSpecList'], $storeId);
+            // 新增商品sku信息
+            GoodsSkuModel::add((int)$model['goods_id'], $data['spec_type'], $data['newSkuList'], $storeId);
+        });
+           
+        return true;
+    }
     /**
      * 后台单个采集
      * [single description]
@@ -150,7 +217,9 @@ class Collector extends BaseService
             $original = $this->collector($url, $storeId);
             if ($original['spec_type'] == 20) {
                 $original['spec_type'] = 10;
-                unset($original['specData']);
+                if (isset($original['specData'])) {
+                    unset($original['specData']);
+                }
                 
             }
             $original['goods_price'] = $form['goods_price'] ?? 0.00;
@@ -232,15 +301,20 @@ class Collector extends BaseService
 
             if ($original['spec_type'] == 20) {
                 $original['spec_type'] = 10;
-
-                $skuList = array_column($original['specData']['skuList'], null, "goods_sku_no");
-                $goods_price = $skuList[$original['goods_sku_no']]['goods_price'] ?? 0;
+                //兼容99api和lt数据源
+                if (isset($original['specData'])) {
+                    $skuList = array_column($original['specData']['skuList'], null, "goods_sku_no");
+                    $goods_price = $skuList[$original['goods_sku_no']]['goods_price'] ?? 0;
+                    unset($original['specData']);
+                } else{
+                    $goods_price = $original['goods_price'];
+                }
                 
                 $original['goods_price'] = $goods_price;
                 $original['line_price'] = $goods_price;
                 $original['data_type'] = 1;//苏宁的数据变成的人工数据了,不再使用苏宁接口更新数据
                 $original['link_other'] = $url;
-                unset($original['specData']);
+                
                 
             }
             $form['imageStorage'] = 20;
@@ -331,7 +405,7 @@ class Collector extends BaseService
         return $model['status'] == GoodsCollectorStatusEnum::FAIL || $model['is_delete'];
     }
     /**
-     * 抓取第三方商品内容
+     * 抓取第三方商品内容-lt数据源
      * @param string $url
      * @param int $storeId
      * @return array
@@ -340,19 +414,28 @@ class Collector extends BaseService
      * @throws \think\db\exception\DbException
      * @throws \think\db\exception\ModelNotFoundException
      */
-    public function collector1(string $url, int $storeId): array
+    public function collector(string $url, int $storeId): array
     {
         // 获取商品来源
         $store = $this->getStore($url);
         // 获取url中的商品ID
         $itemId = $this->getItemId($url, $store);
-        getJdGoodsBySku
+        $res = getJdGoodsBySku([$itemId]);
+        $item = $res[$itemId] ?? [];
+        $item['spec_type'] = 20;//设置默认走多规格的逻辑
+        $item['goodsImages'] = $item['mainImages'];
+        $item['goods_price'] = $item['proxyPrice'];
+        $item['goods_name'] = $item['name'];
+        $item['imageStorage'] = 10;
 
+        // echo "
";
+        // print_r($item);
+        //exit();
         $item['goods_sku_no'] = $itemId;
         return $item;
     }
     /**
-     * 抓取第三方商品内容
+     * 抓取第三方商品内容-99api数据源
      * @param string $url
      * @param int $storeId
      * @return array
@@ -361,7 +444,7 @@ class Collector extends BaseService
      * @throws \think\db\exception\DbException
      * @throws \think\db\exception\ModelNotFoundException
      */
-    public function collector(string $url, int $storeId): array
+    public function collector1(string $url, int $storeId): array
     {
         // 获取商品来源
         $store = $this->getStore($url);
@@ -470,7 +553,6 @@ class Collector extends BaseService
             'data_type' => $original['data_type'],//数据类型
             'link' => $original['link'],//采集地址
             'spec_type' => $original['spec_type'],
-            'unicode' => $original['spec_type'],
             'delivery_id' => $form['delivery_id'] ?? 0,
             'content' => $original['content'] ?? '',
             'sort' => 100,
diff --git a/app/store/model/goods/Import.php b/app/store/model/goods/Import.php
index b84b6819..42cd600b 100644
--- a/app/store/model/goods/Import.php
+++ b/app/store/model/goods/Import.php
@@ -142,10 +142,12 @@ class Import extends ImportModel
         foreach ($execlData as &$value) {
             $value['channel'] = $form['channel']??'zy';
         }
+
+
         // $obj = new \app\job\service\goods\GoodsStoreImport();
         // $service = new \app\job\service\goods\Collector();
         // foreach ($execlData as $item) {
-        //     $info = \app\common\model\Goods::where('goods_no', $item['C'])->where('store_id', self::$storeId)->where('channel', $item['channel'])->where('is_delete',0)->find();
+        //     $info = \app\common\model\Goods::where('goods_no', $item['C'])->where('store_id', self::$storeId)->where('merchant_id', self::$merchantId)->where('channel', $item['channel'])->where('is_delete',0)->find();
         //     // var_dump($item['C']);
         //     // var_dump($info);
         //     if ($info) {
@@ -156,12 +158,16 @@ class Import extends ImportModel
         //     //var_dump($info->goods_id);
         //     //exit();
         //     $data = $obj->createData($item, self::$storeId);
-        //     $service->single($item['D'], $data, self::$storeId);
+        //     $service->single1($item['D'], $data, self::$storeId);
+        //     exit();
         //     // // 记录导入成功
         //     // $this->successCount++;
         // }
         // var_dump(\count($execlData));
         // exit();
+        
+
+
         // 新增商品导入记录
         $recordId = $this->addRecord(\count($execlData), $form['channel'] ?? 'zy');