From 951a3d2124d65e5f10d92e6e0339bac4fd54a696 Mon Sep 17 00:00:00 2001 From: zhang zhuo Date: Mon, 17 Nov 2025 18:08:41 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/Controller/Admin/System.php | 11 +---- app/Model/GenTable.php | 45 +++++++++++++++--- app/Utils/Str.php | 84 +++++++++++++++++++++++++++++++++ 3 files changed, 124 insertions(+), 16 deletions(-) diff --git a/app/Controller/Admin/System.php b/app/Controller/Admin/System.php index a54c102..0ac3e36 100644 --- a/app/Controller/Admin/System.php +++ b/app/Controller/Admin/System.php @@ -538,15 +538,8 @@ class System extends Base if (in_array($item->Name, $tables)) { return false; } - // 表名筛选 - if (isset($param['table_name']) && $param['table_name'] != "" && !str_contains($item->Name, $param['table_name'])) { - return false; - } - // 表描述筛选 - if (isset($param['table_comment']) && $param['table_comment'] != "" && !str_contains($item->Comment, $param['table_comment'])) { - return false; - } - return true; + // 表筛选 + return in_array($item->Name, $param['names']); }); if (empty($rows)) { return $this->error("数据表不为空"); diff --git a/app/Model/GenTable.php b/app/Model/GenTable.php index 41824af..23b7d51 100644 --- a/app/Model/GenTable.php +++ b/app/Model/GenTable.php @@ -4,6 +4,7 @@ declare(strict_types=1); namespace App\Model; +use App\Utils\Str; use Hyperf\DbConnection\Db; /** @@ -49,17 +50,47 @@ class GenTable extends Model ->orderByDesc('table_id')->paginate((int)$param['limit']); } - public static function genTable(array $rows) + public static function genTable(array $rows): bool { Db::beginTransaction(); foreach ($rows as $row) { - var_dump($row); // 添加主表 -// $res = self::insert([ -// 'table_name' => $row, -// ]); -// $columns = Db::select("SHOW FULL COLUMNS FROM `{$row}`"); -// var_dump($columns); + $id = self::add([ + 'table_name' => $row->Name, + 'table_comment' => $row->Comment, + 'controller_name' => Str::snakeToCamel(str_replace("pi_", "", $row->Name), true), + 'module_name' => 'App\Controller\Admin' + ]); + if (!$id) { + Db::rollBack(); + return false; + } + // 字段管理 + $columns = Db::select("SHOW FULL COLUMNS FROM `{$row->Name}`"); + $i = 1; + foreach ($columns as $column) { + $res = GenTableColumn::add([ + 'table_id' => $id, + 'column_name' => $column->Field, + 'column_comment' => $column->Comment, + 'column_type' => $column->Type, + 'php_type' => Str::typeMysqlToPhp($column->Type), + 'php_field' => $column->Field, + 'is_insert' => Str::isInsert($column->Field, $column->Extra), + 'is_edit' => Str::isEdit($column->Field, $column->Extra), + 'is_list' => Str::isList($column->Field, $column->Extra), + 'is_query' => Str::isQuery($column->Field, $column->Extra), + 'is_required' => 0, + 'query_type' => str_contains($column->Field, "name") ? "like" : "eq", + 'html_type' => Str::htmlType($column->Field), + 'rank' => $i + ]); + $i++; + if (!$res) { + Db::rollBack(); + return false; + } + } } Db::commit(); return true; diff --git a/app/Utils/Str.php b/app/Utils/Str.php index 0c590ce..c3d1202 100644 --- a/app/Utils/Str.php +++ b/app/Utils/Str.php @@ -5,6 +5,8 @@ namespace App\Utils; +use function PHPUnit\Framework\matches; + class Str { /** @@ -40,4 +42,86 @@ class Str return $prefix . $_str; } + static function snakeToCamel($str, $capitalized = false) + { + $result = str_replace('_', '', ucwords($str, '_')); + if (!$capitalized) { + $result = lcfirst($result); + } + return $result; + } + + public static function typeMysqlToPhp($Type): string + { + switch ($Type) { + case str_contains($Type, "int"): + case str_contains($Type, "smallint"): + case str_contains($Type, "tinyint"): + case str_contains($Type, "mediumint"): + case str_contains($Type, "bigint"): + case str_contains($Type, "bool"): + case str_contains($Type, "boolean"): + return "int"; + case str_contains($Type, "float"): + case str_contains($Type, "double"): + return "float"; + case str_contains($Type, "text"): + case str_contains($Type, "char"): + case str_contains($Type, "varchar"): + case str_contains($Type, "blob"): + case str_contains($Type, "decimal"): + case str_contains($Type, "numeric"): + case str_contains($Type, "date"): + case str_contains($Type, "datetime"): + case str_contains($Type, "timestamp"): + default: + return "string"; + } + } + + public static function isInsert($field, $extra): int + { + if ($extra == "auto_increment") { + return 0; + } + return !in_array($field, ["deleted_at", "update_time"]) ? 1 : 0; + } + + public static function isEdit($field, $extra): int + { + if ($extra == "auto_increment") { + return 0; + } + return !in_array($field, ["deleted_at", "create_time"]) ? 1 : 0; + } + + public static function isList($field, $extra): int + { + if ($extra == "auto_increment") { + return 1; + } + return !in_array($field, ["deleted_at"]) ? 1 : 0; + } + + public static function isQuery($field, $extra): int + { + if ($extra == "auto_increment") { + return 0; + } + return !in_array($field, ["deleted_at", "update_time", "create_time"]) ? 1 : 0; + } + + public static function htmlType($field): string + { + switch ($field) { + case "status": + return "radio"; + case str_contains($field, "_time"): + return "datetime"; + case str_contains($field, "_date"): + return "date"; + default: + return "input"; + } + } } \ No newline at end of file