作者 lyh

gx服务器排序字段

  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\Models\Project\Project;
  13 +use App\Models\Template\Setting;
  14 +use App\Services\ProjectServer;
  15 +use Hashids\Hashids;
  16 +use Illuminate\Console\Command;
  17 +use Illuminate\Support\Facades\DB;
  18 +use Illuminate\Support\Facades\Log;
  19 +use Illuminate\Support\Facades\Schema;
  20 +
  21 +class CopyOldProject extends Command
  22 +{
  23 + /**
  24 + * The name and signature of the console command.
  25 + *
  26 + * @var string
  27 + */
  28 + protected $signature = 'copy_project_s {old_project_id} {project_id}';
  29 +
  30 + /**
  31 + * The console command description.
  32 + *
  33 + * @var string
  34 + */
  35 + protected $description = 'copy--复制项目';
  36 +
  37 + public function handle()
  38 + {
  39 + $old_project_id = $this->argument('old_project_id');
  40 + $this->output('CopyProjectJob start, old_project_id: ' . $old_project_id);
  41 + $project_id = $this->argument('project_id');
  42 + $this->output('CopyProjectJob start, project_id: ' . $project_id);
  43 + $this->copyMysql($old_project_id,$project_id);
  44 + return true;
  45 + }
  46 +
  47 +
  48 +
  49 + //复制数据库
  50 + public function copyMysql($project_id,$new_project_id){
  51 + //切换数据库配置
  52 + $project = ProjectServer::useProject($new_project_id);
  53 + //创建数据库
  54 + ProjectServer::createDatabase($project);
  55 + //创建表
  56 + $this->initTable($project_id,$new_project_id);
  57 + }
  58 +
  59 + /**
  60 + * @remark :创建数据库
  61 + * @name :initTable
  62 + * @author :lyh
  63 + * @method :post
  64 + * @time :2023/12/11 10:09
  65 + */
  66 + public function initTable($project_id,$news_project_id)
  67 + {
  68 + config(['database.connections.custom_tmp_mysql_copy.database' => 'gl_data_'.$project_id]);
  69 + $database_name = DB::connection('custom_tmp_mysql_copy')->getDatabaseName();
  70 + $tables = Schema::connection('custom_tmp_mysql_copy')->getAllTables();
  71 + $tables = array_column($tables, 'Tables_in_' . $database_name);
  72 + foreach ($tables as $table) {
  73 + $has_table = Schema::connection('custom_mysql')->hasTable($table);
  74 + if (!$has_table) {
  75 + $sql = DB::connection('custom_tmp_mysql_copy')->select("SHOW CREATE TABLE {$table}");
  76 + DB::connection('custom_mysql')->statement(get_object_vars($sql[0])['Create Table']);
  77 + }
  78 + if($table == 'gl_customer_visit' || $table == 'gl_customer_visit_item' || $table == 'gl_inquiry_other' || $table == 'gl_inquiry_form_data' || $table == 'gl_inquiry_form'){
  79 + continue;
  80 + }
  81 + DB::connection('custom_mysql')->table($table)->truncate(); // 清空目标表数据
  82 + DB::connection('custom_mysql')->table($table)->insertUsing(
  83 + [], // 列名数组,留空表示插入所有列
  84 + function ($query) use ($table,$project_id) {
  85 + $name = 'gl_data_'.$project_id.'.'.$table;
  86 + $query->select('*')->from("{$name}");
  87 + }
  88 + );
  89 + if (Schema::connection('custom_mysql')->hasColumn($table, 'project_id')) {
  90 + DB::connection('custom_mysql')->table($table)->update(['project_id' => $news_project_id]);
  91 + }
  92 + }
  93 + return true;
  94 + }
  95 +
  96 + /**
  97 + * @param $message
  98 + * @return bool
  99 + */
  100 + public function output($message)
  101 + {
  102 + $date = date('Y-m-d H:i:s');
  103 + $output = $date . ', ' . $message . PHP_EOL;
  104 + echo $output;
  105 + Log::info($output);
  106 + return true;
  107 + }
  108 +}
@@ -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 }