| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181 |
- <?php
- namespace App\Filament\Pages\TextbookImport;
- use App\Services\Import\TextbookExcelImporter;
- use App\Services\TextbookApiService;
- use PhpOffice\PhpSpreadsheet\IOFactory;
- use Filament\Pages\Page;
- use UnitEnum;
- use BackedEnum;
- use Filament\Actions\Action;
- use Filament\Forms;
- use Filament\Forms\Components\FileUpload;
- use Filament\Notifications\Notification;
- use Illuminate\Support\Facades\Storage;
- use Illuminate\Support\Facades\Log;
- use Livewire\WithFileUploads;
- use Livewire\Component;
- use Livewire\Attributes\Validate;
- class TextbookExcelImportPage extends Page
- {
- use WithFileUploads;
- protected static string|BackedEnum|null $navigationIcon = 'heroicon-o-document-arrow-up';
- protected static ?string $navigationLabel = 'Excel导入';
- protected static UnitEnum|string|null $navigationGroup = '教材管理';
- protected static ?int $navigationSort = 4;
- public string $view = 'filament.pages.textbook-import';
- #[Validate('required|string')]
- public $selectedType = 'textbook_series';
- #[Validate('required|file|mimes:xlsx,xls|max:10240')]
- public $file;
- public $importResult = null;
- protected $importer;
- protected $apiService;
- public function boot()
- {
- $this->importer = app(TextbookExcelImporter::class);
- $this->apiService = app(TextbookApiService::class);
- }
- public function mount()
- {
- $this->importer = app(TextbookExcelImporter::class);
- $this->apiService = app(TextbookApiService::class);
- // 检查URL参数,设置默认导入类型
- $type = request()->get('type');
- if (in_array($type, ['textbook_series', 'textbook', 'textbook_catalog'])) {
- $this->selectedType = $type;
- }
- }
- public function getHeaderActions(): array
- {
- return [
- Action::make('downloadTemplate')
- ->label('下载模板')
- ->icon('heroicon-o-arrow-down-tray')
- ->color('primary')
- ->action('downloadTemplate'),
- Action::make('import')
- ->label('导入数据')
- ->icon('heroicon-o-cloud-arrow-up')
- ->color('success')
- ->action('importData')
- ->requiresConfirmation()
- ->modalHeading('确认导入')
- ->modalDescription('确定要导入Excel文件中的数据吗?此操作将同步到题库服务。')
- ->modalSubmitActionLabel('确认导入'),
- ];
- }
- public function downloadTemplate()
- {
- try {
- $importer = app(TextbookExcelImporter::class);
- if ($this->selectedType === 'textbook_series') {
- $filePath = $importer->generateTextbookSeriesTemplate();
- $fileName = '教材系列导入模板.xlsx';
- } elseif ($this->selectedType === 'textbook') {
- $filePath = $importer->generateTextbookTemplate();
- $fileName = '教材导入模板.xlsx';
- } else {
- $filePath = $importer->generateTextbookCatalogTemplate();
- $fileName = '教材目录导入模板.xlsx';
- }
- return response()->download($filePath, $fileName)->deleteFileAfterSend();
- } catch (\Exception $e) {
- Notification::make()
- ->title('模板生成失败')
- ->body($e->getMessage())
- ->danger()
- ->send();
- }
- }
- public function importData()
- {
- $this->validate();
- try {
- $importer = app(TextbookExcelImporter::class);
- // 保存上传的文件
- $path = $this->file->store('imports', 'local');
- // 获取完整文件路径
- $fullPath = storage_path('app/' . $path);
- // 执行导入
- if ($this->selectedType === 'textbook_series') {
- $result = $importer->importTextbookSeries($fullPath);
- } elseif ($this->selectedType === 'textbook') {
- $result = $importer->importTextbook($fullPath);
- } else {
- // 教材目录导入 - 需要从Excel中获取textbook_id
- $spreadsheet = IOFactory::load($fullPath);
- $sheet = $spreadsheet->getActiveSheet();
- $data = $sheet->toArray();
- // 获取第一行数据中的教材ID
- $firstRow = $data[1] ?? [];
- $textbookId = (int)($firstRow[0] ?? 0);
- if ($textbookId <= 0) {
- throw new \Exception('Excel文件中未找到有效的教材ID,请确保第一列包含教材ID');
- }
- $result = $importer->importTextbookCatalog($fullPath, $textbookId);
- }
- // 删除临时文件
- Storage::disk('local')->delete($path);
- $this->importResult = $result;
- if ($result['success']) {
- $message = sprintf(
- '导入完成!成功: %d 条,失败: %d 条',
- $result['success_count'],
- $result['error_count']
- );
- Notification::make()
- ->title('导入成功')
- ->body($message)
- ->success()
- ->send();
- } else {
- Notification::make()
- ->title('导入失败')
- ->body($result['message'])
- ->danger()
- ->send();
- }
- } catch (\Exception $e) {
- Notification::make()
- ->title('导入失败')
- ->body($e->getMessage())
- ->danger()
- ->send();
- Log::error('Excel导入失败', ['error' => $e->getMessage()]);
- }
- }
- }
|