update:优化数据表搜索

This commit is contained in:
JaguarJack 2020-08-10 11:37:34 +08:00
parent e15838386f
commit 03bb91cd9e

View File

@ -6,6 +6,7 @@ use catcher\base\CatchController;
use catcher\CatchResponse; use catcher\CatchResponse;
use catcher\exceptions\FailedException; use catcher\exceptions\FailedException;
use catcher\library\BackUpDatabase; use catcher\library\BackUpDatabase;
use think\Collection;
use think\facade\Console; use think\facade\Console;
use think\facade\Db; use think\facade\Db;
use think\Paginator; use think\Paginator;
@ -22,36 +23,26 @@ class DataDictionary extends CatchController
{ {
$tables = Db::query('show table status'); $tables = Db::query('show table status');
$tablename = $request->get('tablename'); // 重组数据
$engine = $request->get('engine');
$searchTables = [];
$searchMode = false;
if ($tablename || $engine) {
$searchMode = true;
}
foreach ($tables as $key => &$table) { foreach ($tables as $key => &$table) {
$table = array_change_key_case($table); $table = array_change_key_case($table);
$table['index_length'] = $table['index_length'] > 1024 ? intval($table['index_length']/1024) .'MB' : $table['index_length'].'KB'; $table['index_length'] = $table['index_length'] > 1024 ? intval($table['index_length']/1024) .'MB' : $table['index_length'].'KB';
$table['data_length'] = $table['data_length'] > 1024 ? intval($table['data_length']/1024) .'MB' : $table['data_length'].'KB'; $table['data_length'] = $table['data_length'] > 1024 ? intval($table['data_length']/1024) .'MB' : $table['data_length'].'KB';
$table['create_time'] = date('Y-m-d', strtotime($table['create_time'])); $table['create_time'] = date('Y-m-d', strtotime($table['create_time']));
// 搜索
if ($tablename && !$engine && stripos($table['name'], $tablename) !== false) {
$searchTables[] = $table;
}
// 搜索
if (!$tablename && $engine && stripos($table['engine'], $engine) !== false) {
$searchTables[] = $table;
}
if ($tablename && $engine && stripos($table['engine'], $engine) !== false && stripos($table['name'], $tablename) !== false) {
$searchTables[] = $table;
}
} }
// 搜素
$tables = new Collection($tables);
// 名称搜索
if ($name = $request->get('tablename', null)) {
$tables = $tables->where('name', 'like', $name)->values();
}
// 引擎搜索
if ($engine = $request->get('engine', null)) {
$tables = $tables->where('engine', $engine)->values();
}
return CatchResponse::paginate(Paginator::make(!$searchMode ? $tables : $searchTables, $request->get('limit') ?? 10, $request->get('page') ?? 1, $searchMode ? count($searchTables) : count($tables), false, [])); return CatchResponse::paginate(Paginator::make($tables->toArray(), $request->get('limit') ?? 10, $request->get('page') ?? 1, $tables->count(), false, []));
} }
/** /**