You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

162 lines
5.7 KiB

<?php
namespace App\Models;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Schema;
use Illuminate\Support\Str;
class CustomForm extends SoftDeletesModel
{
public static $cache_ttl = 60;
/**
* 关联关系
* @return \Illuminate\Database\Eloquent\Relations\HasMany
*/
public function relation()
{
return $this->hasMany(CustomFormRelation::class, 'custom_form_id', 'id');
}
public function fields()
{
return $this->hasMany(CustomFormField::class, 'custom_form_id', 'id');
}
/**
* 判断自定义表单是否存在表名
*/
public function hasTable($tableName)
{
return Cache::remember('has_table_' . $tableName, CustomForm::$cache_ttl, function () use ($tableName) {
return self::where('table_name', $tableName)->first();
});
}
/**
* 根据表名获取模型
* @param $tableName
* @return string
*/
public static function tableToModel($tableName)
{
// 将表名转换为模型命名格式
$modelName = Str::studly(Str::singular($tableName));
// 拼接完整的模型类名
$fullModelName = "App\\Models\\" . $modelName;
// 检查模型类是否存在
if (class_exists($fullModelName)) {
// 实例化模型
return new $fullModelName();
}
// 如果模型类不存在,则返回 null 或抛出异常
return null;
}
/**
* 获取实体表备注
* @param $tableName
* @return null
*/
public function getTableComment($tableName)
{
$tableComment = DB::select(DB::raw("SELECT table_comment FROM information_schema.tables WHERE table_name = ? AND table_schema = DATABASE()"), [$tableName]);
if (!empty($tableComment)) {
return $tableComment[0]->table_comment;
}
return $tableName;
}
/**
* 判断是否存在实体表
*/
public function hasRealTable($tableName)
{
return Cache::remember('has_real_table_' . $tableName, CustomForm::$cache_ttl, function () use ($tableName) {
return Schema::hasTable($tableName);
});
}
/**
* 创建表
* @param $tableName
* @return bool
*/
public function createTable($customFormId)
{
$customForm = self::find($customFormId);
if (!Schema::hasTable($customForm->table_name)) {
Schema::create($customForm->table_name, function (Blueprint $table) {
// 核心字段
$table->increments('id')->comment('Id');
$table->integer('admin_id')->nullable()->comment('管理员id');
$table->integer('department_id')->nullable()->comment('部门id');
$table->dateTime('created_at')->nullable()->comment('创建时间');
$table->dateTime('updated_at')->nullable()->comment('更新时间');
$table->dateTime('deleted_at')->nullable()->comment('删除时间');
});
}
return true;
}
/**
* 更新表
*/
public function updateTable($customFormId)
{
try {
$customForm = self::find($customFormId);
$customFormField = new CustomFormField();
// 如果实体表不存在则先创建
if (!Schema::hasTable($customForm->table_name)) $this->createTable($customFormId);
// 获取数据表里的字段配置信息
$customFormFileds = $customFormField->where('custom_form_id', $customForm->id)->get();
// 获取实体数据表里的非白名单字段
$rowTableFields = $customFormField->getRowTableFieldsByType($customForm->table_name, false, false);
// 获取实体数据表里字段和注释
$rowTableFieldsByComment = $customFormField->getRowTableFieldsByComment($customForm->table_name);
Schema::table($customForm->table_name, function (Blueprint $table) use ($customFormFileds, $rowTableFields, $rowTableFieldsByComment) {
// 其他字段。获取字段映射关系
$editToMigration = EditToMigration::getAll();
// 修正和新增字段
foreach ($customFormFileds as $item) {
$filedType = $editToMigration[$item->edit_input];
if (isset($rowTableFields[$item->field])) {
// 字段存在,如果类型不一样或者备注不一样,则修改
if ($rowTableFields[$item->field] != $editToMigration[$item->edit_input] || $item->name != $rowTableFieldsByComment[$item->field]) {
$table->$filedType($item->field)->comment($item->name)->nullable()->change();
}
} else {
// 字段不存在则创建
$table->$filedType($item->field)->comment($item->name)->nullable();
}
}
});
// 删除多余字段
$this->delMoreFields($customForm->table_name, $customFormFileds->pluck('field')->toArray(), $rowTableFields);
return '数据表更新成功';
} catch (\Exception $exception) {
return $exception->getMessage();
}
}
/**
* 删除实体数据表冗余字段
*/
public function delMoreFields($tableName, $customFormFileds, $rowTableFields)
{
foreach ($rowTableFields as $key => $item) {
if (!in_array($key, $customFormFileds)) {
Schema::dropColumns($tableName, $key);
}
}
return true;
}
}