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.
139 lines
4.9 KiB
139 lines
4.9 KiB
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use App\Exceptions\ErrorException;
|
|
use Illuminate\Database\Schema\Blueprint;
|
|
use Illuminate\Support\Facades\Cache;
|
|
use Illuminate\Support\Facades\Schema;
|
|
|
|
class CustomForm extends SoftDeletesModel
|
|
{
|
|
public static $cache_ttl = 60;
|
|
|
|
/**
|
|
* 获取请求中的表名
|
|
*/
|
|
public static function tableName()
|
|
{
|
|
$tableName = request('table_name');
|
|
if (empty($tableName)) {
|
|
throw new ErrorException('表名不存在');
|
|
}
|
|
return $tableName;
|
|
}
|
|
|
|
/**
|
|
* 关联关系
|
|
* @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 customForm($tableName = null)
|
|
{
|
|
if (empty($tableName)) $tableName = self::tableName();
|
|
return Cache::remember('has_table_' . $tableName, CustomForm::$cache_ttl, function () use ($tableName) {
|
|
return self::where('table_name', $tableName)->first();
|
|
});
|
|
}
|
|
|
|
/**
|
|
* 判断是否存在实体表
|
|
*/
|
|
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->rowTableFieldsByType($customForm->table_name, 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;
|
|
}
|
|
|
|
}
|