作者 赵彬吉
<?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;
}
}
... ...
... ... @@ -224,29 +224,43 @@ class CopyProject extends Command
* @method :post
* @time :2023/12/11 10:09
*/
public function initTable($project_id,$news_project_id)
public function initTable($project_id, $news_project_id)
{
config(['database.connections.custom_tmp_mysql_copy.database' => 'gl_data_'.$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) {
$sql = DB::connection('custom_tmp_mysql_copy')->select("SHOW CREATE TABLE {$table}");
DB::connection('custom_mysql')->statement(get_object_vars($sql[0])['Create Table']);
if ($has_table) {
// 1. 删除目标数据库中的表
DB::connection('custom_mysql')->statement("DROP TABLE IF EXISTS {$table}");
}
if($table == 'gl_customer_visit' || $table == 'gl_customer_visit_item' || $table == 'gl_inquiry_other' || $table == 'gl_inquiry_form_data' || $table == 'gl_inquiry_form'){
// 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;
}
DB::connection('custom_mysql')->table($table)->truncate(); // 清空目标表数据
// 4. 重新插入数据
DB::connection('custom_mysql')->table($table)->insertUsing(
[], // 列名数组,留空表示插入所有列
function ($query) use ($table,$project_id) {
$name = 'gl_data_'.$project_id.'.'.$table;
[], // 插入所有列
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]);
}
... ...
... ... @@ -42,6 +42,7 @@ use App\Models\Template\BCustomTemplate;
use App\Models\Template\BTemplateCom;
use App\Models\Template\Setting;
use App\Models\Template\Template;
use Hashids\Hashids;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\DB;
... ... @@ -66,12 +67,33 @@ class Demo extends Command
protected $description = 'demo';
public function handle(){
// echo date('Y-m-d H:i:s') . 'project_id:' . PHP_EOL;
// ProjectServer::useProject(3092);
// $this->delProduct();
// DB::disconnect('custom_mysql');
// echo date('Y-m-d H:i:s') . 'end' . PHP_EOL;
return $this->projectList();
$this->copyMysql($old_project_id,$project_id);
}
public function copyProject($old_project_id){
$projectModel = new Project();
$data = $projectModel::where('id', $old_project_id)->first();
$data = $data->getAttributes();
$type = $data['type'];
$data['type'] = 0;
$data['status'] = 0;
$data['finish_remain_day'] = 0;
$data['title'] = $data['title'].'-copy';
$data['delete_status'] = 1;
unset($data['id'],$data['robots'],$data['is_translate_tag'],$data['is_translate'],$data['is_minor_languages'],$data['uptime']);
$project_id = $projectModel->insertGetId($data);
$hashids = new Hashids($data['from_order_id'], 13, 'abcdefghjkmnpqrstuvwxyz1234567890');
$projectModel->edit(['from_order_id'=>$hashids->encode($project_id)],['id'=>$project_id]);
//复制设置的模版
$settingTemplateModel = new Setting();
$settingData = $settingTemplateModel::where('project_id', $old_project_id)->first();
if(!empty($settingData)){
$data = [
'template_id' =>$settingData['template_id'],
'project_id' => $project_id
];
$settingTemplateModel->add($data);
}
return ['project_id'=>$project_id,'type'=>$type];
}
public function toQueue(){
... ...
... ... @@ -121,6 +121,10 @@ class ComController extends BaseController
if($is_comment != 1){
$info['role_menu'] = trim(str_replace(',55,',',',','.$info['role_menu'].','),',');
}
$is_blogs = $this->getIsBlog();
if($is_blogs != 1){
$info['role_menu'] = trim(str_replace(',57,',',',','.$info['role_menu'].','),',');
}
$this->map = [
'status'=>0,
'is_role'=>0,
... ... @@ -165,6 +169,10 @@ class ComController extends BaseController
if($is_comment != 1){
$data[] = 55;
}
$is_ai_blog = $this->getIsAiBlog();
if($is_ai_blog != 1){
$data[] = 57;
}
if(!empty($data)){
$this->map['id'] = ['not in',$data];
}
... ... @@ -236,7 +244,7 @@ class ComController extends BaseController
public function getIsSubscribe(){
return $this->project['is_subscribe'];
return $this->project['is_subscribe'] ?? 0;
}
/**
... ... @@ -251,26 +259,33 @@ class ComController extends BaseController
}
/**
* @remark :是否开启了ai——blog
* @name :getIsAiBlog
* @author :lyh
* @method :post
* @time :2025/2/19 9:39
*/
public function getIsAiBlog(){
return $this->user['is_ai_blog'] ?? 0;
}
/**
* @name :登录用户编辑资料/修改密码
* @author :liyuhang
* @method
*/
public function edit_info(){
$this->request->validate([
// 'oldPassword'=>'required',
'password' => 'required',
'confirm'=>'required',
], [
// 'oldPassword.required' => '请输入原密码',
'password.required' => '请输入新密码',
'confirm.required' => '请再次输入新密码',
]);
//查询员密码是否正确
$userModel = new User();
$info = $userModel->read(['id'=>$this->user['id']]);
// if($info['password'] != base64_encode(md5($this->param['oldPassword']))){
// $this->response('原密码错误',Code::USER_ERROR);
// }
if($this->param['password'] != $this->param['confirm']){
$this->response('两次密码不一致');
}
... ... @@ -408,7 +423,6 @@ class ComController extends BaseController
*/
public function month_counts(){
shell_exec('php artisan month_project '.$this->user['project_id']);
// Artisan::call('month_project '.$this->user['project_id']);
$this->response('重新刷新中,请稍后刷新查询');
}
}
... ...
... ... @@ -818,6 +818,12 @@ class ProjectLogic extends BaseLogic
* @time :2023/11/8 14:23
*/
public function copyProject(){
//查看当前是否有执行任务
$noticeModel = new NoticeLog();
$info = $noticeModel->read(['type'=>NoticeLog::TYPE_COPY_PROJECT,'status'=>0,'data'=>['like','%"'.$this->param['project_id'].'"%']]);
if($info !== false){
return $this->success('当前项目已在复制中');
}
NoticeLog::createLog(NoticeLog::TYPE_COPY_PROJECT, ['project_id' => $this->param['project_id']]);
return $this->success('项目复制中,请稍后前往初始化项目查看;');
}
... ...
... ... @@ -262,6 +262,7 @@ class UserLoginLogic
$info['is_show_blog'] = $project['is_show_blog'];
$info['upload_config'] = $project['upload_config'];
$info['main_lang_id'] = $project['main_lang_id'];
$info['is_ai_blog'] = $project['is_ai_blog'];
$info['image_max'] = $project['image_max'];
$info['is_del_inquiry'] = $project['is_del_inquiry'] ?? 0;
$info['uptime_type'] = $this->getHistory($project);
... ...