作者 李宇航

合并分支 'master-server' 到 'master'

Master server



查看合并请求 !1135
  1 +<?php
  2 +/**
  3 + * @remark :
  4 + * @name :CopyOldProject.php
  5 + * @author :lyh
  6 + * @method :post
  7 + * @time :2025/2/18 14:10
  8 + */
  9 +
  10 +namespace App\Console\Commands\Project;
  11 +
  12 +use App\Services\ProjectServer;
  13 +use Illuminate\Console\Command;
  14 +use Illuminate\Support\Facades\DB;
  15 +use Illuminate\Support\Facades\Log;
  16 +use Illuminate\Support\Facades\Schema;
  17 +
  18 +class CopyOldProject extends Command
  19 +{
  20 + /**
  21 + * The name and signature of the console command.
  22 + *
  23 + * @var string
  24 + */
  25 + protected $signature = 'copy_project_s {old_project_id} {project_id}';
  26 +
  27 + /**
  28 + * The console command description.
  29 + *
  30 + * @var string
  31 + */
  32 + protected $description = 'copy--复制项目';
  33 +
  34 + public function handle()
  35 + {
  36 + $old_project_id = $this->argument('old_project_id');
  37 + $this->output('CopyProjectJob start, old_project_id: ' . $old_project_id);
  38 + $project_id = $this->argument('project_id');
  39 + $this->output('CopyProjectJob start, project_id: ' . $project_id);
  40 + $this->copyMysql($old_project_id,$project_id);
  41 + return true;
  42 + }
  43 +
  44 +
  45 +
  46 + //复制数据库
  47 + public function copyMysql($project_id,$new_project_id){
  48 + //切换数据库配置
  49 + $project = ProjectServer::useProject($new_project_id);
  50 + //创建数据库
  51 + ProjectServer::createDatabase($project);
  52 + //创建表
  53 + $this->initTable($project_id,$new_project_id);
  54 + }
  55 +
  56 + /**
  57 + * @remark :创建数据库
  58 + * @name :initTable
  59 + * @author :lyh
  60 + * @method :post
  61 + * @time :2023/12/11 10:09
  62 + */
  63 + public function initTable($project_id,$news_project_id)
  64 + {
  65 + config(['database.connections.custom_tmp_mysql_copy.database' => 'gl_data_'.$project_id]);
  66 + $database_name = DB::connection('custom_tmp_mysql_copy')->getDatabaseName();
  67 + $tables = Schema::connection('custom_tmp_mysql_copy')->getAllTables();
  68 + $tables = array_column($tables, 'Tables_in_' . $database_name);
  69 + foreach ($tables as $table) {
  70 + $has_table = Schema::connection('custom_mysql')->hasTable($table);
  71 + if (!$has_table) {
  72 + $sql = DB::connection('custom_tmp_mysql_copy')->select("SHOW CREATE TABLE {$table}");
  73 + DB::connection('custom_mysql')->statement(get_object_vars($sql[0])['Create Table']);
  74 + }
  75 + if($table == 'gl_customer_visit' || $table == 'gl_customer_visit_item' || $table == 'gl_inquiry_other' || $table == 'gl_inquiry_form_data' || $table == 'gl_inquiry_form'){
  76 + continue;
  77 + }
  78 + DB::connection('custom_mysql')->table($table)->truncate(); // 清空目标表数据
  79 + DB::connection('custom_mysql')->table($table)->insertUsing(
  80 + [], // 列名数组,留空表示插入所有列
  81 + function ($query) use ($table,$project_id) {
  82 + $name = 'gl_data_'.$project_id.'.'.$table;
  83 + $query->select('*')->from("{$name}");
  84 + }
  85 + );
  86 + if (Schema::connection('custom_mysql')->hasColumn($table, 'project_id')) {
  87 + DB::connection('custom_mysql')->table($table)->update(['project_id' => $news_project_id]);
  88 + }
  89 + }
  90 + return true;
  91 + }
  92 +
  93 + /**
  94 + * @param $message
  95 + * @return bool
  96 + */
  97 + public function output($message)
  98 + {
  99 + $date = date('Y-m-d H:i:s');
  100 + $output = $date . ', ' . $message . PHP_EOL;
  101 + echo $output;
  102 + Log::info($output);
  103 + return true;
  104 + }
  105 +}
@@ -42,6 +42,7 @@ use App\Models\Template\BCustomTemplate; @@ -42,6 +42,7 @@ use App\Models\Template\BCustomTemplate;
42 use App\Models\Template\BTemplateCom; 42 use App\Models\Template\BTemplateCom;
43 use App\Models\Template\Setting; 43 use App\Models\Template\Setting;
44 use App\Models\Template\Template; 44 use App\Models\Template\Template;
  45 +use Hashids\Hashids;
45 use Illuminate\Console\Command; 46 use Illuminate\Console\Command;
46 use Illuminate\Support\Facades\Config; 47 use Illuminate\Support\Facades\Config;
47 use Illuminate\Support\Facades\DB; 48 use Illuminate\Support\Facades\DB;
@@ -66,12 +67,33 @@ class Demo extends Command @@ -66,12 +67,33 @@ class Demo extends Command
66 protected $description = 'demo'; 67 protected $description = 'demo';
67 68
68 public function handle(){ 69 public function handle(){
69 -// echo date('Y-m-d H:i:s') . 'project_id:' . PHP_EOL;  
70 -// ProjectServer::useProject(3092);  
71 -// $this->delProduct();  
72 -// DB::disconnect('custom_mysql');  
73 -// echo date('Y-m-d H:i:s') . 'end' . PHP_EOL;  
74 - return $this->projectList(); 70 + $this->copyMysql($old_project_id,$project_id);
  71 + }
  72 + public function copyProject($old_project_id){
  73 + $projectModel = new Project();
  74 + $data = $projectModel::where('id', $old_project_id)->first();
  75 + $data = $data->getAttributes();
  76 + $type = $data['type'];
  77 + $data['type'] = 0;
  78 + $data['status'] = 0;
  79 + $data['finish_remain_day'] = 0;
  80 + $data['title'] = $data['title'].'-copy';
  81 + $data['delete_status'] = 1;
  82 + unset($data['id'],$data['robots'],$data['is_translate_tag'],$data['is_translate'],$data['is_minor_languages'],$data['uptime']);
  83 + $project_id = $projectModel->insertGetId($data);
  84 + $hashids = new Hashids($data['from_order_id'], 13, 'abcdefghjkmnpqrstuvwxyz1234567890');
  85 + $projectModel->edit(['from_order_id'=>$hashids->encode($project_id)],['id'=>$project_id]);
  86 + //复制设置的模版
  87 + $settingTemplateModel = new Setting();
  88 + $settingData = $settingTemplateModel::where('project_id', $old_project_id)->first();
  89 + if(!empty($settingData)){
  90 + $data = [
  91 + 'template_id' =>$settingData['template_id'],
  92 + 'project_id' => $project_id
  93 + ];
  94 + $settingTemplateModel->add($data);
  95 + }
  96 + return ['project_id'=>$project_id,'type'=>$type];
75 } 97 }
76 98
77 public function toQueue(){ 99 public function toQueue(){
@@ -818,6 +818,12 @@ class ProjectLogic extends BaseLogic @@ -818,6 +818,12 @@ class ProjectLogic extends BaseLogic
818 * @time :2023/11/8 14:23 818 * @time :2023/11/8 14:23
819 */ 819 */
820 public function copyProject(){ 820 public function copyProject(){
  821 + //查看当前是否有执行任务
  822 + $noticeModel = new NoticeLog();
  823 + $info = $noticeModel->read(['type'=>NoticeLog::TYPE_COPY_PROJECT,'status'=>0,'data'=>['like','%"'.$this->param['project_id'].'"%']]);
  824 + if($info !== false){
  825 + return $this->success('当前项目已在复制中');
  826 + }
821 NoticeLog::createLog(NoticeLog::TYPE_COPY_PROJECT, ['project_id' => $this->param['project_id']]); 827 NoticeLog::createLog(NoticeLog::TYPE_COPY_PROJECT, ['project_id' => $this->param['project_id']]);
822 return $this->success('项目复制中,请稍后前往初始化项目查看;'); 828 return $this->success('项目复制中,请稍后前往初始化项目查看;');
823 } 829 }