作者 lyh

gx

  1 +<?php
  2 +
  3 +namespace App\Listeners;
  4 +
  5 +use App\Events\CopyProject;
  6 +use App\Events\UpdateHtml;
  7 +use App\Jobs\updateHtmlJob;
  8 +use App\Models\Project\After;
  9 +use App\Models\Project\DeployBuild;
  10 +use App\Models\Project\DeployOptimize;
  11 +use App\Models\Project\Payment;
  12 +use App\Models\Project\Project;
  13 +use App\Models\Template\Setting;
  14 +use App\Models\User\User as UserModel;
  15 +use App\Services\ProjectServer;
  16 +use Hashids\Hashids;
  17 +use Illuminate\Contracts\Queue\ShouldQueue;
  18 +use Illuminate\Support\Facades\DB;
  19 +use Illuminate\Support\Facades\Schema;
  20 +
  21 +class CopyProjectListener implements ShouldQueue
  22 +{
  23 + public function __construct()
  24 + {
  25 + //
  26 + }
  27 +
  28 + /**
  29 + * Handle the event.
  30 + *
  31 + * @param UpdateHtml $event
  32 + * @return void
  33 + */
  34 + public function handle(CopyProject $event)
  35 + {
  36 + $this->param = $event->data;
  37 + $this->model = new Project();
  38 + DB::beginTransaction();
  39 + try {
  40 + //复制初始项目
  41 + $data = $this->model::where('id', $this->param['project_id'])->first();
  42 + $data = $data->getAttributes();
  43 + $type = $data['type'];
  44 + $data['type'] = 0;
  45 + $data['title'] = $data['title'].'-copy';
  46 + unset($data['id']);
  47 + $project_id = $this->model->insertGetId($data);
  48 + //复制部署表
  49 + $buildModel = new DeployBuild();
  50 + $buildData = $buildModel::where('project_id', $this->param['project_id'])->first();
  51 + if(!empty($buildData)){
  52 + $buildData = $buildData->getAttributes();
  53 + $buildData['project_id'] = $project_id;
  54 + $hashids = new Hashids('test_domain', 5, 'abcdefghjkmnpqrstuvwxyz1234567890');
  55 + $code = $hashids->encode($project_id);
  56 + $buildData['test_domain'] = 'https://v6-' . $code . '.globalso.site/';
  57 + unset($buildData['id']);
  58 + $buildModel->insert($buildData);
  59 + }
  60 + //复制优化表
  61 + $optimizeModel = new DeployOptimize();
  62 + $optimizeData = $optimizeModel::where('project_id', $this->param['project_id'])->first();
  63 + if(!empty($optimizeData)){
  64 + $optimizeData = $optimizeData->getAttributes();
  65 + unset($optimizeData['id'],$optimizeData['domain']);
  66 + $optimizeData['project_id'] = $project_id;
  67 + $optimizeModel->insert($optimizeData);
  68 + }
  69 + //复制付费表
  70 + $paymentModel = new Payment();
  71 + $paymentData = $paymentModel::where('project_id', $this->param['project_id'])->first();
  72 + if(!empty($paymentData)){
  73 + $paymentData = $paymentData->getAttributes();
  74 + unset($paymentData['id']);
  75 + $paymentData['project_id'] = $project_id;
  76 + $paymentModel->insert($paymentData);
  77 + }
  78 + //复制售后表
  79 + $afterModel = new After();
  80 + $afterData = $afterModel::where('project_id', $this->param['project_id'])->first();
  81 + if(!empty($afterData)){
  82 + $afterData = $afterData->getAttributes();
  83 + unset($afterData['id']);
  84 + $afterData['project_id'] = $project_id;
  85 + $afterModel->insert($afterData);
  86 + }
  87 + //复制用户
  88 + $userModel = new UserModel();
  89 + $userData = $userModel::where('project_id', $this->param['project_id'])->where('role_id',0)->first();
  90 + if(!empty($userData)){
  91 + $userData = $userData->getAttributes();
  92 + unset($userData['id']);
  93 + $userData['project_id'] = $project_id;
  94 + $userModel->insert($userData);
  95 + }
  96 + //复制设置的模版
  97 + $settingTemplateModel = new Setting();
  98 + $settingData = $settingTemplateModel::where('project_id', $this->param['project_id'])->first();
  99 + if(!empty($settingData)){
  100 + $settingData = $settingData->getAttributes();
  101 + unset($settingData['id']);
  102 + $settingData['project_id'] = $project_id;
  103 + $settingTemplateModel->insert($settingData);
  104 + }
  105 + DB::commit();
  106 + }catch (\Exception $e){
  107 + DB::rollBack();
  108 + $this->fail('error');
  109 + }
  110 + if($type != 0){
  111 + $this->copyMysql($this->param['project_id'],$project_id);
  112 + }
  113 + return true;
  114 + }
  115 +
  116 + //复制数据库
  117 + public function copyMysql($project_id,$new_project_id){
  118 + //切换数据库配置
  119 + $project = ProjectServer::useProject($new_project_id);
  120 + //创建数据库
  121 + ProjectServer::createDatabase($project);
  122 + //创建表
  123 + $this->initTable($project_id,$new_project_id);
  124 + }
  125 +
  126 + /**
  127 + * @remark :创建数据库
  128 + * @name :initTable
  129 + * @author :lyh
  130 + * @method :post
  131 + * @time :2023/12/11 10:09
  132 + */
  133 + public function initTable($project_id,$news_project_id)
  134 + {
  135 + config(['database.connections.custom_tmp_mysql_copy.database' => 'gl_data_'.$project_id]);
  136 + $database_name = DB::connection('custom_tmp_mysql_copy')->getDatabaseName();
  137 + $tables = Schema::connection('custom_tmp_mysql_copy')->getAllTables();
  138 + $tables = array_column($tables, 'Tables_in_' . $database_name);
  139 + foreach ($tables as $table) {
  140 + $has_table = Schema::connection('custom_mysql')->hasTable($table);
  141 + if (!$has_table) {
  142 + $sql = DB::connection('custom_tmp_mysql_copy')->select("SHOW CREATE TABLE {$table}");
  143 + DB::connection('custom_mysql')->statement(get_object_vars($sql[0])['Create Table']);
  144 + }
  145 + DB::connection('custom_mysql')->table($table)->truncate(); // 清空目标表数据
  146 + DB::connection('custom_mysql')->table($table)->insertUsing(
  147 + [], // 列名数组,留空表示插入所有列
  148 + function ($query) use ($table,$project_id) {
  149 + $name = 'gl_data_'.$project_id.'.'.$table;
  150 + $query->select('*')->from("{$name}");
  151 + }
  152 + );
  153 + if (Schema::connection('custom_mysql')->hasColumn($table, 'project_id')) {
  154 + DB::connection('custom_mysql')->table($table)->update(['project_id' => $news_project_id]);
  155 + }
  156 + }
  157 + return true;
  158 + }
  159 +}