262 lines
7.9 KiB
PHP
262 lines
7.9 KiB
PHP
<?php
|
|
|
|
namespace app\admin\service;
|
|
|
|
use app\admin\validate\BrandSeriesValidate;
|
|
use app\common\logic\Fingerprint;
|
|
use app\common\logic\ModelNaming;
|
|
use app\common\logic\SkuCodeBuilder;
|
|
use app\common\model\Brand;
|
|
use app\common\model\VehVehicleSku;
|
|
use app\common\service\CrawlService;
|
|
use app\common\service\VehVehicleSkuService;
|
|
use app\common\traits\crud\CrudServiceTrait;
|
|
use app\common\model\BrandSeries;
|
|
use app\common\utils\CrudUtil;
|
|
use support\Log;
|
|
use support\Request;
|
|
use Webman\RedisQueue\Redis;
|
|
|
|
class BrandSeriesService
|
|
{
|
|
|
|
use CrudServiceTrait;
|
|
|
|
/**
|
|
* 指定当前要操作的模型
|
|
*/
|
|
protected string $modelClass = BrandSeries::class;
|
|
|
|
/**
|
|
* 验证器
|
|
*/
|
|
protected $validator = BrandSeriesValidate::class;
|
|
|
|
/**
|
|
* 获取验证器
|
|
*/
|
|
public function getValidator()
|
|
{
|
|
return new $this->validator;
|
|
}
|
|
|
|
/**
|
|
* 构建查询条件
|
|
*/
|
|
|
|
protected function buildQueryConfig(Request $request, string $scene = 'default'): array
|
|
{
|
|
$p = $request->all();
|
|
|
|
$config = [];
|
|
$config['model'] = $this->modelClass;
|
|
switch ($scene) {
|
|
case 'index':
|
|
// ====== ====== 排序 ====== ======
|
|
$config['order']['sort'] = 'desc';
|
|
$config['order']['id'] = 'desc';
|
|
|
|
// ====== ====== 筛选 ====== ======
|
|
$config['where']['brand_id'] = $p['brand_id'];
|
|
// 状态
|
|
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 (['series_code', 'series_name'] as $col) {
|
|
$config['orWhere'][] = [$col, 'like', $k];
|
|
}
|
|
}
|
|
//$config['where']['status'] = 1;
|
|
break;
|
|
case 'show':
|
|
$config['where']['id'] = $p['id'];
|
|
//$config['where']['status'] = 1;
|
|
break;
|
|
case 'store':
|
|
break;
|
|
case 'update':
|
|
$config['where']['id'] = $p['id'];
|
|
break;
|
|
case 'destroy':
|
|
$config['where']['id'] = $p['id'];
|
|
break;
|
|
}
|
|
|
|
return $config;
|
|
}
|
|
|
|
/**
|
|
* 生成唯一的7位颜色编码
|
|
* 格式:C + (O/I) + 5位字母数字组合
|
|
*
|
|
* @param int $type 类型
|
|
* @return string 7位唯一编码
|
|
*/
|
|
public function generateUniqueModificationCode($brand_id)
|
|
{
|
|
$chars = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ';
|
|
$maxAttempts = 10; // 防止无限循环
|
|
|
|
$brand_code = Brand::where('id', $brand_id)->value('brand_code');
|
|
$brand_code = $brand_code ?? 'BS';
|
|
for ($attempt = 0; $attempt < $maxAttempts; $attempt++) {
|
|
// 生成后5位随机字符
|
|
$suffix = '';
|
|
for ($i = 0; $i < 6; $i++) {
|
|
$suffix .= $chars[rand(0, strlen($chars) - 1)];
|
|
}
|
|
|
|
$code = $brand_code . $suffix;
|
|
|
|
// 检查是否已存在
|
|
$existing = BrandSeries::where('series_code', $code)->first();
|
|
if (!$existing) {
|
|
return $code;
|
|
}
|
|
}
|
|
|
|
// 如果1000次尝试后仍然重复,使用时间戳确保唯一性
|
|
$timestamp = substr(time(), -6);
|
|
return $brand_code . $timestamp;
|
|
}
|
|
|
|
/**
|
|
* 添加前置处理
|
|
*/
|
|
public function beforeServiceStore($data, Request $request)
|
|
{
|
|
$brand_id = $data['brand_id'] ?? 0;
|
|
$data['series_code'] = $this->generateUniqueModificationCode($brand_id);
|
|
return $data;
|
|
}
|
|
|
|
/**
|
|
* 抓取车型数据
|
|
* @param Request $request
|
|
* @return array
|
|
* @throws \Exception
|
|
*/
|
|
public function crawlModel(Request $request)
|
|
{
|
|
$series_id = $request->get('brand_series_id');
|
|
|
|
// 验证品牌是否存在
|
|
$series = BrandSeries::where('id', $series_id)->first();
|
|
if (!$series) {
|
|
throw new \Exception("系列ID {$series_id} 不存在");
|
|
}
|
|
|
|
// 获取品牌信息
|
|
$series = BrandSeries::where('id', $series_id)->first();
|
|
if (!$series) {
|
|
throw new \Exception("系列ID {$series_id} 不存在");
|
|
}
|
|
|
|
// 获取品牌信息
|
|
$brand = Brand::where('id', $series->brand_id)->first();
|
|
|
|
// 执行抓取
|
|
$crawlService = new CrawlService();
|
|
$models = $crawlService->seriesModel($brand->brand_name . ' ' . $series->series_name);
|
|
|
|
foreach ($models as $key => $model) {
|
|
$models[$key]['year'] = $this->extractYearFromCarModel($model['title']);
|
|
}
|
|
|
|
return $models;
|
|
|
|
// // 将抓取任务加入队列
|
|
// Redis::send('series-crawl', [
|
|
// 'brand_series_id' => $series
|
|
// ]);
|
|
//
|
|
// return true;
|
|
}
|
|
|
|
/**
|
|
* @param Request $request
|
|
* @return array|string
|
|
* @throws \Exception
|
|
*/
|
|
public function crawlModelCreate(Request $request)
|
|
{
|
|
$series_id = $request->input('series_id');
|
|
// $brand_id = $request->input('brand_id');
|
|
$models = $request->input('models');
|
|
|
|
// 验证系列是否存在
|
|
$series = BrandSeries::where('id', $series_id)->first();
|
|
if (!$series) {
|
|
throw new \Exception("系列ID {$series_id} 不存在");
|
|
}
|
|
// 获取品牌信息
|
|
$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'],
|
|
];
|
|
$modelData['model_code'] = ModelNaming::buildModelCode($modelData);
|
|
$modelData['sku_code'] = SkuCodeBuilder::build($modelData);
|
|
// 指纹
|
|
$modelData['fingerprint'] = Fingerprint::makeFromArray($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;
|
|
}
|
|
|
|
|
|
/**
|
|
* 从车型名中提取年份
|
|
* @param string $carModel 车型名称
|
|
* @return string|null 提取到的年份或null
|
|
*/
|
|
function extractYearFromCarModel($carModel)
|
|
{
|
|
// 匹配4位数字 + "款" 的模式
|
|
if (preg_match('/(\d{4})款/', $carModel, $matches)) {
|
|
return $matches[1];
|
|
}
|
|
|
|
// 匹配4位数字 + 空格或结尾的模式
|
|
if (preg_match('/^(\d{4})(?=\s|$)/', $carModel, $matches)) {
|
|
return $matches[1];
|
|
}
|
|
|
|
return null;
|
|
}
|
|
} |