server/app/Model/GenTable.php

206 lines
6.9 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Model;
use App\Utils\Str;
use Hyperf\DbConnection\Db;
/**
* @property int $table_id
* @property string $table_name
* @property string $table_comment
* @property string $controller_name
* @property string $module_name
* @property string $remark
* @property string $deleted_at
* @property string $create_time
* @property string $update_time
*/
class GenTable extends Model
{
/**
* The table associated with the model.
*/
protected ?string $table = 'gen_table';
protected string $primaryKey = 'table_id';
/**
* The attributes that are mass assignable.
*/
protected array $fillable = [];
/**
* The attributes that should be cast to native types.
*/
protected array $casts = ['table_id' => 'integer'];
/**
* @param array $param
*/
public static function list(array $param)
{
return (new self())->when(isset($param['table_name']) && $param['table_name'] != "", function ($query) use ($param) {
$query->where('table_name', "like", "%{$param['table_name']}%");
})->when(isset($param['table_comment']) && $param['table_comment'] != "", function ($query) use ($param) {
$query->where('table_comment', "like", "%{$param['table_comment']}%");
})->select(['table_id', 'table_name', 'table_comment', 'controller_name', 'module_name', 'remark', 'create_time', 'update_time'])
->orderByDesc('table_id')->paginate((int)$param['limit']);
}
public static function genTable(array $rows): bool
{
Db::beginTransaction();
foreach ($rows as $row) {
// 添加主表
$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;
}
public static function editData(array $param): bool
{
Db::beginTransaction();
// 修改主表
$gen_table_columns = $param['gen_table_columns'];
unset($param['gen_table_columns']);
$res = self::edit($param);
if (!$res) {
Db::rollBack();
return false;
}
// 修改子表
$i = 1;
foreach ($gen_table_columns as $column) {
$column['rank'] = $i;
$res = GenTableColumn::edit($column);
$i++;
if (!$res) {
Db::rollBack();
return false;
}
}
Db::commit();
return true;
}
public static function info(int $id)
{
return self::query()
->with(["genTableColumns" => function ($query) {
$query->select(['column_id', 'table_id', 'column_name', 'column_comment', 'column_type', 'php_type',
'php_field', 'is_insert', 'is_edit', 'is_list', 'is_query', 'is_required', 'query_type', 'html_type'])->orderBy("rank");
}])
->where("table_id", $id)
->select(['table_id', 'table_name', 'table_comment', 'controller_name', 'module_name', 'remark', 'create_time', 'update_time'])
->orderByDesc('table_id')
->first()
->toArray();
}
public static function syncTable($table_id)
{
// 查询表信息
$info = self::getById($table_id);
if (empty($info)) {
return false;
}
Db::beginTransaction();
$table = Db::selectOne("SHOW TABLE STATUS WHERE Name = '{$info['table_name']}'");
if (empty($table)) {
Db::rollBack();
return false;
}
// 修改主表信息
$res = self::edit([
'table_id' => $table_id,
'table_name' => $table->Name,
'table_comment' => $table->Comment,
'controller_name' => Str::snakeToCamel(str_replace("pi_", "", $table->Name), true),
'module_name' => 'App\Controller\Admin'
]);
if (!$res) {
Db::rollBack();
return false;
}
// 删除字段
GenTableColumn::where("table_id", $table_id)->delete();
// 修改子表
$columns = Db::select("SHOW FULL COLUMNS FROM `{$table->Name}`");
$i = 1;
foreach ($columns as $column) {
$res = GenTableColumn::add([
'table_id' => $table_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;
}
public function genTableColumns()
{
return $this->hasMany(
GenTableColumn::class,
'table_id',
'table_id'
);
}
}