作者 lyh

变更数据

@@ -30,6 +30,8 @@ class GeoQuestionRes extends Command @@ -30,6 +30,8 @@ class GeoQuestionRes extends Command
30 */ 30 */
31 protected $signature = 'geo_question_result'; 31 protected $signature = 'geo_question_result';
32 32
  33 + public $porject_id;//记录当时执行的project_id
  34 +
33 /** 35 /**
34 * The console command description. 36 * The console command description.
35 * 37 *
@@ -337,15 +339,18 @@ class GeoQuestionRes extends Command @@ -337,15 +339,18 @@ class GeoQuestionRes extends Command
337 $key = 'geo_task_list'; 339 $key = 'geo_task_list';
338 $task_id = Redis::rpop($key); 340 $task_id = Redis::rpop($key);
339 if(empty($task_id)){ 341 if(empty($task_id)){
  342 + //todo::这里需要执行统计一次,统计当前项目当前日期的统计
  343 +
340 # TODO 按照项目进行获取, 一个项目当天需要将所有跑完 344 # TODO 按照项目进行获取, 一个项目当天需要将所有跑完
341 $project_id = GeoQuestion::where('status', GeoQuestion::STATUS_OPEN)->where('next_time', '<=', date('Y-m-d'))->value('project_id'); 345 $project_id = GeoQuestion::where('status', GeoQuestion::STATUS_OPEN)->where('next_time', '<=', date('Y-m-d'))->value('project_id');
342 - if (empty($project_id))  
343 - return $task_id;  
344 - $ids = GeoQuestion::where(['project_id' => $project_id, 'status' => GeoQuestion::STATUS_OPEN])->where('current_time', '<>', date('Y-m-d'))->pluck('id');  
345 - foreach ($ids as $id) {  
346 - Redis::lpush($key, $id); 346 + if (!empty($project_id)){
  347 + $this->project_id = $project_id;
  348 + $ids = GeoQuestion::where(['project_id' => $project_id, 'status' => GeoQuestion::STATUS_OPEN])->where('current_time', '<>', date('Y-m-d'))->pluck('id');
  349 + foreach ($ids as $id) {
  350 + Redis::lpush($key, $id);
  351 + }
  352 + $task_id = Redis::rpop($key);
347 } 353 }
348 - $task_id = Redis::rpop($key);  
349 } 354 }
350 return $task_id; 355 return $task_id;
351 } 356 }
@@ -69,45 +69,70 @@ class CopyOldProject extends Command @@ -69,45 +69,70 @@ class CopyOldProject extends Command
69 $tables = Schema::connection('custom_tmp_mysql_copy')->getAllTables(); 69 $tables = Schema::connection('custom_tmp_mysql_copy')->getAllTables();
70 $tables = array_column($tables, 'Tables_in_' . $database_name); 70 $tables = array_column($tables, 'Tables_in_' . $database_name);
71 foreach ($tables as $table) { 71 foreach ($tables as $table) {
72 - // 目标数据库是否存在该表  
73 - $has_table = Schema::connection('custom_mysql')->hasTable($table);  
74 - if ($has_table) {  
75 - // 1. 删除目标数据库中的表  
76 - DB::connection('custom_mysql')->statement("DROP TABLE IF EXISTS {$table}");  
77 - }  
78 - // 2. 重新创建表  
79 - $sql = DB::connection('custom_tmp_mysql_copy')->select("SHOW CREATE TABLE {$table}");  
80 - DB::connection('custom_mysql')->statement(get_object_vars($sql[0])['Create Table']);  
81 - // 3. 跳过指定的表  
82 - if (in_array($table, [  
83 - 'gl_customer_visit',  
84 - 'gl_customer_visit_item',  
85 - 'gl_inquiry_other',  
86 - 'gl_inquiry_form_data',  
87 - 'gl_inquiry_form',  
88 - 'gl_ai_blog',  
89 - 'gl_ai_blog_author',  
90 - 'gl_ai_blog_list',  
91 - 'gl_ai_blog_log',  
92 - ])) {  
93 - continue;  
94 - }  
95 - // 4. 重新插入数据  
96 - DB::connection('custom_mysql')->table($table)->insertUsing(  
97 - [], // 插入所有列  
98 - function ($query) use ($table, $project_id) {  
99 - $name = 'gl_data_' . $project_id . '.' . $table;  
100 - $query->select('*')->from("{$name}"); 72 + try {
  73 + // 1. 检查源表是否存在(防止 gl_data_{$project_id} 下缺表)
  74 + $exists = Schema::connection('custom_tmp_mysql_copy')->hasTable($table);
  75 + if (!$exists) {
  76 + @file_put_contents(
  77 + storage_path('logs/copy_mysql_error.log'),
  78 + "源库中不存在表:{$table}" . PHP_EOL,
  79 + FILE_APPEND
  80 + );
  81 + continue;
  82 + }
  83 +
  84 + // 2. 删除目标数据库中的表
  85 + $result = DB::connection('custom_mysql')->statement("DROP TABLE IF EXISTS `{$table}`");
  86 + @file_put_contents(storage_path('logs/copy_mysql_error.log'), "删除旧表:{$table} => {$result}" . PHP_EOL, FILE_APPEND);
  87 +
  88 + // 3. 复制建表 SQL
  89 + $sql = DB::connection('custom_tmp_mysql_copy')->select("SHOW CREATE TABLE `{$table}`");
  90 + $createSql = get_object_vars($sql[0])['Create Table'];
  91 + $result1 = DB::connection('custom_mysql')->statement($createSql);
  92 + @file_put_contents(storage_path('logs/copy_mysql_error.log'), "创建表:{$table} => {$result1}" . PHP_EOL, FILE_APPEND);
  93 + // 4. 跳过指定表
  94 + if (in_array($table, [
  95 + 'gl_customer_visit', 'gl_customer_visit_item',
  96 + 'gl_inquiry_other', 'gl_inquiry_form_data', 'gl_inquiry_form',
  97 + 'gl_ai_blog', 'gl_ai_blog_author', 'gl_ai_blog_list', 'gl_ai_blog_log'
  98 + ])) {
  99 + continue;
101 } 100 }
102 - );  
103 - // 5. 更新 project_id(如果存在)  
104 - if (Schema::connection('custom_mysql')->hasColumn($table, 'project_id')) {  
105 - DB::connection('custom_mysql')->table($table)->update(['project_id' => $news_project_id]); 101 + // 5. 插入数据前,再次确认源表存在(双保险)
  102 + if (!Schema::connection('custom_tmp_mysql_copy')->hasTable($table)) {
  103 + @file_put_contents(storage_path('logs/copy_mysql_error.log'), "插入数据前发现表不存在:{$table}" . PHP_EOL, FILE_APPEND);
  104 + continue;
  105 + }
  106 + // 6. 原生 SQL 插入数据(完全复制)
  107 + $insert_sql = "INSERT INTO `{$table}` SELECT * FROM `gl_data_{$project_id}`.`{$table}`";
  108 + try {
  109 + $result2 = DB::connection('custom_mysql')->statement($insert_sql);
  110 + @file_put_contents(storage_path('logs/copy_mysql_error.log'), "插入数据成功:{$table} => {$result2}" . PHP_EOL, FILE_APPEND);
  111 + } catch (\Exception $e) {
  112 + @file_put_contents(storage_path('logs/copy_mysql_error.log'),
  113 + "插入数据失败:{$table} => " . $e->getMessage() . PHP_EOL,
  114 + FILE_APPEND
  115 + );
  116 + continue; // 跳过这个表,不中断整个流程
  117 + }
  118 +
  119 + // 7. 更新 project_id(如果存在)
  120 + if (Schema::connection('custom_mysql')->hasColumn($table, 'project_id')) {
  121 + DB::connection('custom_mysql')->table($table)->update(['project_id' => $news_project_id]);
  122 + }
  123 + } catch (\Exception $e) {
  124 + @file_put_contents(
  125 + storage_path('logs/copy_mysql_error.log'),
  126 + "处理表 {$table} 出错:" . $e->getMessage() . PHP_EOL,
  127 + FILE_APPEND
  128 + );
  129 + continue;
106 } 130 }
107 } 131 }
108 return true; 132 return true;
109 } 133 }
110 134
  135 +
111 /** 136 /**
112 * @param $message 137 * @param $message
113 * @return bool 138 * @return bool
@@ -239,28 +239,68 @@ class CopyProject extends Command @@ -239,28 +239,68 @@ class CopyProject extends Command
239 $tables = Schema::connection('custom_tmp_mysql_copy')->getAllTables(); 239 $tables = Schema::connection('custom_tmp_mysql_copy')->getAllTables();
240 $tables = array_column($tables, 'Tables_in_' . $database_name); 240 $tables = array_column($tables, 'Tables_in_' . $database_name);
241 foreach ($tables as $table) { 241 foreach ($tables as $table) {
242 - // 1. 删除目标数据库中的表  
243 - $result = DB::connection('custom_mysql')->statement("DROP TABLE IF EXISTS {$table}");  
244 - @file_put_contents(storage_path('logs/copy_mysql_error.log'), var_export('先删除对应数据库的对应表返回结果:'.$result, true) . PHP_EOL, FILE_APPEND);  
245 - // 2. 复制建表 SQL  
246 - $sql = DB::connection('custom_tmp_mysql_copy')->select("SHOW CREATE TABLE `{$table}`");  
247 - $result1 = DB::connection('custom_mysql')->statement(get_object_vars($sql[0])['Create Table']);  
248 - @file_put_contents(storage_path('logs/copy_mysql_error.log'), var_export('创建对应表数据:'.$result1, true) . PHP_EOL, FILE_APPEND);  
249 - // 3. 跳过指定的表  
250 - 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'])) { 242 + try {
  243 + // 1. 检查源表是否存在(防止 gl_data_{$project_id} 下缺表)
  244 + $exists = Schema::connection('custom_tmp_mysql_copy')->hasTable($table);
  245 + if (!$exists) {
  246 + @file_put_contents(
  247 + storage_path('logs/copy_mysql_error.log'),
  248 + "源库中不存在表:{$table}" . PHP_EOL,
  249 + FILE_APPEND
  250 + );
  251 + continue;
  252 + }
  253 + // 2. 删除目标数据库中的表
  254 + $result = DB::connection('custom_mysql')->statement("DROP TABLE IF EXISTS `{$table}`");
  255 + @file_put_contents(storage_path('logs/copy_mysql_error.log'), "删除旧表:{$table} => {$result}" . PHP_EOL, FILE_APPEND);
  256 + // 3. 复制建表 SQL
  257 + $sql = DB::connection('custom_tmp_mysql_copy')->select("SHOW CREATE TABLE `{$table}`");
  258 + $createSql = get_object_vars($sql[0])['Create Table'];
  259 + $result1 = DB::connection('custom_mysql')->statement($createSql);
  260 + @file_put_contents(storage_path('logs/copy_mysql_error.log'), "创建表:{$table} => {$result1}" . PHP_EOL, FILE_APPEND);
  261 +
  262 + // 4. 跳过指定表
  263 + if (in_array($table, [
  264 + 'gl_customer_visit', 'gl_customer_visit_item',
  265 + 'gl_inquiry_other', 'gl_inquiry_form_data', 'gl_inquiry_form',
  266 + 'gl_ai_blog', 'gl_ai_blog_author', 'gl_ai_blog_list', 'gl_ai_blog_log'
  267 + ])) {
  268 + continue;
  269 + }
  270 + // 5. 插入数据前,再次确认源表存在(双保险)
  271 + if (!Schema::connection('custom_tmp_mysql_copy')->hasTable($table)) {
  272 + @file_put_contents(storage_path('logs/copy_mysql_error.log'), "插入数据前发现表不存在:{$table}" . PHP_EOL, FILE_APPEND);
  273 + continue;
  274 + }
  275 + // 6. 原生 SQL 插入数据(完全复制)
  276 + $insert_sql = "INSERT INTO `{$table}` SELECT * FROM `gl_data_{$project_id}`.`{$table}`";
  277 + try {
  278 + $result2 = DB::connection('custom_mysql')->statement($insert_sql);
  279 + @file_put_contents(storage_path('logs/copy_mysql_error.log'), "插入数据成功:{$table} => {$result2}" . PHP_EOL, FILE_APPEND);
  280 + } catch (\Exception $e) {
  281 + @file_put_contents(storage_path('logs/copy_mysql_error.log'),
  282 + "插入数据失败:{$table} => " . $e->getMessage() . PHP_EOL,
  283 + FILE_APPEND
  284 + );
  285 + continue; // 跳过这个表,不中断整个流程
  286 + }
  287 + // 7. 更新 project_id(如果存在)
  288 + if (Schema::connection('custom_mysql')->hasColumn($table, 'project_id')) {
  289 + DB::connection('custom_mysql')->table($table)->update(['project_id' => $news_project_id]);
  290 + }
  291 + } catch (\Exception $e) {
  292 + @file_put_contents(
  293 + storage_path('logs/copy_mysql_error.log'),
  294 + "处理表 {$table} 出错:" . $e->getMessage() . PHP_EOL,
  295 + FILE_APPEND
  296 + );
251 continue; 297 continue;
252 } 298 }
253 - // 4. 原生 SQL 插入数据(完全复制)  
254 - $insert_sql = "INSERT INTO `{$table}` SELECT * FROM `gl_data_{$project_id}`.`{$table}`";  
255 - $result2 = DB::connection('custom_mysql')->statement($insert_sql);  
256 - @file_put_contents(storage_path('logs/copy_mysql_error.log'), var_export('对应表插入数据:'.$result2, true) . PHP_EOL, FILE_APPEND);  
257 - // 5. 更新 project_id(如果存在)  
258 - if (Schema::connection('custom_mysql')->hasColumn($table, 'project_id')) {  
259 - DB::connection('custom_mysql')->table($table)->update(['project_id' => $news_project_id]);  
260 - }  
261 } 299 }
  300 +
262 return true; 301 return true;
263 } 302 }
  303 +
264 /** 304 /**
265 * @param $message 305 * @param $message
266 * @return bool 306 * @return bool
@@ -141,7 +141,6 @@ class GeoQuestionResLogic extends BaseLogic @@ -141,7 +141,6 @@ class GeoQuestionResLogic extends BaseLogic
141 'keywords_url_count'=>$keywordUrlCount, 141 'keywords_url_count'=>$keywordUrlCount,
142 'keywords_arr' => $keywordArr, 142 'keywords_arr' => $keywordArr,
143 ]; 143 ];
144 -  
145 } 144 }
146 //问题达标数据 145 //问题达标数据
147 $data['question_qualify_count'] = $questionLogModel->where('project_id', $this->user['project_id']) 146 $data['question_qualify_count'] = $questionLogModel->where('project_id', $this->user['project_id'])