refactor: kb & station & terminal
This commit is contained in:
@@ -3,8 +3,8 @@
|
||||
namespace App\Filament\Pages;
|
||||
|
||||
use App\Models\Document;
|
||||
use App\Models\Group;
|
||||
use App\Models\KnowledgeBase;
|
||||
use App\Models\Station;
|
||||
use App\Services\DocumentSearchService;
|
||||
use App\Services\DocumentService;
|
||||
use Filament\Forms\Components\Select;
|
||||
@@ -19,7 +19,6 @@ use Filament\Tables\Columns\TextColumn;
|
||||
use Filament\Tables\Concerns\InteractsWithTable;
|
||||
use Filament\Tables\Contracts\HasTable;
|
||||
use Filament\Tables\Table;
|
||||
use Illuminate\Contracts\View\View;
|
||||
use Illuminate\Database\Eloquent\Builder;
|
||||
use Illuminate\Support\Facades\Auth;
|
||||
|
||||
@@ -29,41 +28,25 @@ class SearchPage extends Page implements HasForms, HasTable
|
||||
use InteractsWithTable;
|
||||
|
||||
protected static ?string $navigationIcon = 'heroicon-o-magnifying-glass';
|
||||
|
||||
protected static string $view = 'filament.pages.search-page';
|
||||
|
||||
protected static ?string $navigationLabel = '搜索文档';
|
||||
|
||||
protected static ?string $title = '搜索文档';
|
||||
|
||||
protected static ?int $navigationSort = 2;
|
||||
|
||||
// 表单数据
|
||||
public ?string $searchQuery = null;
|
||||
public ?string $documentType = null;
|
||||
public ?int $groupId = null;
|
||||
public ?int $knowledgeBaseId = null;
|
||||
|
||||
// 搜索结果
|
||||
public $searchResults = null;
|
||||
public ?array $stationIds = [];
|
||||
public ?array $knowledgeBaseIds = [];
|
||||
public bool $hasSearched = false;
|
||||
|
||||
/**
|
||||
* 挂载页面时的初始化
|
||||
*/
|
||||
public function mount(): void
|
||||
{
|
||||
$this->form->fill([
|
||||
'searchQuery' => '',
|
||||
'documentType' => null,
|
||||
'groupId' => null,
|
||||
'knowledgeBaseId' => null,
|
||||
'stationIds' => [],
|
||||
'knowledgeBaseIds' => [],
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* 定义搜索表单
|
||||
*/
|
||||
public function form(Form $form): Form
|
||||
{
|
||||
return $form
|
||||
@@ -74,35 +57,25 @@ class SearchPage extends Page implements HasForms, HasTable
|
||||
->required()
|
||||
->maxLength(255),
|
||||
|
||||
Select::make('documentType')
|
||||
->label('文档类型')
|
||||
->placeholder('全部类型')
|
||||
->options([
|
||||
'global' => '全局知识库',
|
||||
'dedicated' => '专用知识库',
|
||||
])
|
||||
->native(false),
|
||||
|
||||
Select::make('groupId')
|
||||
->label('所属分组')
|
||||
->placeholder('全部分组')
|
||||
->options(Group::pluck('name', 'id'))
|
||||
Select::make('stationIds')
|
||||
->label('线站')
|
||||
->placeholder('全部线站')
|
||||
->options(Station::pluck('name', 'id'))
|
||||
->multiple()
|
||||
->searchable()
|
||||
->native(false),
|
||||
|
||||
Select::make('knowledgeBaseId')
|
||||
Select::make('knowledgeBaseIds')
|
||||
->label('知识库')
|
||||
->placeholder('全部知识库')
|
||||
->options(KnowledgeBase::pluck('name', 'id'))
|
||||
->options(KnowledgeBase::where('status', 'active')->pluck('name', 'id'))
|
||||
->multiple()
|
||||
->searchable()
|
||||
->native(false),
|
||||
])
|
||||
->columns(4);
|
||||
->columns(3);
|
||||
}
|
||||
|
||||
/**
|
||||
* 定义搜索结果表格
|
||||
*/
|
||||
public function table(Table $table): Table
|
||||
{
|
||||
return $table
|
||||
@@ -114,29 +87,12 @@ class SearchPage extends Page implements HasForms, HasTable
|
||||
->sortable()
|
||||
->limit(50),
|
||||
|
||||
TextColumn::make('markdown_preview')
|
||||
->label('内容片段')
|
||||
->limit(100)
|
||||
->wrap()
|
||||
->default('暂无内容预览'),
|
||||
TextColumn::make('knowledgeBase.name')
|
||||
->label('所属知识库')
|
||||
->sortable(),
|
||||
|
||||
TextColumn::make('type')
|
||||
->label('文档类型')
|
||||
->badge()
|
||||
->formatStateUsing(fn (string $state): string => match ($state) {
|
||||
'global' => '全局知识库',
|
||||
'dedicated' => '专用知识库',
|
||||
default => $state,
|
||||
})
|
||||
->color(fn (string $state): string => match ($state) {
|
||||
'global' => 'success',
|
||||
'dedicated' => 'info',
|
||||
default => 'gray',
|
||||
}),
|
||||
|
||||
TextColumn::make('group.name')
|
||||
->label('所属分组')
|
||||
->default('无')
|
||||
TextColumn::make('uploader.name')
|
||||
->label('上传者')
|
||||
->sortable(),
|
||||
|
||||
TextColumn::make('created_at')
|
||||
@@ -157,7 +113,7 @@ class SearchPage extends Page implements HasForms, HasTable
|
||||
->modalSubmitAction(false)
|
||||
->modalCancelActionLabel('关闭')
|
||||
->visible(fn (Document $record) => $record->conversion_status === 'completed'),
|
||||
|
||||
|
||||
Action::make('download')
|
||||
->label('下载')
|
||||
->icon('heroicon-o-arrow-down-tray')
|
||||
@@ -165,11 +121,7 @@ class SearchPage extends Page implements HasForms, HasTable
|
||||
try {
|
||||
$documentService = app(DocumentService::class);
|
||||
$user = Auth::user();
|
||||
|
||||
// 记录下载日志
|
||||
$documentService->logDownload($record, $user);
|
||||
|
||||
// 返回文件下载响应
|
||||
return $documentService->downloadDocument($record, $user);
|
||||
} catch (\Exception $e) {
|
||||
Notification::make()
|
||||
@@ -187,56 +139,39 @@ class SearchPage extends Page implements HasForms, HasTable
|
||||
->emptyStateIcon('heroicon-o-magnifying-glass');
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取表格查询构建器
|
||||
*/
|
||||
protected function getTableQuery(): Builder
|
||||
{
|
||||
if (!$this->hasSearched || empty($this->searchQuery)) {
|
||||
// 如果还没有搜索或搜索关键词为空,返回空查询
|
||||
return Document::query()->whereRaw('1 = 0');
|
||||
}
|
||||
|
||||
// 使用 DocumentSearchService 进行搜索
|
||||
$searchService = app(DocumentSearchService::class);
|
||||
$user = Auth::user();
|
||||
|
||||
$filters = [];
|
||||
if ($this->documentType) {
|
||||
$filters['type'] = $this->documentType;
|
||||
if (!empty($this->stationIds)) {
|
||||
$filters['station_ids'] = $this->stationIds;
|
||||
}
|
||||
if ($this->groupId) {
|
||||
$filters['group_id'] = $this->groupId;
|
||||
}
|
||||
if ($this->knowledgeBaseId) {
|
||||
$filters['knowledge_base_id'] = $this->knowledgeBaseId;
|
||||
if (!empty($this->knowledgeBaseIds)) {
|
||||
$filters['knowledge_base_ids'] = $this->knowledgeBaseIds;
|
||||
}
|
||||
|
||||
// 执行搜索
|
||||
$results = $searchService->search($this->searchQuery, $user, $filters);
|
||||
|
||||
// 获取搜索结果的 ID 列表
|
||||
$accessibleStationIds = Auth::user()->getAccessibleStationIds();
|
||||
$results = $searchService->search($this->searchQuery, $accessibleStationIds, $filters);
|
||||
$documentIds = $results->pluck('id')->toArray();
|
||||
|
||||
// 返回包含这些 ID 的查询构建器
|
||||
if (empty($documentIds)) {
|
||||
return Document::query()->whereRaw('1 = 0');
|
||||
}
|
||||
|
||||
return Document::query()
|
||||
->whereIn('id', $documentIds)
|
||||
->with(['group', 'uploader']);
|
||||
->with(['knowledgeBase', 'uploader']);
|
||||
}
|
||||
|
||||
/**
|
||||
* 执行搜索
|
||||
*/
|
||||
public function search(): void
|
||||
{
|
||||
// 验证表单
|
||||
$data = $this->form->getState();
|
||||
|
||||
// 检查搜索关键词是否为空
|
||||
if (empty($data['searchQuery'])) {
|
||||
Notification::make()
|
||||
->title('请输入搜索关键词')
|
||||
@@ -245,14 +180,11 @@ class SearchPage extends Page implements HasForms, HasTable
|
||||
return;
|
||||
}
|
||||
|
||||
// 更新搜索参数
|
||||
$this->searchQuery = $data['searchQuery'];
|
||||
$this->documentType = $data['documentType'];
|
||||
$this->groupId = $data['groupId'];
|
||||
$this->knowledgeBaseId = $data['knowledgeBaseId'] ?? null;
|
||||
$this->stationIds = $data['stationIds'] ?? [];
|
||||
$this->knowledgeBaseIds = $data['knowledgeBaseIds'] ?? [];
|
||||
$this->hasSearched = true;
|
||||
|
||||
// 重置表格分页
|
||||
$this->resetTable();
|
||||
|
||||
Notification::make()
|
||||
@@ -261,22 +193,17 @@ class SearchPage extends Page implements HasForms, HasTable
|
||||
->send();
|
||||
}
|
||||
|
||||
/**
|
||||
* 清空搜索
|
||||
*/
|
||||
public function clearSearch(): void
|
||||
{
|
||||
$this->form->fill([
|
||||
'searchQuery' => '',
|
||||
'documentType' => null,
|
||||
'groupId' => null,
|
||||
'knowledgeBaseId' => null,
|
||||
'stationIds' => [],
|
||||
'knowledgeBaseIds' => [],
|
||||
]);
|
||||
|
||||
$this->searchQuery = null;
|
||||
$this->documentType = null;
|
||||
$this->groupId = null;
|
||||
$this->knowledgeBaseId = null;
|
||||
$this->stationIds = [];
|
||||
$this->knowledgeBaseIds = [];
|
||||
$this->hasSearched = false;
|
||||
|
||||
$this->resetTable();
|
||||
@@ -287,9 +214,6 @@ class SearchPage extends Page implements HasForms, HasTable
|
||||
->send();
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取页面头部操作
|
||||
*/
|
||||
protected function getHeaderActions(): array
|
||||
{
|
||||
return [];
|
||||
|
||||
Reference in New Issue
Block a user