作者 lyh

变更数据

... ... @@ -30,6 +30,8 @@ class GeoQuestionRes extends Command
*/
protected $signature = 'geo_question_result';
public $porject_id;//记录当时执行的project_id
/**
* The console command description.
*
... ... @@ -337,15 +339,18 @@ class GeoQuestionRes extends Command
$key = 'geo_task_list';
$task_id = Redis::rpop($key);
if(empty($task_id)){
//todo::这里需要执行统计一次,统计当前项目当前日期的统计
# TODO 按照项目进行获取, 一个项目当天需要将所有跑完
$project_id = GeoQuestion::where('status', GeoQuestion::STATUS_OPEN)->where('next_time', '<=', date('Y-m-d'))->value('project_id');
if (empty($project_id))
return $task_id;
$ids = GeoQuestion::where(['project_id' => $project_id, 'status' => GeoQuestion::STATUS_OPEN])->where('current_time', '<>', date('Y-m-d'))->pluck('id');
foreach ($ids as $id) {
Redis::lpush($key, $id);
if (!empty($project_id)){
$this->project_id = $project_id;
$ids = GeoQuestion::where(['project_id' => $project_id, 'status' => GeoQuestion::STATUS_OPEN])->where('current_time', '<>', date('Y-m-d'))->pluck('id');
foreach ($ids as $id) {
Redis::lpush($key, $id);
}
$task_id = Redis::rpop($key);
}
$task_id = Redis::rpop($key);
}
return $task_id;
}
... ...
... ... @@ -69,45 +69,70 @@ class CopyOldProject extends Command
$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',
'gl_ai_blog',
'gl_ai_blog_author',
'gl_ai_blog_list',
'gl_ai_blog_log',
])) {
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}");
try {
// 1. 检查源表是否存在(防止 gl_data_{$project_id} 下缺表)
$exists = Schema::connection('custom_tmp_mysql_copy')->hasTable($table);
if (!$exists) {
@file_put_contents(
storage_path('logs/copy_mysql_error.log'),
"源库中不存在表:{$table}" . PHP_EOL,
FILE_APPEND
);
continue;
}
// 2. 删除目标数据库中的表
$result = DB::connection('custom_mysql')->statement("DROP TABLE IF EXISTS `{$table}`");
@file_put_contents(storage_path('logs/copy_mysql_error.log'), "删除旧表:{$table} => {$result}" . PHP_EOL, FILE_APPEND);
// 3. 复制建表 SQL
$sql = DB::connection('custom_tmp_mysql_copy')->select("SHOW CREATE TABLE `{$table}`");
$createSql = get_object_vars($sql[0])['Create Table'];
$result1 = DB::connection('custom_mysql')->statement($createSql);
@file_put_contents(storage_path('logs/copy_mysql_error.log'), "创建表:{$table} => {$result1}" . PHP_EOL, FILE_APPEND);
// 4. 跳过指定表
if (in_array($table, [
'gl_customer_visit', 'gl_customer_visit_item',
'gl_inquiry_other', 'gl_inquiry_form_data', 'gl_inquiry_form',
'gl_ai_blog', 'gl_ai_blog_author', 'gl_ai_blog_list', 'gl_ai_blog_log'
])) {
continue;
}
);
// 5. 更新 project_id(如果存在)
if (Schema::connection('custom_mysql')->hasColumn($table, 'project_id')) {
DB::connection('custom_mysql')->table($table)->update(['project_id' => $news_project_id]);
// 5. 插入数据前,再次确认源表存在(双保险)
if (!Schema::connection('custom_tmp_mysql_copy')->hasTable($table)) {
@file_put_contents(storage_path('logs/copy_mysql_error.log'), "插入数据前发现表不存在:{$table}" . PHP_EOL, FILE_APPEND);
continue;
}
// 6. 原生 SQL 插入数据(完全复制)
$insert_sql = "INSERT INTO `{$table}` SELECT * FROM `gl_data_{$project_id}`.`{$table}`";
try {
$result2 = DB::connection('custom_mysql')->statement($insert_sql);
@file_put_contents(storage_path('logs/copy_mysql_error.log'), "插入数据成功:{$table} => {$result2}" . PHP_EOL, FILE_APPEND);
} catch (\Exception $e) {
@file_put_contents(storage_path('logs/copy_mysql_error.log'),
"插入数据失败:{$table} => " . $e->getMessage() . PHP_EOL,
FILE_APPEND
);
continue; // 跳过这个表,不中断整个流程
}
// 7. 更新 project_id(如果存在)
if (Schema::connection('custom_mysql')->hasColumn($table, 'project_id')) {
DB::connection('custom_mysql')->table($table)->update(['project_id' => $news_project_id]);
}
} catch (\Exception $e) {
@file_put_contents(
storage_path('logs/copy_mysql_error.log'),
"处理表 {$table} 出错:" . $e->getMessage() . PHP_EOL,
FILE_APPEND
);
continue;
}
}
return true;
}
/**
* @param $message
* @return bool
... ...
... ... @@ -239,28 +239,68 @@ class CopyProject extends Command
$tables = Schema::connection('custom_tmp_mysql_copy')->getAllTables();
$tables = array_column($tables, 'Tables_in_' . $database_name);
foreach ($tables as $table) {
// 1. 删除目标数据库中的表
$result = DB::connection('custom_mysql')->statement("DROP TABLE IF EXISTS {$table}");
@file_put_contents(storage_path('logs/copy_mysql_error.log'), var_export('先删除对应数据库的对应表返回结果:'.$result, true) . PHP_EOL, FILE_APPEND);
// 2. 复制建表 SQL
$sql = DB::connection('custom_tmp_mysql_copy')->select("SHOW CREATE TABLE `{$table}`");
$result1 = DB::connection('custom_mysql')->statement(get_object_vars($sql[0])['Create Table']);
@file_put_contents(storage_path('logs/copy_mysql_error.log'), var_export('创建对应表数据:'.$result1, true) . PHP_EOL, FILE_APPEND);
// 3. 跳过指定的表
if (in_array($table, ['gl_customer_visit', 'gl_customer_visit_item', 'gl_inquiry_other', 'gl_inquiry_form_data', 'gl_inquiry_form','gl_ai_blog', 'gl_ai_blog_author', 'gl_ai_blog_list','gl_ai_blog_log'])) {
try {
// 1. 检查源表是否存在(防止 gl_data_{$project_id} 下缺表)
$exists = Schema::connection('custom_tmp_mysql_copy')->hasTable($table);
if (!$exists) {
@file_put_contents(
storage_path('logs/copy_mysql_error.log'),
"源库中不存在表:{$table}" . PHP_EOL,
FILE_APPEND
);
continue;
}
// 2. 删除目标数据库中的表
$result = DB::connection('custom_mysql')->statement("DROP TABLE IF EXISTS `{$table}`");
@file_put_contents(storage_path('logs/copy_mysql_error.log'), "删除旧表:{$table} => {$result}" . PHP_EOL, FILE_APPEND);
// 3. 复制建表 SQL
$sql = DB::connection('custom_tmp_mysql_copy')->select("SHOW CREATE TABLE `{$table}`");
$createSql = get_object_vars($sql[0])['Create Table'];
$result1 = DB::connection('custom_mysql')->statement($createSql);
@file_put_contents(storage_path('logs/copy_mysql_error.log'), "创建表:{$table} => {$result1}" . PHP_EOL, FILE_APPEND);
// 4. 跳过指定表
if (in_array($table, [
'gl_customer_visit', 'gl_customer_visit_item',
'gl_inquiry_other', 'gl_inquiry_form_data', 'gl_inquiry_form',
'gl_ai_blog', 'gl_ai_blog_author', 'gl_ai_blog_list', 'gl_ai_blog_log'
])) {
continue;
}
// 5. 插入数据前,再次确认源表存在(双保险)
if (!Schema::connection('custom_tmp_mysql_copy')->hasTable($table)) {
@file_put_contents(storage_path('logs/copy_mysql_error.log'), "插入数据前发现表不存在:{$table}" . PHP_EOL, FILE_APPEND);
continue;
}
// 6. 原生 SQL 插入数据(完全复制)
$insert_sql = "INSERT INTO `{$table}` SELECT * FROM `gl_data_{$project_id}`.`{$table}`";
try {
$result2 = DB::connection('custom_mysql')->statement($insert_sql);
@file_put_contents(storage_path('logs/copy_mysql_error.log'), "插入数据成功:{$table} => {$result2}" . PHP_EOL, FILE_APPEND);
} catch (\Exception $e) {
@file_put_contents(storage_path('logs/copy_mysql_error.log'),
"插入数据失败:{$table} => " . $e->getMessage() . PHP_EOL,
FILE_APPEND
);
continue; // 跳过这个表,不中断整个流程
}
// 7. 更新 project_id(如果存在)
if (Schema::connection('custom_mysql')->hasColumn($table, 'project_id')) {
DB::connection('custom_mysql')->table($table)->update(['project_id' => $news_project_id]);
}
} catch (\Exception $e) {
@file_put_contents(
storage_path('logs/copy_mysql_error.log'),
"处理表 {$table} 出错:" . $e->getMessage() . PHP_EOL,
FILE_APPEND
);
continue;
}
// 4. 原生 SQL 插入数据(完全复制)
$insert_sql = "INSERT INTO `{$table}` SELECT * FROM `gl_data_{$project_id}`.`{$table}`";
$result2 = DB::connection('custom_mysql')->statement($insert_sql);
@file_put_contents(storage_path('logs/copy_mysql_error.log'), var_export('对应表插入数据:'.$result2, true) . PHP_EOL, FILE_APPEND);
// 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
... ...
... ... @@ -141,7 +141,6 @@ class GeoQuestionResLogic extends BaseLogic
'keywords_url_count'=>$keywordUrlCount,
'keywords_arr' => $keywordArr,
];
}
//问题达标数据
$data['question_qualify_count'] = $questionLogModel->where('project_id', $this->user['project_id'])
... ...