表前缀

This commit is contained in:
李琦
2026-01-23 17:13:23 +08:00
parent 0d21609586
commit a1abba3835
8 changed files with 1850 additions and 2 deletions

View File

@@ -260,6 +260,7 @@ class CodeGenerationService
$this->genViews();
// 生成菜单
$this->genMenu();
DB::commit();
} catch (Exception $e) {
DB::rollBack();
// ds([
@@ -537,4 +538,172 @@ class CodeGenerationService
$this->utils->errorThrow($e->getMessage());
}
}
/**
* 获取数据库表列表
* @return array
*/
public function getTableList(): array
{
$database = config('database.connections.mysql.database');
$prefix = config('database.prefix', 'nl_');
$tables = DB::select("
SELECT
TABLE_NAME as `name`,
TABLE_COMMENT as `comment`,
TABLE_ROWS as `rows`,
CREATE_TIME as create_time
FROM information_schema.TABLES
WHERE TABLE_SCHEMA = ?
AND TABLE_NAME LIKE ?
ORDER BY TABLE_NAME
", [$database, $prefix . '%']);
$result = [];
foreach ($tables as $table) {
$result[] = [
'name' => $table->name,
'comment' => $table->comment ?: $table->name,
'rows' => $table->rows,
'create_time' => $table->create_time,
];
}
return $result;
}
/**
* 获取表字段信息
* @param string $tableName
* @return array
* @throws Exception
*/
public function getTableColumns(string $tableName): array
{
$database = config('database.connections.mysql.database');
$columns = DB::select("
SELECT
COLUMN_NAME as `name`,
DATA_TYPE as `type`,
CHARACTER_MAXIMUM_LENGTH as `length`,
COLUMN_DEFAULT as default_value,
COLUMN_COMMENT as `comment`,
IS_NULLABLE as nullable,
COLUMN_KEY as key_type,
EXTRA as extra
FROM information_schema.COLUMNS
WHERE TABLE_SCHEMA = ?
AND TABLE_NAME = ?
ORDER BY ORDINAL_POSITION
", [$database, $tableName]);
if (empty($columns)) {
$this->utils->errorThrow('表不存在或没有字段');
}
$result = [];
foreach ($columns as $column) {
// 跳过id字段代码生成会自动添加
if ($column->name === 'id') {
continue;
}
// 跳过系统字段
if (in_array($column->name, ['created_at', 'updated_at', 'deleted_at', 'status'])) {
continue;
}
// 转换数据类型
$type = strtoupper($column->type);
$typeLength = '';
if (in_array($type, ['VARCHAR', 'CHAR'])) {
$typeLength = $column->length ?: '255';
} elseif (in_array($type, ['INT', 'TINYINT', 'BIGINT', 'SMALLINT'])) {
$typeLength = $column->length ?: '11';
} elseif (in_array($type, ['DECIMAL', 'FLOAT', 'DOUBLE'])) {
$typeLength = $column->length ?: '10,2';
}
// 判断表单组件类型
$formType = 'VbenInput';
if (strpos($type, 'TEXT') !== false) {
$formType = 'VbenTextarea';
} elseif (in_array($type, ['TINYINT', 'INT']) && $column->length == 1) {
$formType = 'VbenSwitch';
} elseif (strpos($column->name, 'time') !== false || strpos($column->name, 'date') !== false) {
$formType = 'VbenDatePicker';
}
$result[] = [
'name' => $column->name,
'type' => $type,
'type_length' => $typeLength,
'default' => $column->default_value ?? '',
'comment' => $column->comment ?: $column->name,
'not_null' => $column->nullable === 'NO' ? 1 : 0,
'formShow' => 1,
'tableShow' => 1,
'formType' => $formType,
'search' => in_array($type, ['VARCHAR', 'CHAR', 'INT', 'TINYINT', 'BIGINT']) ? 1 : 0,
'searchValue' => in_array($type, ['VARCHAR', 'CHAR']) ? 'like' : '=',
];
}
return $result;
}
/**
* 将数据库表结构转换为代码生成数据格式
* @param string $tableName
* @param string $className
* @param string $classComment
* @param string $icon
* @param int $sort
* @param int $pid
* @return array
* @throws Exception
*/
public function convertTableToCodeGenData(
string $tableName,
string $className = '',
string $classComment = '',
string $icon = '',
int $sort = 9999,
int $pid = 0
): array {
// 如果没有提供类名,从表名生成
if (empty($className)) {
$prefix = config('database.prefix', 'nl_');
$tableNameWithoutPrefix = str_replace($prefix, '', $tableName);
// 下划线转驼峰
$className = str_replace('_', '', ucwords($tableNameWithoutPrefix, '_'));
}
// 如果没有提供中文名称,使用表注释或表名
if (empty($classComment)) {
$database = config('database.connections.mysql.database');
$tableInfo = DB::selectOne("
SELECT TABLE_COMMENT
FROM information_schema.TABLES
WHERE TABLE_SCHEMA = ? AND TABLE_NAME = ?
", [$database, $tableName]);
$classComment = $tableInfo->TABLE_COMMENT ?: $tableName;
}
// 获取字段信息
$fields = $this->getTableColumns($tableName);
return [
'class_name' => $className,
'class_comment' => $classComment,
'icon' => $icon,
'sort' => $sort,
'pid' => $pid,
'field' => $fields,
];
}
}