作者 赵彬吉
@@ -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 }
@@ -64,50 +64,74 @@ class CopyOldProject extends Command @@ -64,50 +64,74 @@ class CopyOldProject extends Command
64 { 64 {
65 // 设置源数据库 65 // 设置源数据库
66 config(['database.connections.custom_tmp_mysql_copy.database' => 'gl_data_' . $project_id]); 66 config(['database.connections.custom_tmp_mysql_copy.database' => 'gl_data_' . $project_id]);
  67 + DB::purge('custom_tmp_mysql_copy');
  68 + DB::reconnect('custom_tmp_mysql_copy');
67 $database_name = DB::connection('custom_tmp_mysql_copy')->getDatabaseName(); 69 $database_name = DB::connection('custom_tmp_mysql_copy')->getDatabaseName();
68 // 获取源数据库的所有表 70 // 获取源数据库的所有表
69 $tables = Schema::connection('custom_tmp_mysql_copy')->getAllTables(); 71 $tables = Schema::connection('custom_tmp_mysql_copy')->getAllTables();
70 $tables = array_column($tables, 'Tables_in_' . $database_name); 72 $tables = array_column($tables, 'Tables_in_' . $database_name);
71 foreach ($tables as $table) { 73 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}"); 74 + try {
  75 + // 1. 检查源表是否存在(防止 gl_data_{$project_id} 下缺表)
  76 + $exists = Schema::connection('custom_tmp_mysql_copy')->hasTable($table);
  77 + if (!$exists) {
  78 + @file_put_contents(
  79 + storage_path('logs/copy_mysql_error.log'),
  80 + "源库中不存在表:{$table}" . PHP_EOL,
  81 + FILE_APPEND
  82 + );
  83 + continue;
  84 + }
  85 + // 2. 删除目标数据库中的表
  86 + $result = DB::connection('custom_mysql')->statement("DROP TABLE IF EXISTS `{$table}`");
  87 + @file_put_contents(storage_path('logs/copy_mysql_error.log'), "删除旧表:{$table} => {$result}" . PHP_EOL, FILE_APPEND);
  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;
  100 + }
  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;
101 } 105 }
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]); 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 + // 7. 更新 project_id(如果存在)
  119 + if (Schema::connection('custom_mysql')->hasColumn($table, 'project_id')) {
  120 + DB::connection('custom_mysql')->table($table)->update(['project_id' => $news_project_id]);
  121 + }
  122 + } catch (\Exception $e) {
  123 + @file_put_contents(
  124 + storage_path('logs/copy_mysql_error.log'),
  125 + "处理表 {$table} 出错:" . $e->getMessage() . PHP_EOL,
  126 + FILE_APPEND
  127 + );
  128 + continue;
106 } 129 }
107 } 130 }
108 return true; 131 return true;
109 } 132 }
110 133
  134 +
111 /** 135 /**
112 * @param $message 136 * @param $message
113 * @return bool 137 * @return bool
@@ -234,33 +234,75 @@ class CopyProject extends Command @@ -234,33 +234,75 @@ class CopyProject extends Command
234 { 234 {
235 // 设置源数据库 235 // 设置源数据库
236 config(['database.connections.custom_tmp_mysql_copy.database' => 'gl_data_' . $project_id]); 236 config(['database.connections.custom_tmp_mysql_copy.database' => 'gl_data_' . $project_id]);
  237 + DB::purge('custom_tmp_mysql_copy');
  238 + DB::reconnect('custom_tmp_mysql_copy');
237 $database_name = DB::connection('custom_tmp_mysql_copy')->getDatabaseName(); 239 $database_name = DB::connection('custom_tmp_mysql_copy')->getDatabaseName();
238 // 获取源数据库的所有表 240 // 获取源数据库的所有表
239 $tables = Schema::connection('custom_tmp_mysql_copy')->getAllTables(); 241 $tables = Schema::connection('custom_tmp_mysql_copy')->getAllTables();
240 $tables = array_column($tables, 'Tables_in_' . $database_name); 242 $tables = array_column($tables, 'Tables_in_' . $database_name);
241 foreach ($tables as $table) { 243 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'])) { 244 + try {
  245 + // 1. 检查源表是否存在(防止 gl_data_{$project_id} 下缺表)
  246 + $exists = Schema::connection('custom_tmp_mysql_copy')->hasTable($table);
  247 + if (!$exists) {
  248 + @file_put_contents(
  249 + storage_path('logs/copy_mysql_error.log'),
  250 + "源库中不存在表:{$table}" . PHP_EOL,
  251 + FILE_APPEND
  252 + );
  253 + continue;
  254 + }
  255 + // 2. 删除目标数据库中的表
  256 + $result = DB::connection('custom_mysql')->statement("DROP TABLE IF EXISTS `{$table}`");
  257 + @file_put_contents(storage_path('logs/copy_mysql_error.log'), "删除旧表:{$table} => {$result}" . PHP_EOL, FILE_APPEND);
  258 + // 3. 复制建表 SQL
  259 + $sql = DB::connection('custom_tmp_mysql_copy')->select("SHOW CREATE TABLE `{$table}`");
  260 + $createSql = get_object_vars($sql[0])['Create Table'];
  261 + $result1 = DB::connection('custom_mysql')->statement($createSql);
  262 + @file_put_contents(storage_path('logs/copy_mysql_error.log'), "创建表:{$table} => {$result1}" . PHP_EOL, FILE_APPEND);
  263 +
  264 + // 4. 跳过指定表
  265 + if (in_array($table, [
  266 + 'gl_customer_visit', 'gl_customer_visit_item',
  267 + 'gl_inquiry_other', 'gl_inquiry_form_data', 'gl_inquiry_form',
  268 + 'gl_ai_blog', 'gl_ai_blog_author', 'gl_ai_blog_list', 'gl_ai_blog_log'
  269 + ])) {
  270 + continue;
  271 + }
  272 + // 5. 插入数据前,再次确认源表存在(双保险)
  273 + if (!Schema::connection('custom_tmp_mysql_copy')->hasTable($table)) {
  274 + @file_put_contents(storage_path('logs/copy_mysql_error.log'), "插入数据前发现表不存在:{$table}" . PHP_EOL, FILE_APPEND);
  275 + continue;
  276 + }
  277 + // 6. 原生 SQL 插入数据(完全复制)
  278 + $insert_sql = "INSERT INTO `{$table}` SELECT * FROM `gl_data_{$project_id}`.`{$table}`";
  279 + try {
  280 + $result2 = DB::connection('custom_mysql')->statement($insert_sql);
  281 + @file_put_contents(storage_path('logs/copy_mysql_error.log'), "插入数据成功:{$table} => {$result2}" . PHP_EOL, FILE_APPEND);
  282 + } catch (\Exception $e) {
  283 + @file_put_contents(storage_path('logs/copy_mysql_error.log'),
  284 + "插入数据失败:{$table} => " . $e->getMessage() . PHP_EOL,
  285 + FILE_APPEND
  286 + );
  287 + continue; // 跳过这个表,不中断整个流程
  288 + }
  289 + // 7. 更新 project_id(如果存在)
  290 + if (Schema::connection('custom_mysql')->hasColumn($table, 'project_id')) {
  291 + DB::connection('custom_mysql')->table($table)->update(['project_id' => $news_project_id]);
  292 + }
  293 + } catch (\Exception $e) {
  294 + @file_put_contents(
  295 + storage_path('logs/copy_mysql_error.log'),
  296 + "处理表 {$table} 出错:" . $e->getMessage() . PHP_EOL,
  297 + FILE_APPEND
  298 + );
251 continue; 299 continue;
252 } 300 }
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 } 301 }
  302 +
262 return true; 303 return true;
263 } 304 }
  305 +
264 /** 306 /**
265 * @param $message 307 * @param $message
266 * @return bool 308 * @return bool
@@ -190,9 +190,9 @@ class WorkOrderDing extends Command @@ -190,9 +190,9 @@ class WorkOrderDing extends Command
190 } 190 }
191 }catch (\Exception $exception){ 191 }catch (\Exception $exception){
192 echo now() . " | ERROR | gl_ticket_dings ID {$tickDing->id} {$exception->getMessage()} {$exception->getTraceAsString()} \n"; 192 echo now() . " | ERROR | gl_ticket_dings ID {$tickDing->id} {$exception->getMessage()} {$exception->getTraceAsString()} \n";
193 - $ding->status = 2; // 标记为失败  
194 - $ding->errorMsg = $exception->getMessage();  
195 - $ding->save(); 193 + $tickDing->status = 2; // 标记为失败
  194 + $tickDing->errorMsg = $exception->getMessage();
  195 + $tickDing->save();
196 } 196 }
197 } 197 }
198 } 198 }
@@ -121,7 +121,6 @@ class GeoQuestionResLogic extends BaseLogic @@ -121,7 +121,6 @@ class GeoQuestionResLogic extends BaseLogic
121 'keywords_arr' => $keywordArr, 121 'keywords_arr' => $keywordArr,
122 'core_keyword_url_count'=>$coreKeywordUrlCount ?? 0 122 'core_keyword_url_count'=>$coreKeywordUrlCount ?? 0
123 ]; 123 ];
124 - return $this->success($data);  
125 }else{ 124 }else{
126 $keywordUrlCount = $questionLogModel->counts(['project_id'=>$this->user['project_id'],'hit'=>['!=',0]]); 125 $keywordUrlCount = $questionLogModel->counts(['project_id'=>$this->user['project_id'],'hit'=>['!=',0]]);
127 foreach ($list as $item){ 126 foreach ($list as $item){
@@ -141,7 +140,6 @@ class GeoQuestionResLogic extends BaseLogic @@ -141,7 +140,6 @@ class GeoQuestionResLogic extends BaseLogic
141 'keywords_url_count'=>$keywordUrlCount, 140 'keywords_url_count'=>$keywordUrlCount,
142 'keywords_arr' => $keywordArr, 141 'keywords_arr' => $keywordArr,
143 ]; 142 ];
144 -  
145 } 143 }
146 //问题达标数据 144 //问题达标数据
147 $data['question_qualify_count'] = $questionLogModel->where('project_id', $this->user['project_id']) 145 $data['question_qualify_count'] = $questionLogModel->where('project_id', $this->user['project_id'])