This commit is contained in:
zhang zhuo 2025-11-17 18:08:41 +08:00
parent 557d70b0aa
commit 951a3d2124
3 changed files with 124 additions and 16 deletions

View File

@ -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("数据表不为空");

View File

@ -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;

View File

@ -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";
}
}
}