关键词抓取车型后创建
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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']);
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
Reference in New Issue
Block a user