From d4397c2d14136a95d4f992151842725b235fb174 Mon Sep 17 00:00:00 2001 From: zhaiyuxin103 Date: Tue, 11 Feb 2025 16:17:16 +0800 Subject: [PATCH] =?UTF-8?q?feat(exports):=20=E5=AF=BC=E5=87=BA=E6=95=B0?= =?UTF-8?q?=E6=8D=AE=E5=BA=93=E7=BB=93=E6=9E=84=20(#96)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .prettierignore | 1 + app/Console/Commands/DatabaseStructExport.php | 42 +++++++++++++ app/Exports/DatabaseStructExport.php | 41 +++++++++++++ app/Exports/Sheets/TableSheet.php | 59 +++++++++++++++++++ app/Exports/Sheets/TablesSheet.php | 50 ++++++++++++++++ .../exports/database_struct/table.blade.php | 52 ++++++++++++++++ .../exports/database_struct/tables.blade.php | 25 ++++++++ 7 files changed, 270 insertions(+) create mode 100644 app/Console/Commands/DatabaseStructExport.php create mode 100644 app/Exports/DatabaseStructExport.php create mode 100644 app/Exports/Sheets/TableSheet.php create mode 100644 app/Exports/Sheets/TablesSheet.php create mode 100644 resources/views/exports/database_struct/table.blade.php create mode 100644 resources/views/exports/database_struct/tables.blade.php diff --git a/.prettierignore b/.prettierignore index 26c8be6..eddc954 100644 --- a/.prettierignore +++ b/.prettierignore @@ -8,6 +8,7 @@ resources/views/emails resources/views/mail resources/views/vendor resources/views/welcome.blade.php +storage/framework/cache/laravel-excel storage/framework/views composer.lock docker-compose.yml diff --git a/app/Console/Commands/DatabaseStructExport.php b/app/Console/Commands/DatabaseStructExport.php new file mode 100644 index 0000000..99f44fe --- /dev/null +++ b/app/Console/Commands/DatabaseStructExport.php @@ -0,0 +1,42 @@ +info('Exporting database structure...'); + + try { + Excel::store(new \App\Exports\DatabaseStructExport(), 'database-struct.xlsx'); + } catch (Throwable $th) { + $this->error('Failed to export database structure.'); + } + + $this->info('Database structure exported successfully.'); + } +} diff --git a/app/Exports/DatabaseStructExport.php b/app/Exports/DatabaseStructExport.php new file mode 100644 index 0000000..d8c9ce9 --- /dev/null +++ b/app/Exports/DatabaseStructExport.php @@ -0,0 +1,41 @@ +tables = array_map(function ($table) { + $key = 'Tables_in_' . Str::lower(DB::connection()->getDatabaseName()); + + // 获取表注释 + $comment = DB::select('SHOW TABLE STATUS WHERE Name = ?', [$table->{$key}])[0]->Comment; + + return [ + 'name' => $table->{$key}, + 'title' => $comment, + 'description' => $comment, + ]; + }, DB::select('SHOW TABLES')); + } + + public function sheets(): array + { + return array_merge([ + new Sheets\TablesSheet($this->tables), + ], array_map(function ($table) { + return new Sheets\TableSheet($table); + }, $this->tables)); + } +} diff --git a/app/Exports/Sheets/TableSheet.php b/app/Exports/Sheets/TableSheet.php new file mode 100644 index 0000000..a6f78c3 --- /dev/null +++ b/app/Exports/Sheets/TableSheet.php @@ -0,0 +1,59 @@ +columns = DB::select('SHOW FULL COLUMNS FROM ' . data_get($this->table, 'name')); + } + + public function title(): string + { + return data_get($this->table, 'name'); + } + + public function columnWidths(): array + { + return [ + 'B' => 20, + ]; + } + + public function styles(Worksheet $sheet): void + { + $sheet->getStyle('B2:C4')->getBorders()->applyFromArray([ + 'allBorders' => [ + 'borderStyle' => Border::BORDER_THIN, + ], + ]); + $sheet->getStyle('B6:H' . (count($this->columns) + 6))->getBorders()->applyFromArray([ + 'allBorders' => [ + 'borderStyle' => Border::BORDER_THIN, + ], + ]); + } + + public function view(): View + { + return view('exports.database_struct.table', [ + 'table' => $this->table, + 'columns' => $this->columns, + ]); + } +} diff --git a/app/Exports/Sheets/TablesSheet.php b/app/Exports/Sheets/TablesSheet.php new file mode 100644 index 0000000..8d46c5d --- /dev/null +++ b/app/Exports/Sheets/TablesSheet.php @@ -0,0 +1,50 @@ + 10, + ]; + } + + public function styles(Worksheet $sheet): void + { + $sheet->getStyle('B2')->getFont()->setBold(true)->setSize(18); + $sheet->getStyle('B3:D57')->getBorders()->applyFromArray([ + 'allBorders' => [ + 'borderStyle' => Border::BORDER_THIN, + ], + ]); + } + + public function view(): View + { + return view('exports.database_struct.tables', [ + 'tables' => $this->tables, + ]); + } +} diff --git a/resources/views/exports/database_struct/table.blade.php b/resources/views/exports/database_struct/table.blade.php new file mode 100644 index 0000000..a60d422 --- /dev/null +++ b/resources/views/exports/database_struct/table.blade.php @@ -0,0 +1,52 @@ + + + + + + + + + + + + + + + + + + + +
テーブル名称管理者と店舗の中間テーブル
テーブル名 + {{ data_get($table, 'name') }} +
概要 + {{ data_get($table, 'description') }} +
+ + + + + + + + + + + + + + + @foreach ($columns as $key => $column) + + + + + + + + + + + @endforeach + +
No.カラム名データ型桁数Not Nullデフォルト備考
{{ $key + 1 }}{{ $column->Field }}{{ $column->Type }}{{ $column->Type }}{{ $column->Null }}{{ $column->Default }}{{ $column->Comment }}
diff --git a/resources/views/exports/database_struct/tables.blade.php b/resources/views/exports/database_struct/tables.blade.php new file mode 100644 index 0000000..611e091 --- /dev/null +++ b/resources/views/exports/database_struct/tables.blade.php @@ -0,0 +1,25 @@ + + + + + + + + + + + + + + @foreach ($tables as $key => $table) + + + + + + + @endforeach + +
テーブル一覧
No.テーブル名備考
{{ $key + 1 }}{{ data_get($table, 'name') }} + {{ data_get($table, 'title') }} +