2025-09-16 10:53:57 +08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace app\admin\service;
|
|
|
|
|
|
|
|
|
|
use app\admin\validate\BrandValidate;
|
2025-10-10 16:48:33 +08:00
|
|
|
use app\common\model\BrandSeries;
|
|
|
|
|
use app\common\service\CrawlService;
|
2025-09-16 10:53:57 +08:00
|
|
|
use app\common\traits\crud\CrudServiceTrait;
|
|
|
|
|
use app\common\model\Brand;
|
2025-10-10 16:48:33 +08:00
|
|
|
use app\common\utils\CrudUtil;
|
|
|
|
|
use support\Log;
|
2025-09-16 10:53:57 +08:00
|
|
|
use support\Request;
|
2025-10-10 16:48:33 +08:00
|
|
|
use Webman\RedisQueue\Redis;
|
2025-09-16 10:53:57 +08:00
|
|
|
|
|
|
|
|
class BrandService
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
use CrudServiceTrait;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 指定当前要操作的模型
|
|
|
|
|
*/
|
|
|
|
|
protected string $modelClass = Brand::class;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 验证器
|
|
|
|
|
*/
|
|
|
|
|
protected $validator = BrandValidate::class;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 获取验证器
|
|
|
|
|
*/
|
|
|
|
|
public function getValidator()
|
|
|
|
|
{
|
2025-10-10 16:48:33 +08:00
|
|
|
return new $this->validator;
|
2025-09-16 10:53:57 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 构建查询条件
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
protected function buildQueryConfig(Request $request, string $scene = 'default'): array
|
|
|
|
|
{
|
|
|
|
|
$p = $request->all();
|
|
|
|
|
|
|
|
|
|
$config = [];
|
|
|
|
|
$config['model'] = $this->modelClass;
|
|
|
|
|
switch ($scene) {
|
|
|
|
|
case 'index':
|
|
|
|
|
// ====== ====== 排序 ====== ======
|
2025-09-17 14:15:09 +08:00
|
|
|
$config['order']['hot'] = 'desc';
|
2025-09-16 10:53:57 +08:00
|
|
|
$config['order']['sort'] = 'desc';
|
|
|
|
|
$config['order']['id'] = 'desc';
|
|
|
|
|
|
|
|
|
|
// ====== ====== 筛选 ====== ======
|
|
|
|
|
// 状态
|
2025-10-10 16:48:33 +08:00
|
|
|
if (isset($p['status']) && in_array($p['status'], [0, 1])) {
|
2025-09-16 10:53:57 +08:00
|
|
|
$config['where']['status'] = $p['status'];
|
|
|
|
|
}
|
|
|
|
|
// 关键字
|
|
|
|
|
if (isset($p['keyword']) && $p['keyword']) {
|
|
|
|
|
$k = '%' . $p['keyword'] . '%';
|
2025-10-10 16:48:33 +08:00
|
|
|
foreach (['brand_code', 'brand_name'] as $col) {
|
2025-09-16 10:53:57 +08:00
|
|
|
$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;
|
|
|
|
|
}
|
2025-10-10 16:48:33 +08:00
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
}
|
2026-02-04 17:35:08 +08:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 生成唯一的7位颜色编码
|
|
|
|
|
* 格式:C + (O/I) + 5位字母数字组合
|
|
|
|
|
*
|
|
|
|
|
* @param int $type 类型
|
|
|
|
|
* @return string 7位唯一编码
|
|
|
|
|
*/
|
|
|
|
|
public function generateUniqueCode()
|
|
|
|
|
{
|
|
|
|
|
$chars = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ';
|
|
|
|
|
$maxAttempts = 10; // 防止无限循环
|
|
|
|
|
|
|
|
|
|
for ($attempt = 0; $attempt < $maxAttempts; $attempt++) {
|
|
|
|
|
// 生成后5位随机字符
|
|
|
|
|
$suffix = '';
|
|
|
|
|
for ($i = 0; $i < 8; $i++) {
|
|
|
|
|
$suffix .= $chars[rand(0, strlen($chars) - 1)];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$code = $suffix;
|
|
|
|
|
|
|
|
|
|
// 检查是否已存在
|
|
|
|
|
$existing = Brand::where('brand_code', $code)->first();
|
|
|
|
|
if (!$existing) {
|
|
|
|
|
return $code;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 如果1000次尝试后仍然重复,使用时间戳确保唯一性
|
|
|
|
|
$timestamp = substr(time(), -6);
|
|
|
|
|
return $suffix . $timestamp;
|
|
|
|
|
}
|
2025-09-16 10:53:57 +08:00
|
|
|
}
|