作者 lyh

gxai

@@ -20,6 +20,7 @@ use App\Models\User\User as UserModel; @@ -20,6 +20,7 @@ use App\Models\User\User as UserModel;
20 use App\Services\ProjectServer; 20 use App\Services\ProjectServer;
21 use Hashids\Hashids; 21 use Hashids\Hashids;
22 use Illuminate\Console\Command; 22 use Illuminate\Console\Command;
  23 +use Illuminate\Support\Facades\Artisan;
23 use Illuminate\Support\Facades\DB; 24 use Illuminate\Support\Facades\DB;
24 use Illuminate\Support\Facades\Log; 25 use Illuminate\Support\Facades\Log;
25 use Illuminate\Support\Facades\Schema; 26 use Illuminate\Support\Facades\Schema;
@@ -205,62 +206,13 @@ class CopyProject extends Command @@ -205,62 +206,13 @@ class CopyProject extends Command
205 } 206 }
206 //复制数据库 207 //复制数据库
207 public function copyMysql($project_id,$new_project_id){ 208 public function copyMysql($project_id,$new_project_id){
208 - //切换数据库配置  
209 - $project = ProjectServer::useProject($new_project_id);  
210 - //创建数据库  
211 - ProjectServer::createDatabase($project);  
212 - //创建表  
213 - $this->initTable($project_id,$new_project_id); 209 + Artisan::call("php artisan copy_project_s $project_id $new_project_id");
214 //修改项目状态 210 //修改项目状态
215 $projectModel = new Project(); 211 $projectModel = new Project();
216 $projectModel->edit(['delete_status'=>0],['id'=>$new_project_id]); 212 $projectModel->edit(['delete_status'=>0],['id'=>$new_project_id]);
217 } 213 }
218 214
219 /** 215 /**
220 - * @remark :创建数据库  
221 - * @name :initTable  
222 - * @author :lyh  
223 - * @method :post  
224 - * @time :2023/12/11 10:09  
225 - */  
226 - public function initTable($project_id, $news_project_id)  
227 - {  
228 - // 设置源数据库  
229 - config(['database.connections.custom_tmp_mysql_copy.database' => 'gl_data_' . $project_id]);  
230 - $database_name = DB::connection('custom_tmp_mysql_copy')->getDatabaseName();  
231 - // 获取源数据库的所有表  
232 - $tables = Schema::connection('custom_tmp_mysql_copy')->getAllTables();  
233 - $tables = array_column($tables, 'Tables_in_' . $database_name);  
234 - foreach ($tables as $table) {  
235 - // 1. 删除目标数据库中的表  
236 - DB::connection('custom_mysql')->statement("DROP TABLE IF EXISTS {$table}");  
237 - // 2. 重新创建表  
238 - $sql = DB::connection('custom_tmp_mysql_copy')->select("SHOW CREATE TABLE {$table}");  
239 - DB::connection('custom_mysql')->statement(get_object_vars($sql[0])['Create Table']);  
240 - // 3. 跳过指定的表  
241 - if (in_array($table, ['gl_customer_visit', 'gl_customer_visit_item', 'gl_inquiry_other', 'gl_inquiry_form_data', 'gl_inquiry_form'])) {  
242 - continue;  
243 - }  
244 - try {  
245 - // 4. 重新插入数据  
246 - DB::connection('custom_mysql')->table($table)->insertUsing(  
247 - [], // 插入所有列  
248 - function ($query) use ($table, $project_id) {  
249 - $name = 'gl_data_' . $project_id . '.' . $table;  
250 - $query->select('*')->from("{$name}");  
251 - }  
252 - );  
253 - }catch (\Exception $e){  
254 - continue;  
255 - }  
256 - // 5. 更新 project_id(如果存在)  
257 - if (Schema::connection('custom_mysql')->hasColumn($table, 'project_id')) {  
258 - DB::connection('custom_mysql')->table($table)->update(['project_id' => $news_project_id]);  
259 - }  
260 - }  
261 - return true;  
262 - }  
263 - /**  
264 * @param $message 216 * @param $message
265 * @return bool 217 * @return bool
266 */ 218 */