From 25f8017b3ca9e23af20f77c15b3d0f5ba7c53124 Mon Sep 17 00:00:00 2001 From: "WIN-C27C5MSVTPO\\16961" <1696136552@qq.com> Date: Thu, 30 Oct 2025 14:15:18 +0800 Subject: [PATCH] =?UTF-8?q?=E5=85=B3=E9=94=AE=E8=AF=8D=E6=8A=93=E5=8F=96?= =?UTF-8?q?=E8=BD=A6=E5=9E=8B=E5=90=8E=E5=88=9B=E5=BB=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/admin/service/BrandSeriesService.php | 100 +++++++++++++++++- .../controller/BrandSeriesController.php | 19 +++- config/route.php | 1 + 3 files changed, 118 insertions(+), 2 deletions(-) diff --git a/app/admin/service/BrandSeriesService.php b/app/admin/service/BrandSeriesService.php index 0cf4901..323e117 100644 --- a/app/admin/service/BrandSeriesService.php +++ b/app/admin/service/BrandSeriesService.php @@ -312,7 +312,6 @@ class BrandSeriesService return $msg; } - /** * 抓取车型数据(通过接口) * @param Request $request @@ -343,7 +342,16 @@ class BrandSeriesService // 获取品牌信息 $series = BrandSeries::where('series_name', $brandSeries['series'])->first(); $result['brand'] = ['brand_name' => $brandSeries['brand'], 'exists' => $brand?1:0]; + if($brand){ + $result['brand']['id'] = $brand['id']; + } $result['series'] = ['series_name' => $brandSeries['series'], 'exists' => $series?1:0]; + if($series){ + $result['series']['id'] = $series['id']; + if(empty($result['brand']['id'])){ + $result['brand']['id'] = $series['brand_id']; + } + } try { //获取车型数据 @@ -354,6 +362,96 @@ class BrandSeriesService } } + /** + * @param Request $request + * @return array|string + * @throws \Exception + */ + public function crawlKeywordCreate(Request $request) + { + $series_id = $request->input('series_id'); + $brand_id = $request->input('brand_id'); + $series_name = $request->input('series_name'); + $models = $request->input('models'); + + if(empty($brand_id)){ + throw new \Exception("请选择品牌"); + } + if(empty($series_id) && empty($series_name)){ + throw new \Exception("请选择车系"); + } + + if (empty($series_id)) { + $series = BrandSeries::where('brand_id', $brand_id)->where('series_name', $series_name)->first(); + if (empty($brandseries)) { //如果没有该车系则创建 + $seriesService = new \app\admin\service\BrandSeriesService(); + $seriesData = [ + 'brand_id' => $brand_id, + 'series_code' => $seriesService->generateUniqueModificationCode($brand_id), + 'series_name' => $series_name, + ]; + $series = CrudUtil::create(BrandSeries::class, $seriesData); + } + $series_id = $series->id; + }else{ + $series = BrandSeries::where('id', $series_id)->first(); + } + // 获取品牌信息 + $brand = Brand::where('id', $series->brand_id)->first(); + + // 保存车型数据 + $createdCount = 0; + $repeat = 0; + $fail = 0; + $msg = ''; + + foreach ($models as $item) { + $modelData = [ + 'brand_code' => $brand->brand_code, + 'brand_name' => $brand->brand_name, + 'series_code' => $series->series_code, + 'series_name' => $series->series_name, + 'model_name' => $item['title'], + 'model_year' => $item['year'], + 'msrp' => $item['msrp'], + ]; + //指纹数组 + $fingerprintArr = [ + 'brand_name' => $modelData['brand_name'], + 'series_name' => $modelData['series_name'], + 'model_name' => $modelData['model_name'], + 'model_year' => $modelData['model_year'], + 'msrp' => $modelData['msrp'], + ]; + // 生成指纹 + $modelData['fingerprint'] = Fingerprint::makeFromArray($fingerprintArr); + + $modelData['model_code'] = ModelNaming::buildModelCode($modelData); + $modelData['sku_code'] = SkuCodeBuilder::build($modelData); + + try { + $model = VehVehicleSku::where('brand_name', $brand->brand_name)->where('series_name', $series->series_name)->where('model_name', $item['title'])->first(); + if (!empty($model)) { + $repeat++; + continue; + } + CrudUtil::create(VehVehicleSku::class, $modelData); + $createdCount++; + } catch (\Exception $e) { + // 记录错误但继续处理其他车型 + $msg .= "创建车型失败: " . $e->getMessage() . "\n"; + Log::error("创建车型失败:" . $msg, ['series_id' => $series_id, 'model' => $item]); + $fail++; + } + } + + Log::info('series-crawl-' . $series_id . ' ' . $msg); + $msg = '创建车型成功' . $createdCount . '条'; + $msg .= (!empty($repeat)) ? ',重复' . $repeat . '条' : ''; + $msg .= (!empty($fail)) ? ',失败' . $fail . '条' : ''; + return $msg; + } + /** * 获取车系下车型数据 * @return array diff --git a/app/common/controller/BrandSeriesController.php b/app/common/controller/BrandSeriesController.php index 61c1b37..3cd32d3 100644 --- a/app/common/controller/BrandSeriesController.php +++ b/app/common/controller/BrandSeriesController.php @@ -83,7 +83,6 @@ class BrandSeriesController extends BaseController } } - /** * 关键词抓取品牌、车系、车型 * @param Request $request @@ -101,4 +100,22 @@ class BrandSeriesController extends BaseController return $this->error($e->getMessage()); } } + + /** + * 保存抓取的系列车型 + * @param Request $request + * @return \support\Response + */ + public function crawlKeywordCreate(Request $request){ + try { + $result = $this->service->crawlKeywordCreate($request); + if($result){ + return $this->success($result,'成功'); + }else{ + return $this->error('创建失败'); + } + } catch (\Exception $e) { + return $this->error($e->getMessage()); + } + } } \ No newline at end of file diff --git a/config/route.php b/config/route.php index 6fa5100..fb407ac 100644 --- a/config/route.php +++ b/config/route.php @@ -110,6 +110,7 @@ Route::group('/common', function () { Route::any('/crawlModels', [app\common\controller\BrandSeriesController::class, 'crawlModels']); Route::any('/crawlModelCreate', [app\common\controller\BrandSeriesController::class, 'crawlModelCreate']); Route::any('/crawlKeyword', [app\common\controller\BrandSeriesController::class, 'crawlKeyword']); + Route::any('/crawlKeywordCreate', [app\common\controller\BrandSeriesController::class, 'crawlKeywordCreate']); }); });