CopyOldProject.php 3.8 KB
<?php
/**
 * @remark :
 * @name   :CopyOldProject.php
 * @author :lyh
 * @method :post
 * @time   :2025/2/18 14:10
 */

namespace App\Console\Commands\Project;

use App\Services\ProjectServer;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Schema;

class CopyOldProject extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'copy_project_s {old_project_id} {project_id}';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'copy--复制项目';

    public function handle()
    {
        $old_project_id = $this->argument('old_project_id');
        $this->output('CopyProjectJob start, old_project_id: ' . $old_project_id);
        $project_id = $this->argument('project_id');
        $this->output('CopyProjectJob start, project_id: ' . $project_id);
        $this->copyMysql($old_project_id,$project_id);
        return true;
    }



    //复制数据库
    public function copyMysql($project_id,$new_project_id){
        //切换数据库配置
        $project = ProjectServer::useProject($new_project_id);
        //创建数据库
        ProjectServer::createDatabase($project);
        //创建表
        $this->initTable($project_id,$new_project_id);
    }

    /**
     * @remark :创建数据库
     * @name   :initTable
     * @author :lyh
     * @method :post
     * @time   :2023/12/11 10:09
     */
    public function initTable($project_id, $news_project_id)
    {
        // 设置源数据库
        config(['database.connections.custom_tmp_mysql_copy.database' => 'gl_data_' . $project_id]);
        $database_name = DB::connection('custom_tmp_mysql_copy')->getDatabaseName();
        // 获取源数据库的所有表
        $tables = Schema::connection('custom_tmp_mysql_copy')->getAllTables();
        $tables = array_column($tables, 'Tables_in_' . $database_name);
        foreach ($tables as $table) {
            // 目标数据库是否存在该表
            $has_table = Schema::connection('custom_mysql')->hasTable($table);
            if ($has_table) {
                // 1. 删除目标数据库中的表
                DB::connection('custom_mysql')->statement("DROP TABLE IF EXISTS {$table}");
            }
            // 2. 重新创建表
            $sql = DB::connection('custom_tmp_mysql_copy')->select("SHOW CREATE TABLE {$table}");
            DB::connection('custom_mysql')->statement(get_object_vars($sql[0])['Create Table']);
            // 3. 跳过指定的表
            if (in_array($table, [
                'gl_customer_visit',
                'gl_customer_visit_item',
                'gl_inquiry_other',
                'gl_inquiry_form_data',
                'gl_inquiry_form'
            ])) {
                continue;
            }
            // 4. 重新插入数据
            DB::connection('custom_mysql')->table($table)->insertUsing(
                [], // 插入所有列
                function ($query) use ($table, $project_id) {
                    $name = 'gl_data_' . $project_id . '.' . $table;
                    $query->select('*')->from("{$name}");
                }
            );
            // 5. 更新 project_id(如果存在)
            if (Schema::connection('custom_mysql')->hasColumn($table, 'project_id')) {
                DB::connection('custom_mysql')->table($table)->update(['project_id' => $news_project_id]);
            }
        }
        return true;
    }

    /**
     * @param $message
     * @return bool
     */
    public function output($message)
    {
        $date = date('Y-m-d H:i:s');
        $output = $date . ', ' . $message . PHP_EOL;
        echo $output;
        Log::info($output);
        return true;
    }
}