车辆品牌与抓取车系

This commit is contained in:
2025-10-10 16:48:33 +08:00
parent 3091944e6a
commit 4fe008354d
9 changed files with 711 additions and 9 deletions
+77 -3
View File
@@ -3,9 +3,14 @@
namespace app\admin\service;
use app\admin\validate\BrandValidate;
use app\common\model\BrandSeries;
use app\common\service\CrawlService;
use app\common\traits\crud\CrudServiceTrait;
use app\common\model\Brand;
use app\common\utils\CrudUtil;
use support\Log;
use support\Request;
use Webman\RedisQueue\Redis;
class BrandService
{
@@ -27,7 +32,7 @@ class BrandService
*/
public function getValidator()
{
return new $this->validator;
return new $this->validator;
}
/**
@@ -49,13 +54,13 @@ class BrandService
// ====== ====== 筛选 ====== ======
// 状态
if (isset($p['status']) && in_array($p['status'], [0,1])) {
if (isset($p['status']) && in_array($p['status'], [0, 1])) {
$config['where']['status'] = $p['status'];
}
// 关键字
if (isset($p['keyword']) && $p['keyword']) {
$k = '%' . $p['keyword'] . '%';
foreach (['brand_code','brand_name'] as $col) {
foreach (['brand_code', 'brand_name'] as $col) {
$config['orWhere'][] = [$col, 'like', $k];
}
}
@@ -77,4 +82,73 @@ class BrandService
return $config;
}
public function crawlSeries(Request $request)
{
$brand_id = $request->get('brand_id');
// 验证品牌是否存在
$brand = Brand::where('id', $brand_id)->first();
if (!$brand) {
throw new \Exception("品牌ID {$brand_id} 不存在");
}
// 获取品牌信息
try {
// 执行抓取
$crawlService = new CrawlService();
$series = $crawlService->brandSeries($brand->brand_name);
if (empty($series)) {
throw new \Exception("未抓取到品牌 {$brand->brand_name} 的系列数据");
}
return ['series' => $series];
} catch (\Throwable $th) {
Log::error($th);
throw $th;
}
}
public function crawlSeriesCreate(Request $request)
{
$brand_id = $request->input('brand_id');
$series = $request->input('series');
// 保存系列数据
$seriesService = new \app\admin\service\BrandSeriesService();
$createdCount = 0;
$repeat = 0;
$fail = 0;
$msg = '';
foreach ($series as $item) {
$seriesData = [
'brand_id' => $brand_id,
'series_code' => $seriesService->generateUniqueModificationCode($brand_id),
'series_name' => $item,
];
try {
$brandseries = BrandSeries::where('brand_id', $brand_id)->where('series_name', $item)->first();
if (!empty($brandseries)) {
$repeat++;
continue;
}
CrudUtil::create(BrandSeries::class, $seriesData);
$createdCount++;
} catch (\Exception $e) {
// 记录错误但继续处理其他系列
$msg .= "创建系列失败: " . $e->getMessage() . "\n";
$fail++;
}
}
Log::info('brand-crawl-' . $brand_id . ' ' . $msg);
$msg = '创建系列成功' . $createdCount . '条';
$msg .= (!empty($repeat))?',重复' . $repeat . '条':'';
$msg .= (!empty($fail))?',失败' . $fail . '条':'';
return $msg;
}
}