diff --git a/app/controller/admin/Common.php b/app/controller/admin/Common.php index 4f24c87..707be15 100644 --- a/app/controller/admin/Common.php +++ b/app/controller/admin/Common.php @@ -18,6 +18,7 @@ use app\common\repositories\user\UserVisitRepository; use crmeb\basic\BaseController; use crmeb\services\HttpService; use crmeb\services\UploadService; +use think\facade\Db; use think\facade\Cache; /** @@ -428,5 +429,115 @@ class Common extends BaseController app()->make(CacheRepository::class)->saveAll($data); return app('json')->success('修改成功'); } + + + + public function test(){ + + + // $this->sqlInit(); + + + + debug("调试结束..."); + + } + + + /******** 数据库批量处理 ***************************************/ + private function sqlInit(){ + // 不清除数据的表 + $notCleanUp = []; + // 获取全部数据库列表 + $tables = $this->getAllTable(); + // 获取存在数据的表 + $hasTable = $this->getHasDataTable($tables,$notCleanUp); + + debug($hasTable); + // 执行清除操作 + // foreach($tables as $name){ + // if(!in_array($name,$notCleanUp)){ + // // 判断:是否操作uniacid=0的数据 存在则执行删除存在,不存在则执行清除操作 + // $tableName = str_replace('ims_','',$name); + // if($this->isHasZeroData($name)){ + // // echo $name."=有","
"; + // $columns = \DB::select('SHOW COLUMNS FROM '.$name); + // $fields = array_column($columns,'Field'); + // $primaryKey = $fields[0]; + // $max = \DB::table($tableName)->where('uniacid',0)->max($primaryKey); + // \DB::table($tableName)->where('uniacid','>',0)->delete(); + // // \DB::select('alter table '.$name.' AUTO_INCREMENT='.$max); + // }else{ + // \DB::select('TRUNCATE TABLE '.$name.';'); + // } + // } + // } + + debug("处理完成"); + } + // 数据库处理 - 获取全部数据库 + private function getAllTable(){ + $database = Db::query('SELECT DATABASE() as database_name')[0]['database_name']; + $tables = Db::query('SHOW TABLES'); + return array_column($tables,'Tables_in_'.$database); + } + // 数据库处理 - 获取存在数据的表 + private function getHasDataTable($tables,$notCleanUp){ + $tableList = []; + foreach($tables as $tableName){ + if(!in_array($tableName,$notCleanUp)){ + echo "{$tableName}\n\r"; + // 获取字段信息 + $allFieldDesc = Db::query("show columns from {$tableName}"); + $allField = array_column($allFieldDesc,'Field'); + // if($this->isHasSpecifyData($allField,$name)) $tableList[] = $name; + + + $res = $this->isHasSpecifyData($allField,$tableName); + echo $res ? "存在\n\r" : "不存在\n\r"; + if($res){ + // 批量替换 + $query = "UPDATE {$tableName} SET "; + $handleFieldNum = 0; + foreach($allFieldDesc as $fieldInfo){ + if(strpos($fieldInfo['Type'], 'int') !== false || $fieldInfo['Key'] == 'PRI') continue; + + $fieldName = $fieldInfo['Field']; + $query .= "`{$fieldName}` = REPLACE(`{$fieldName}`,'bt.test.cdlfjy.com','zc.test.cdlfjy.com'),"; + ++$handleFieldNum; + } + $query = trim($query); + $query = trim($query,','); + $query = trim($query); + echo "SQL({$handleFieldNum}):{$query}\n"; + if($handleFieldNum > 0) Db::query($query); + } + + echo "\n\n\n"; + } + } + + return $tableList; + } + // 数据库处理 - 判断当前表是否具有指定内容 + private function isHasSpecifyData($allField,$tableName){ + $query = "select * from {$tableName} where ("; + foreach($allField as $fieldName){ + $query .= "`{$fieldName}` like '%bt.test.cdlfjy.com%' OR "; + } + $query = trim($query); + $query = trim($query,'OR'); + $query = trim($query) . ")"; + + $result = Db::query($query); + if($result) return true; + + return false; + } + + + + + } diff --git a/config/swoole.php b/config/swoole.php index 4a7df64..feaa2e5 100644 --- a/config/swoole.php +++ b/config/swoole.php @@ -9,7 +9,7 @@ use think\swoole\websocket\socketio\Parser; return [ 'server' => [ 'host' => env('SWOOLE_HOST', '0.0.0.0'), // 监听地址 - 'port' => env('SWOOLE_PORT', 8324), // 监听端口 + 'port' => env('SWOOLE_PORT', 8334), // 监听端口 'mode' => SWOOLE_PROCESS, // 运行模式 默认为SWOOLE_PROCESS 'sock_type' => SWOOLE_SOCK_TCP, // sock type 默认为SWOOLE_SOCK_TCP 'options' => [ diff --git a/route/admin.php b/route/admin.php index 0914949..50daec7 100644 --- a/route/admin.php +++ b/route/admin.php @@ -7,6 +7,8 @@ use app\common\middleware\LogMiddleware; use app\common\middleware\AdminAuthMiddleware; use app\common\middleware\AdminTokenMiddleware; +Route::any('admin/test', 'admin.Common/test'); + Route::group(config('admin.admin_prefix'), function () { Route::miss(function () { $DB = DIRECTORY_SEPARATOR;