validator; } /** * 构建查询条件 */ protected function buildQueryConfig(Request $request, string $scene = 'default'): array { $p = $request->all(); $config = []; $config['model'] = $this->modelClass; switch ($scene) { case 'index': // ====== ====== 排序 ====== ====== $config['order']['hot'] = 'desc'; $config['order']['sort'] = 'desc'; $config['order']['id'] = 'desc'; // ====== ====== 筛选 ====== ====== // 状态 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) { $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; } 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; } }